A进程代码:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include<errno.h>
#include<string.h>
#include<pthread.h>
pthread_t tid1;
pthread_t tid2;
char buf[128] = "";
char buf1[128] = "";
ssize_t res = 0;
//分支线程从向管道2中读取数据
void *callback1(void *arg)
{
while(1)
{
bzero(buf1,sizeof(buf1));
res = read(*(int*)arg,buf1,sizeof(buf1));
if(res < 0)
{
perror("read");
return NULL;
}
else if(res == 0)
{
fprintf(stderr,"对方进程退出\n");
break;
}
if(strcmp(buf1,"quit") == 0)
{
printf("退出\n");
break;
}
printf("成功接收数据 buf1=%s\n",buf1);
}
pthread_cancel(tid2); //终止线程2
pthread_exit(NULL);
}
void *callback2(void* arg)
{
while(1)
{
//主线程向管道中写入数据
bzero(buf,sizeof(buf));
printf("请输入>>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = 0;
res = write(*(int *)arg,buf,sizeof(buf));
if(res < 0)
{
perror("write");
return NULL;
}
printf("成功发送数据 res=%ld\n",res);
if(strcmp(buf,"quit") == 0)
{
printf("退出\n");
break;
}
}
pthread_cancel(tid1); //终止线程1
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
//清空umask
umask(0);
//创建fifo1文件
if(mkfifo("./myfifo1",0664) < 0)
{
if(errno != EEXIST) //EEXIST=17
{
perror("mkfifo");
return -1;
}
}
printf("create FIFO success\n");
//创建fifo2文件
if(mkfifo("./myfifo2",0664) < 0)
{
if(errno != EEXIST) //EEXIST=17
{
perror("mkfifo");
return -1;
}
}
printf("create myofifo1 sucess\n");
//以写的方式打开有名管道1文件
int fd = open("./myfifo1",O_WRONLY); //阻塞
if(fd <0)
{
perror("open");
return -1;
}
printf("open myofifo1 sucess\n");
//以读的方式打开有名管道2文件1
int fd1 = open("./myfifo2",O_RDONLY); //阻塞
if(fd <0)
{
perror("open");
return -1;
}
printf("open myofifo2 success\n");
//创建2个分支线程
if(pthread_create(&tid1,NULL,callback1,&fd1) !=0)
{
fprintf(stderr,"pthread_create failed\n");
return -1;
}
if(pthread_create(&tid2,NULL,callback2,&fd) !=0)
{
fprintf(stderr,"pthread_create failed\n");
return -1;
}
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
//关闭有名管道文件
close(fd);
close(fd1);
return 0;
}
B进程代码
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include<errno.h>
#include<string.h>
#include<pthread.h>
pthread_t tid1;
pthread_t tid2;
char buf[128] = "";
char buf1[128] = "";
ssize_t res = 0;
void *callback1(void *arg)
{
while(1)
{
//向管道2中写入数据
bzero(buf1,sizeof(buf1));
printf("请输入>>>");
fgets(buf1,sizeof(buf1),stdin);
buf1[strlen(buf1)-1] = 0;
res = write(*(int*)arg,buf1,sizeof(buf1));
if(res < 0)
{
perror("write");
return NULL;
}
if(strcmp(buf1,"quit") == 0)
{
printf("退出\n");
break;
}
printf("成功发送数据 res=%ld\n",res);
}
pthread_cancel(tid2);
pthread_exit(NULL);
}
void *callback2(void *arg)
{
while(1)
{
//分支线程2从向管道中读取数据
bzero(buf,sizeof(buf));
res = read(*(int*)arg,buf,sizeof(buf));
if(res < 0)
{
perror("read");
return NULL;
}
else if(res == 0)
{
fprintf(stderr,"对方进程退出\n");
break;
}
if(strcmp(buf,"quit") == 0)
{
printf("退出\n");
break;
}
pthread_cancel(tid1);
printf("成功接收数据 buf=%s\n",buf);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
//清空umask
umask(0);
//创建fifo1文件
if(mkfifo("./myfifo1",0664) < 0)
{
if(errno != EEXIST) //EEXIST=17
{
perror("mkfifo");
return -1;
}
}
printf("create FIFO1 success\n");
//创建fifo2文件
if(mkfifo("./myfifo2",0664) < 0)
{
if(errno != EEXIST) //EEXIST=17
{
perror("mkfifo");
return -1;
}
}
printf("create FIFO2 success\n");
//以只读的方式打开有名管道文件1 //阻塞
int fd = open("./myfifo1",O_RDONLY);
if(fd <0)
{
perror("open");
return -1;
}
printf("open FIFO1 success\n");
//以只写的方式打开有名管道文件2 //阻塞
int fd1 = open("./myfifo2",O_WRONLY);
if(fd1 <0)
{
perror("open");
return -1;
}
printf("open FIFO2 success\n");
//创建2个分支线程
if(pthread_create(&tid1,NULL,callback1,&fd1))
{
fprintf(stderr,"pthread_create failed\n"); //写进程
return -1;
}
pthread_t tid2;
if(pthread_create(&tid2,NULL,callback2,&fd))
{
fprintf(stderr,"pthread_create failed\n"); //读进程
return -1;
}
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
//关闭有名管道文件
close(fd);
close(fd1);
return 0;
}
运行结果: