简体   繁体   English

fork()和父/子进程ID

[英]fork() and parent/child process ids

I am a bit confused about why the child process in the following two programs is showing different parents ids. 我对以下两个程序中的子进程为何显示不同的父代id感到困惑。

First program: 第一个程序:

 int main ( void ) {
   int pid, fpid, ppid;
   fpid = fork ();
   pid = getpid();
   ppid = getppid();
   printf ("fpid is %d\n", fpid);
   sleep(5);
   if (fpid > 0){
       printf ("\nThis is Parent. My pid %d. My parent's pid %d\n",pid,ppid);
   }
   else if (fpid ==0){
       sleep(1);
       printf ("\nThis is Child. My pid %d. My parent's pid %d\n",pid,ppid);
   }
   else
       printf ("fork failed\n");
   return (0);
}

Output: 输出:

fpid is 53560
fpid is 0

This is Parent. My pid 53559. My parent's pid 44632

MacBook-Pro:~/Desktop/$ 

This is Child. My pid 53560. My parent's pid 53559

Second program: 第二个程序:

int main ( void ) {
   int pid, fpid, ppid;
   fpid = fork ();
   printf ("fpid is is %d\n", fpid);
   sleep(5);
   if (fpid > 0){
       pid = getpid();
       ppid = getppid();
       printf ("\nThis is Parent. My pid %d. My parent's pid %d\n",pid,ppid);
   }
   else if (fpid ==0){
       sleep(1);
       pid = getpid();
       ppid = getppid();
       printf ("\nThis is Child. My pid %d. My parent's pid %d\n",pid,ppid);
   }
   else
       printf ("fork failed\n");
   return (0);
}

Output: 输出:

fpid is is 53635
fpid is is 0

This is Parent. My pid 53634. My parent's pid 44632

MacBook-Pro:~/Desktop$ 

This is Child. My pid 53635. My parent's pid 1

I understand that process 1 is the process that takes over as a parent once the original parent terminates. 我知道进程1是原父终止后接任父进程的过程。 I guess what I want to know is: isn't the parent process being finished before the child process can process its printf in both cases? 我想我想知道的是:在这两种情况下,父进程不是在子进程可以处理其printf之前完成的吗? Shouldn't the outputs be the same? 输出不应该相同吗?

Since parent and child processes run concurrently, the order of execution depends on runtime. 由于父进程和子进程同时运行,因此执行顺序取决于运行时。 One of them can finish earlier. 其中之一可以提前完成。 When parent finishes before child reaches its getppid() , child process would be adopted by init . 当父项在子项到达其getppid()之前完成时, init将采用子项进程。 Hence the parent id 1. 因此,父代ID为1。
To see child's actual parent process id: 要查看孩子的实际父进程ID:

  1. Let the parent wait for its child termination using wait() or waitpid() , or 让父级使用wait()waitpid()等待其子级终止,或者
  2. Let parent sleep for some perceivable amount like sleep(120) after 'This is parent' printf() . 在'This is printf()之后,让父级以某种可觉察的量睡眠,例如sleep(120) printf()

isn't the parent process being finished before the child process can process it's printf in both cases? 在这两种情况下,父进程不是在子进程可以处理其printf之前完成的吗?

Very likely so, but not absolutely certain. 很有可能这样,但不是绝对确定的。 You cannot ensure that by sleep() ing for any length of time. 你不能保证sleep()荷兰国际集团任何时间长度。

Shouldn't the outputs be the same? 输出不应该相同吗?

They could be the same, but they are unlikely to be. 它们可能相同,但不太可能。 It's important to note when in each program getppid() is called. 请务必注意在每个程序中getppid()调用getppid() The parent process ID reported is the one that applied at the time of the call; 报告的父进程ID是调用时应用的父进程ID。 it is not necessarily still applicable later, when the value is printed. 当打印该值时,它不一定在以后仍然适用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM