“fork()”之后的printf异常
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;}
输出量
Hello, my pid is 1111I was forked! :DHello, my pid is 11112222 was forked!
pid
.
\n
#include <stdio.h>#include <system.h>int main(){ int pid; printf( "Hello, my pid is %d\n", getpid() ); // SIC!! pid = fork(); if( pid == 0 ) { printf( "I was forked! :D" ); // removed the '\n', no matter sleep( 3 ); } else { waitpid( pid, NULL, 0 ); printf( "\n%d was forked!", pid ); } return 0;}
输出量:
Hello, my pid is 1111I was forked! :D2222 was forked!
繁星淼淼
慕娘9325324