有名管道例子

测试系统:RedHat Linux 9.0
 
源文件:
[csharp]  view plain copy
  1. /* 
  2.  
  3.  * 管道通信:有名管道 
  4.  
  5.  * 无名管道只能用于具有亲缘关系的进程之间,而有名管道可以在互不相关的两个进程间 
  6.  
  7.  * 实现彼此通信。要注意,FIFO严格按照先进先出的规则,对管道及FIFO的读总是从开始 
  8.  
  9.  * 处返回数据,对它们的写则把数据添加到末尾,不支持lseek等文件定位操作。 
  10.  
  11.  * 
  12.  
  13.  * 有名管道的创建使用mkfifo()。创建成功后就可以使用open、read、write这些函数了。 
  14.  
  15.  * 读管道部分 
  16.  
  17.  */  
  18.  
  19.  
  20.  
  21. #include <stdio.h>  
  22.  
  23. #include <stdlib.h>  
  24.  
  25. #include <string.h>  
  26.  
  27. #include <errno.h>  
  28.  
  29. #include <unistd.h>  
  30.  
  31. #include <sys/types.h>  
  32.  
  33. #include <sys/stat.h>  
  34.  
  35. #include <fcntl.h>  
  36.   
  37.   
  38.   
  39. /*在这里设置打开管道文件的mode为只读形式*/  
  40.  
  41. #define FIFOMODE (O_CREAT | O_RDWR | O_NONBLOCK)  
  42.  
  43. #define OPENMODE (O_RDONLY | O_NONBLOCK)  
  44.  
  45. #define FIFO_SERVER "myfifo"  
  46.   
  47.   
  48.   
  49. int main(void)  
  50.   
  51. {  
  52.   
  53.         char buf[100];  
  54.   
  55.         int fd;  
  56.   
  57.         int readnum;  
  58.   
  59.   
  60.   
  61.         /*创建有名管道,设置为可读写,无阻塞,如果不存在则按照指定权限创建*/  
  62.   
  63.         if ((mkfifo(FIFO_SERVER, FIFOMODE) < 0) && (errno != EEXIST)) {  
  64.   
  65.                 printf("cannot create fifoserver/n");  
  66.   
  67.                 exit(1);  
  68.   
  69.         }  
  70.   
  71.   
  72.   
  73.         printf("Preparing for reading bytes... .../n");  
  74.   
  75.   
  76.   
  77.         /*打开有名管道,并设置非阻塞标志*/  
  78.   
  79.         if ((fd = open(FIFO_SERVER, OPENMODE)) < 0) {  
  80.   
  81.                 perror("open");  
  82.   
  83.                 exit(1);  
  84.   
  85.         }  
  86.   
  87.   
  88.   
  89.         while (1) {  
  90.   
  91.                 /*初始化缓冲区*/  
  92.   
  93.                 bzero(buf, sizeof(buf));  
  94.   
  95.                 /*读取管道数据*/  
  96.   
  97.                 if ((readnum = read(fd, buf, sizeof(buf))) < 0) {  
  98.   
  99.                         if (errno == EAGAIN) {  
  100.   
  101.                                 printf("no data yet/n");  
  102.   
  103.                         }  
  104.   
  105.                 }  
  106.   
  107.                 /*如果读到数据则打印出来,如果没有数据,则忽略*/  
  108.   
  109.                 if (readnum != 0) {  
  110.   
  111.                         buf[readnum] = '/0';  
  112.   
  113.                         printf("read %s from FIFO_SERVER/n", buf);  
  114.   
  115.                 }  
  116.   
  117.                 sleep(1);  
  118.   
  119.         }  
  120.   
  121.   
  122.   
  123.         return 0;  
  124.   
  125. }  

