我有以下的C代码。奇怪的输出在C fork调用
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int i=1;
pid_t child_pid = fork();
if (child_pid == 0)
{
printf ("%d\n", i++);
printf ("%d\n", i++);
printf ("This is child process.");
return 0;
}
else if (child_pid > 0) {
printf ("%d\n", i++);
printf ("This is parent process.");
}
else {
printf("Fork failed");
}
}
我编译如下如下:gcc testFork.c
并通过输入./a.out
运行的代码。
我得到的输出是:
[email protected]:~/Desktop/Test C$ ./a.out
1
This is parent [email protected]:~/Desktop/Test C$ 1
2
This is child process.
为什么[email protected]:~/Desktop/Test C$
出现在中间的地方?
我只是希望这样的输出:
[email protected]:~/Desktop/Test C$ ./a.out
1
This is parent process.1
2
This is child process.
一旦父进程结束,shell将恢复;它死的第一件事是打印命令提示符。但是,这个孩子仍然在等待着开始。 – rici
这是你的提示。 –