主题 : fork妙处 复制链接 | 浏览器收藏 | 打印
级别: 新手上路
UID: 18505
精华: 0
发帖: 48
金钱: 250 两
威望: 50 点
综合积分: 96 分
注册时间: 2010-04-10
最后登录: 2011-05-05
楼主  发表于: 2010-06-11 10:32

 fork妙处

main()
{
pid-t pid;
//此处仅有一个进程
pid= fork();
//此时已经有两个进程在同时运行
if(pid<0)
   printf('error in fork');
else if (pid==0)
printf("I am the child process,")
else
printf("I am the parent process")
可以发现后两条能同时打印,原因就是有两个进程在同时进行
子进程中fork的返回值是0,父进程中fork的返回值是大于0的



}
学习交流
*無鈳取玳
级别: 论坛版主
UID: 27
精华: 12
发帖: 5398
金钱: 40120 两
威望: 17929 点
综合积分: 11036 分
注册时间: 2008-01-16
最后登录: 2014-11-22
1楼  发表于: 2010-06-11 11:04
不错,不过这个东西在所有的操作系统理论书上都已经讲过了。
"If you have an apple and I have an apple and we exchange apples, then you and I will
still each have one apple. But if you have an idea and I have an idea and we exchange
these ideas, then each of us will have two ideas."