C 使用fork创建进程

C 使用fork创建进程,c,unix,process,fork,C,Unix,Process,Fork,我试图理解如何创建叉树,有没有简单的方法可以理解 例如: include<stdio.h> include<unistd.h> void main(){ fork(); if fork(); if fork(); fork(); sleep(10); } 包括 包括 void main(){ fork(); if fork(); if fork(); fork(); 睡眠(10); } 来自linux手册: fork()通过复制调用进程来创建新进程 基本上,

我试图理解如何创建叉树,有没有简单的方法可以理解

例如:

    include<stdio.h>
include<unistd.h>
void main(){

fork();
if fork();
if fork();
fork();
sleep(10);

}
包括
包括
void main(){
fork();
if fork();
if fork();
fork();
睡眠(10);
}
来自linux手册:

fork()通过复制调用进程来创建新进程

基本上,它创建了一个称为子进程的新进程,它与称为父进程的调用进程完全相同,代码相同,除了一些事情(请看一下
manfork
)。如果您是父进程,它将返回
子进程ID
;如果您是子进程,它将返回
0
;如果您是子进程,它将返回
-1
(并在失败时将
errno
)设置为父进程。下面是叉树的代码示例:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

/*
 * I'm going to create a fork tree
 * 
 */


int main(){
    pid_t pid; /*Use it for fork() calls*/
    pid = fork(); /*Generating the first child*/
    if(pid == 0){ /*I'm the child*/
        pid_t pid_child = fork();
        if(pid_child == 0){ /*I'm the grandchild*/
            printf("I'M THE GRANDCHILD\n");
            return 0; /*Terminates the new process*/
        }else if(pid_child > 0){ /* I'm the child*/ 
            waitpid(pid_child,NULL,0);
            printf("I'M THE CHILD\n");
            return 0; /*Terminates the new process*/
        }
    }else if(pid > 0){ /*I'm the parent*/
        waitpid(pid,NULL,0); /*Waiting for the child*/
        printf("I'M THE PARENT\n");
    }
    return 0;
}
#包括
#包括
#包括
#包括
/*
*我将创建一个叉树
* 
*/
int main(){
pid\u t pid;/*用于fork()调用*/
pid=fork();/*生成第一个子级*/
如果(pid==0){/*我是孩子*/
pid_t pid_child=fork();
如果(pid_child==0){/*我是孙子*/
printf(“我是孙辈”\n);
返回0;/*终止新进程*/
}否则如果(pid_child>0){/*我是孩子*/
waitpid(pid_子项,NULL,0);
printf(“我是孩子”\n);
返回0;/*终止新进程*/
}
}否则如果(pid>0){/*我是家长*/
waitpid(pid,NULL,0);/*正在等待子项*/
printf(“我是父母”);
}
返回0;
}
来自linux手册:

fork()通过复制调用进程来创建新进程

基本上,它创建了一个称为子进程的新进程,它与称为父进程的调用进程完全相同,代码相同,除了一些事情(请看一下
manfork
)。如果您是父进程,它将返回
子进程ID
;如果您是子进程,它将返回
0
;如果您是子进程,它将返回
-1
(并在失败时将
errno
)设置为父进程。下面是叉树的代码示例:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

/*
 * I'm going to create a fork tree
 * 
 */


int main(){
    pid_t pid; /*Use it for fork() calls*/
    pid = fork(); /*Generating the first child*/
    if(pid == 0){ /*I'm the child*/
        pid_t pid_child = fork();
        if(pid_child == 0){ /*I'm the grandchild*/
            printf("I'M THE GRANDCHILD\n");
            return 0; /*Terminates the new process*/
        }else if(pid_child > 0){ /* I'm the child*/ 
            waitpid(pid_child,NULL,0);
            printf("I'M THE CHILD\n");
            return 0; /*Terminates the new process*/
        }
    }else if(pid > 0){ /*I'm the parent*/
        waitpid(pid,NULL,0); /*Waiting for the child*/
        printf("I'M THE PARENT\n");
    }
    return 0;
}
#包括
#包括
#包括
#包括
/*
*我将创建一个叉树
* 
*/
int main(){
pid\u t pid;/*用于fork()调用*/
pid=fork();/*生成第一个子级*/
如果(pid==0){/*我是孩子*/
pid_t pid_child=fork();
如果(pid_child==0){/*我是孙子*/
printf(“我是孙辈”\n);
返回0;/*终止新进程*/
}否则如果(pid_child>0){/*我是孩子*/
waitpid(pid_子项,NULL,0);
printf(“我是孩子”\n);
返回0;/*终止新进程*/
}
}否则如果(pid>0){/*我是家长*/
waitpid(pid,NULL,0);/*正在等待子项*/
printf(“我是父母”);
}
返回0;
}

