题目
1.用有名管道实现两个进程间的通信
create.c
#include <myhead.h>
int main()
{
//创建一个有名管道文件
if (mkfifo("./linux", 0664) == -1)
{
perror("mkfifo error");
return -1;
}
getchar();
system("rm linux");
return 0;
}
create1.c
#include <myhead.h>
int main()
{
//创建第二个有名管道文件
if (mkfifo("./linux1", 0664) == -1)
{
perror("mkfifo error");
return -1;
}
getchar();
system("rm linux1");
return 0;
}
send.c
#include <myhead.h>
int main()
{
//创建两个进程
pid_t pid = fork();
//子进程
if (pid == 0)
{
//以写的形式打开管道文件
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);
}
//父进程
else if (pid > 0)
{
//以读的形式打开文件
int afd = open("./linux1", O_RDONLY);
if (afd == -1)
{
perror("open error");
return -1;
}
printf("管道文件读端以打开\n");
//定义接受容器
char abuf[128] = "";
while (1)
{
bzero(abuf, sizeof(abuf));
read(afd, abuf, sizeof(abuf));
if (strcmp(abuf, "quit") == 0)
{
break;
}
printf("\n");
printf("收到的消息为:%s\n", abuf);
}
close(afd);
}
return 0;
}
recv.c
#include <myhead.h>
int main()
{
//创建两个进程
pid_t pid = fork();
//父进程
if (pid > 0)
{
//以读的形式打开文件
int rfd = open("./linux", O_RDONLY);
if (rfd == -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("\n");
printf("收到的消息为:%s\n", rbuf);
}
close(rfd);
}
//子进程
else if (pid == 0)
{
//以写的形式打开管道文件
int bfd = open("./linux1", O_WRONLY);
if (bfd == -1)
{
perror("open error");
return -1;
}
printf("管道文件已经打开\n");
//发送数据
char bbuf[128] = "";
while (1)
{
printf("请输入>>>");
fgets(bbuf, sizeof(bbuf), stdin);
bbuf[strlen(bbuf) - 1] = 0;
//将数据发送到管道中
write(bfd, bbuf, strlen(bbuf));
//判断数据
if (strcmp(bbuf, "quit") == 0)
{
break;
}
}
//关闭文件描述符
close(bfd);
}
return 0;
}
2.使用有名管道实现,一个进程用于给另一个进程发消息,另一个进程收到消息后,展示到终端上,并且将消息保存到文件上一份
create.c
#include <myhead.h>
int main()
{
//创建一个有名管道文件
if (mkfifo("./linux", 0664) == -1)
{
perror("mkfifo error");
return -1;
}
getchar();
system("rm linux");
return 0;
}
send.c
#include <myhead.h>
int main()
{
//以写的形式打开管道文件
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)] = 0;
//将数据发送到管道中
write(wfd, wbuf, strlen(wbuf));
//判断数据
if (strcmp(wbuf, "quit") == 0)
{
break;
}
}
//关闭文件描述符
close(wfd);
return 0;
}
rev.c
#include <myhead.h>
int main()
{
//以只读的形式打开管道文件
int rfd = open("./linux", O_RDONLY);
if (rfd == -1)
{
perror("open error");
return -1;
}
printf("管道文件读端已打开\n");
//以只写的形式打开要将数据存入的文件
int yfd = open("./test.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (yfd == -1)
{
perror("open 1 error");
return -1;
}
//定义接受容器
char rbuf[128] = "";
ssize_t bytesRead;
int count = 0;
while (count != 5)
{
bytesRead = read(rfd, rbuf, sizeof(rbuf));
rbuf[bytesRead] = '\0'; // 确保字符串正确终止
if (strcmp(rbuf, "quit") == 0)
{
break;
}
write(yfd, rbuf, bytesRead);
printf("收到的消息为%s\n", rbuf);
count++;
}
printf("数据写入成功\n");
close(rfd);
close(yfd);
return 0;
}