免费注册 查看新帖 |

Chinaunix

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

管道的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-11-12 09:56 |只看该作者 |倒序浏览
写了个简单的测试管道功能的代码,结果让我百思不得其解。
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <signal.h>
#include <unistd.h>

#define MAXLEN 1024

static void sig_handler(int signo);

int main()
{
  pid_t pid;
  int fd[2];
  char buf[MAXLEN];
  int cnt=0;
  int i=0;
  if(pipe(fd)<0)
   perror("pipe error!");
  if(signal(SIGCHLD,sig_handler)==SIG_ERR)
    perror("signal error!");
  for(i=0;i<=1;i++)
  {
   
    if((pid=fork())<0)
      perror("fork error!");
    else if(pid==0)
    {
      close(fd[0]);
      cnt++;
      sprintf(buf,"%d\n",cnt);
      write(fd[1],buf,MAXLEN);
      exit(0);
    }
    else
    {
      close(fd[1]);
      read(fd[0],buf,MAXLEN);
      
      printf("buf %s\n",buf);
      cnt=atoi(buf);
      printf("cnt %d\n",cnt);
    }
  }
  return 0;
  }
  
  static void sig_handler(int signo)
  {
    pid_t pid;
    if((pid=waitpid(-1,NULL,WNOHANG))<0)
      perror("waitpid error!");
    return ;
  }

编译:
[root@localhost exercise]# gcc -Wall -g -o pipe2 pipe2.c
[root@localhost exercise]# ./pipe2
buf 1
cnt 1
buf 1
cnt 1
第二次读出的值为什么还是1.想不明白?
而我修改下,去掉子进程的exit后,
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <signal.h>
#include <unistd.h>

#define MAXLEN 1024

static void sig_handler(int signo);

int main()
{
  pid_t pid;
  int fd[2];
  char buf[MAXLEN];
  int cnt=0;
  int i=0;
  if(pipe(fd)<0)
   perror("pipe error!");
  if(signal(SIGCHLD,sig_handler)==SIG_ERR)
    perror("signal error!");
  for(i=0;i<=1;i++)
  {
   
    if((pid=fork())<0)
      perror("fork error!");
    else if(pid==0)
    {
      close(fd[0]);
      cnt++;
      sprintf(buf,"%d",cnt);
      write(fd[1],buf,MAXLEN);
      //exit(0);
    }
    else
    {
      close(fd[1]);
      read(fd[0],buf,MAXLEN);
      
      printf("buf %s\n",buf);
      cnt=atoi(buf);
      printf("cnt %d\n",cnt);
    }
  }
  return 0;
  }
  
  static void sig_handler(int signo)
  {
    pid_t pid;
    if((pid=waitpid(-1,NULL,WNOHANG))<0)
      perror("waitpid error!");
    return ;
  }
编译为
[root@localhost exercise]# gcc -Wall -g -o pipe2 pipe2.c
[root@localhost exercise]# ./pipe2
buf 1
cnt 1
buf 1
cnt 1
buf 2
cnt 2
感觉好奇怪:gdb pipe2发现刚执行到fork时就打印前两行,后面那四行发现用能读出增加后的cnt值。
[root@localhost exercise]# uname -a
Linux localhost.localdomain 2.6.18.8 #1 SMP Tue Feb 24 14:47:02 CST 2009 i686 i686 i386 GNU/Linux

论坛徽章:
0
2 [报告]
发表于 2009-11-12 10:35 |只看该作者
else
    {
      close(fd[1]);
      read(fd[0],buf,MAXLEN);
      
      printf("buf %s\n",buf);
      cnt=atoi(buf);
      printf("cnt %d\n",cnt);
    }


这里的问题,第一次你不该close(fd[1]);

论坛徽章:
0
3 [报告]
发表于 2009-11-12 14:48 |只看该作者
谢谢你的提醒,我明白了.父进程关了后,第二个子进程继承不到写端口了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP