IPC--------fifo的进阶使用
2013-06-22 16:09
141 查看
练习:
写两个程序fifo1 、fifo2,各自建立父子进程,子进程用来读管道,父进程用来写管道(即fifo1 的父进程写管道A,fifo2的子进程读A,结果输出到终端;fifo2的父进程写B,fifo1的子进程读B,结果输出到终端),实现fifo1 、fifo2之间的通信
调试:./A fifo1 fifo2
./B fifo1 fifo2
运行结果:
源代码:
fifo1.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
int read_fifo(int fifo_fd)
{
int n;
char buf[1024];
while(1)
{
n = read(fifo_fd,buf,sizeof(buf) -1);
buf
= '\0';
printf("Read %d bytes : %s.\n",n,buf);
if(strncmp(buf,"quit",4) == 0)
break;
}
return 0;
}
int write_fifo(int fifo_fd)
{
char buf[1024];
while(1)
{
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf) - 1] = '\0';
write(fifo_fd,buf,strlen(buf));
if(strncmp(buf,"quit",4) == 0)
break;
}
return 0;
}
//./a.out fifo1 fifo2
int main(int argc,char *argv[])
{
pid_t pid;
int n;
char buf[1024];
int fifo_fd1,fifo_fd2;
if(argc < 3)
{
fprintf(stderr,"Usage : %s argv[1] argv[2].\n",argv[0]);
exit(EXIT_FAILURE);
}
//鍒涘缓鏈夊悕绠¢亾
if(mkfifo(argv[1],0666) < 0 && errno != EEXIST)
{
printf("errno = %d.\n",errno);
fprintf(stderr,"Fail to mkfifo %s : %s.\n",argv[1],strerror(errno));
exit(EXIT_FAILURE);
}
//鍒涘缓鏈夊悕绠¢亾
if(mkfifo(argv[2],0666) < 0 && errno != EEXIST)
{
printf("errno = %d.\n",errno);
fprintf(stderr,"Fail to mkfifo %s : %s.\n",argv[1],strerror(errno));
exit(EXIT_FAILURE);
}
if((fifo_fd1 = open(argv[1],O_RDONLY)) < 0)
{
fprintf(stderr,"Fail to open %s : %s.\n",argv[1],strerror(errno));
exit(EXIT_FAILURE);
}
printf("open fifo1 success for readonly.\n ");
if((fifo_fd2 = open(argv[2],O_WRONLY)) < 0)
{
fprintf(stderr,"Fail to open %s : %s.\n",argv[1],strerror(errno));
exit(EXIT_FAILURE);
}
printf("open fifo2 success for writeonly.\n ");
if((pid = fork()) < 0)
{
perror("Fail to fork");
exit(EXIT_FAILURE);
}
if(pid == 0)
{
read_fifo(fifo_fd1);
}
if(pid > 0)
{
write_fifo(fifo_fd2);
}
exit(EXIT_SUCCESS);
}
fifo2.c
写两个程序fifo1 、fifo2,各自建立父子进程,子进程用来读管道,父进程用来写管道(即fifo1 的父进程写管道A,fifo2的子进程读A,结果输出到终端;fifo2的父进程写B,fifo1的子进程读B,结果输出到终端),实现fifo1 、fifo2之间的通信
调试:./A fifo1 fifo2
./B fifo1 fifo2
运行结果:
源代码:
fifo1.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
int read_fifo(int fifo_fd)
{
int n;
char buf[1024];
while(1)
{
n = read(fifo_fd,buf,sizeof(buf) -1);
buf
= '\0';
printf("Read %d bytes : %s.\n",n,buf);
if(strncmp(buf,"quit",4) == 0)
break;
}
return 0;
}
int write_fifo(int fifo_fd)
{
char buf[1024];
while(1)
{
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf) - 1] = '\0';
write(fifo_fd,buf,strlen(buf));
if(strncmp(buf,"quit",4) == 0)
break;
}
return 0;
}
//./a.out fifo1 fifo2
int main(int argc,char *argv[])
{
pid_t pid;
int n;
char buf[1024];
int fifo_fd1,fifo_fd2;
if(argc < 3)
{
fprintf(stderr,"Usage : %s argv[1] argv[2].\n",argv[0]);
exit(EXIT_FAILURE);
}
//鍒涘缓鏈夊悕绠¢亾
if(mkfifo(argv[1],0666) < 0 && errno != EEXIST)
{
printf("errno = %d.\n",errno);
fprintf(stderr,"Fail to mkfifo %s : %s.\n",argv[1],strerror(errno));
exit(EXIT_FAILURE);
}
//鍒涘缓鏈夊悕绠¢亾
if(mkfifo(argv[2],0666) < 0 && errno != EEXIST)
{
printf("errno = %d.\n",errno);
fprintf(stderr,"Fail to mkfifo %s : %s.\n",argv[1],strerror(errno));
exit(EXIT_FAILURE);
}
if((fifo_fd1 = open(argv[1],O_RDONLY)) < 0)
{
fprintf(stderr,"Fail to open %s : %s.\n",argv[1],strerror(errno));
exit(EXIT_FAILURE);
}
printf("open fifo1 success for readonly.\n ");
if((fifo_fd2 = open(argv[2],O_WRONLY)) < 0)
{
fprintf(stderr,"Fail to open %s : %s.\n",argv[1],strerror(errno));
exit(EXIT_FAILURE);
}
printf("open fifo2 success for writeonly.\n ");
if((pid = fork()) < 0)
{
perror("Fail to fork");
exit(EXIT_FAILURE);
}
if(pid == 0)
{
read_fifo(fifo_fd1);
}
if(pid > 0)
{
write_fifo(fifo_fd2);
}
exit(EXIT_SUCCESS);
}
fifo2.c
#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <fcntl.h> #include <sys/stat.h> #include <errno.h> #include <string.h> int read_fifo(int fifo_fd) { int n; char buf[1024]; while(1) { n = read(fifo_fd,buf,sizeof(buf) -1); buf = '\0'; printf("Read %d bytes : %s.\n",n,buf); if(strncmp(buf,"quit",4) == 0) break; } return 0; } int write_fifo(int fifo_fd) { char buf[1024]; while(1) { fgets(buf,sizeof(buf),stdin); buf[strlen(buf) - 1] = '\0'; write(fifo_fd,buf,strlen(buf)); if(strncmp(buf,"quit",4) == 0) break; } return 0; } //./a.out fifo1 fifo2 int main(int argc,char *argv[]) { pid_t pid; int n; char buf[1024]; int fifo_fd1,fifo_fd2; if(argc < 3) { fprintf(stderr,"Usage : %s argv[1] argv[2].\n",argv[0]); exit(EXIT_FAILURE); } //鍒涘缓鏈夊悕绠¢亾 if(mkfifo(argv[1],0666) < 0 && errno != EEXIST) { printf("errno = %d.\n",errno); fprintf(stderr,"Fail to mkfifo %s : %s.\n",argv[1],strerror(errno)); exit(EXIT_FAILURE); } //鍒涘缓鏈夊悕绠¢亾 if(mkfifo(argv[2],0666) < 0 && errno != EEXIST) { printf("errno = %d.\n",errno); fprintf(stderr,"Fail to mkfifo %s : %s.\n",argv[1],strerror(errno)); exit(EXIT_FAILURE); } if((fifo_fd2 = open(argv[1],O_WRONLY)) < 0) { fprintf(stderr,"Fail to open %s : %s.\n",argv[1],strerror(errno)); exit(EXIT_FAILURE); } printf("open fifo2 success for writeonly.\n "); if((fifo_fd1 = open(argv[2],O_RDONLY)) < 0) { fprintf(stderr,"Fail to open %s : %s.\n",argv[1],strerror(errno)); exit(EXIT_FAILURE); } printf("open fifo1 success for readonly.\n "); if((pid = fork()) < 0) { perror("Fail to fork"); exit(EXIT_FAILURE); } if(pid == 0) { read_fifo(fifo_fd1); } if(pid > 0) { write_fifo(fifo_fd2); } exit(EXIT_SUCCESS); }
相关文章推荐
- Android进阶:AIDL实现IPC使用详解
- Android开发进阶—跨进程通信(IPC)使用AIDL(上)
- log4j使用方法(二)—— log4j配置进阶
- EventBus使用详解(二)——EventBus使用进阶
- cocos进阶教程(5)各种动画使用心得
- Android SVG使用进阶
- vi/vim使用进阶: 目录
- 【Android进阶】怎样使用文件来保存程序中的数据
- Inno Setup使用进阶(一)
- Android的IPC机制(六)—— BroadcastReceiver的使用
- android UI进阶之弹窗的使用
- mongo db 入门进阶 第二章:命令使用
- vi/vim使用进阶: lookupfile插件
- Java记录 -85- Reflection API的使用示例进阶
- Android进阶学习-SurfaceView的使用及其模板(View的孪生兄弟)
- MySQL进阶——使用xtrabackup进行数据库备份
- Android高手进阶教程(四)之----Android 中自定义属性(attr.xml,TypedArray)的使用!
- Hadoop MapReduce进阶 使用DataJoin包实现Join
- ORM进阶:使用hql优化查询
- android跨进程通信(IPC):使用AIDL