Display number of processes in loop with fork

家住魔仙堡 提交于 2019-12-19 09:47:25

问题


How can I display the number of processes created?
(without using a formula)

for (i=0; i<3; i++)
fork();
count = count + 1;
printf("%d",count);

回答1:


There are a number of ways to do this, and a good technique is to have each child write one byte into a file descriptor which the original process can read. Note that, for the sake of brevity, the following code contains absolutely no error checking. Also, we report only the number of spawned processes (7) rather than counting the original to get a count of 8:

int main(void) {
    int fd[2];
    int depth = 0; /* keep track of number of generations from original */
    int i;
    pipe(fd);  /* create a pipe which will be inherited by all children */
    for(i=0; i<3; i++) {
        if(fork() == 0) {  /* fork returns 0 in the child */
            write(fd[1], &i, 1);  /* write one byte into the pipe */
            depth += 1;
        }
    }
    close(fd[1]);  /* exercise for the reader to learn why this is needed */
    if( depth == 0 ) { /* original process */
      i=0;
      while(read(fd[0],&depth,1) != 0)
        i += 1;
      printf( "%d total processes spawned", i);
    }

    return 0;
}



回答2:


Printing the count value out just once is the easy part. Because you can get the process pid before the for loop. And then get the pid again after the for loop and only print if the pids match. For the counting part, it depends on whether your child processes exit or not. If they exit the solution is easier. The below code demonstrates one possible solution if the child processes exit (for brevity have not done full error checking). The idea is that each child process counts its own children. Parent waits for each child to complete and adds in its count. Haven't had time to fully test/debug the program so there may be some errors. But hopefully gives you the general idea.

#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

int main(void)
{
    pid_t before_pid, after_pid;
    pid_t forked_pid;
    int count;
    int i;
    int status;

    before_pid = getpid();
    count = 1; /* count self */
    for (i = 0; i < 3; i++) {
        forked_pid = fork();

        if (forked_pid > 0) {
            waitpid(forked_pid, &status, 0);
            /* parent process - count child and descendents */
            count += WEXITSTATUS(status); 
        } else {
            /* Child process - init with self count */
            count = 1;
        }
    }

    after_pid = getpid();
    if (after_pid == before_pid) {
        printf("%d processes created\n", count);
    }

    return (count);
}


来源:https://stackoverflow.com/questions/29244170/display-number-of-processes-in-loop-with-fork

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!