我正在尝试学习 UNIX 编程,并遇到了一个问题fork(),我无法解释下面 2 个程序的输出。
我知道这fork()会创建与当前正在运行的进程相同的进程,但它从哪里开始呢?例如,如果我有下面这两个程序,输出是什么以及它是如何工作的?
#include<stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main (int argc, char **argv)
{
int retval;
printf ("This is most definitely the parent process\n");
// now here fork will create a child process
// i need to know from which line child process starts execution
retval = fork ();
printf ("Which process printed this?\n");
return (0);
}
Run Code Online (Sandbox Code Playgroud)
在子进程执行方面,上面的程序和下面的程序有什么区别:
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int main (int argc, char **argv)
{
int …Run Code Online (Sandbox Code Playgroud)