免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 2859 | 回复: 11
打印 上一主题 下一主题

请老师们教教我,请教一个问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-02-04 23:00 |只看该作者 |倒序浏览
我刚刚接触linux编程,对于fork系统调用不是很理解fork默认返回两个值,一个子进程一个父进程,我在一个程序里一次创建了3个进程,用switch语句判断调用子进程过程和父进程关系,结果返回了12次的值!不知道原因请解释下:)

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
2 [报告]
发表于 2009-02-04 23:56 |只看该作者

回复 #1 nervfzb 的帖子

把完整的代码贴上来。

论坛徽章:
3
戌狗
日期:2014-09-10 17:07:162015年辞旧岁徽章
日期:2015-03-03 16:54:15wusuopu
日期:2016-06-17 17:43:45
3 [报告]
发表于 2009-02-05 00:33 |只看该作者
原帖由 nervfzb 于 2009-2-4 23:00 发表
我刚刚接触linux编程,对于fork系统调用不是很理解fork默认返回两个值,一个子进程一个父进程,我在一个程序里一次创建了3个进程,用switch语句判断调用子进程过程和父进程关系,结果返回了12次的值!不知道原因 ...

子进程里也执行fork了吧

论坛徽章:
0
4 [报告]
发表于 2009-02-05 18:04 |只看该作者
源程序:
int main()
{
  pid_t pid1,pid2,pid3;
  pid1=fork();
  pid2=fork();
  pid3=fork();
  switch(pid1)
{
   case 0:
    printf(".....");
   break;
   case -1:
    perror("......");
    breark;
   default:
    printf("....");
    break;
  }
  switch(pid2)
{
   结构同上面一样
}
  switch(pid3)
  {
    结构同上面一样
  }
  return 0;
}

论坛徽章:
0
5 [报告]
发表于 2009-02-05 18:05 |只看该作者
老师们啊,对不起了没有及时发源程序,顶啊,不能沉啦.....

论坛徽章:
0
6 [报告]
发表于 2009-02-05 18:06 |只看该作者
在顶下:)求解答啊

论坛徽章:
3
CU十二周年纪念徽章
日期:2013-10-24 15:41:34摩羯座
日期:2013-12-01 00:21:362015年迎新春徽章
日期:2015-03-04 09:49:45
7 [报告]
发表于 2009-02-05 19:10 |只看该作者
按LZ贴出的 格式 是应该没有问题的,不知道具体所说的12个值是什么情形的
一个进程打印出4个值还是怎么

论坛徽章:
0
8 [报告]
发表于 2009-02-05 19:10 |只看该作者
#include "stdio.h"
#include "sys/types.h"
#include "unistd.h"
int main()
{
pid_t pid1,pid2,pid3;
int a=1,b=1,c=1;
pid1=fork();
pid2=fork();
pid3=fork();
switch(pid1)
{
  case 0:
  printf("Child process 1 is running.\n");
  a+=4;
  printf("Value a in Child process 1 is:%d\n",a);
  break;
  case -1:
  perror("Child process 1 is failed.\n");
  exit(1);
  default:
  printf("Value a in Parent process is:%d\n",a);
  break;
}
switch(pid2)
{
  case 0:
  printf("Child process 2 is runing.\n");
  b+=4;
  printf("Value b in Child process 2 is:%d\n",b);
  break;
  case -1:
  perror("Child process 1 is failed.\n");
  exit(1);
  default:
  printf("Value b in Parent process is:%d\n",b);
  break;
}
switch(pid3)
{
  case 0:
  printf("Child process 3 is running.\n");
  c+=4;
  printf("Value c in Child process 3 is:%d\n",c);
  break;
  case -1:
perror("Child process 3 is failed.\n");
  exit(1);
  default:
  printf("Value c in Parent process is:%d\n",c);
  break;
}
return 0;
}
这个是完整的代码

下面是编译过程和结果
[root@LinuxFzb tmp]# vi fork5.c
[root@LinuxFzb tmp]# gcc -o fork5 fork5.c
[root@LinuxFzb tmp]# ./fork5
Child process 1 is running.
Value a in Child process 1 is:5
Child process 2 is runing.
Value b in Child process 2 is:5
Child process 3 is running.
Value c in Child process 3 is:5
Child process 1 is running.
Value a in Child process 1 is:5
Child process 2 is runing.
Value b in Child process 2 is:5
Value c in Parent process is:1
Child process 1 is running.
Value a in Child process 1 is:5
Child process 1 is running.
Value a in Child process 1 is:5
Value b in Parent process is:1
Value c in Parent process is:1
Value a in Parent process is:1
Child process 2 is runing.
Value b in Child process 2 is:5
Child process 3 is running.
Value c in Child process 3 is:5
Value a in Parent process is:1
Child process 2 is runing.
Value b in Child process 2 is:5
Value c in Parent process is:1
Value a in Parent process is:1
Value b in Parent process is:1
Child process 3 is running.
Value c in Child process 3 is:5
Value a in Parent process is:1
Value b in Parent process is:1
Value c in Parent process is:1
[root@LinuxFzb tmp]# Value b in Parent process is:1
Child process 3 is running.
Value c in Child process 3 is:5

论坛徽章:
0
9 [报告]
发表于 2009-02-05 19:12 |只看该作者
还望各位老师赏脸看看啊,小弟在此跪谢

论坛徽章:
3
戌狗
日期:2014-09-10 17:07:162015年辞旧岁徽章
日期:2015-03-03 16:54:15wusuopu
日期:2016-06-17 17:43:45
10 [报告]
发表于 2009-02-06 02:14 |只看该作者
原帖由 nervfzb 于 2009-2-5 18:04 发表
源程序:
int main()
{
  pid_t pid1,pid2,pid3;
  pid1=fork();
  pid2=fork();
  pid3=fork();

  switch(pid1)
{
   case 0:
    printf(".....");
   break;
   case -1:
    perror("......") ...

pid2=fork();
pid3=fork();
不止执行一次
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP