1.要求实现AB进程对话
A进程先发送一句话给B进程,B进程接收后打印
B进程再回复一句话给A进程,A进程接收后打印
重复1.2步骤,当收到quit后,要结束AB进程
fifo_read.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char const *argv[])
{
umask(0);
if(mkfifo("myfifo",0777)<0)
{
printf("errno = %d\n",errno);
//#define EEXITST 17 ==> 文件存在的错误排除
if(17!=errno)
{
perror("mkfifo");
return -1;
}
}
printf("fifo create success\n");
//只能用文件IO,不能用标准IO
int fd = open("myfifo",O_RDONLY);
if(fd < 0)
{
perror("open");
return -1;
}
printf("RDONLY open myfifo success\n");
/************************************/
if(mkfifo("myfifo1",0777)<0)
{
printf("errno = %d\n",errno);
//#define EEXITST 17 ==> 文件存在的错误排除
if(17!=errno)
{
perror("mkfifo1");
return -1;
}
}
printf("fifo create success\n");
//只能用文件IO,不能用标准IO
int fd1 = open("myfifo1",O_WRONLY);
if(fd1 < 0)
{
perror("open");
return -1;
}
printf("WRONLY open myfifo1 success\n");
/******************************************/
//myfifo读取数据
char buf[128] = "";
ssize_t ret = 0;
while(1)
{
//myfifo1写入数据
bzero(buf,sizeof(buf));
printf("A请输入>>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = 0;
if (write(fd1,buf,sizeof(buf))<0)
{
perror("write");
return -1;
}
printf("A发送 %s 成功\n",buf);
if(strcasecmp(buf,"quit")==0)
{
break;
}
/*****************************/
bzero(buf,sizeof(buf));
ret = read(fd,buf,sizeof(buf));
if(ret < 0)
{
perror("read");
return -1;
} else if(0==ret)
{
fprintf(stderr,"对方进程退出\n");
break;
}
printf("A 接收==>[%ld] : %s\n",ret,buf);
if(strcasecmp(buf,"quit")==0)
{
break;
}
}
close(fd);
close(fd1);
return 0;
}
fifo_write.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char const *argv[])
{
char buf[128] = "";
ssize_t ret = 0;
umask(0);
if(mkfifo("myfifo",0777)<0)
{
printf("errno = %d\n",errno);
//#define EEXITST 17 ==> 文件存在的错误排除
if(17!=errno)
{
perror("mkfifo");
return -1;
}
}
printf("fifo create success\n");
//只能用文件IO,不能用标准IO
int fd = open("myfifo",O_WRONLY);
if(fd < 0)
{
perror("open");
return -1;
}
printf("WRONLY open myfifo success\n");
/*******************************************/
if(mkfifo("myfifo1",0777)<0)
{
printf("errno = %d\n",errno);
//#define EEXITST 17 ==> 文件存在的错误排除
if(17!=errno)
{
perror("mkfifo1");
return -1;
}
}
printf("fifo1 create success\n");
//只能用文件IO,不能用标准IO
int fd1 = open("myfifo1",O_RDONLY);
if(fd1 < 0)
{
perror("open");
return -1;
}
printf("RDONLY open myfifo1 success\n");
/*********************************************/
//myfifo发送数据
while (1)
{
//myfifo1读取数据
bzero(buf,sizeof(buf));
ret = read(fd1,buf,sizeof(buf));
if(ret < 0)
{
perror("read");
return -1;
} else if(0==ret)
{
fprintf(stderr,"对方进程退出\n");
break;
}
printf("B==>收到 [%ld] : %s\n",ret,buf);
if(strcasecmp(buf,"quit")==0)
{
break;
}
/************/
bzero(buf,sizeof(buf));
printf("B请输入>>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = 0;
if (write(fd,buf,sizeof(buf))<0)
{
perror("write");
return -1;
}
printf("B发出 %s 成功\n",buf);
if(strcasecmp(buf,"quit")==0)
{
break;
}
}
close(fd);
close(fd1);
return 0;
}
运行效果:
2.在上述练习基础上实现AB进程对话,要求AB进程能够随时收发。
提示:多进程,多线程。
thread_read.c
#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* callback1(void *arg)
{
umask(0);
if(mkfifo("myfifo",0777)<0)
{
printf("errno = %d\n",errno);
//#define EEXITST 17 ==> 文件存在的错误排除
if(17!=errno)
{
perror("mkfifo");
return NULL;
}
}
printf("fifo create success\n");
//只能用文件IO,不能用标准IO
int fd = open("myfifo",O_RDONLY);
if(fd < 0)
{
perror("open");
return NULL;
}
//printf("RDONLY open myfifo success\n");
/*****************************/
char buf[128] = "";
ssize_t ret = 0;
while (1)
{
bzero(buf,sizeof(buf));
ret = read(fd,buf,sizeof(buf));
if(ret < 0)
{
perror("read");
return NULL;
} else if(0==ret)
{
fprintf(stderr,"对方进程退出\n");
break;
}
printf("A 接收==>[%ld] : %s\n",ret,buf);
if(strcasecmp(buf,"quit")==0)
{
exit(0);
}
}
close(fd);
}
void* callback2(void *arg)
{
umask(0);
if(mkfifo("myfifo1",0777)<0)
{
printf("errno = %d\n",errno);
//#define EEXITST 17 ==> 文件存在的错误排除
if(17!=errno)
{
perror("mkfifo1");
return NULL;
}
}
printf("fifo create success\n");
//只能用文件IO,不能用标准IO
int fd1 = open("myfifo1",O_WRONLY);
if(fd1 < 0)
{
perror("open");
return NULL;
}
printf("WRONLY open myfifo1 success\n");
//myfifo读取数据
char buf[128] = "";
ssize_t ret = 0;
while(1)
{
//myfifo1写入数据
bzero(buf,sizeof(buf));
//printf("A请输入>>>\n");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = 0;
if (write(fd1,buf,sizeof(buf))<0)
{
perror("write");
return NULL;
}
printf("A发送 %s 成功\n",buf);
if(strcasecmp(buf,"quit")==0)
{
exit(0);
}
}
close(fd1);
}
int main(int argc, char const *argv[])
{
//创建两个线程,分别用作A的收和发
pthread_t tid1,tid2;
if(pthread_create(&tid1,NULL,callback1,NULL)!=0)
{
perror("pthread_create");
return -1;
}
if(pthread_create(&tid2,NULL,callback2,NULL)!=0)
{
perror("pthread_create");
return -1;
}
pthread_detach(tid1);
// pthread_detach(tid2);
//pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
return 0;
}
thread_write.c
#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* callback1(void *arg)
{
char buf[128] = "";
ssize_t ret = 0;
umask(0);
if(mkfifo("myfifo1",0777)<0)
{
printf("errno = %d\n",errno);
//#define EEXITST 17 ==> 文件存在的错误排除
if(17!=errno)
{
perror("mkfifo1");
return NULL;
}
}
printf("fifo1 create success\n");
//只能用文件IO,不能用标准IO
int fd1 = open("myfifo1",O_RDONLY);
if(fd1 < 0)
{
perror("open");
return NULL;
}
//printf("RDONLY open myfifo1 success\n");
while(1)
{
//myfifo1读取数据
bzero(buf,sizeof(buf));
ret = read(fd1,buf,sizeof(buf));
if(ret < 0)
{
perror("read");
return NULL;
} else if(0==ret)
{
fprintf(stderr,"对方进程退出\n");
break;
}
printf("B==>收到 [%ld] : %s\n",ret,buf);
if(strcasecmp(buf,"quit")==0)
{
exit(0);
}
}
close(fd1);
}
void* callback2(void *arg)
{
char buf[128] = "";
ssize_t ret = 0;
umask(0);
if(mkfifo("myfifo",0777)<0)
{
printf("errno = %d\n",errno);
//#define EEXITST 17 ==> 文件存在的错误排除
if(17!=errno)
{
perror("mkfifo");
return NULL;
}
}
printf("fifo create success\n");
//只能用文件IO,不能用标准IO
int fd = open("myfifo",O_WRONLY);
if(fd < 0)
{
perror("open");
return NULL;
}
printf("WRONLY open myfifo success\n");
//myfifo发送数据
while (1)
{
/************/
bzero(buf,sizeof(buf));
//printf("B请输入>>>\n");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = 0;
if (write(fd,buf,sizeof(buf))<0)
{
perror("write");
return NULL;
}
printf("B发出 %s 成功\n",buf);
if(strcasecmp(buf,"quit")==0)
{
exit(0);
}
}
close(fd);
}
int main(int argc, char const *argv[])
{
//创建两个线程,分别用作B的收和发
pthread_t tid1,tid2;
if(pthread_create(&tid1,NULL,callback1,NULL)!=0)
{
perror("pthread_create");
return -1;
}
if(pthread_create(&tid2,NULL,callback2,NULL)!=0)
{
perror("pthread_create");
return -1;
}
pthread_detach(tid2);
// pthread_join(tid1,NULL);
pthread_join(tid1,NULL);
return 0;
}
运行效果