Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7540584
  • 博文数量: 961
  • 博客积分: 15795
  • 博客等级: 上将
  • 技术积分: 16612
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-07 14:23
文章分类

全部博文(961)

文章存档

2016年(1)

2015年(61)

2014年(41)

2013年(51)

2012年(235)

2011年(391)

2010年(181)

分类: C/C++

2011-06-17 21:12:18

  1. /*
  2.  * 匿名管道
  3.  * 用于有亲缘关系的进程,单向,自动同步
  4.  * Lzy    2011-6-14
  5.  */

  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #include <string.h>

  10. int main(void)
  11. {
  12.     char buf[10];            //自定义缓冲区
  13.     pid_t pid;            //定义进程ID
  14.     int fd[2];            //定义管道句柄
  15.     
  16.     if(pipe(fd) < 0)        //创建匿名管道
  17.     {
  18.         perror("pipe");
  19.         exit(0);
  20.     }
  21.     
  22.     pid = fork();            //创建子进程
  23.     if(pid == 0)            //子进程
  24.     {
  25.         close(fd[0]);        //子进程关闭管道读端        
  26.         write(fd[1], "hello\n", 6);    //子进程写管道
  27.     }
  28.     else if(pid > 0)
  29.     {
  30.         close(fd[1]);        //父进程关闭管道写端        
  31.         read(fd[0], buf, 6);    //父进程读管道
  32.         printf("%s",buf);
  33.     }
  34.     return 0;
  35. }
  1. /*
  2.  * 命名管道(FIFO)
  3.  * 用于任何进程之间通信
  4.  * int mkfifo(const char * pathname, mode_t mode);
  5.  * 命令行命名管道
  6.     mkfifo cmd_fifo
  7.     cat cmd_fifo //tty1
  8.     echo hi > cmd_fifo //tty2
  9.  *Lzy    2011-6-14
  10.  */

  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <sys/types.h>
  14. #include <sys/stat.h>

  15. /*
  16.  * 创建命名管道
  17.  */
  18. int main(int argc, char *argv[])
  19. {
  20.     mode_t mode = 0755;        //定义文件的询问权限
  21.     
  22.     if(mkfifo(argv[1], mode) < 0)    //创建FIFO文件
  23.     {
  24.         perror("mkfifo");
  25.         exit(0);
  26.     }
  27.     else
  28.         printf("Done!\n");

  29.     return 0;
  30. }
  1. /*
  2.  * 命名管道实现非亲缘关系进程的进程
  3.  * 发送程序
  4.  * 从键盘输入一串字符至管道文件
  5.  * Lzy    2011-6-14
  6.  */

  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include <fcntl.h>
  11. #include <string.h>

  12. int main(void)
  13. {
  14.     int fd;
  15.     char buf[10];        //自定义缓冲区

  16.     fd = open("fifo", O_WRONLY);    //以写方式打开管道文件
  17.     if(fd < 0)
  18.     {
  19.         perror("open");
  20.         exit(0);
  21.     }
  22.     
  23.     read(0, buf, sizeof(buf));            //从键盘读入
  24.     write(fd, buf,strlen(buf));        //写入管道文件

  25.     return 0;
  26. }
  1. /*
  2.  * 命名管道实现非亲缘关系进程的进程
  3.  * 接收程序
  4.  * 从管道文件读内容至屏幕
  5.  * Lzy    2011-6-14
  6.  */

  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include <fcntl.h>
  11. #include <string.h>

  12. int main(void)
  13. {
  14.     int fd;
  15.     char buf[10];        //自定义缓冲区

  16.     fd = open("fifo", O_RDONLY);    //以读方式打开管道文件
  17.     if(fd < 0)
  18.     {
  19.         perror("open");
  20.         exit(0);
  21.     }
  22.     
  23.     read(fd, buf, sizeof(buf));        //从管道文件读入
  24.     write(1, buf,strlen(buf));        //写到屏幕

  25.     return 0;
  26. }
阅读(1235) | 评论(0) | 转发(2) |
0

上一篇:共享内存

下一篇:信号集

给主人留下些什么吧!~~