代码:
A进程:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <pthread.h>
pthread_t tid1,tid2;
void *read_from_two(void *arg)
{
//打开管道文件2,开启读端
int fd2 = open("./myfifo2",O_RDONLY);
if(fd2 < 0 )
{
perror("open");
return NULL;
}
printf("open fifo2 sucess!\n");
char buf[128] = "";
char str[] = "quit";
ssize_t res;
while(1)
{
bzero(buf,sizeof(buf));//清除数组中读取或写入的数据
res = read(fd2,buf,sizeof(buf));//向管道文件2读取
if(res < 0)
{
perror("read");
return NULL;
}
else if(res == 0)
{
printf("文件2写端关闭\n");
break;
}
if(strcmp(str,buf) == 0)//判断是否是退出条件
{
printf("退出\n");
break;
}
printf("读端2读取>>%s\n",buf);
}
close(fd2);
pthread_cancel(tid2);
pthread_exit(NULL);
}
void *write_in_one(void *arg)
{
sleep(1);
//打开管道文件1,开启写端
int fd1 = open("./myfifo1",O_WRONLY);
if(fd1 < 0 )
{
perror("open");
return NULL;
}
printf("open fifo1 sucess!\n");
char buf[128] = "";
char str[] = "quit";
while(1)
{
bzero(buf,sizeof(buf));//清除数组中读取或写入的数据
fgets(buf,sizeof(buf),stdin);//从终端读取
buf[strlen(buf)-1] = '\0';//由于fgets会读到\n结束,为了消除\n所以将其置0
if(write(fd1,buf,sizeof(buf)) < 0)//将数组内容写入管道文件中
{
perror("write");
return NULL;
}
if(strcmp(str,buf) == 0)//判断是否是退出条件
{
printf("退出\n");
break;
}
}
close(fd1);
pthread_cancel(tid1);
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
umask(0);
//创建有名管道文件1
if(mkfifo("./myfifo1",0664) < 0)
{
if(errno != EEXIST)
{
perror("open");
return -1;
}
}
//创建管道文件2
if(mkfifo("./myfifo2",0664) < 0)
{
if(errno != EEXIST)
{
perror("open");
return -1;
}
}
printf("create fifo2 sucess!\n");
printf("create fifo1 sucess!\n");
if(pthread_create(&tid1,NULL,read_from_two,NULL) != 0)//创建读取线程
{
fprintf(stderr,"pthread_create failed\n");
return -1;
}
if(pthread_create(&tid2,NULL,write_in_one,NULL) != 0)//创建写入线程
{
fprintf(stderr,"pthread_create failed\n");
return -1;
}
//回收子线程
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
return 0;
}
B进程:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <pthread.h>
pthread_t tid1,tid2;
void *read_from_one(void *arg)//从管道文件1读取
{
//打开管道文件1,开启读端
int fd1 = open("./myfifo1",O_RDONLY);
if(fd1 < 0)
{
perror("open");
return NULL;
}
printf("open1 fifo1 sucess!\n");
char buf[128] = "";
char str[] = "quit";
ssize_t res;
while(1)
{
bzero(buf,sizeof(buf));//清除数组中读取或写入的数据
res = read(fd1,buf,sizeof(buf));//待子进程写入后向管道文件1读取
if(res < 0)
{
perror("read");
return NULL;
}
else if(res == 0)
{
fprintf(stderr,"写端退出\n");
break;
}
if(strcmp(str,buf) == 0)//判断是否是退出条件
{
printf("退出\n");
break;
}
printf("读端1读取>>%s\n",buf);
}
close(fd1);
pthread_cancel(tid2);
pthread_exit(NULL);
}
void *write_in_two(void *arg)//在管道文件2写入
{
sleep(1);
//打开管道文件2,开启写端
int fd2 = open("./myfifo2",O_WRONLY);
if(fd2 < 0)
{
perror("open");
return NULL;
}
printf("open fifo2 sucess!\n");
char buf[128] = "";
char str[] = "quit";
while(1)
{
bzero(buf,sizeof(buf));//清除数组中读取或写入的数据
fgets(buf,sizeof(buf),stdin);//从终端读取
buf[strlen(buf)-1] = '\0';//由于fgets会读到\n结束,为了消除\n所以将其置0
if(write(fd2,buf,sizeof(buf)) < 0)//将数组内容写入管道文件2中
{
perror("write");
return NULL;
}
if(strcmp(str,buf) == 0)//判断是否是退出条件
{
printf("退出\n");
break;
}
}
close(fd2);
pthread_cancel(tid1);
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
umask(0);
//创建管道文件2
if(mkfifo("./myfifo2",0664) < 0)
{
if(errno != EEXIST)
{
perror("open");
return -1;
}
}
printf("create fifo2 sucess!\n");
//创建管道文件1
if(mkfifo("./myfifo1",0664) < 0)
{
if(errno != EEXIST)
{
perror("open");
return -1;
}
}
printf("create fifo1 sucess!\n");
if(pthread_create(&tid1,NULL,read_from_one,NULL) != 0)//创建读取线程
{
fprintf(stderr,"pthread_create failed\n");
return -1;
}
if(pthread_create(&tid2,NULL,write_in_two,NULL) != 0)//创建写入线程
{
fprintf(stderr,"pthread_create failed\n");
return -1;
}
//回收子线程
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
return 0;
}
现象: