Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 试图理解fork函数返回值的逻辑_C++_Linux_Operating System - Fatal编程技术网

C++ 试图理解fork函数返回值的逻辑

C++ 试图理解fork函数返回值的逻辑,c++,linux,operating-system,C++,Linux,Operating System,考虑一下这个简单的程序: using namespace std; int main(int argc, const char * argv[]) { cout << "first pid: " << getpid() << endl; int a = fork(); int b = fork(); cout << a << " " << b << endl; retur

考虑一下这个简单的程序:

using namespace std;

int main(int argc, const char * argv[]) {
    cout << "first pid: " << getpid() << endl;
    int a = fork();
    int b = fork();
    cout << a << " " << b << endl;
    return 0;
}
我所了解的是,现在有4个进程处于活动状态,因为第一个fork包含2个进程,第二个fork将它们分成4个进程。
我不理解的是打印到控制台的ID的逻辑。

逻辑是零表示您是
fork()的子对象,-1表示错误,任何正数都是子对象的PID,您是父对象


关于如何分配PID,没有指定的“逻辑”。不要查找任何值。

逻辑是零表示您是
fork()的子对象,-1表示错误,任何正数表示子对象的PID,您是父对象


关于如何分配PID,没有指定的“逻辑”。不要找任何东西。

我在OP中添加了一些注释来描述发生了什么:

using namespace std;

int main(int argc, const char * argv[]) {
    cout << "first pid: " << getpid() << endl;
    int a = fork();
    // from here two processes run:
    // in parent a is PID of child
    // in child a is 0
    int b = fork();
    // from here four processes run:
    // a is for parent and child identical 0 or PID
    // in parents b is PID of child
    // in children b is 0
    // Thus, the following shows all 4 combinations of 0 and not 0.
    cout << a << " " << b << endl;
    return 0;
}
使用名称空间std;
int main(int argc,const char*argv[]{

cout我在OP中添加了注释来描述发生了什么:

using namespace std;

int main(int argc, const char * argv[]) {
    cout << "first pid: " << getpid() << endl;
    int a = fork();
    // from here two processes run:
    // in parent a is PID of child
    // in child a is 0
    int b = fork();
    // from here four processes run:
    // a is for parent and child identical 0 or PID
    // in parents b is PID of child
    // in children b is 0
    // Thus, the following shows all 4 combinations of 0 and not 0.
    cout << a << " " << b << endl;
    return 0;
}
使用名称空间std;
int main(int argc,const char*argv[]{

cout这是一张正在发生的事情的表格:

gen_0     | gen_1         | gen_2_0       | gen_2_1
          |               |               |
==[ before first fork ]==================================
started   |               |               |
pid = 123 |               |               |
          |               |               |
--[ after first fork ]===================================
          | started       |               |
          | pid = 1111    |               |
          | parent: gen_0 |               |
a = 1111  | a = 0         |               |
          |               |               |
--[ after second fork ]==================================
          |               | started       | started
          |               | pid = 2222    | pid = 3333
          |               | parent: gen_0 | parent: gen_1
a = 1111  | a = 0         | a = 1111      | a = 0
b = 2222  | b = 3333      | b = 0         | b = 0

下面是一张正在发生的事情的表格:

gen_0     | gen_1         | gen_2_0       | gen_2_1
          |               |               |
==[ before first fork ]==================================
started   |               |               |
pid = 123 |               |               |
          |               |               |
--[ after first fork ]===================================
          | started       |               |
          | pid = 1111    |               |
          | parent: gen_0 |               |
a = 1111  | a = 0         |               |
          |               |               |
--[ after second fork ]==================================
          |               | started       | started
          |               | pid = 2222    | pid = 3333
          |               | parent: gen_0 | parent: gen_1
a = 1111  | a = 0         | a = 1111      | a = 0
b = 2222  | b = 3333      | b = 0         | b = 0

由于子进程未同步,因此它们以随机顺序打印。ID由操作系统根据其自身固有的“逻辑”分配。必须(由操作系统)授予ID唯一性,但除此之外没有其他(接受应该有一个特殊的No PID值).64541在第一个输出中出现两次,64628在第二个输出中出现两次。unix系统上的PID通常按升序分配。需要记住的重要一点是
fork
是唯一返回两次的函数:一次在父进程中,一次在子进程中。在子进程中
fork
返回
0
进程之间没有同步,一个进程可能在另一个进程调用第二个
fork
调用之前运行完成,这意味着可以重用进程id。0 0由第二个fork()的子进程打印,该子进程是第一个fork()的子进程。!0!0由第二个fork()的父进程打印它是第一个fork()中的父进程…由于子进程未同步,它们以随机顺序打印。ID由操作系统根据其自身的内在“逻辑”进行分配。必须(由操作系统)授予ID唯一性,但除此之外没有其他(接受应该有一个特殊的No PID值).64541在第一个输出中出现两次,64628在第二个输出中出现两次。unix系统上的PID通常按升序分配。需要记住的重要一点是
fork
是唯一返回两次的函数:一次在父进程中,一次在子进程中。在子进程中
fork
返回
0
进程之间没有同步,一个进程可能在另一个进程调用第二个
fork
调用之前运行完成,这意味着可以重用进程id。0 0由第二个fork()的子进程打印,该子进程是第一个fork()的子进程。!0!0由第二个fork()的父进程打印第一个fork()中的父对象…但有一种模式,在每次运行中,有一个进程打印2个非0的数字,其余进程打印1或2个零。对此有何解释?有一种模式,但在每次运行中,有一个进程打印2个非0的数字,其余进程打印1或2个零。对此有何解释?