c - fork 及其功能?

标签 c fork

我有以下代码,并被问到“A”、“B”、“C”、“D”、“E”将被打印多少次

fun() {
  printf("A");
  fork();
  printf("B");
  if (fork() != 0) {
     printf("C");
     fork();
     printf("D");
  }
  printf("E");
}

应该是:

A
A 
B
E

我不确定我上面的回答是否正确? if(fork() !=0 ) 这行是做什么的?

最佳答案

来自documentation :

On success, the PID of the child process is returned in the parent, and 0 is returned in the child. On failure, -1 is returned in the parent, no child process is created, and errno is set appropriately.

关于c - fork 及其功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13792363/

相关文章:

c - 使用exec运行/usr/sbin下的文件

c - Linux:当父进程退出时退出 fork 的子进程

结合 uint8_t、uint16_t 和 uint8_t

c - 如何让 child 等待 parent 返回(而不是退出)然后自杀?

c - 寻找一个免费且轻量级的HTML解析器

c - 出现段错误?

c - execve 失败 - 忽略退出语句

c - Fork 和 dup2 - 子进程未终止 - 文件描述符问题?

c - 在数组中使用 C 字符串

c - 如何在 Visual Studio 2012 中添加 Kiss FFT 并确保已添加?