每次调用
fork()
时,您都在创建一个,该子拥有父亲目前拥有的确切代码,但它拥有自己的内存映射

然后,您必须使用相同的代码创建两个进程。如果你想让他们做一些不同的事情,你必须使用
fork()
的return。Fork返回孩子的pid,并在父亲的内存中“分配”它。通过这种机制,父亲可以使用只有他自己知道的pid(进程ID)来引用孩子。如果child试图通过
fork()
查看为其创建的确切pid,则无法看到,并且将为零(因为fork将pid返回给其他child进程的进程)

上述示例代码如下所示:

void  main(void)
{
    char sth[20]="something";
    pid_t  pid;

    pid = fork(); // Create a child
    // At this line (so this specific comment if you may like) has 2 processes with the above code
    printf("I am process with ID<%ld> and i will print sth var <%s>", getpid(),sth);
    // The above printf would be printed by both processes because you haven't issued yet a way to make each process run a different code.
    // To do that you have to create the following if statement and check PID according to what said above.
    if (pid == 0) // If PID == 0, child will run the code
        printf("Hello from child process with pid <%ld>",getpid());
        printf(", created by process with id <%ld>\n",getppid());
    else          // Else the father would run the code
        printf("Hello from father process with pid <%ld>",getpid());
}
void主管道(void)
{
char sth[20]=“某物”;
pid_t pid;
pid=fork();//创建子对象
//在这一行(如果您愿意,也可以使用这个特定的注释)有两个具有上述代码的进程
printf(“我是一个ID为的进程,我将打印sth var”,getpid(),sth);
//上面的printf将由两个进程打印,因为您还没有发布一种使每个进程运行不同代码的方法。
//要做到这一点,您必须创建下面的if语句,并根据上面所说的内容检查PID。
if(pid==0)//如果pid==0,子级将运行代码
printf(“Hello from child process with pid”,getpid());
printf(“,由id为\n的进程创建”,getppid());
否则,父亲就会运行代码
printf(“来自具有pid的父进程的Hello”,getpid());
}

我尽量表现得天真。希望它能有所帮助。

每次你调用
fork()
时,你都在创建一个孩子,这个孩子拥有父亲到目前为止所拥有的确切代码,但它拥有自己的记忆地图

然后,您必须使用相同的代码创建两个进程。如果你想让他们做一些不同的事情,你必须使用
fork()
的return。Fork返回孩子的pid,并在父亲的内存中“分配”它。通过这种机制,父亲可以使用只有他自己知道的pid(进程ID)来引用孩子。如果child试图通过
fork()
查看为其创建的确切pid,则无法看到,并且将为零(因为fork将pid返回给其他child进程的进程)

上述示例代码如下所示:

void  main(void)
{
    char sth[20]="something";
    pid_t  pid;

    pid = fork(); // Create a child
    // At this line (so this specific comment if you may like) has 2 processes with the above code
    printf("I am process with ID<%ld> and i will print sth var <%s>", getpid(),sth);
    // The above printf would be printed by both processes because you haven't issued yet a way to make each process run a different code.
    // To do that you have to create the following if statement and check PID according to what said above.
    if (pid == 0) // If PID == 0, child will run the code
        printf("Hello from child process with pid <%ld>",getpid());
        printf(", created by process with id <%ld>\n",getppid());
    else          // Else the father would run the code
        printf("Hello from father process with pid <%ld>",getpid());
}
void主管道(void)
{
char sth[20]=“某物”;
pid_t pid;
pid=fork();//创建子对象
//在这一行(如果您愿意,也可以使用这个特定的注释)有两个具有上述代码的进程
printf(“我是一个ID为的进程,我将打印sth var”,getpid(),sth);
//上面的printf将由两个进程打印,因为您还没有发布一种使每个进程运行不同代码的方法。
//要做到这一点,您必须创建下面的if语句,并根据上面所说的内容检查PID。
if(pid==0)//如果pid==0,子级将运行代码
printf(“Hello from child process with pid”,getpid());