【问题标题】:Only 1 child after 5 forks (C)5 个叉子后只有 1 个孩子 (C)
【发布时间】:2017-01-23 13:16:43
【问题描述】:

我正在尝试在 C 中创建一个处理器场。我从打开消息队列开始,然后尝试创建工作进程:(注意 NROF_WORKERS 为 5)

static void
makechildren (void) {
    // Only the parent should fork. Any children created will become workers. 
    pid_t   processID; 
    pid_t   farmerPID = getpid(); // To identify who the farmer is

    // Loop creating processes, indexed by NROF_WORKERS
    int i = 0; 
    while (i < NROF_WORKERS){
        if (getpid() == farmerPID){
            i++; 
            printf ("Parent is creating a child!%d\n", getpid()); 
            processID = fork();
        }
    }

    if (processID < 0){
        perror("fork() failed");
        exit(1);
    }
    else {
    // If parent, start farming
        if (processID == farmerPID) {
            printf("Parent reporting in!%d\n");
        }
    // If child, become a worker
        if (processID == 0) {
            printf("Child reporting in!%d\n", getpid()); 
            join();
        }
    }
}

如您所见,我希望父母在任何时候创建孩子时报告,然后我希望父母和所有孩子都报告。然而,这就是我得到的全部:

Parent is creating a child!11909
Parent is creating a child!11909
Parent is creating a child!11909
Parent is creating a child!11909
Parent is creating a child!11909
Child reporting in!11914

现在,我确实注意到 11909 和 11914 的差异是 5。所以我的问题是:是否创建了其他进程?如果是这样,他们为什么不报告?如果没有,我做错了什么?还有,家长根本不报,这是怎么造成的?

【问题讨论】:

    标签: c process fork pid


    【解决方案1】:

    您总是打印farmerPid!但是当消息被打印 5 次时,您实际上创建了 5 个进程:

    while (i < NROF_WORKERS){
        if (getpid() == farmerPID){
            i++; 
            printf ("Parent is creating a child!%d\n", getpid()); 
            processID = fork();
        }
    }
    

    如果您想打印子 pid,那么您的代码必须在父子之间产生差异,如下所示:

    while (i < NROF_WORKERS){
        if (getpid() == farmerPID){
            i++; 
            printf ("Parent is creating a child!\n"); 
            processID = fork();
            if (processID==0) { // child
                printf("I am the child %d\n",getpid());
            } else { // parent
                printf("Parent just created child %d\n",processID);
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      所有子代都已创建,但将在 while 循环中永远循环,因为 i 仅针对父代递增:

      int i = 0; 
      while (i < NROF_WORKERS){
          if (getpid() == farmerPID){    
              i++;             // <---- This is happening for the parent process only.
              printf ("Parent is creating a child!%d\n", getpid()); 
              processID = fork();
          }
      }
      

      唯一要终止的孩子是最后一个,i 等于 NROF_WORKERS

      也父是“不报告”,因为processID 你检查等于父PID 永远不会等于它,因为它等于最新的fork 结果,即最新创建的子PID:

      .........
       processID = fork();
      .........
      .........
       if (processID == farmerPID) {
                  printf("Parent reporting in!%d\n");
       }
      

      【讨论】:

        猜你喜欢
        • 2020-07-02
        • 2016-07-08
        • 1970-01-01
        • 2018-12-17
        • 2017-08-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-13
        相关资源
        最近更新 更多