关于fork后父子进程的关系

tnfyj 2008-10-20 06:02:16
小弟菜鸟,刚接触UNIX下开发,最近发现在公司的主机上运行一段程序,fork之后运行了两次子进程的内容,而父进程纹丝未动...
这是程序的代码
#include <stdlib.h>
#include <stdio.h>

int main(){
pid_t pid;
if(pid=fork()<0) {
printf("frok error\n");
}
if(pid==0) {
printf("This is the child\n");
}
else{
printf("This is the parent\n");
}

exit(0);
}

也就是输出了两次 This is the child
请教各位大侠这是为什么?
顺便,公司的主机版本为:Red Hat Enterprise Linux AS release 4
...全文
82 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
tnfyj 2008-10-20
  • 打赏
  • 举报
回复
谢了,高人啊,看了好几遍没看出来问题出在哪。。。
once_and_again 2008-10-20
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 daniel_yao 的回复:]
优先级, 你的pid=fork应该先括号起来。 <的优先级比=号的大
[/Quote]./a.out
This is the child
This is the parent
once_and_again 2008-10-20
  • 打赏
  • 举报
回复
./a.out
This is the child
This is the child
daniel_yao 2008-10-20
  • 打赏
  • 举报
回复
优先级, 你的pid=fork应该先括号起来。 <的优先级比=号的大

23,116

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