C fork 处理全局变量
我不理解这个程序的输出:
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
int i = 0;
int main()
{
while(i<3)
{
fork();
printf("%d\n",i);
++i;
}
}
输出是:
0
1
2
2
1
2
0
1
2
2
2
1
2
2
请有人告诉我应该如何解决这个问题,以便完全理解为什么我会得到这个输出?
I'm not understanding the output of this program:
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
int i = 0;
int main()
{
while(i<3)
{
fork();
printf("%d\n",i);
++i;
}
}
The output is:
0
1
2
2
1
2
0
1
2
2
2
1
2
2
Can please someone tell me how I should tackle this issue in order to fully understand why I'm getting this output?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Fork 将复制该进程。进程的独立副本。因此,如果在 fork 时全局变量包含 3,则进程的每个副本都会获得自己的 3。如果它们进行修改,则它们的修改是完全独立的。
Fork will make a copy of the process. An independent copy of the process. So, if a global variable contains 3 at the time you fork, each copy of the process gets their very own 3. And if they modify, their modifications are completely independent.
将代码更改为这样,输出应该更有意义:
Change your code to this and the output should make a lot more sense:
当您 fork() 时,将在当前状态下创建当前进程的完整副本。这意味着您的初始进程将创建三个位于 while 循环中间的新进程,其中每个进程的
i
分别为 0、1 和 2。它还会打印他自己的i
值。它的每个子级将通过打印其初始
i
值、递增和循环来继续从fork()
调用开始的循环。这意味着子级 0 将打印 0、1 和 2,并生成两个新子级,其“初始”值为i
1 和 2。子级 1 将打印 1 和 2,并再生成一个子级,i
的“初始”值为 2。子级 2 将打印 2 并离开循环。如果继续这个推理,您将得出这样的结论:总共将打印两个 0、四个 1 和八个 2。但是,由于执行顺序取决于操作系统如何调度并发进程,因此您无法保证这些进程的打印顺序。
When you fork(), a complete copy of the current process is created in its current state. This means that your initial process will create three new processes that are in the middle of the while loop, with
i
being respectively 0, 1, and 2 in each one of them. It will also print his own values ofi
.Each of its children will continue the loop from the
fork()
call by printing out its initiali
value, incrementing and looping. This means that children 0 will print 0, 1, and 2, and spawn two new children, with "initial" values ofi
1 and 2. Children 1 will print 1 and 2 and spawn one more children, with an "initial" value ofi
of 2. Children 2 will print 2 and leave the loop.If you continue this reasoning you will come to the conclusion that in total two 0's, four 1's and eight 2's will be printed. But, since the order of execution depends on how the OS schedules the concurrent processes, you cannot have guarantees on the order those are printed.
如果您想在进程内创建线程以进行并发编程,请尝试使用 pthreads。您想要的函数是 pthread_create 和 pthread_join 以便稍后进行整理。
像这样:
但也许不是,这取决于你的实际需要。
Try using pthreads if you want to create a thread inside the process for concurrent programming. The function you want is pthread_create and pthread_join for tidying up later.
Something like this:
But perhaps not, depending on your actual needs.
它类似于...
...等
但是,进程输出的顺序是不确定的,因此您可能不会每次都看到完全相同的输出,即使您这样做,也无法保证。
正如其他人所说,每个进程都有自己跟踪的全局“i”,其值只是分叉进程的 i 在分叉处的值。
It's something like...
...etc
The order that the processes are outputting in, however, is undetermined, so you likely won't see the exact same output every time, and even if you do it's not something you can guarantee.
As other people said, each process has its own global 'i' that it keeps track of, and its value is simply the value of the forking process's i at the fork.