简单版:
要求:进程A发送一句,进程B接收一句。然后进程B发送一句,进程A接收一句。
主要步骤:
需要创建两个有名管道文件,分别为fifo1和fifo2。
进程A向fifo1中写入,进程B从fifo1中读。
进程B向fifo2中写入,进程A从fifo2中读。
A进程代码:chatA.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
int main() {
// 1.判断有名管道文件是否存在
int ret = access("fifo1", F_OK);
if(ret == -1) {
// 文件不存在
printf("管道不存在,创建fifo1有名管道\n");
ret = mkfifo("fifo1", 0664);
if(ret == -1) {
perror("mkfifo");
exit(0);
}
}
ret = access("fifo2", F_OK);
if(ret == -1) {
// 文件不存在
printf("管道不存在,创建fifo2有名管道\n");
ret = mkfifo("fifo2", 0664);
if(ret == -1) {
perror("mkfifo");
exit(0);
}
}
// 2.以只写的方式打开管道fifo1
int fdw = open("fifo1", O_WRONLY);
if(fdw == -1) {
perror("open");
exit(0);
}
printf("打开管道fifo1成功,等待写入...\n");
// 3.以只读的方式打开管道fifo2
int fdr = open("fifo2", O_RDONLY);
if(fdr == -1) {
perror("open");
exit(0);
}
printf("打开管道fifo2成功,等待读取...\n");
char buf[128];
// 4.循环的写读数据
while(1) {
memset(buf, 0, 128); //清空一下
// 获取标准输入的数据
fgets(buf, 128, stdin);
// 写数据
ret = write(fdw, buf, strlen(buf));
if(ret == -1) {
perror("write");
exit(0);
}
// 5.读管道数据
memset(buf, 0, 128);
ret = read(fdr, buf, 128);
if(ret <= 0) {
perror("read");
break;
}
printf("A receive: %s\n", buf);
}
// 6.关闭文件描述符
close(fdr);
close(fdw);
return 0;
}
B进程代码:chatB.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
int main() {
// 1.判断有名管道文件是否存在
int ret = access("fifo1", F_OK);
if(ret == -1) {
// 文件不存在
printf("管道不存在,创建fifo1有名管道\n");
ret = mkfifo("fifo1", 0664);
if(ret == -1) {
perror("mkfifo");
exit(0);
}
}
ret = access("fifo2", F_OK);
if(ret == -1) {
// 文件不存在
printf("管道不存在,创建fifo2有名管道\n");
ret = mkfifo("fifo2", 0664);
if(ret == -1) {
perror("mkfifo");
exit(0);
}
}
// 2.以只读的方式打开管道fifo1
int fdr = open("fifo1", O_RDONLY);
if(fdr == -1) {
perror("open");
exit(0);
}
printf("打开管道fifo1成功,等待读取...\n");
// 3.以只写的方式打开管道fifo2
int fdw = open("fifo2", O_WRONLY);
if(fdw == -1) {
perror("open");
exit(0);
}
printf("打开管道fifo2成功,等待写入...\n");
char buf[128];
// 4.循环的读写数据
while(1) {
// 5.读管道数据
memset(buf, 0, 128);
ret = read(fdr, buf, 128);
if(ret <= 0) {
perror("read");
break;
}
printf("B receive: %s\n", buf);
memset(buf, 0, 128);
// 获取标准输入的数据
fgets(buf, 128, stdin);
// 写数据
ret = write(fdw, buf, strlen(buf));
if(ret == -1) {
perror("write");
exit(0);
}
}
// 6.关闭文件描述符
close(fdr);
close(fdw);
return 0;
}
执行chatA.c
执行chatB.c
开始写入数据,注意,这个程序只能一收一发,比如B在发送 i am b之后再发送数据A是收不到的。原因是,B是在不停地读和写,那么B写完之后,下一次是该读了,但是A并没有发送过来数据,所以阻塞在了B的read函数这里。hahaha这些数据其实是存在于标准输入的缓冲区中。
A回了消息之后,B的hahaha发送出去了!!.进阶版:
要求:A可以不断的发送,B可以不断的收到。同理B可以不断发送,A可以不断接收。
思路:在chatA和chatB中,读和写管道不放在同一个进程中。因为放在同一个进程中,必定有一个是阻塞的,也就是说你不能在写的同时读数据,也不能在读数据的同时写数据。因此创建一个子进程,父子进行分别实现读和写。
改进版hchatA.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
int main() {
// 1.判断有名管道文件是否存在
int ret = access("fifo1", F_OK);
if(ret == -1) {
// 文件不存在
printf("管道不存在,创建fifo1有名管道\n");
ret = mkfifo("fifo1", 0664);
if(ret == -1) {
perror("mkfifo");
exit(0);
}
}
ret = access("fifo2", F_OK);
if(ret == -1) {
// 文件不存在
printf("管道不存在,创建fifo2有名管道\n");
ret = mkfifo("fifo2", 0664);
if(ret == -1) {
perror("mkfifo");
exit(0);
}
}
// 2.以只写的方式打开管道fifo1
int fdw = open("fifo1", O_WRONLY);
if(fdw == -1) {
perror("open");
exit(0);
}
printf("打开管道fifo1成功,等待写入...\n");
// 3.以只读的方式打开管道fifo2
int fdr = open("fifo2", O_RDONLY);
if(fdr == -1) {
perror("open");
exit(0);
}
printf("打开管道fifo2成功,等待读取...\n");
pid_t pid = fork(); //创建子进程
//父进程循环地写
if (pid > 0)
{
char bufw[128];
while(1)
{
memset(bufw, 0, 128); //清空一下
// 获取标准输入的数据
fgets(bufw, 128, stdin);
// 写数据
int ret2 = write(fdw, bufw, strlen(bufw));
if(ret2 == -1)
{
perror("write");
exit(0);
}
}
close(fdw);
}
else if (pid == 0) //子进程循环地读
{
char bufr[128];
while(1)
{
memset(bufr, 0, 128);
int ret3 = read(fdr, bufr, 128);
if(ret3 <= 0)
{
perror("read");
break;
}
printf("A receive: %s\n", bufr);
}
close(fdr);
}
return 0;
}
改进版hchatB.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
int main() {
// 1.判断有名管道文件是否存在
int ret = access("fifo1", F_OK);
if(ret == -1) {
// 文件不存在
printf("管道不存在,创建fifo1有名管道\n");
ret = mkfifo("fifo1", 0664);
if(ret == -1) {
perror("mkfifo");
exit(0);
}
}
ret = access("fifo2", F_OK);
if(ret == -1) {
// 文件不存在
printf("管道不存在,创建fifo2有名管道\n");
ret = mkfifo("fifo2", 0664);
if(ret == -1) {
perror("mkfifo");
exit(0);
}
}
// 2.以只读的方式打开管道fifo1
int fdr = open("fifo1", O_RDONLY);
if(fdr == -1) {
perror("open");
exit(0);
}
printf("打开管道fifo1成功,等待读取...\n");
// 3.以只写的方式打开管道fifo2
int fdw = open("fifo2", O_WRONLY);
if(fdw == -1) {
perror("open");
exit(0);
}
printf("打开管道fifo2成功,等待写入...\n");
char buf[128];
pid_t pid = fork();
if (pid > 0) //父进程循环地读取数据
{
char bufw[128];
while(1)
{
memset(bufw, 0, 128);
int ret2 = read(fdr, bufw, 128);
if(ret2 <= 0)
{
perror("read");
break;
}
printf("B receive: %s\n", bufw);
}
close(fdw);
}
else if (pid == 0) //子进程循环地写数据
{
char bufr[128];
while(1)
{
memset(bufr, 0, 128);
// 获取标准输入的数据
fgets(bufr, 128, stdin);
// 写数据
int ret3 = write(fdw, bufr, strlen(bufr));
if(ret3 == -1)
{
perror("write");
exit(0);
}
}
close(fdr);
}
return 0;
}
运行结果:
这样的话,就可以实现收发多次了。