1、作业要求
2、实现过程
1)A进程
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/stat.h>
#include <sys/types.h>
void* pthread_c1(void *arg);
void* pthread_c2(void *arg);
char str[100] = "";
//char str2[100] = "";
int main(int argc, const char *argv[])
{
//清空umask
umask(0);
//创建fifo_1文件
if(mkfifo("./myfifo_1",0664)<0){
if(errno!=EEXIST){ //EEXIST <==> 17
perror("mkfifo");
return -1;
}
}
//创建fifo_2文件
if(mkfifo("./myfifo_2",0664)<0){
if(errno!=EEXIST){ //EEXIST <==> 17
perror("mkfifo");
return -1;
}
}
//printf("成功创建fifo文件\n");
//以只读的方式打开文件
int fd1 = open("./myfifo_1",O_RDONLY);
if(fd1<0){
perror("open");
return -1;
}
//以只写的方式打开文件
int fd2 = open("./myfifo_2",O_WRONLY);
if(fd2<0){
perror("open");
return -1;
}
//创建A线程,接受数据
pthread_t tid1,tid2;
int res1 = pthread_create(&tid1,NULL,pthread_c1,&fd1);
if(res1!=0){
fprintf(stderr,"A线程创建失败\n");
return -1;
}
//创建B线程,发送数据
int res2 = pthread_create(&tid2,NULL,pthread_c2,&fd2);
if(res2!=0){
fprintf(stderr,"B线程创建失败\n");
return -1;
}
//等待分支线程退出
pthread_join(tid2,NULL);
//当终端输入quit后,B线程自动退出,然后请求A线程退出
pthread_cancel(tid1);
pthread_join(tid1,NULL);
//关闭文件
close(fd1);
close(fd2);
return 0;
}
//A线程,接收数据
void* pthread_c1(void *arg){
int fd1 = *(int*)arg;
ssize_t res1 = 0;
while(1){
bzero(str,sizeof(str));
res1 = read(fd1,str,sizeof(str));
if(res1<0){
perror("read");
return NULL;
}
if(strcmp(str,"quit")==0){
printf("对方已退出\n");
break;
}
printf("对方说>>> %s\n",str);
//printf("请输入");
}
pthread_exit(NULL);
}
//B线程,发送数据
void* pthread_c2(void *arg){
int fd2 = *(int*)arg;
ssize_t res2 = 0;
while(1){
bzero(str,sizeof(str));
//printf("请输入>>>");
fgets(str,sizeof(str),stdin);
str[strlen(str)-1] = '\0';
res2 = write(fd2,str,sizeof(str));
if(res2<0){
perror("write");
return NULL;
}
if(strcmp(str,"quit")==0){
printf("自己退出\n");
break;
}
}
pthread_exit(NULL);
}
2)B进程
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/stat.h>
#include <sys/types.h>
void* pthread_c1(void *arg);
void* pthread_c2(void *arg);
char str[100] = "";
//char str2[100] = "";
int main(int argc, const char *argv[])
{
//清空umask
umask(0);
//创建fifo_1文件
if(mkfifo("./myfifo_1",0664)<0){
if(errno!=EEXIST){ //EEXIST <==> 17
perror("mkfifo");
return -1;
}
}
//创建fifo_2文件
if(mkfifo("./myfifo_2",0664)<0){
if(errno!=EEXIST){ //EEXIST <==> 17
perror("mkfifo");
return -1;
}
}
//以只写的方式打开文件
int fd1 = open("./myfifo_1",O_WRONLY);
if(fd1<0){
perror("open");
return -1;
}
//以只读的方式打开文件
int fd2 = open("./myfifo_2",O_RDONLY);
if(fd2<0){
perror("open");
return -1;
}
//创建A线程,接受数据
pthread_t tid1,tid2;
int res1 = pthread_create(&tid1,NULL,pthread_c1,&fd1);
if(res1!=0){
fprintf(stderr,"A线程创建失败\n");
return -1;
}
//创建B线程,发送数据
int res2 = pthread_create(&tid2,NULL,pthread_c2,&fd2);
if(res2!=0){
fprintf(stderr,"B线程创建失败\n");
return -1;
}
//等待分支线程退出
pthread_join(tid1,NULL);
//当终端输入quit后,A线程自动退出,然后请求B线程退出
pthread_cancel(tid2);
pthread_join(tid2,NULL);
//关闭文件
close(fd1);
close(fd2);
return 0;
}
//A线程,发送数据
void* pthread_c1(void *arg){
int fd1 = *(int*)arg;
ssize_t res1 = 0;
while(1){
bzero(str,sizeof(str));
//printf("请输入>>>");
fgets(str,sizeof(str),stdin);
str[strlen(str)-1] = '\0';
res1 = write(fd1,str,sizeof(str));
if(res1<0){
perror("write");
return NULL;
}
if(strcmp(str,"quit")==0){
printf("自己退出\n");
break;
}
}
}
//B线程,接受数据
void* pthread_c2(void *arg){
int fd2 = *(int*)arg;
ssize_t res2 = 0;
while(1){
bzero(str,sizeof(str));
res2 = read(fd2,str,sizeof(str));
if(res2<0){
perror("read");
return NULL;
}
if(strcmp(str,"quit")==0){
printf("对方已退出\n");
break;
}
printf("对方说>>> %s\n",str);
//printf("请输入");
}
}
3、实现效果