Chinaunix首页 | 论坛 | 博客
  • 博客访问: 9152721
  • 博文数量: 1727
  • 博客积分: 12961
  • 博客等级: 上将
  • 技术积分: 19860
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-09 11:25
个人简介

偷得浮生半桶水(半日闲), 好记性不如抄下来(烂笔头). 信息爆炸的时代, 学习是一项持续的工作.

文章分类

全部博文(1727)

文章存档

2024年(3)

2023年(26)

2022年(112)

2021年(217)

2020年(157)

2019年(192)

2018年(81)

2017年(78)

2016年(70)

2015年(52)

2014年(40)

2013年(51)

2012年(85)

2011年(45)

2010年(231)

2009年(287)

分类: 其他平台

2018-03-29 19:47:09


点击(此处)折叠或打开

  1. /*
  2. pipe-write
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>

  8. #include <sys/types.h>
  9.        #include <sys/stat.h>
  10.        #include <fcntl.h>
  11. #include <errno.h>

  12. /*
  13. * if do write first, write-app will be blocked until read-app start.
  14. * in runtime, when read-app closed, then write-app will be receive signal(SIGPIPE), and then terminate self.
  15. */

  16. #define FIFO_FFPLAY        "/tmp/my_fifo"

  17. int main()
  18. {
  19.     int fd = mkfifo(FIFO_FFPLAY, 0777);
  20.     if (fd == 0) {
  21.         printf("create FIFO OK.\n");
  22.     }
  23.     else {
  24.         if (EEXIST != errno) {
  25.             printf("mkfifo failed. \n");
  26.             exit(-1);
  27.         }
  28.     }
  29.     
  30.     fd = open(FIFO_FFPLAY, O_WRONLY);
  31.     if (fd < 0) {
  32.         printf("open FIFO failed. \n");
  33.         exit(-1);
  34.     }
  35.     
  36.     printf("Write data to FIFO.\n");
  37.     
  38.     char line[128] = {0};
  39.     int idx = 0;
  40.     while (1) {
  41.         sprintf(line, "%04d.\n 000", idx++);
  42.         printf(line);
  43.         write(fd, line, strlen(line));
  44.         usleep(1000000);
  45.     }
  46.     
  47.     close(fd);
  48.     //unlink(FIFO_FFPLAY);
  49.     
  50.     return 0;
  51. }


点击(此处)折叠或打开

  1. /*
  2. pipe-read
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <string.h>
  9. #include <sys/types.h>
  10.        #include <sys/stat.h>
  11.        #include <fcntl.h>
  12. #include <errno.h>

  13. #define FIFO_FFPLAY        "/tmp/my_fifo"

  14. int main()
  15. {
  16.     int fd = mkfifo(FIFO_FFPLAY, 0777);
  17.     if (fd == 0) {
  18.         printf("create FIFO OK.\n");
  19.     }
  20.     else {
  21.         if (EEXIST != errno) {
  22.             printf("mkfifo failed. \n");
  23.             exit(-1);
  24.         }
  25.     }
  26.     
  27.     fd = open(FIFO_FFPLAY, O_RDONLY);
  28.     if (fd < 0) {
  29.         printf("open FIFO failed. \n");
  30.         exit(-1);
  31.     }
  32.     
  33.     FILE *file = fdopen(fd, "r");
  34.     printf("reading data from FIFO.\n");
  35.     
  36.     char line[128] = {0};
  37.     errno = 0;
  38.     while (1) {
  39.         if (fgets(line, 127, file)) {
  40.         //if (read(fd, line, 127) > 0) {
  41.             printf("### [%s] ###\n", line);
  42.         }
  43.         usleep(100000);
  44.     }
  45.     
  46.     close(fd);
  47.     //unlink(FIFO_FFPLAY);
  48.     
  49.     return 0;
  50. }

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