Chinaunix首页 | 论坛 | 博客
  • 博客访问: 101453975
  • 博文数量: 19283
  • 博客积分: 9968
  • 博客等级: 上将
  • 技术积分: 196062
  • 用 户 组: 普通用户
  • 注册时间: 2007-02-07 14:28
文章分类

全部博文(19283)

文章存档

2011年(1)

2009年(125)

2008年(19094)

2007年(63)

分类: C/C++

2008-05-01 17:56:54

#include
#include
#include
#include
#include
#include
#include
#include

int main()
{
   int        ipc[2];
   int i = 0;
   int maxfd;
   fd_set rset;
   int rc;
   char buf[BUFSIZ]; //4096

   if(pipe(ipc) == -1)
   {
       printf("error: pipe");
       return -1;
   }
   
   printf("pipe success\n");
   FD_ZERO(&rset);
   FD_SET(ipc[0], &rset);
   maxfd = ipc[0];
   
   while(1)
   {

//        rc = select(maxfd + 1, &rset, NULL, NULL, NULL); //阻塞
       
//        if(rc == -1)
//        {
//            printf("Error: Select");
//            continue; //继续等待
//        }
       
       
           sleep(1);
           printf("write\n");
           write(ipc[1], "hello,world", 12);
           
           
           if(FD_ISSET(ipc[0], &rset)) //阻塞
           {
               //读取
               read(ipc[0], buf, BUFSIZ);
               printf("buf = %s\n", buf);
           }
           else
               printf("can not read\n");
           
           
   }
   return 1;    
}


如果select不设置超时即
   struct timeval    to;
   to.tv_sec=30;
   to.tv_usec=0;
select(fd+1, &rset, NULL, NULL, &to);
那么到select 语句,将一直阻塞。

如果前面没有write数据,那么
FD_ISSET(ipc[0], &rset) 也是一直阻塞。

 

原文:http://careerman.blog.ccidnet.com/blog-htm-do-showone-uid-12026-type-blog-itemid-286248.html

阅读(1004) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~