fork()之后的PID是多少?

发布于 2024-10-17 20:03:02 字数 173 浏览 3 评论 0原文

我正在 C 程序中连续执行 3 个 fork。
1. 是否会按照相同的顺序执行? (我的猜测是肯定的)。
2. 如果我从 shell 执行 pgrep myexecutable ,它会按照启动时的顺序给出进程 ID 吗? (我的猜测是否定的,因为你不能保证系统给孩子的pid是什么,对吧?)

I am doing 3 consecutive forks in a C program.
1. Is it going to execute in the same order ? ( My guess is yes ).
2. If I do a pgrep myexecutable from shell, would it give the process ids in the same order as they were started ? ( my guess is no, because you cant guarantee what pid the system gives the child, right ? )

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

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

发布评论

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

评论(2

梦纸 2024-10-24 20:03:02

执行 3 个 fork 后,总共将运行 8 个进程

在此处输入图像描述

所以现在 pid 将取决于子进程的创建顺序以及子进程的子进程的创建顺序。

可能就像

main - 12345

child1_of_main_after_fork1  12346

child2_of_child1_after_fork2  12347

child3_of_main_after_fork2 12348

child4_of_main_after_fork3 12349

child5_of_child1_after_fork3 12350

child6_of_child2_after_fork3 12351

child7_of_child3_after_fork3 12352

There would be a total of 8 processes running after 3 forks are executed

enter image description here

so now the pid would be depend on which order the child process are created by and also in which order the children of children are created.

could be like

main - 12345

child1_of_main_after_fork1  12346

child2_of_child1_after_fork2  12347

child3_of_main_after_fork2 12348

child4_of_main_after_fork3 12349

child5_of_child1_after_fork3 12350

child6_of_child2_after_fork3 12351

child7_of_child3_after_fork3 12352
暖阳 2024-10-24 20:03:02

Shrinath,您应该检查 fork() 的文档,如下:

Upon successful completion, fork() returns a value of 0 to the child
process and returns the process ID of the child process to the parent
process.  Otherwise, a value of -1 is returned to the parent process, no
child process is created, and the global variable errno is set to indi-
cate the error.

对您来说,这意味着您的父进程在分叉时将获得子进程的 PID。子进程知道它是子进程,因为 fork() 会将 0 返回给子进程,因此类似于:

if((cpid = fork()))
{ 
  // This is the parent processs, child pid 
  // is in `cpid` variable
}else{
  // This is the child process, do your child
  // work here.
}

当心您得到负数的机会(因此没有子进程),您应该检查那。

ps 的输出会因系统而异,但如果您查看整个树,您应该会看到一个示例(让您的进程休眠,以便您有时间检查 ps) > 输出。)

Shrinath, you should check the documentation for fork(), here it is:

Upon successful completion, fork() returns a value of 0 to the child
process and returns the process ID of the child process to the parent
process.  Otherwise, a value of -1 is returned to the parent process, no
child process is created, and the global variable errno is set to indi-
cate the error.

All that means for you, is that means your parent process will get the child's PID when it forks. The child knows it's the child, because fork() will return 0 to the child, so something like:

if((cpid = fork()))
{ 
  // This is the parent processs, child pid 
  // is in `cpid` variable
}else{
  // This is the child process, do your child
  // work here.
}

Beware for the chance that you get a minus number (so there's no child), you should check for that.

The output of ps will vary by system, but you should see an example, if you look at the whole tree (make your processes sleep, so you have time to check the ps output.)

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