C 从子进程创建子进程

C 从子进程创建子进程,c,linux,fork,C,Linux,Fork,我不完全确定何时对子进程使用fork() 是否存在创建其他子进程的子进程,即使该子进程是其他进程的父进程(状态为0) 不是这样的 do ma do do to to 调用fork的子进程不返回0,因为我有两个ma,而不是一个,我不知道为什么。该过程创建一个子项,然后再创建另一个子项,成为其父项。 您可以使用fork return number来区分子级和父级。发件人: 您需要检查fork()返回值。如果返回0,则表示此进程是子进程。如果返回的值大于0,则此进程为父进程,返回的值为子进程id。

我不完全确定何时对子进程使用fork() 是否存在创建其他子进程的子进程,即使该子进程是其他进程的父进程(状态为0)

不是这样的

do ma
do do to
to

调用fork的子进程不返回0,因为我有两个ma,而不是一个,我不知道为什么。该过程创建一个子项,然后再创建另一个子项,成为其父项。
您可以使用fork return number来区分子级和父级。发件人:

您需要检查fork()返回值。如果返回0,则表示此进程是子进程。如果返回的值大于0,则此进程为父进程,返回的值为子进程id。 看看这个节目。我添加了
usleep
调用,以使流程一个接一个地打印:

#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
int main(){
    // lets create a family!
    pid_t grandpa_pid = getpid();
    pid_t child_pid;
    if ((child_pid = fork()) == 0){
        pid_t parent_pid = getpid();
        pid_t child_child_pid;
        if ((child_child_pid = fork()) == 0) {
            usleep(200);
            printf("I am child and I will write third. My pid is %d, %d is my parent and %d is my parents parent, so my grandpa.", getpid(), parent_pid, grandpa_pid);
        } else {
            usleep(100);
            // parent_pid = getpid()
            printf("I am parent and I will write second. My pid is %d, %d is my child and %d is my parent.", getpid(), child_child_pid, grandpa_pid);
        }
    } else {
        // grandpa_pid == getpid()
        printf("I am grandpa and I will write first. My pid is %d and %d is my child.", getpid(), child_pid);
    }
    printf("\n");
}

如果您的子进程执行
fork
,那么它也将成为父进程。然后,按照家庭术语,您将有一个祖父母,一个祖父母也是祖父母的孩子,还有一个父母是祖父母的孩子。看起来您在第一个
{
}
if之后缺少了一些大括号。您继续编辑代码,但您当前的代码创建了一个孩子和一个孙子。我不知道你问的是什么问题。你有没有试着在else子句中插入一些打印语句,只是运行代码?你是否混淆了状态和pid?他们有时会匹配。。。
do ma 
do ma to
do to
do
do ma
do do to
to
On success, the PID of the child process is returned in the parent, 
and 0 is returned in the child. On failure, -1 is returned in the parent, 
no child process is created, and errno is set appropriately.
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
int main(){
    // lets create a family!
    pid_t grandpa_pid = getpid();
    pid_t child_pid;
    if ((child_pid = fork()) == 0){
        pid_t parent_pid = getpid();
        pid_t child_child_pid;
        if ((child_child_pid = fork()) == 0) {
            usleep(200);
            printf("I am child and I will write third. My pid is %d, %d is my parent and %d is my parents parent, so my grandpa.", getpid(), parent_pid, grandpa_pid);
        } else {
            usleep(100);
            // parent_pid = getpid()
            printf("I am parent and I will write second. My pid is %d, %d is my child and %d is my parent.", getpid(), child_child_pid, grandpa_pid);
        }
    } else {
        // grandpa_pid == getpid()
        printf("I am grandpa and I will write first. My pid is %d and %d is my child.", getpid(), child_pid);
    }
    printf("\n");
}
I am grandpa and I will write first. My pid is 5 and 6 is my child.                                                                                                                
I am parent and I will write second. My pid is 6, 7 is my child and 5 is my parent.                                                                                                
I am child and I will write third. My pid is 7, 6 is my parent and 5 is my parents parent, so my grandpa.