枪战英雄生存攻略大全:请问fork()返回值的不同,在linux0.11中是如何实现的?

来源:百度文库 编辑:杭州交通信息网 时间:2024/05/10 18:30:45

如果fork函数执行成功,父进程会返回新建子进程的进程ID(PID),子进程中也会获取返回值0。
如果fork执行失败,则返回-1,错误代码及具体原因可以用errno、strerror(errno)获取。

#include <stdio.h>
#include <unistd.h>
#include <errno.h>

int main(void)
{
int i;
if( (i=fork())==0 )
printf("child process\n");
else if( i>0 )
printf("process process\n");
else
printf("error:%d:%s\n", errno, strerror(errno));

return 0;
}