Skip to content

Instantly share code, notes, and snippets.

@caigen
Created October 28, 2013 13:54
Show Gist options
  • Select an option

  • Save caigen/7197190 to your computer and use it in GitHub Desktop.

Select an option

Save caigen/7197190 to your computer and use it in GitHub Desktop.
fork面试题通常画二叉树解,理解内核的实现则秒解。如下代码输出2+2+2=8个'-'是因为缓冲区被复制到子进程。使用fflush或'\n'则2+4=6个'-'。
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
for (int i = 0; i < 2; i++) {
fork();
printf("-");
// fflush(stdout);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment