进程间通信系列(一)管道读写实例

一、管道读写注意点

1.只有在管道读端存在时,向管道写入才有意义;否则,会收到内核中的出错信号:SIFPIPE

2.向管道写入数据时不保证写入的原子性,管道缓冲区一有空闲区域,写进程就试图向其写入内容。若读进程不读取管道中的内容,则写进程会一直阻塞。

3.父子进程在运行时,它们的先后顺序得不到保证。因此在这里,为保证父进程关闭读描述符,可向子进程加入sleep(2)。

二、实例

1.无名管道

  1. /*pipe_rw.c*/  
  2. #include <unistd.h>  
  3. #include <sys/types.h>  
  4. #include <errno.h>  
  5. #include <stdio.h>  
  6. #include <stdlib.h>  
  7.   
  8. int main()  
  9. {  
  10.     int pipe_fd[2];  
  11.     pid_t pid;  
  12.     char buf_r[100];  
  13.     char* p_wbuf;  
  14.     int r_num;  
  15.     memset(buf_r,0,sizeof(buf_r));  
  16.     if(pipe(pipe_fd)<0)  
  17.     {  
  18.     printf("pipe create error\n");  
  19.     return -1;  
  20.     }  
  21.     if((pid=fork())==0)    //若是子进程  
  22.     {  
  23.         printf("\n");  
  24.         /*关闭子进程管道写端。睡眠2秒,确保父进程已相应地关闭了管道读端*/  
  25.         close(pipe_fd[1]);  
  26.         sleep(2);  
  27.         /*子进程读取管道内容*/  
  28.         if((r_num=read(pipe_fd[0],buf_r,100))>0){  
  29.             printf(   "%d numbers read from the pipe is %s\n",r_num,buf_r);  
  30.         }     
  31.         /*关闭子进程读端*/  
  32.         close(pipe_fd[0]);  
  33.         exit(0);  
  34.     }  
  35.     else if(pid>0)  
  36.     {   /*关闭父进程读端*/  
  37.         close(pipe_fd[0]);  
  38.         /*分两次向管道写入数据*/  
  39.         if(write(pipe_fd[1],"Hello",5)!=-1)  
  40.             printf("parent write1 success!\n");  
  41.         if(write(pipe_fd[1]," Pipe",5)!=-1)  
  42.             printf("parent write2 success!\n");  
  43.         /*关闭父进程写端并睡眠3秒,让子进程读数据*/  
  44.         close(pipe_fd[1]);  
  45.         sleep(3);  
  46.         /*收集子进程退出信息*/  
  47.         waitpid(pid,NULL,0);  
  48.         exit(0);  
  49.     }  
  50. }  
运行结果如下:
  1. [root@localhost ipc]# ./pipe_rw  
  2.   
  3. parent write1 success!  
  4. parent write2 success!  
  5. 10 numbers read from the pipe is Hello Pipe<span style="font-family:Arial, Verdana, sans-serif;"><span style="white-space: normal;">  
  6. </span></span>  


2.有名管道

  1. /*fifo_write.c*/  
  2. #include <sys/types.h>  
  3. #include <sys/stat.h>  
  4. #include <errno.h>  
  5. #include <fcntl.h>  
  6. #include <stdio.h>  
  7. #include <stdlib.h>  
  8. #include <string.h>  
  9. #define FIFO_SERVER "/tmp/myfifo"  
  10.   
  11. main(int argc,char** argv)  
  12. {  
  13.     int fd;  
  14.     char w_buf[100];  
  15.     int nwrite;  
  16.       
  17.     if(fd==-1)  
  18.         if(errno==ENXIO)  
  19.             printf("open error; no reading process\n");  
  20.     /*打开有名管道,并设置为非阻塞*/  
  21.     fd=open(FIFO_SERVER,O_WRONLY|O_NONBLOCK,0);  
  22.     if(argc==1)  
  23.         printf("Please send something\n");  
  24.     strcpy(w_buf,argv[1]);  
  25.     /*向管道写入字符串*/  
  26.     if((nwrite=write(fd,w_buf,100))==-1)  
  27.     {  
  28.         if(errno==EAGAIN)  
  29.             printf("The FIFO has not been read yet.Please try later\n");  
  30.     }  
  31.     else   
  32.         printf("write %s to the FIFO\n",w_buf);  
  33. }  
  34.   
  35.   
  36. /*fifo_read.c*/  
  37.   
  38. #include <sys/types.h>  
  39. #include <sys/stat.h>  
  40. #include <errno.h>  
  41. #include <fcntl.h>  
  42. #include <stdio.h>  
  43. #include <stdlib.h>  
  44. #include <string.h>  
  45. #define FIFO "/tmp/myfifo"  
  46.   
  47. main(int argc,char** argv)  
  48. {  
  49.     char buf_r[100];  
  50.     int  fd;  
  51.     int  nread;  
  52.       
  53.     /*创建有名管道,并设置相应的权限*/  
  54.     if((mkfifo(FIFO,O_CREAT|O_EXCL)<0)&&(errno!=EEXIST))  
  55.         printf("cannot create fifoserver\n");  
  56.     printf("Preparing for reading bytes...\n");  
  57.       
  58.     memset(buf_r,0,sizeof(buf_r));  
  59.     /*打开有名管道,并设置非阻塞标志*/  
  60.     fd=open(FIFO,O_RDONLY|O_NONBLOCK,0);  
  61.     if(fd==-1)  
  62.     {  
  63.         perror("open");  
  64.         exit(1);      
  65.     }  
  66.     while(1)  
  67.     {  
  68.         memset(buf_r,0,sizeof(buf_r));  
  69.         /*读取管道中的字符串*/  
  70.         if((nread=read(fd,buf_r,100))==-1){  
  71.             if(errno==EAGAIN)  
  72.                 printf("no data yet\n");  
  73.         }  
  74.         printf("read %s from FIFO\n",buf_r);  
  75.         sleep(1);  
  76.     }     
  77.     pause();  
  78.     unlink(FIFO);  
  79. }  
运行结果:

终端1:

  1. [root@localhost ipc]# ./fifo_write 123123  
  2. write 123123 to the FIFO  
终端2:
  1. [root@localhost ipc]# ./fifo_read  
  2. Preparing for reading bytes...  
  3. read  from FIFO  
  4. read  from FIFO  
  5. read  from FIFO  
  6. read  from FIFO  
  7. read  from FIFO  
  8. read 123123 from FIFO  
  9. read  from FIFO 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值