C语言中的Fork()函数 [英] Fork() function in C

查看:368
本文介绍了C语言中的Fork()函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是运行中的Fork函数的示例.下面也是输出.我的主要问题与fork有关,即如何更改值.因此,pid1,2和3从0开始,并在发生分叉时被更改.这是因为每次发生派生时都会将值复制到子项中,而特定值会在父项中更改吗?基本上,值如何通过fork函数更改?

Below is an example of the Fork function in action. Below is also the output. My main question has to to do with the a fork is called how values are changed. So pid1,2 and 3 start off at 0 and get changed as the forks happen. Is this because each time a fork happens the values are copied to the child and the specific value gets changed in the parent? Basically how do values change with fork functions?

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

int main() {
    pid_t pid1, pid2, pid3;
    pid1=0, pid2=0, pid3=0;
    pid1= fork(); /* A */
    if(pid1==0){
        pid2=fork(); /* B */
        pid3=fork(); /* C */
    } else {
        pid3=fork(); /* D */
        if(pid3==0) {
            pid2=fork(); /* E */
        }
        if((pid1 == 0)&&(pid2 == 0))
            printf("Level 1\n");
        if(pid1 !=0)
            printf("Level 2\n");
        if(pid2 !=0)
           printf("Level 3\n");
        if(pid3 !=0)
           printf("Level 4\n");
       return 0;
    }
}

然后这就是执行.

----A----D--------- (pid1!=0, pid2==0(as initialized), pid3!=0, print "Level 2" and "Level 4")
    |    |
    |    +----E---- (pid1!=0, pid2!=0, pid3==0, print "Level 2" and "Level 3")
    |         |
    |         +---- (pid1!=0, pid2==0, pid3==0, print "Level 2")
    |
    +----B----C---- (pid1==0, pid2!=0, pid3!=0, print nothing)
         |    |
         |    +---- (pid1==0, pid2==0, pid3==0, print nothing)
         |
         +----C---- (pid1==0, pid2==0, pid3!=0, print nothing)
              |
              +---- (pid1==0, pid2==0, pid3==0, print nothing)

理想情况下,我希望看到这种解释,因为这种方式对我来说很有意义. *是我主要的困惑所在.例如,当子进程分叉pid1 = fork();时,该进程将使用父进程的所有值创建一个进程,但随后是否将诸如1的值传递给父进程pid1?意味着孩子将具有pid 1 = 0,pid2 = 0和pid3 = 0,然后父级则为pid1 = 2且pid2和3等于0?

Ideally below is how I would like to see it explained as this way makes sense to me. The * are where my main confusion lies. When the child forks for example pid1 = fork(); that creates a process with all the values of the parent, but does it then pass up a value like lets say 1 to the parents pid1? Meaning the child would have pid 1=0, pid2=0 and pid3=0 and the parent then as pid1=2 and pid2 and 3 equal to 0?

推荐答案

系统调用fork()用于创建进程.它不接受任何参数,并返回一个进程ID. fork()的目的是创建一个新进程,该进程成为调用方的子进程.创建新的子进程后,两个进程都将在fork()系统调用之后执行下一条指令.因此,我们必须将父母与孩子区分开.这可以通过测试fork()的返回值来完成

System call fork() is used to create processes. It takes no arguments and returns a process ID. The purpose of fork() is to create a new process, which becomes the child process of the caller. After a new child process is created, both processes will execute the next instruction following the fork() system call. Therefore, we have to distinguish the parent from the child. This can be done by testing the returned value of fork()

Fork是一个系统调用,您不应将其视为普通的C函数.当fork()发生时,您可以有效地创建两个具有自己地址空间的新进程.在fork()调用之前初始化的变量在两个地址空间中存储相同的值.但是,在任一进程的地址空间内修改的值在其他进程中均不受影响,其中一个是父进程,另一个是子进程. 所以,如果

Fork is a system call and you shouldnt think of it as a normal C function. When a fork() occurs you effectively create two new processes with their own address space.Variable that are initialized before the fork() call store the same values in both the address space. However values modified within the address space of either of the process remain unaffected in other process one of which is parent and the other is child. So if,

pid=fork();

如果在随后的代码块中检查pid的值,则两个进程都将在代码的整个长度上运行.那么我们如何区分它们. 同样, Fork是系统调用,这是有区别的.在新创建的子进程pid内将存储0,而在父进程中将存储一个正值.在pid内的负值表示一个fork错误.

If in the subsequent blocks of code you check the value of pid.Both processes run for the entire length of your code. So how do we distinguish them. Again Fork is a system call and here is difference.Inside the newly created child process pid will store 0 while in the parent process it would store a positive value.A negative value inside pid indicates a fork error.

当我们测试pid的值时 要找出它等于零还是大于零,我们正在有效地发现我们是在子进程还是父进程中.

When we test the value of pid to find whether it is equal to zero or greater than it we are effectively finding out whether we are in the child process or the parent process.

详细了解Fork

这篇关于C语言中的Fork()函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