Fork off more processes as previous finish,直到达到最大值

Fork off more processes as previous finish, until max is reached

要分叉 X 进程并让 parent 等待所有进程,我有以下代码:

int maxProcesses = 10;

for (int currentChild = 0; currentChild < maxProcesses; currentChild++) {
    pid_t pid = fork();

    if (pid < 0) {
        // Error
    } else if (pid == 0) {
        // Child
    } else {
        // Parent
        // Should I call waitpid on pid and wait here instead?
    }
}

// Wait for all children
for (int currentChild = 0; currentChild < maxProcesses; currentChild++) {
    wait(NULL);
}

现在我想修改代码,以便在总共 X 个进程中,首先分叉 Y 个,然后当它们完成时,进行更多分叉,直到达到所需的总数。我对上面的代码做了一些修改,有一些问题。

int totalProcessesToBeForked = 10;
int maxAllowedAtOnce = 5;

for (int currentChild = 0; currentChild < maxAllowedAtOnce; currentChild++) {
    forkChild(currentChild);
}

// Wait for all children
// # How do I modify this to wait for new children forked as well
// # if I move it inside parent, it will make things easier, right?
for (int currentChild = 0; currentChild < maxAllowedAtOnce; currentChild++) {
    wait(NULL);
}

void forkChild(currentChild) {
    pid_t pid = fork();

    if (pid < 0) {
        // Error
    } else if (pid == 0) {
        // Child
    } else {
        // Parent
        // # I think waiting here using waitpid will be better b/c
        // # as new forks are made, parent begins to wait for them
    }
}

我可能需要记录有多少 children 已被分叉并将其与 totalProcessesToBeForked 进行比较,并相应地分叉新的。

更新代码 v1:

int maxProcesses = 10;
int maxAllowedAtOnce = 5;

int main(int argc, char* argv[]) {
    // Start timer
    alarm(10);                  // Terminate after 10s
    signal(SIGALRM, onTimeout);
    signal(SIGCHLD, catchChild);

    for (int currentChild = 0; currentChild < maxAllowedAtOnce; currentChild++) {
        forkChild(currentChild);
    }

    // # This sections runs immediately before death of any child is reported
    // # and starts cleanup processes, thus killing any/all running children

    // Completed before timeout
    endTimer = true;
    int timeRemaining = alarm(0);
    if (timeRemaining > 0) {
        printf("\nCompleted w/ %is remaining. Performing cleanup.\n", timeRemaining);

        // Kill children any running child processes, cleanup
        cleanup();
    }

    return 0;
}

void forkChild(currentChild) {
    pid_t pid = fork();

    if (pid < 0) {
        // Error
    } else if (pid == 0) {
        // Child
        execl("/bin/date", "date", 0, 0);
    } else {
        // Parent
        printf("#Log: Started %i.\n", currentChild + 1);
    }
}

void catchChild(int sig) {
    pid_t p;
    int state;
    p=wait(&state);
    printf("Got child %d\n",p);
}

void cleanup() {
    // Cleanup Code
}

示例运行:

编辑#2: http://ideone.com/noUs3m

与使用 wait 不同,您想研究信号处理来处理 child 进程死亡。

您在开始之前添加 forking,这一行

signal(SIGCHLD,catchchild);

并将此函数添加到您的代码中

void catchchild(int sig)
  {
  pid_t p;
  int state;
  p=wait(&state);
  printf("Got child %d\n",p);
  }

然后每当 child 进程终止时,您的主进程将调用 catchchild

正如您已经计算出的那样,如果您计算出有多少 children 已经分叉,您可以 catchchild 更新它以便您的主代码知道fork 新 child.

要像下面这样回答您的评论,但要进行更多的错误检查

while(totalProcessesToBeForked)
  {
  if(currentChild < maxAllowedAtOnce)
    {
    forkChild(currentChild);
    totalProcessesToBeForked--;
    }
  sleep(1);
  }