免费注册 查看新帖 |

Chinaunix

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

[C] 请教一下Linux 下C语言的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-10-03 14:15 |只看该作者 |倒序浏览
用C写个程序 需要创造2个进程,其中第一个子进程应该创造2个进程,第二个子进程创造3个以上的进程.每个(5个当中)重孙进程(不是其他的进程或者子进程)应该写它的进程数,以及标准输出它双亲的进程数(用16进制表示)这个C程序应该不包含子程序或者"defines"(定义) 只仅仅要单一的输出状态(output statement)
  


请问下这个要怎么做啊, 我只会写一层结点的,但这个2层结点的还没搞明白有高手帮帮我吗.
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

main() {
  int i,n;
  pid_t child;
  printf("How many processes do you want? \n");
  scanf("%d",&n);
  for (i=1; i<n; ++i)
    if (child = fork()) break;
  printf("This is process : %ld, with parent : %ld \n",
    (long)getpid(), (long)getppid());
}

论坛徽章:
0
2 [报告]
发表于 2006-10-03 17:33 |只看该作者
你这个程序,哎。

论坛徽章:
0
3 [报告]
发表于 2006-10-03 22:31 |只看该作者
我也不知道对不对哈,我这是网吧,没有测试....

if ((pid=fork())<0) {
  printf("Fork failure";   // failure
}
else if (pid == 0) {
  /* child 1 */
  for (i=0;i<2;i++) {
    if ((pid=fork())<0)
      printf("fork failure"; //failure
    else if (pid == 0)
      printf("This is process : %ld, with parent : %ld \n", (long)getpid(), (long)getppid());
    else
      continue;
  }
}
else {
  /* parent */
  if((pid=fork())<0) {
    printf("Fork failure";  // failure
  }
  else if (pid == 0) {
    /* child 2 */
    for (i=0;i<3;i++) {
      if ((pid=fork())<0)
        printf("fork failure"; //failure
      else if (pid == 0)
        continue;
      else  // grandchildren
        printf("This is process : %ld, with parent : %ld \n", (long)getpid(), (long)getppid());
    }
  }
  else {
    /* parent */
  }
}

[ 本帖最后由 kert_t8 于 2006-10-3 22:37 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP