作业
题目一
- 要求实现AB进程对话
- A进程先发送一句话给B进程,B进程接收后打印
- B进程再回复一句话给A进程,A进程接收后打印
- 重复1.2步骤,当收到quit后,要结束AB进程
- 提示:两根管道
A进程
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<head.h>
int main(int argc, const char *argv[])
{
//创建有名通道1
if((mkfifo("./myfifo",0666) < 0) && errno != 17)
{
perror("mkfifo");
return -1;
}
printf("mkfifo success\n");
//打开有名通道1
int fd1 = open("./myfifo",O_RDWR);
if(fd1 < 0)
{
perror("open");
return -1;
}
printf("open success fd = %d\n",fd1);
//创建有名通道2
if((mkfifo("./myfifo2",0666) < 0) && errno != 17)
{
perror("mkfifo");
return -1;
}
printf("mkfifo success\n");
//打开有名通道2
int fd2 = open("./myfifo2",O_RDWR);
if(fd2 < 0)
{
perror("open");
return -1;
}
printf("open success fd2 = %d\n",fd2);
char buf[128] = "";
while(1)
{
//通道1中A传给B
printf("请输入\n");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = '\0';
if(write(fd1,buf,sizeof(buf))<0)
{
perror("write");
return -1;
}
//输入quit退出程序
int re = strcmp(buf,"quit");
if(re == 0)
{
printf("A进程退出\n");
return -1;
}
printf("写入成功\n");
bzero(buf,sizeof(buf));
//通道2中B传给A
if(read(fd2,buf,sizeof(buf))<0)
{
perror("read");
return -1;
}
//接收到B的quit一起退出程序
re = strcmp(buf,"quit");
if(re == 0)
{
printf("A进程退出\n");
return -1;
}
printf("读取成功\n");
printf("%s\n",buf);
}
return 0;
}
B进程(代码思路和A程序相差不大)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<head.h>
int main(int argc, const char *argv[])
{
if((mkfifo("./myfifo",0666) < 0) && errno != 17)
{
perror("mkfifo");
return -1;
}
printf("mkfifo success\n");
int fd1 = open("./myfifo",O_RDWR);
if(fd1 < 0)
{
perror("open");
return -1;
}
printf("open success fd1 = %d\n",fd1);
if((mkfifo("./myfifo2",0666) < 0) && errno != 17)
{
perror("mkfifo");
return -1;
}
printf("mkfifo success\n");
int fd2 = open("./myfifo2",O_RDWR);
if(fd2 < 0)
{
perror("open");
return -1;
}
printf("open success fd2 = %d\n",fd2);
char buf[128] = "";
while(1)
{
bzero(buf,sizeof(buf));
if(read(fd1,buf,sizeof(buf))<0)
{
perror("read");
return -1;
}
int re = strcmp(buf,"quit");
if(re == 0)
{
printf("B进程退出\n");
return -1;
}
printf("读取成功\n");
printf("%s\n",buf);
printf("请输入\n");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = '\0';
if(write(fd2,buf,sizeof(buf))<0)
{
perror("write");
return -1;
}
re = strcmp(buf,"quit");
if(re == 0)
{
printf("B进程退出\n");
return -1;
}
printf("写入成功\n");
}
return 0;
}
题目二
在第1题的基础上实现,A能随时发信息给B,B能随时接收A发送的数据
A进程
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<head.h>
//子线程用来读取通道2
void *RD(void* arg)
{
int *fd2 = (int *)arg;
char buf[128] = "";
//读取通道2中B传来的信息
while(1)
{
bzero(buf,sizeof(buf));
if(read(*fd2,buf,sizeof(buf))<0)
{
perror("read");
break;
}
int re = strcmp(buf,"quit");
//接收到B传来的quit退出程序
if(re == 0)
{
printf("A进程退出\n");
exit(1);
}
printf("读取成功\n");
printf("%s\n",buf);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
//创建通道1并打开
if((mkfifo("./myfifo",0666) < 0) && errno != 17)
{
perror("mkfifo");
return -1;
}
printf("mkfifo success\n");
int fd1 = open("./myfifo",O_RDWR);
if(fd1 < 0)
{
perror("open");
return -1;
}
printf("open success fd = %d\n",fd1);
//创建通道2并打开
if((mkfifo("./myfifo2",0666) < 0) && errno != 17)
{
perror("mkfifo");
return -1;
}
printf("mkfifo success\n");
int fd2 = open("./myfifo2",O_RDWR);
if(fd2 < 0)
{
perror("open");
return -1;
}
printf("open success fd2 = %d\n",fd2);
//创建子线程,并传通道2进线程
pthread_t tid;
if(pthread_create(&tid,NULL,RD,(void*)&fd1)<0)
{
perror("create");
return -1;
}
char buf[128] = "";
//用通道1实现A传给B
while(1)
{
printf("请输入\n");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = '\0';
if(write(fd2,buf,sizeof(buf))<0)
{
perror("write");
break;
}
int re = strcmp(buf,"quit");
//写quit退出程序
if(re == 0)
{
printf("A进程退出\n");
exit(1);
}
printf("写入成功\n");
}
pthread_join(tid,NULL);
return 0;
}
B进程(代码思路和A相差不大)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<head.h>
void *RD(void* arg)
{
int *fd2 = (int *)arg;
char buf[128] = "";
while(1)
{
bzero(buf,sizeof(buf));
if(read(*fd2,buf,sizeof(buf))<0)
{
perror("read");
break;
}
int re = strcmp(buf,"quit");
if(re == 0)
{
printf("B进程退出\n");
exit(1);
}
printf("读取成功\n");
printf("%s\n",buf);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
if((mkfifo("./myfifo",0666) < 0) && errno != 17)
{
perror("mkfifo");
return -1;
}
printf("mkfifo success\n");
int fd1 = open("./myfifo",O_RDWR);
if(fd1 < 0)
{
perror("open");
return -1;
}
printf("open success fd1 = %d\n",fd1);
if((mkfifo("./myfifo2",0666) < 0) && errno != 17)
{
perror("mkfifo");
return -1;
}
printf("mkfifo success\n");
int fd2 = open("./myfifo2",O_RDWR);
if(fd2 < 0)
{
perror("open");
return -1;
}
pthread_t tid;
if(pthread_create(&tid,NULL,RD,(void*)&fd2)<0)
{
perror("create");
return -1;
}
char buf[128] = "";
while(1)
{
printf("请输入\n");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = '\0';
if(write(fd1,buf,sizeof(buf))<0)
{
perror("write");
break;
}
int re = strcmp(buf,"quit");
if(re == 0)
{
printf("B进程退出\n");
exit(1);
}
printf("写入成功\n");
}
pthread_join(tid,NULL);
printf("open success fd2 = %d\n",fd2);
return 0;
}