【问题标题】:How to use fork() to create a child process out of a child?如何使用 fork() 从子进程中创建子进程?
【发布时间】:2017-04-18 08:06:03
【问题描述】:

我想用 fork() 创建一个儿子 (1),这个儿子需要创建另一个儿子 (2)。

儿子 (1) 和父亲需要等待他们儿子的结局给出信息。我希望他们都printf他们的PID。

这是我的代码:

#include <stdio.h>
#include <stdlib.h>

int main(){

  int pid; //i know thats not good

  if((pid = fork ()) == 0) { //this isnt good either
    printf ("SON %d\n",getpid());

  } else {
    // sleep(1) not necessary
    printf ("Thats the Father\n");
    printf ("PID of my Son PID %d\n",pid);
  }
}

找到了几个信息来从一个父亲中创建多个孩子,但我不知道如何从一个孩子中创建一个新孩子。

【问题讨论】:

  • 流程不分性别。 fork 创建一个“孩子”,而不是“儿子”。

标签: c fork response pid


【解决方案1】:

找到了几个信息来从 1 个父亲中创建多个孩子,但我不知道如何从孩子中创建一个新孩子。

这与创建第一个子进程的方式完全相同。您需要在子进程中再次fork() 以创建另一个进程。使用wait(2) 等待子进程。

考虑这个例子(没有错误检查):

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(void){

pid_t pid;

if((pid = fork()) == 0) {
    printf ("Child process: %d\n", (int)getpid());
    pid_t pid2;

    if ((pid2 = fork()) == 0) {
        printf("Child's child process: %d\n", (int)getpid());
    } else {
        int st2;
        wait(&st2);
    }
} else {
    printf ("Parent process: %d\n", (int)getpid());
    int st;
    wait(&st);
}

return 0;
}

【讨论】:

    【解决方案2】:

    感谢您的好评。知道了。

    我的实际代码:

    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void){
    
    int pid;
    int pid2;
    int st;
    int st2;
    
    if((pid = fork()) == 0) {
        printf ("Kind: %d\n",getpid());
    
        if ((pid2 = fork()) == 0) {
            printf("Kindes Kind process: %d\n",getpid());
        } else {
                    wait(&st2);
    
        }
    } else {
        printf("Ich warte auf meinen Sohn\n"); //WAITING FOR MY SON
        wait(&st);
        printf("mein Sohn ist fertig\n"); // MY SON IS RDY
            printf ("Vater process: %d\n", getpid());
        printf("Vater: Status = %d\n",st); //MY STATUS AS A FATHER
    }
    
    return 0;
    }
    

    我的结果:

    Ich warte auf meinen Sohn //waiting for my son
    Kind: 2175 //pid children
    Kindes Kind process: 2176 // childrens child pid
    mein Sohn ist fertig //my son finished
    Vater process: 2174 //father pid
    Vater: Status = 0 //father status
    

    我唯一的问题是,第一个孩子的 PID 首先打印出来。 我希望孩子等待他自己的孩子,然后打印出他自己的 pid。

    喜欢:

    waiting for my Son(2)
    pidchild2
    my son2 finished
    status
    waiting for my son
    pidchild1
    my son1 finished
    father pid
    status
    

    编辑:

    知道了,我想

    只需要 printf 超出 wait2。很明显

    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void){
    
    int pid;
    int pid2;
    int st;
    int st2;
    
    if((pid = fork()) == 0) {
    
    
        if ((pid2 = fork()) == 0) {
            printf("Kindes Kind process: %d\n",getpid());
        } else {
      printf("Ich warte auf meinen Sohn1\n");
                    wait(&st2);
      printf("mein Sohn ist fertig2\n"); // MY SON IS RDY
      printf ("Kind: %d\n",getpid());
      printf("Sohn1: Status = %d\n",st2); //MY STATUS AS A FATHER
    
        }
    } else {
        printf("Ich warte auf meinen Sohn\n"); //WAITING FOR MY SON
        wait(&st);
        printf("mein Sohn ist fertig\n"); // MY SON IS RDY
            printf ("Vater process: %d\n", getpid());
        printf("Vater: Status = %d\n",st); //MY STATUS AS A FATHER
    }
    
    return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-10
      • 1970-01-01
      • 1970-01-01
      • 2011-06-28
      • 2018-05-16
      • 1970-01-01
      • 1970-01-01
      • 2014-05-19
      相关资源
      最近更新 更多