进程A代码
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
#include <string.h>
//线程A负责读数据
void* pthreadA_read(void* argv){
int fdA = *((int*)argv);
char bufA[128] = "";
int res;
while(1){
res = read(fdA, bufA, sizeof(bufA));
if(-1 == res){
perror("read");
return NULL;
}else if(0 == res){
fprintf(stderr, "管道fifo1中所有写端均关闭 __%d__\n", __LINE__);
return NULL;
}
if(!strcmp("quit", bufA)){
pthread_exit(NULL);
}
//打印读取到的数据
fprintf(stdout, "收到数据:%s\n", bufA);
memset(bufA, 0, sizeof(bufA));
}
}
//线程B负责写数据
void* pthreadB_write(void* argv){
int fdB = *((int*)argv);
char bufB[128] = "";
while(1){
//从终端获取数据
fgets(bufB, sizeof(bufB), stdin);
bufB[strlen(bufB)-1] = '\0'; //将\n替换为\0字符
if(-1 == write(fdB, bufB, sizeof(bufB))){
perror("write");
return NULL;
}
if(!strcmp("quit", bufB)){
pthread_exit(NULL);
}
printf("发送数据成功\n");
memset(bufB, 0, sizeof(bufB));
}
}
int main(int argc, const char *argv[]){
//创建有名管道fifo1
if(-1 == mkfifo("./fifo1", 0777)){
if(errno != 17){
perror("mkfifo");
return -1;
}
}
//创建有名管道fifo2
if(-1 == mkfifo("./fifo2", 0777)){
if(errno != 17){
perror("mkfifo");
return -1;
}
}
//以读的方式打开管道文件fifo1
int fdA = open("./fifo1", O_RDONLY);
if(-1 == fdA){
perror("open");
return -1;
}
//以写的方式打开管道文件fifo2
int fdB = open("./fifo2", O_WRONLY);
if(-1 == fdB){
perror("open");
return -1;
}
//创建两个线程
pthread_t tidA, tidB;
if(pthread_create(&tidA, NULL, pthreadA_read, (void*)&fdA) != 0){
fprintf(stderr, "pthread_create failed __%d__\n", __LINE__);
return -1;
}
if(pthread_create(&tidB, NULL, pthreadB_write, (void*)&fdB) != 0){
fprintf(stderr, "pthread_create failed __%d__\n", __LINE__);
return -1;
}
//主线程阻塞,监测子线程退出
pthread_detach(tidA);
pthread_join(tidB, NULL);
close(tidA);
close(tidB);
return 0;
}
进程B代码
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
#include <string.h>
//线程A负责读数据
void* pthreadA_read(void* argv){
int fdA = *((int*)argv);
char bufA[128] = "";
int res;
while(1){
res = read(fdA, bufA, sizeof(bufA));
if(-1 == res){
perror("read");
return NULL;
}else if(0 == res){
fprintf(stderr, "管道fifo1中所有写端均关闭 __%d__\n", __LINE__);
return NULL;
}
if(!strcmp("quit", bufA)){
pthread_exit(NULL);
}
//打印读取到的数据
fprintf(stdout, "收到数据:%s\n", bufA);
memset(bufA, 0, sizeof(bufA));
}
}
//线程B负责写数据
void* pthreadB_write(void* argv){
int fdB = *((int*)argv);
char bufB[128] = "";
//从终端获取数据
while(1){
fgets(bufB, sizeof(bufB), stdin);
bufB[strlen(bufB)-1] = '\0'; //将\n替换为\0字符
if(-1 == write(fdB, bufB, sizeof(bufB))){
perror("write");
return NULL;
}
if(!strcmp("quit", bufB)){
pthread_exit(NULL);
}
printf("发送数据成功\n");
memset(bufB, 0, sizeof(bufB));
}
}
int main(int argc, const char *argv[]){
//创建有名管道fifo1
if(-1 == mkfifo("./fifo1", 0777)){
if(errno != 17){
perror("mkfifo");
return -1;
}
}
//创建有名管道fifo2
if(-1 == mkfifo("./fifo2", 0777)){
if(errno != 17){
perror("mkfifo");
return -1;
}
}
//以写的方式打开管道文件fifo1
int fdA = open("./fifo1", O_WRONLY);
if(-1 == fdA){
perror("open");
return -1;
}
//以读的方式打开管道文件fifo2
int fdB = open("./fifo2", O_RDONLY);
if(-1 == fdB){
perror("open");
return -1;
}
//创建两个线程
pthread_t tidA, tidB;
if(pthread_create(&tidA, NULL, pthreadA_read, (void*)&fdB) != 0){
fprintf(stderr, "pthread_create failed __%d__\n", __LINE__);
return -1;
}
if(pthread_create(&tidB, NULL, pthreadB_write, (void*)&fdA) != 0){
fprintf(stderr, "pthread_create failed __%d__\n", __LINE__);
return -1;
}
//主线程阻塞,监测子线程退出
pthread_detach(tidA);
pthread_join(tidB, NULL);
close(tidA);
close(tidB);
return 0;
}