//A进程代码
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>
void* callback(void* arg){
if(mkfifo("./aqx.c",0777)<0){
printf("%d\n",errno);
if(17!=errno){
perror("mkfifo");
return 0;
}
}
printf("fifo2 create success\n");
int fp=open("./aqx.c",O_RDONLY);
if(fp<0){
perror("open2");
return 0;
}
printf("open2 success\n");
char buf[128]="";
ssize_t res;
while(1){
bzero(buf,sizeof(buf));
if((res=read(fp,buf,sizeof(buf)))<0){
perror("write");
break;
}else if(0==res){
printf("对方进程退出\n");
exit(0);
}
printf("收到数据:%s\n",buf);
}
close(fp);
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
umask(0);
if(mkfifo("./axp.c",0777)<0){
printf("%d\n",errno);
if(17!=errno){
perror("mkfifo");
return 0;
}
}
printf("fifo1 create success\n");
int fd=open("./axp.c",O_WRONLY);
if(fd<0){
perror("open1");
return 0;
}
printf("open1 success\n");
pthread_t tid;
if(pthread_create(&tid,NULL,callback,NULL)!=0){
perror("pthread_create");
return 0;
}
char buf[128]="";
while(1){
printf("请输入>>>\n");
bzero(buf,sizeof(buf));
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]='\0';
if((write(fd,buf,sizeof(buf)))<0){
perror("write");
break;
}
if(!strcmp(buf,"quit")){
break;
}
}
close(fd);
return 0;
}
//B进程代码
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>
void* callback(void* arg){
if(mkfifo("./aqx.c",0777)<0){
printf("%d\n",errno);
if(17!=errno){
perror("mkfifo");
return 0;
}
}
printf("fifo2 create success\n");
int fp=open("./aqx.c",O_WRONLY);
if(fp<0){
perror("open2");
return 0;
}
printf("open2 success\n");
char buf[128]="";
while(1){
printf("请输入>>>\n");
bzero(buf,sizeof(buf));
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]='\0';
if((write(fp,buf,sizeof(buf)))<0){
perror("write");
break;
}
if(!strcmp(buf,"quit")){
exit(0);
}
}
close(fp);
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
umask(0);
if(mkfifo("./axp.c",0777)<0){
printf("%d\n",errno);
if(17!=errno){
perror("mkfifo");
return 0;
}
}
printf("fifo1 create success\n");
int fd=open("./axp.c",O_RDONLY);
if(fd<0){
perror("open1");
return 0;
}
printf("open1 success\n");
pthread_t tid;
if(pthread_create(&tid,NULL,callback,NULL)!=0){
perror("pthread_create");
return 0;
}
char buf[128]="";
ssize_t res;
while(1){
bzero(buf,sizeof(buf));
if((res=read(fd,buf,sizeof(buf)))<0){
perror("write");
break;
}else if(0==res){
printf("对方进程退出\n");
break;
}
printf("收到数据:%s\n",buf);
}
close(fd);
return 0;
}
ub