当前位置: 技术问答>linux和unix
linux进程问题
来源: 互联网 发布时间:2016-04-24
本文导语: if((pid=fork())==0) { printf("zi"); } else if(pid>0) { printf("fu"); } 本人菜鸟。。。 上面程序是先打印出fu还是先打印出zi呢?还是随机?? 也就是父进程和子进程的先后问题。。。 | 嗯并不一定 都有可能 ...
if((pid=fork())==0)
{
printf("zi");
}
else if(pid>0)
{
printf("fu");
}
本人菜鸟。。。
上面程序是先打印出fu还是先打印出zi呢?还是随机??
也就是父进程和子进程的先后问题。。。
|
嗯并不一定
都有可能
都有可能
|
根据系统的调度情况,随即的,.
|
以下是节自APUE2nd 8.3. fork Function
In general, we never know whether the child starts executing before the parent or vice versa. This depends on the scheduling algorithm used by the kernel. If it's required that the child and parent synchronize, some form of interprocess communication is required. In the program shown in Figure 8.1, we simply have the parent put itself to sleep for 2 seconds, to let the child execute. There is no guarantee that this is adequate, and we talk about this and other types of synchronization in Section 8.9 when we discuss race conditions. In Section 10.16, we show how to use signals to synchronize a parent and a child after a fork.
|
两种情况都会出现,要想让子进程先运行完后再让父进程运行的话你可以使用vfork()
|
fork()程序有两个返回值,在父进程中返回0,在子进程中返回子进程id号,很迷惑人。
|
随机的,根据内核调度算法