Linux 多路服用 select

 

  1.     #include<fcntl.h> 
  2.     #include<stdio.h> 
  3.     #include<unistd.h> 
  4.     #include<sys/select.h> 
  5.     #include<sys/types.h> 
  6.     #include<sys/time.h> 
  7.     #define PIPENAME "/tmp/p1"   
  8.     #define PIPENAME_TWO "/tmp/p2"   
  9.     #define MSGCOUNT 10   
  10.     #define MSGSIZE 255   
  11.       
  12.     /*管道1,2的文件描述符*/   
  13.     int fd1,fd2;   
  14.     pid_t pid1,pid2; 
  15.   
  16. int 
  17. main(int argc,char *argv[]) {   
  18.     char buf[255],buf2[255];   
  19.     int len,rfd;   
  20.     struct timeval val;   
  21.     fd_set set; 
  22.          
  23.            
  24.         /*創建管道1,並打開*/   
  25.  if(mkfifo(PIPENAME,O_CREAT|O_EXCL)<0) /*創建管道1*/  
  26.   {  
  27.      perror("Create1 error!\n");  
  28.      unlink(PIPENAME);/*清除管道1*/  
  29.      exit(0);  
  30.   }  
  31.     
  32.   fd1=open(PIPENAME,O_RDWR,0);/*打開管道1*/  
  33.   if(fd1<0)  
  34.   {  
  35.      perror("open1 error!\n");  
  36.      unlink(PIPENAME);  
  37.      exit(0);  
  38.   }  
  39.  
  40.         /*創建管道2,並打開*/   
  41.  if(mkfifo(PIPENAME_TWO,O_CREAT|O_EXCL)<0) /*創建管道2*/  
  42.   {  
  43.      perror("Create2 error!\n");  
  44.      unlink(PIPENAME_TWO);/*清除管道2*/  
  45.      exit(0);  
  46.   }  
  47.     
  48.   fd2=open(PIPENAME_TWO,O_RDWR,0);/*打開管道2*/  
  49.   if(fd2<0)  
  50.   {  
  51.      perror("open2 error!\n");  
  52.      unlink(PIPENAME_TWO);  
  53.      exit(0);  
  54.   }  
  55.            
  56.  
  57. /*******創建2個子進程********/ 
  58.     pid1=fork(); 
  59.     if(pid1==0)/*子進程*/ 
  60.     { 
  61.     char msg[]="hello"
  62.     sleep(4);    
  63.     printf("write to Pipe2\n");   
  64.     write(fd2,msg,sizeof(msg)); 
  65.     } 
  66.       else 
  67.     { 
  68.         pid2=fork(); 
  69.         if(pid2==0)/*子進程*/ 
  70.         { 
  71.         char msg[]="hello";   
  72.         sleep(5);   
  73.         printf("write to Pipe1\n");   
  74.         write(fd1,msg,sizeof(msg)); 
  75.         } 
  76.  
  77.  
  78.           
  79.         /*超時時間*/   
  80.         val.tv_sec=5;   
  81.         val.tv_usec=0;  
  82.  
  83.   
  84.         while(1)   
  85.         {   
  86.             /*設置文件描述符集,先清空,再加入管道1,2*/ 
  87.             /*每次循環都要清空集合,否則不能檢測描述符變化 */   
  88.             FD_ZERO(&set);   
  89.             FD_SET(fd1,&set);   
  90.             FD_SET(fd2,&set);  
  91.  
  92.                 rfd=select(FD_SETSIZE,&set,NULL,NULL,&val);   
  93.  
  94.             switch(rfd) 
  95.         { 
  96.         case -1: exit(-1);break
  97.         case 0:break
  98.         default
  99.             if(FD_ISSET(fd1,&set))  //測試fd1是否可讀, 
  100.                  {   
  101.                         read(fd1,buf2,sizeof(buf2));   
  102.                         printf("[1]Get msg!\n");   
  103.                    
  104.                     }   
  105.                     if(FD_ISSET(fd2,&set))   
  106.                     {   
  107.                         read(fd2,buf,sizeof(buf));   
  108.                         printf("[2]Get msg!\n");   
  109.                    
  110.                     }   
  111.         }//switch              
  112.       }//while 
  113.      }//else 
  114.            
  115.     }  //main 

write to Pipe2


[2]Get msg!


write to Pipe1


[1]Get msg!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章