要求实现AB进程对话,实现AB进程能够随时收发数据。
1.A进程先发送一句话给子进程,B进程接收后打印
2.B进程再回复一句话给父进程,A进程接收后打印
3.重复1.2步骤,当收到quit后,要结束AB进程
提示:用多进程或者多线程实现
A进程代码:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <pthread.h>
#include <semaphore.h>
//创建数组
char buf[128] = "";
//创建变量接读写的返回值
ssize_t res = 0;
pthread_t tid_A,tid_B;
void* call_back_A(void* arga){
while(1){
//A写
int fd_A = (*(int* )arga);
bzero(buf,sizeof(buf));
//printf("A写:");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = '\0';
res = write(fd_A,buf,sizeof(buf));
if(res < 0){
perror("write");
return NULL;
}
if(strcmp(buf,"quit") == 0){
break;
}
}
pthread_cancel(tid_B);
pthread_exit(NULL);
}
void* call_back_B(void* argb){
while(1){
//B读
int fd_B=(*(int*) argb);
bzero(buf,sizeof(buf));
res = read(fd_B,buf,sizeof(buf));
if(res < 0){
perror("read");
return NULL;
}else if(0 == res){
fprintf(stderr,"对方进程已退出\n");
break;
}
printf("B说:%s\n",buf);
if(strcmp(buf,"quit") == 0){
break;
}
}
pthread_cancel(tid_A);
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
//清空umask
umask(0);
//创建A有名管道文件
if(mkfifo("myfifo_A",0664) < 0){
printf("errno = %d\n",errno);
if(errno != EEXIST){
perror("mkfifo");
return -1;
}
}
printf("create FIFO_A success\n");
//创建有名管道文件
if(mkfifo("myfifo_B",0664) < 0){
printf("errno = %d\n",errno);
if(errno != EEXIST){
perror("mkfifo");
return -1;
}
}
printf("create FIFO_B success\n");
//打开有名管道文件
int fd_A=open("myfifo_A",O_WRONLY);
if(fd_A < 0){
perror("open");
return -1;
}
printf("open FIFO_A success\n");
//打开有名管道文件
int fd_B=open("myfifo_B",O_RDONLY);
if(fd_B < 0){
perror("open");
return -1;
}
printf("open FIFO_B success\n");
//创建线程
if(pthread_create(&tid_A,NULL,call_back_A,&fd_A) < 0){
fprintf(stderr,"pthread_create failed\n");
return -1;
}
if(pthread_create(&tid_B,NULL,call_back_B,&fd_B) < 0){
fprintf(stderr,"pthread_create failed\n");
return -1;
}
pthread_join(tid_A,NULL);
pthread_join(tid_B,NULL);
close(fd_A);
close(fd_B);
return 0;
}
B进程代码:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <pthread.h>
#include <semaphore.h>
//创建数组
char buf[128] = "";
//创建变量接读写的返回值
ssize_t res = 0;
pthread_t tid_A,tid_B;
void* call_back_A(void* arga){
while(1){
//A读
int fd_A = (*(int*)arga);
bzero(buf,sizeof(buf));
res = read(fd_A, buf ,sizeof(buf));
if(res < 0){
perror("read");
return NULL;
}else if(0 == res){
fprintf(stderr,"对方进程已退出\n");
break;
}
printf("A说:%s\n",buf);
if(strcmp(buf,"quit")==0){
break;
}
}
pthread_cancel(tid_B);
pthread_exit(NULL);
}
void* call_back_B(void* argb){
while(1){
//B写
int fd_B = (*(int*)argb);
bzero(buf,sizeof(buf));
//printf("B写:");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = '\0';
res = write(fd_B,buf,sizeof(buf));
if(res < 0){
perror("write");
return NULL;
}
if(strcmp(buf,"quit")==0){
break;
}
}
pthread_cancel(tid_A);
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
//清空umask
umask(0);
//创建有名管道文件
if(mkfifo("myfifo_A",0664) < 0){
printf("errno = %d\n",errno);
if(errno != EEXIST){
perror("mkfifo");
return -1;
}
}
printf("create FIFO_A success\n");
//创建有名管道文件
if(mkfifo("myfifo_B",0664) < 0){
printf("errno = %d\n",errno);
if(errno != EEXIST){
perror("mkfifo");
return -1;
}
}
printf("create FIFO_B success\n");
//打开有名管道文件
int fd_A=open("myfifo_A",O_RDONLY);
if(fd_A < 0){
perror("open");
return -1;
}
printf("open FIFO_A success\n");
//打开有名管道文件
int fd_B=open("myfifo_B",O_WRONLY);
if(fd_B < 0){
perror("open");
return -1;
}
printf("open FIFO_B success\n");
//创建线程
if(pthread_create(&tid_A,NULL,call_back_A,&fd_A) < 0){
fprintf(stderr,"pthread_create failed\n");
return -1;
}
if(pthread_create(&tid_B,NULL,call_back_B,&fd_B) < 0){
fprintf(stderr,"pthread_create failed\n");
return -1;
}
pthread_join(tid_B,NULL)
pthread_join(tid_A,NULL)
close(fd_A);
close(fd_B);
return 0;
}
运行结果: