//same program different code
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
int pid;
pid=fork();
if(pid<0)
{
printf("\n Error ");
exit(1);
}
else if(pid==0)
{
printf("\n Hello I am the child process ");
printf("\n My pid is %d ",getpid());
exit(0);
}
else
{
printf("\n Hello I am the parent process ");
printf("\n My actual pid is %d \n ",getpid());
exit(1);
}
}
我试过这个,我希望它是正确的。
但我对输出不满意。
输出是:
Hello I am the parent process
My actual pid is 4287
ashu@ashu-VirtualWorld:~/Desktop/4thSemester/testprep$
Hello I am the child process
My pid is 4288
请帮助我,我无法理解它的输出,我希望子进程首先发生,然后是父进程。另外,当执行结束时,控制权转移到程序,所以要返回终端我必须使用 ctrl+c ,我希望在程序执行结束后控制权转移到终端。