Multiple child process

前端 未结 4 1430
死守一世寂寞
死守一世寂寞 2020-12-01 01:59

can someone help me about how to create multiple child processes which have the same parent in order to do \"some\" part of particular job?

for example, an external

相关标签:
4条回答
  • 2020-12-01 02:07

    If you want to launch several forks, you should do it recursively. This is because you must call fork from the parent process. Otherwise, if you launch a second fork, you will duplicate both parent and first child process. Here's an example:

    void forker(int nprocesses)
    {
        pid_t pid;
    
        if(nprocesses > 0)
        {
            if ((pid = fork()) < 0)
            {
                perror("fork");
            }
            else if (pid == 0)
            {
                //Child stuff here
                printf("Child %d end\n", nprocesses);
            }
            else if(pid > 0)
            {
                //parent
                forker(nprocesses - 1);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-01 02:09

    I think it would be worth pointing out why threads are more appropriate here:

    As you are trying to do a "part" of the job in parallel i assume that your program needs to know about the result of the computation. fork()s of a process don't share more then the initial information after fork(). Every change in one process is unknow to the other and you would need to pass the information as a message (e.g. through a pipe, see "man pipe"). Threads in a process share the same adress space and therefor are able to manipulate data and have them visible toeach other "immediatly". Also adding the benefits of being more lightweight, I'd go with pthreads().

    After all: You will learn all you need to know about fork() if you use pthreads anyway.

    0 讨论(0)
  • 2020-12-01 02:18

    Here is how to fork 10 children and wait for them to finish:

    pid_t pids[10];
    int i;
    int n = 10;
    
    /* Start children. */
    for (i = 0; i < n; ++i) {
      if ((pids[i] = fork()) < 0) {
        perror("fork");
        abort();
      } else if (pids[i] == 0) {
        DoWorkInChild();
        exit(0);
      }
    }
    
    /* Wait for children to exit. */
    int status;
    pid_t pid;
    while (n > 0) {
      pid = wait(&status);
      printf("Child with PID %ld exited with status 0x%x.\n", (long)pid, status);
      --n;  // TODO(pts): Remove pid from the pids array.
    }
    
    0 讨论(0)
  • 2020-12-01 02:25

    You can do this with fork. A given parent can fork as may times as it wants. However, I agree with AviD pthreads may be more appropriate.

    pid_t firstChild, secondChild;
    firstChild = fork();
    if(firstChild > 0)
    {
      // In parent
      secondChild = fork();
      if(secondChild > 0)
      {
        // In parent
      }
      else if(secondChild < 0)
      {
        // Error
      }
      else
      {
        // In secondChild
      }
    }
    else if(firstChild < 0 )
    {
      // Error
    } 
    else
    {
      // In firstChild
    }
    
    0 讨论(0)
提交回复
热议问题