当子进程 fork 时,fork() 是如何工作的?

发布于 2024-12-07 12:38:44 字数 430 浏览 1 评论 0原文

我已经执行了一段代码。如下所示:

#include<stdio.h>

main() {
int i=0;
fork();
printf("The value of i is:%d\n",++i);
fork();
printf("The value of j is:%d\n",++i);
fork();
wait();
}

我得到以下输出:

The value of i is:1
The value of j is:2
The value of i is:1
The value of j is:2
The value of j is:2
pckoders@ubuntu:~$ The value of j is:2

谁能向我解释一下 fork() 和 wait() 函数在这里扮演什么角色?

I have executed a block of code. And it is as shown below:

#include<stdio.h>

main() {
int i=0;
fork();
printf("The value of i is:%d\n",++i);
fork();
printf("The value of j is:%d\n",++i);
fork();
wait();
}

And I got following output:

The value of i is:1
The value of j is:2
The value of i is:1
The value of j is:2
The value of j is:2
pckoders@ubuntu:~$ The value of j is:2

Can anyone explain to me what role fork() and wait() functions play here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

鹿! 2024-12-14 12:38:45

该程序生成进程树。在每一个分叉处,这棵树都会分成两部分。如果你抓起一张纸,画出这棵树并不难;唯一困难的是由于使用了前缀 ++,因此无法正确获取 i 值。如果最后让每个进程睡眠几秒钟,您还可以使用pstree程序观察树。

然后,每个进程都运行 wait 系统调用,该调用 等待其任何一个子进程(进程树中的子节点)完成。

The program generates a tree of processes. At every fork, this tree branches in two. If you grab a piece of paper, it's not very hard to draw this tree; the only thing that's hard is getting the i values right due to your use of prefix ++. If you let each of the process sleep for a few seconds at the end, you can also observe the tree using the pstree program.

Each one of the processes then runs the wait system call, which waits for any one of its child processes (child nodes in the process tree) to finish.

锦欢 2024-12-14 12:38:45

在第一个 fork() 之后,有两个进程(当前进程及其精确副本,即子进程),它们都打印 1。

这两个进程中的每一个都通过第二个 fork() 调用复制自身,并且有 4 个进程,每个进程他们打印了 2。

他们的输出以随机顺序出现,就像并行执行时一样。

After the first fork() there were two processes (the current and it's exact copy, the child), which both printed 1.

Each of these two processes duplicated itself with the second fork() call, and there were 4 processes, each of them printed 2.

Their output comes in random order, as it always does with parallel execution.

神也荒唐 2024-12-14 12:38:45

分叉进程以类似树的方式创建子进程。将每个分叉视为二叉树的不同层。当您不发出 fork() 时,您的进程树只有一个根节点。当您发出单个分叉时() 那么你现在有一个有两层的二叉树,第一层将包含父进程,第二层将包含两个进程 - 父进程和子进程。

当你想找出手头有多少进程时,只需继续构建二进制/进程树并查看最后一层有多少个节点,最后一层只不过是进程/树的当前状态。等待函数使您的父进程等待子进程完成执行。在您不想要僵尸进程的应用程序中,您需要发出等待,否则这些僵尸进程将继续占用系统... 链接

请记住,当您总是希望父级在子级之后完成时,等待也很有用。分叉并不总是给出相同的输出,顺序是混乱的,因此要始终获得相同的输出,请使用 wait()。要等待特定子进程,请使用 wait(pid),其中 pid 是特定子进程的 pid,并且可以通过子进程空间内的 getpid 获取该 pid。

The Forking processes creates Children in a tree-like manner.Consider each fork to be the different layers of a Binary Tree.When you dont issue a fork() u have a process tree of only a root node.When u issue a single fork() then u have a binary tree with two levels now , the first level will the contain the parent process , the second level will contain two processes - the parent and the child process.

When u want to find out the number of processes u have at hand just proceed building the binary/process tree and see how many nodes are there at the last level, the last level is nothing but the current state of the process/tree.Wait function makes your parent wait for the child process to finish executing.In applications where u do not want a zombie process you need to issue a wait or else these zombie processes will keep hogging the system... link.

Remember that wait is also useful when you always want the parent to finish after the child . Forking doesnt always give the same output , the order is jumbled , thus to get the same output always , use wait(). To wait on a particular child process , use wait(pid) where the pid is the pid of a specific child and that pid can be obtained by getpid inside the child process's space.

伪心 2024-12-14 12:38:44

fork() 调用实际上是一个 fork。完成后,您将拥有两个具有精确堆栈的精确进程,并且所有描述符都引用相同的对象。您可以通过返回值来区分它们。对于子进程,fork() 返回 0,对于父进程 - 子进程 id。

所以

main() {
int i=0;
fork(); 
// at this point you are having 2 processes. stdout and stdin are basically just dupplicates.
//          (P)
//        /     \
//     (P)       (C)
//   prints1    prints 1
printf("The value of i is:%d\n",++i); // so 2 processes with print 1
fork();
// now you are having 4 processes( both parent and children forked)
//                   (P)
//                 /     \
//               /         \
//           (P)            (C)
//         /     \         /   \
//      (PP)     (PC)    (CP)  (CC)
//   prints 2  prints 2  prints 2  prints 2
printf("The value of j is:%d\n",++i);
fork();
// now 4 processes are forking. now you have 8 processes
//                                   (P)
//                                /       \
//                            /              \
//                         /                    \
//                   (P)                           (C)             
//                 /     \                       /     \           
//               /         \                   /         \          
//          (PP)           (PC)             (CP)          (CC) 
//         /     \        /     \          /    \       /     \        
//     (PPP)    (PPC)  (PCP)   (PCC)   (CPP)   (CPC)  (CCP)  (CCC)   

wait();
}

fork() call is a literally fork. After it's finished you are having 2 exact processes with exact stack and all descriptors are referring to same objects. You can distinguish them by a return value. For child process fork() returns 0, for parent - child's process id.

so

main() {
int i=0;
fork(); 
// at this point you are having 2 processes. stdout and stdin are basically just dupplicates.
//          (P)
//        /     \
//     (P)       (C)
//   prints1    prints 1
printf("The value of i is:%d\n",++i); // so 2 processes with print 1
fork();
// now you are having 4 processes( both parent and children forked)
//                   (P)
//                 /     \
//               /         \
//           (P)            (C)
//         /     \         /   \
//      (PP)     (PC)    (CP)  (CC)
//   prints 2  prints 2  prints 2  prints 2
printf("The value of j is:%d\n",++i);
fork();
// now 4 processes are forking. now you have 8 processes
//                                   (P)
//                                /       \
//                            /              \
//                         /                    \
//                   (P)                           (C)             
//                 /     \                       /     \           
//               /         \                   /         \          
//          (PP)           (PC)             (CP)          (CC) 
//         /     \        /     \          /    \       /     \        
//     (PPP)    (PPC)  (PCP)   (PCC)   (CPP)   (CPC)  (CCP)  (CCC)   

wait();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文