c - 在 fork() 程序中首先执行什么? parent 还是 child ?

标签 c unix fork

我不知道fork中首先执行的是什么。例如我有这样的代码:

int main() {
   int n = 1;
   if(fork() == 0) {
      n = n + 1;
      exit(0);
   }
   n = n + 2;
   printf(“%d: %d\n”, getpid(), n);
   wait(0);
   return 0;
}

这会在屏幕上打印什么?

1: 3
0: 4

0: 4
1: 3

最佳答案

没有具体说明。由操作系统调度程序决定首先调度哪个进程。

After a fork(), it is indeterminate which process—the parent
or the child—next has access to the CPU. On a multiprocessor system,
they may both simultaneously get access to a CPU.

关于c - 在 fork() 程序中首先执行什么? parent 还是 child ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17388621/

相关文章:

c - fseek/循环倒带

c - 如何杀死多个子进程

c - unix中绑定(bind)端口 "Address already in use"TCP套接字编程错误

c - 为什么 main 被调用两次?

当程序从终端运行时,clock_gettime 需要更长的时间来执行

将文件中的数字字符串转换为 C 中的 int 数组

c - write() 不写入数据

java - 在java中制作一个unix守护进程

c - 按顺序 fork 进程有 4 个子进程?

c++ - 打开程序的管道并将其放入标准输入