需求:
A进程发送一句话给B线程,B进程打印
B进程发送一句话给A线程,A进程打印,做到同时收发
代码实现过程:
进程A(fifoW.c):
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#define MSG_ERR(msg){\
fprintf(stderr,"line : %d\n",__LINE__);\
perror("msg");\
}
char str[32]="";
ssize_t res = 0;
char cmp[32]="quit";
int fd;
int fd1;
void* funcW(void *arg){
while(1){
bzero(str,sizeof(str));
//printf("请输入pipe1:");
fgets(str,sizeof(str),stdin);
str[strlen(str)-1]='\0';
if(strcmp(str,cmp)==0){
break;
}
if(write(fd,str,sizeof(str))<0){
perror("write");
return NULL;
}
}
}
void* funcR(void *arg){
while(1){
bzero(str,sizeof(str));
res = read(fd1,str,sizeof(str));
if(strcmp(str,cmp)==0){
break;
}
if(res<0){
perror("read");
return NULL;
}
else if(res == 0){
printf("pipe2写端关闭,管道无数据\n");
break;
}
printf("pipe2 : %s\n",str);
}
}
int main(int argc, const char *argv[])
{
umask(0);
if(mkfifo("./myfifo1",0664)< 0){
//printf("%d\n",errno);
if(errno != 17){
perror("mkfifo1");
return -1;
}
}
printf("pipe1 create success\n");
if(mkfifo("./myfifo2",0664)< 0){
//printf("%d\n",errno);
if(errno != 17){
perror("mkfifo2");
return -1;
}
}
printf("pipe2 create success\n");
fd = open("./myfifo1",O_WRONLY); //只读或只写阻塞,以读写模式打开不阻塞;添加O_NONBLOCK以何种形式打开都则不阻塞
fd1 = open("./myfifo2",O_RDONLY);
pthread_t tid_w;
if(pthread_create(&tid_w,NULL,funcW,NULL)<0){
MSG_ERR(pthread_creat);
return -1;
}
pthread_t tid_r;
if(pthread_create(&tid_r,NULL,funcR,NULL)<0){
MSG_ERR(phthread_create);
return -1;
}
pthread_detach(tid_r);
pthread_join(tid_w,NULL);
close(fd);
close(fd1);
return 0;
}
进程B(fifoR.c):
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#define MSG_ERR(msg){\
fprintf(stderr,"line : %d\n",__LINE__);\
perror("msg");\
}
char str[32]="";
ssize_t res = 0;
char cmp[32]="quit";
int fd;
int fd1;
void* funcR(void *arg){
while(1){
bzero(str,sizeof(str));
res = read(fd,str,sizeof(str));
if(strcmp(str,cmp)==0){
break;
}
if(res <0){
perror("read");
return NULL;
}
else if(res == 0){
printf("pipe1写端关闭,管道无数据\n");
break;
}
printf("pipe1 : %s\n",str);
}
}
void* funcW(void *arg){
while(1){
bzero(str,sizeof(str));
//printf("请输入pipe2:");
fgets(str,sizeof(str),stdin);
str[strlen(str)-1]='\0';
if(strcmp(str,cmp)==0){
break;
}
if(write(fd1,str,sizeof(str))<0){
perror("write");
return NULL;
}
}
}
int main(int argc, const char *argv[])
{
umask(0);
if(mkfifo("./myfifo2",0664)< 0){
//printf("%d\n",errno);
if(errno != 17){
perror("mkfifo2");
return -1;
}
}
printf("pipe2 create success\n");
if(mkfifo("./myfifo1",0664)< 0){
//printf("%d\n",errno);
if(errno != 17){
perror("mkfifo1");
return -1;
}
}
printf("pipe1 create success\n");
fd = open("./myfifo1",O_RDONLY); //只读或只写阻塞,以读写模式打开不阻塞;添加O_NONBLOCK以何种形式打开都则不阻塞
fd1 = open("./myfifo2",O_WRONLY);
pthread_t tid_r;
if(pthread_create(&tid_r,NULL,funcR,NULL)<0){
MSG_ERR(pthread_create);
return -1;
}
pthread_detach(tid_r);
pthread_t tid_w;
if(pthread_create(&tid_w,NULL,funcW,NULL)<0){
MSG_ERR(phthread_create);
return -1;
}
pthread_join(tid_w,NULL);
close(fd);
close(fd1);
return 0;
}
代码实现结果: