Multiple child process

前端 未结 4 1529
死守一世寂寞
死守一世寂寞 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: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
    }
    

提交回复
热议问题