Как исправить бесконечные дочерние процессы, создаваемые после установки rlim_cur в 7 - PullRequest
0 голосов
/ 02 мая 2019

Я бы хотел ограничить дочерний процесс до 7. Я установил для rlim_cur значение 7, но при запуске программы непрерывно создавались дочерние процессы. Я установил для rlim_cur значение 7 между getrlimit и setrlimit, но не могу понять, почему программа не прекращает создание после того, как достигнет 7.

//#include statements

int main() {

        struct rlimit limit;
        int counter = 0;
        errno = 0;
        pid_t pid;
        char buff[8];

        if(getrlimit(RLIMIT_NPROC, &limit) < 0) {
                printf("%s\n", strerror(errno));
        }

        limit.rlim_cur = 7;

        if(setrlimit(RLIMIT_NPROC, &limit) < 0) {
                printf("%s\n", strerror(errno));
        }

        while(1) {

                snprintf(buff, sizeof(buff), "%d", ++counter);
                if ((pid = fork())== 0) {

                        printf("child %d: before execl\n", counter);
                        if(execl("./child", "child", buff, NULL) == -1) {
                                printf("%s\n", strerror(errno));
                                //exit(1);
                        }
                }
                else if (pid > 0) {
                        if (waitpid(pid, NULL, 0) == -1) {
                                printf("Error waiting on child: %s\n", strerror(errno));
                                exit(1);
                        }

                }

                 else if (pid == -1) {
                        printf("parent: error creating child: %s\n", strerror(errno));
                        sleep(2);
                        exit(1);
                }

                printf("Parent: child %d created with pid: %d\n", counter, getpid());
        }

        return 0;

}
...