[csharp]  view plain copy
  1. /* 
  2.  
  3.  * 管道通信:有名管道 
  4.  
  5.  * 无名管道只能用于具有亲缘关系的进程之间,而有名管道可以在互不相关的两个进程间 
  6.  
  7.  * 实现彼此通信。要注意,FIFO严格按照先进先出的规则,对管道及FIFO的读总是从开始 
  8.  
  9.  * 处返回数据,对它们的写则把数据添加到末尾,不支持lseek等文件定位操作。 
  10.  
  11.  * 
  12.  
  13.  * 有名管道的创建使用mkfifo()。创建成功后就可以使用open、read、write这些函数了。 
  14.  
  15.  * 写管道部分 
  16.  
  17.  */  
  18.  
  19.  
  20.  
  21. #include <stdio.h>  
  22.  
  23. #include <stdlib.h>  
  24.  
  25. #include <sys/types.h>  
  26.  
  27. #include <sys/stat.h>  
  28.  
  29. #include <fcntl.h>  
  30.  
  31. #include <string.h>  
  32.  
  33. #include <unistd.h>  
  34.  
  35. #include <errno.h>  
  36.   
  37.   
  38.   
  39. /*特别注意写管道时,设置打开管道文件的格式必须为可写*/  
  40.  
  41. #define FIFO_SERVER "myfifo"  
  42.  
  43. #define OPENMODE (O_WRONLY | O_NONBLOCK)  
  44.   
  45.   
  46.   
  47. int main(int argc, char **argv)  
  48.   
  49. {  
  50.   
  51.         int fd;  
  52.   
  53.         int nwrite;  
  54.   
  55.   
  56.   
  57.         /*打开管道文件,可写非阻塞*/  
  58.   
  59.         if ((fd = open(FIFO_SERVER, OPENMODE)) < 0) {  
  60.   
  61.                 perror("open");  
  62.   
  63.                 exit(1);  
  64.   
  65.         }  
  66.   
  67.   
  68.   
  69.         /*如果没有在命令行中写入参数,那么要重新运行程序*/  
  70.   
  71.         if (argc == 1) {  
  72.   
  73.                 printf("Please send something/n");  
  74.   
  75.                 exit(1);  
  76.   
  77.         }  
  78.   
  79.   
  80.   
  81.         /*向管道文件中写入数据,在这里要用strlen,如果用sizeof,则只是4个字节的指针长度*/  
  82.   
  83.         if ((nwrite = write(fd, argv[1], strlen(argv[1]))) < 0) {  
  84.   
  85.                 if (errno == EAGAIN) {  
  86.   
  87.                         printf("The FIFO has not been read yet.Please try later/n");  
  88.   
  89.                 }  
  90.   
  91.         }  
  92.   
  93.         else {  
  94.   
  95.                 printf("write %s to FIFO/n", argv[1]);  
  96.   
  97.         }  
  98.   
  99.   
  100.   
  101.         return 0;  
  102.   
  103. }  

Makefile:
[csharp]  view plain copy
  1. CC = gcc  
  2.   
  3. OBJS = fifo_write fifo_read  
  4.   
  5. CFLAGS = -Wall -O2 -g  
  6.   
  7.   
  8.   
  9. all: fifo_write fifo_read  
  10.   
  11.   
  12.   
  13. fifo_write: fifo_write.c  
  14.   
  15.         $(CC) $(CFLAGS) $^ -o $@  
  16.   
  17.   
  18.   
  19. fifo_read: fifo_read.c  
  20.   
  21.         $(CC) $(CFLAGS) $^ -o $@  
  22.   
  23.   
  24.   
  25. .PHONY: clean dist distclean  
  26.   
  27. clean:  
  28.   
  29.         rm -rf $(OBJS) myfifo  
  30.   
  31.   
  32.   
  33. dist:  
  34.   
  35.         tar zcvf fifo.tar.gz * && echo "OK"  
  36.   
  37.   
  38.   
  39. distclean:  
  40.   
  41.         rm -rf $(OBJS) fifo.tar.gz myfifo  
测试过程,开两个窗口,先打开fifo_read,然后打开fifo_write写入数据,观察情况。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值