2个或更多fork系统调用如何工作? [英] How do 2 or more fork system calls work?

查看:72
本文介绍了2个或更多fork系统调用如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个代码,其中我使用2个fork()系统一个接一个地调用- 它实际上是如何工作的?

 #include <unistd.h>
 #include <iostream.h>
 using namespace std;

 int main()
 {
    cout << "0. I am process " << getpid() << endl;
    (void) fork();
    cout << "1. I am process " << getpid() << endl;
    (void) fork();
    cout << "2. I am process " << getpid() << endl;
}

我得到的输出为:
0.我正在处理27701
1.我正在处理25915
1.我正在处理27701
2.我正在处理27781
2.我正在处理26170
2.我正在处理27701

这是我使用3个fork系统调用的下一个程序,如何获得这样的输出?如果我要手动解决此代码,那么逻辑是什么?

#include <unistd.h>
#include <iostream>
using namespace std;

int main()
{
    cout << "0. I am process " << getpid() << endl;
    (void) fork();
    cout << "1. I am process " << getpid() << endl;
    (void) fork();
    cout << "2. I am process " << getpid() << endl;
    (void) fork();
    cout << "3. I am process " << getpid() << endl;
}

在这里我得到的输出是:
0.我正在处理27116
1.我正在处理26147
2.我正在处理27371
2.我正在处理26147
3.我正在处理24416
3.我正在处理27371
3.我正在处理27508
3.我正在处理26147
1.我正在处理27116
2.我正在处理21406
2.我正在处理27116
3.我正在处理27369
3.我正在处理21406
3.我正在处理26752
3.我正在处理27116

解决方案

您的程序完全错误.您应该切勿忽略fork 的结果.

阅读高级Linux编程书和

同样,对于pid_t pid2=fork();,然后对于pid_t pid3=fork();,依此类推. 因此,每次调用fork都应处理3种情况fork的结果(失败,即<0,子进程==0,父进程>0)

原则上,您有3 3 ,即27种可能性.但是您可以尽早处理失败案例,剩下2 3 即8种可能性

别忘了处理fork的故障.您可以使用以下方法降低进程限制(使用 setrlimit(2) RLIMIT_NPROC或等效的 ulimit bash内置)来简化对fork失败的测试. /p>

Here's a code where I use 2 fork() system calls one after another - How does it actually work?

 #include <unistd.h>
 #include <iostream.h>
 using namespace std;

 int main()
 {
    cout << "0. I am process " << getpid() << endl;
    (void) fork();
    cout << "1. I am process " << getpid() << endl;
    (void) fork();
    cout << "2. I am process " << getpid() << endl;
}

I get the output as :
0. I am process 27701
1. I am process 25915
1. I am process 27701
2. I am process 27781
2. I am process 26170
2. I am process 27701

This is the next program where I've used 3 fork system calls, how do I get such an output? If I were to solve this code manually, what would be the logic?

#include <unistd.h>
#include <iostream>
using namespace std;

int main()
{
    cout << "0. I am process " << getpid() << endl;
    (void) fork();
    cout << "1. I am process " << getpid() << endl;
    (void) fork();
    cout << "2. I am process " << getpid() << endl;
    (void) fork();
    cout << "3. I am process " << getpid() << endl;
}

Here I get the output as :
0. I am process 27116
1. I am process 26147
2. I am process 27371
2. I am process 26147
3. I am process 24416
3. I am process 27371
3. I am process 27508
3. I am process 26147
1. I am process 27116
2. I am process 21406
2. I am process 27116
3. I am process 27369
3. I am process 21406
3. I am process 26752
3. I am process 27116

解决方案

Your program is utterly wrong. You should never ignore the result of fork.

Read the Advanced Linux programming book and the fork(2) man page (read that page several times and carefully).

Typical code should be:

  pid_t pid1 = fork();
  if (pid1<0) { perror("fork1 failed"); exit(EXIT_FAILURE); }
  else if (pid1 == 0) {
     // you are in the child process
  }
  else // pid1>0 
  {  // you are in the parent process
  }

And likewise for pid_t pid2=fork(); and then for pid_t pid3=fork(); etc.... So each call to fork should handle the 3 cases of result of fork (failure i.e. <0, child process ==0, parent process >0)

In principle you have 33 i.e. 27 possibilities. But you could handle early the failure case, which leaves 23 i.e. 8. possibilities

Don't forget to handle the failure of fork. You might lower your process limit (with setrlimit(2) using RLIMIT_NPROC or the equivalent ulimit bash builtin) to ease the test of fork failure.

这篇关于2个或更多fork系统调用如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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