这段C代码的作用是什么?
尽管我已经完成了大量其他类型的编程,但我对 C 编程确实很陌生。
我想知道是否有人可以向我解释为什么这个程序输出 10。
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
int value = 10;
int main()
{
pid_t pid;
pid = fork();
if(pid == 0){
value += 10;
}
else if(pid > 0){
wait(NULL);
printf("parent: value = %d\n", value); //Line A
exit(0);
}
}
我知道输出是“parent: value = 10”。有人知道为什么吗?
谢谢!
I'm really new to C programming, although I have done quite a bit of other types of programming.
I was wondering if someone could explain to me why this program outputs 10.
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
int value = 10;
int main()
{
pid_t pid;
pid = fork();
if(pid == 0){
value += 10;
}
else if(pid > 0){
wait(NULL);
printf("parent: value = %d\n", value); //Line A
exit(0);
}
}
I know the output is "parent: value = 10". Anyone know why?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
fork
创建两个进程(“父进程”和“子进程”)。在您的示例中,每个进程都有不同的 pid 值。子进程的 pid 为 0。父进程的 pid 为子进程的操作系统 pid(由操作系统分配)。在您的示例中,每个进程在其内存中都有自己的
值
。它们不共享内存(就像您认为它们应该通过您的问题一样。)如果您更改一个进程(if的第一部分),它将不会反映在第二个进程(if的第二部分)中if。)编辑:解释了 pid 的值。
fork
creates two processes (the "parent" and the "child"). Each process has a different value of pid in your example. The child process has a pid of 0. The parent process has a pid of the child's operating system pid (assigned by the OS.)In your example, each process has it's own
value
in its memory. They do not share memory (like you think they should by your question.) If you change one process (the first part of the if) it will not be reflected in the second process (the second part of the if.)Edit: Explained the value of pid.
关于 fork() :
子进程的创建是
不成功。
创建的子进程。
子进程的进程ID,
家长。
所以在你的情况下它一定会返回一个大于 0 的数字因此该值将保持为 10 &将被打印。
About fork() :
the creation of a child process was
unsuccessful.
created child process.
process ID of the child process, to
the parent.
So in you case it bound to return a number greater than 0 & thus the value will remain 10 & will be printed.
好吧,
fork
产生了一个新进程。它或多或少地复制了当前进程,并且新进程(子进程)和旧进程(父进程)在代码中的同一点继续运行。但这里有一个显着的区别(我们感兴趣):对于子进程,fork
返回 0。对于父进程,它返回子进程的进程 ID。所以
if(pid ==0)
部分对于子进程来说是正确的。子进程简单地将 10 添加到他的值
,然后退出,因为没有进一步的代码。else
部分对于父级来说是正确的(除了fork
返回错误 -1 的极少数情况)。父级只需等待
子级退出即可。但是子进程已经修改了自己的value
副本,而父进程的副本仍然保持不变,这就是为什么你会得到“10”的输出。然后父母也退出。Well,
fork
spawns a new process. It more or less copies the current process, and both the new one (the child) and the old one (the parent) go on at the same point in the code. But there is one significant difference (that interests us) here: for the child,fork
returns 0. For the parent, it returns the process ID of the child.So the
if(pid ==0)
part is true for the child. The child simple add 10 to hisvalue
, and then exits since there is no further code.The
else
part is true for the parent (except for the very rare case thatfork
returned an error with -1). The parent simplywait
s for the child to exit. But the child has modified its own copy ofvalue
, the one of the parent is still untouched and that is why you get the output of "10". Then the parent also exits.fork()
创建一个新进程:它在两个不同的上下文中有两个返回值,因此两条路径都在 if 语句中运行。条件主要用于确定fork后运行的是哪个进程。fork()
creates a new process: it has two return values in two different contexts, so both paths run in your if statement. The conditional is mostly used to determine which process you run in after the fork.当您调用
fork
时,它会创建进程的副本,使得两个副本的程序计数器
位于其代码部分中的相同位置。因此,当这些副本中的任何一个恢复执行时,两者都将刚刚完成对fork
的调用。所以它们两者应该执行相同。
但是,
fork
在子进程中返回0
,在父进程中返回子进程的pid
。这解释了
if( pid==0 )
部分背后的魔力。因此,当子进程更改
value
的值时,它实际上会在自己的副本中更改该值(请记住:进程被复制,因此数据部分也被复制)。同时,父进程使用其旧值
value
执行,即 10。即使子进程更改其
value
副本并终止,父进程的副本仍然是 10。when you call
fork
, it creates a copy of the process in such a way that both the copies'program counter
s are at the same position in their code sections. Hence when any of these copies resumes execution, both will just be finishing the call tofork
.So both of them should execute identically.
BUT,
fork
returns0
in the child process, and thepid
of the child process in the parent process.That explains the mojo behind the
if( pid==0 )
part.So when the child process changes the value of
value
, it actually changes that in its own copy (remember: the process got copied, so the data sections got copied too).Meanwhile, the parent process executes with its old value of
value
, which is 10.Even after the child changes its copy of
value
and dies, the parent's copy is still 10.fork 系统调用创建一个新进程作为现有(父)进程的子进程。父进程和子进程都在 fork 语句后面的行继续执行,但是子进程会获得父进程地址空间的精确副本。
fork 系统调用将新创建进程的进程 ID 返回给父进程,将零返回给子进程,因此在这段代码中,子进程将增加自己的 value 变量副本,而父进程将打印出自己的副本。
您经常会在子程序中看到 fork 后跟一个 exec,以便它用另一个程序替换自己。
The fork system call creates a new process as a child of the existing (parent) process. Both the parent and the child continue execution at the line following the fork statement, however the child process is given an exact copy of the parents address space.
The fork system call returns the process id of the newly created process to the parent and zero to the child, therefore within this code the child will increment its own copy of the value variable and the parent will print out its own copy.
You will often see fork followed by an exec within the child so that it replaces itself with another program.