1> 使用有名管道实现,一个进程用于给另一个进程发消息,另一个进程收到消息后,展示到终端上,并且将消息保存到文件上一份
#include <myhead.h>
int main(int argc, char const *argv[])
{
//创建一个有名管道文件
if(mkfifo("./linux", 0664) == -1)
{
perror("mkfifo error");
return -1;
}
getchar();
system("rm linux");
return 0;
}
#include <myhead.h>
int main(int argc, char const *argv[])
{
//以写的形式打开管道文件
int wfd = open("./linux", O_WRONLY);
if(wfd == -1)
{
perror("open error");
return -1;
}
printf("管道文件写端打开\n");
//发送数据
char wbuf[128] = "";
while(1)
{
printf("请输入>>>");
fgets(wbuf, sizeof(wbuf), stdin);
wbuf[strlen(wbuf)-1] = '\0';
//将数据写入管道文件
write(wfd, wbuf, strlen(wbuf));
if(strcmp(wbuf, "quit") == 0)
{
break;
}
}
close(wfd);
return 0;
}
#include <myhead.h>
int main(int argc, char const *argv[])
{
//以读的形式打开管道文件
int rfd = open("./linux", O_RDONLY);
int fd = open("./file.txt", O_WRONLY|O_CREAT|O_TRUNC, 0664);
if(rfd == -1)
{
perror("open error");
return -1;
}
if(fd == -1)
{
perror("open error");
return -1;
}
printf("管道文件读端打开\n");
//定义接受容器
char rbuf[128] = "";
while(1)
{
bzero(rbuf, sizeof(rbuf));
//读取数据
read(rfd, rbuf, sizeof(rbuf));
if(strcmp(rbuf, "quit") == 0)
{
break;
}
printf("收到消息为:%s\n", rbuf);
//写入文件
write(fd, rbuf, strlen(rbuf));
write(fd, "\n", 1);
}
//关闭文件描述符
close(rfd);
close(fd);
return 0;
}
2> 使用有名管道实现两个进程间相互通信
#include <myhead.h>
int main(int argc, char const *argv[])
{
//创建有名管道文件
if(mkfifo("./chatAtoB", 0664) == -1)
{
perror("mkfifo error");
return -1;
}
if(mkfifo("./chatBtoA", 0664) == -1)
{
perror("mkfifo error");
return -1;
}
getchar();
system("rm chatAtoB");
system("rm chatBtoA");
return 0;
}
#include <myhead.h>
int main(int argc, char const *argv[])
{
//以只写的形式打开AtoB管道文件
int wfd_AtoB = open("./chatAtoB", O_WRONLY);
if(wfd_AtoB == -1)
{
perror("open error");
return -1;
}
printf("AtoB写端打开\n");
//以只读的形式打开BtoA管道文件
int rfd_BtoA = open("./chatBtoA", O_RDONLY);
if(rfd_BtoA == -1)
{
perror("open error");
return -1;
}
printf("BtoA读端打开\n");
//创建进程
pid_t pid = fork();
if(pid < 0)
{
perror("fork error");
return -1;
}
else if(pid == 0) //子进程,完成AtoB写入
{
char wbuf[128] = "";
while(1)
{
printf("请输入>>>");
fgets(wbuf, sizeof(wbuf), stdin);
wbuf[strlen(wbuf)-1] = '\0';
write(wfd_AtoB, wbuf, strlen(wbuf));
if(strcmp(wbuf, "quit") == 0)
{
break;
}
}
close(wfd_AtoB);
//退出进程
exit(EXIT_SUCCESS);
}
//父进程,完成BtoA读取
char rbuf[128] = "";
while(1)
{
bzero(rbuf, sizeof(rbuf));
read(rfd_BtoA, rbuf, sizeof(rbuf));
if(strcmp(rbuf, "quit") == 0)
{
break;
}
printf("B:%s\n", rbuf);
}
close(rfd_BtoA);
wait(NULL);
return 0;
}
#include <myhead.h>
int main(int argc, char const *argv[])
{
//以只读的形式打开AtoB管道文件
int rfd_AtoB = open("./chatAtoB", O_RDONLY);
if(rfd_AtoB == -1)
{
perror("open error");
return -1;
}
printf("AtoB读端打开\n");
//以只写的形式打开BtoA管道文件
int wfd_BtoA = open("./chatBtoA", O_WRONLY);
if(wfd_BtoA == 1)
{
perror("open error");
return -1;
}
printf("BtoA写端打开\n");
//创建进程
pid_t pid = fork();
if(pid < 0)
{
perror("fork error");
return -1;
}
else if(pid == 0) //子进程,完成AtoB读取
{
char rbuf[128] = "";
while(1)
{
bzero(rbuf, sizeof(rbuf));
read(rfd_AtoB, rbuf, sizeof(rbuf));
if(strcmp(rbuf, "quit") == 0)
{
break;
}
printf("A:%s\n", rbuf);
}
close(rfd_AtoB);
//退出进程
exit(EXIT_SUCCESS);
}
//父进程,完成BtoA写入
char wbuf[128] = "";
while(1)
{
printf("请输入>>>");
fgets(wbuf, sizeof(wbuf), stdin);
wbuf[strlen(wbuf)-1] = '\0';
write(wfd_BtoA, wbuf, strlen(wbuf));
if(strcmp(wbuf, "quit") == 0)
{
break;
}
}
close(wfd_BtoA);
wait(NULL);
return 0;
}
思维导图: