操作系统:Linux,语言:纯C
我正在学习一般的C编程,以及在特殊情况下在UNIX下进行C编程.
printf()在使用fork()呼叫后,我发现了一个奇怪的(对我来说)函数的行为.
码
#include <stdio.h>
#include <system.h>
int main()
{
int pid;
printf( "Hello, my pid is %d", getpid() );
pid = fork();
if( pid == 0 )
{
printf( "\nI was forked! :D" );
sleep( 3 );
}
else
{
waitpid( pid, NULL, 0 );
printf( "\n%d was forked!", pid );
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
产量
Hello, my pid is 1111
I was forked! :DHello, my pid is 1111
2222 was forked!
Run Code Online (Sandbox Code Playgroud)
为什么第二个"Hello"字符串出现在子输出中?
是的,这正是父母在开始时与父母一起打印的内容pid …