1)要求AB进程做通信
A进程发送一句话,B进程接收打印
然后B进程发送给A进程一句话,A进程接收打印
重复1,2步骤,直到A进程或者B进程收到quit,退出AB进程;
2)在第一题的基础上实现,AB进程能够随时收发数据(附加题)
第一题:
A进程:
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
int main(int argc, const char *argv[])
{
if(mkfifo("./fifo",0777) < 0)
{
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
if(mkfifo("./fifo1",0777) < 0)
{
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
//以只写的方式打开
int fd = open("./fifo",O_WRONLY);
if(fd < 0)
{
perror("open");
return -1;
}
int fd1 = open("./fifo1",O_RDONLY);
if(fd < 0)
{
perror("open");
return -1;
}
char str[128];
ssize_t res = 0;
while(1)
{
bzero(str, sizeof(str));
printf("A进程说>>>");
//从标准输入读取数据,其实就是从终端获取数据
fgets(str, sizeof(str), stdin);
str[strlen(str)-1] = '\0'; //将获取到的字符串最后一个字节的\n修改成\0
//向管道中写入数据
if(write(fd, str, sizeof(str)) < 0)
{
perror("write");
return -1;
}
//忽略大小写的比较
if(strcasecmp(str, "quit") == 0)
break;
//从管道中读取数据
bzero(str, sizeof(str));
//当管道中没有数据的时候,read函数阻塞
res = read(fd1, str, sizeof(str));
if(res < 0)
{
perror("read");
return -1;
}
else if(0 == res)
{
printf("写端关闭\n");
break;
}
printf("A进程接收到B进程说:%s\n", str);
if(strcasecmp(str, "quit") == 0)
break;
}
//阻塞等待子进程退出
wait(NULL);
close(fd);
close(fd1);
return 0;
}
B进程:
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
int main(int argc, const char *argv[])
{
if(mkfifo("./fifo",0777) < 0)
{
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
if(mkfifo("./fifo1",0777) < 0)
{
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
//以只写的方式打开
int fd = open("./fifo",O_RDONLY);
if(fd < 0)
{
perror("open");
return -1;
}
int fd1 = open("./fifo1",O_WRONLY);
if(fd < 0)
{
perror("open");
return -1;
}
char str[128];
ssize_t res = 0;
while(1)
{
//从管道中读取数据
bzero(str, sizeof(str));
//当管道中没有数据的时候,read函数阻塞
res = read(fd, str, sizeof(str));
if(res < 0)
{
perror("read");
return -1;
}
else if(0 == res)
{
printf("写端关闭\n");
break;
}
printf("B进程接收到A进程说:%s\n", str);
if(strcasecmp(str, "quit") == 0)
break;
bzero(str, sizeof(str));
printf("B进程说>>>");
//从标准输入读取数据,其实就是从终端获取数据
fgets(str, sizeof(str), stdin);
str[strlen(str)-1] = '\0'; //将获取到的字符串最后一个字节的\n修改成\0
//向管道中写入数据
if(write(fd1, str, sizeof(str)) < 0)
{
perror("write");
return -1;
}
//忽略大小写的比较
if(strcasecmp(str, "quit") == 0)
break;
}
//阻塞等待子进程退出
wait(NULL);
close(fd);
close(fd1);
return 0;
}
第二题(实现QQ类型):
A进程:
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
int main(int argc, const char *argv[])
{
if(mkfifo("./fifo",0777) < 0)
{
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
if(mkfifo("./fifo1",0777) < 0)
{
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
int pid;
int pidw;
int pidr;
ssize_t res = 0;
char arr[128] = {0};
char brr[128] = {0};
pid = fork();
if(pid > 0)
{
pidw = open("./fifo1",O_WRONLY);
if(pidw < 0)
{
perror("open");
return -1;
}
while(1)
{
bzero(arr, sizeof(arr));
scanf("%s",arr);
write(pidw,arr,sizeof(arr));
if(strcmp(arr,"quit") == 0)
{
break;
}
}
close(pidw);
wait(NULL);
}
if(pid == 0)
{
pidr = open("./fifo",O_RDONLY);
if(pidr < 0)
{
perror("open");
return -1;
}
while(1)
{
bzero(brr,sizeof(brr));
res = read(pidr,brr,sizeof(brr));
if(res < 0)
{
perror("read");
return -1;
}
else if(0 == res)
{
printf("写端关闭\n");
break;
}
printf("A进程接收到B进程说:%s\n", brr);
if(strcmp(brr,"quit") == 0)
{
break;
}
}
close(pidr);
}
return 0;
}
B进程:
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
int main(int argc, const char *argv[])
{
if(mkfifo("./fifo",0777) < 0)
{
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
if(mkfifo("./fifo1",0777) < 0)
{
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
int pid;
int pidw;
int pidr;
ssize_t res = 0;
char arr[128] = {0};
char brr[128] = {0};
pid = fork();
if(pid == 0)
{
pidw = open("./fifo",O_WRONLY);
if(pidr < 0)
{
perror("open");
return -1;
}
while(1)
{
bzero(arr, sizeof(arr));
scanf("%s",arr);
write(pidw,arr,sizeof(arr));
if(strcmp(arr,"quit") == 0)
{
break;
}
}
close(pidw);
}
if(pid > 0)
{
pidr = open("./fifo1",O_RDONLY);
if(pidr < 0)
{
perror("open");
return -1;
}
while(1)
{
bzero(brr,sizeof(brr));
res = read(pidr,brr,sizeof(brr));
if(res < 0)
{
perror("read");
return -1;
}
else if(0 == res)
{
printf("写端关闭\n");
break;
}
printf("B进程接收到A进程说:%s\n", brr);
if(strcmp(brr,"quit") == 0)
{
break;
}
}
close(pidr);
wait(NULL);
}
return 0;
}