#include <stdio.h>
#include <unistd.h>
int main()
{
pid_t fpid;
int count = 0;
fpid = fork();
if (fpid > 0) {
printf("I am the parent process, my process id is %d\n",getpid());
count++;
printf("parent process"s count is %d\n\n",count);
// sleep(5);
}
else if (fpid == 0){
printf("I am the child process, my process id is %d\n",getpid());
count++;
printf("child process"s count is %d\n\n",count);
}
else {
printf("error\n");
}
printf("the last count is %d\n",count);
return 0;
}
运行后的结果如下图所示:
正如上面的截图所示,在运行完父进程后,会出现Linux下的shell 命令提示行后再执行子进程,讨教下这是为什么的?
解决方案
80
上一个都没有输入,就不会再输出提示符啊。
printf只是让终端显示,并没有作为指令输入。
也可以这样想,输入命令一个进程,你的程序也一个进程。他们之间互不干扰,只是要在同一个终端显示,只能互相让位置。