您的位置:首页 > 其它

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
#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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: