网站扁平化布局,长春商城网站建设,代码,虚拟机怎么做多个网站目录 有名管道有名管道使用有名管道的注意事项读写特性有名管道实现简单版聊天功能拓展#xff1a;如何解决聊天过程的阻塞 有名管道
可以用在没有关系的进程之间#xff0c;进行通信
有名管道使用 通过命令创建有名管道 mkfifo 名字 通过函数创建有名管道 int mkfifo
… 目录 有名管道有名管道使用有名管道的注意事项读写特性有名管道实现简单版聊天功能拓展如何解决聊天过程的阻塞 有名管道
可以用在没有关系的进程之间进行通信
有名管道使用 通过命令创建有名管道 mkfifo 名字 通过函数创建有名管道 int mkfifo
#include stdio.h
#include sys/types.h
#include sys/stat.h
#include string.h
#include stdlib.hint main(){int retmkfifo(fifo1,0664);if(ret-1){perror(mkfifo);exit(0);}return 0;
}write.c
#include stdio.h
#include sys/types.h
#include sys/stat.h
#include string.h
#include stdlib.h
#include unistd.h
#include fcntl.h//向管道中写数据
int main(){//1.判断文件是否存在int retaccess(test,F_OK);if(ret-1){printf(管道不存在创建管道\n);//2.创建管道文件retmkfifo(test,0664);if(ret-1){perror(mkfifo);exit(0);} }//3.打开管道int fdopen(test,O_WRONLY);if(fd-1){perror(open);exit(0);}//写数据for(int i0;i100;i){char buf[1024];sprintf(buf,hello,%d\n,i);printf(write data:%s\n,buf);write(fd,buf,strlen(buf));sleep(1);}close(fd);return 0;
}读端read.c
#include stdio.h
#include sys/types.h
#include sys/stat.h
#include string.h
#include stdlib.h
#include unistd.h
#include fcntl.h//向管道中写数据
int main(){//1.打开管道文件int fdopen(test,O_RDONLY);if(fd-1){perror(open);exit(0);}//读数据while(1){char buf[1024]{0};int lenread(fd,buf,sizeof(buf));if(len0){printf(写端断开连接了..\n);break;}printf(recv buf:%s\n,buf);}close(fd);return 0;
}读端和写端一起打开才能写出数据
写端暂停之后也读取不到了就退出程序了
如果先关闭读端写端也立马终止程序了 读端关闭了还在写数据会产生信号因为没有读端了还写数据管道会破裂所以产生信号立马终止。
有名管道的注意事项
1.一个为只读而打开一个管道的进程会阻塞直到另外一个进程为只写打开管道 2.一个为只写而打开一个管道的进程会阻塞直到另一个进程为只读打开管道与上一个相对
读写特性
读管道 管道中有数据read返回实际读到的字节数 管道中无数据管道写端被全部关闭read返回0相当于读到文件末尾 写端没有全部被关闭read阻塞等待
写管道 管道读端被全部关闭进程会异常终止进程会收到sigpipe信号。 管道读端没有被全部关闭管道已经满了write会阻塞管道未满,write将数据写入并返回实际写入的字节数
有名管道实现简单版聊天功能
如何实现互发呢 创建两个管道一个从A到B一个从B到A chatA
#include stdio.h
#include sys/types.h
#include sys/stat.h
#include string.h
#include stdlib.h
#include unistd.h
#include fcntl.hint main(){//1.判断有名管道文件是否存在int retaccess(fifo1,F_OK);if(ret-1){//文件不存在printf(管道不存在创建对应的有名管道\n);retmkfifo(fifo1,0664);if(ret-1){perror(mkfifo);exit(0);}}retaccess(fifo2,F_OK);if(ret-1){//文件不存在printf(管道不存在创建对应的有名管道\n);retmkfifo(fifo2,0664);if(ret-1){perror(mkfifo);exit(0);}}//2.以只写的方式打开管道fifo1int fdwopen(fifo1,O_WRONLY);if(fdw-1){perror(open);exit(0);}printf(打开管道fifo1成功,等待写入...\n);//3.以只读的方式打开管道fifo2int fdropen(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);//写数据retwrite(fdw,buf,strlen(buf));if(ret-1){perror(write);exit(0);}//5.读管道数据memset(buf,0,128);retread(fdr,buf,128);if(ret0){perror(read);break;}printf(buf:%s\n,buf);}//6.关闭文件描述符close(fdr);close(fdw);return 0;
}chatB
#include stdio.h
#include sys/types.h
#include sys/stat.h
#include string.h
#include stdlib.h
#include unistd.h
#include fcntl.hint main(){//1.判断有名管道文件是否存在int retaccess(fifo1,F_OK);if(ret-1){//文件不存在printf(管道不存在创建对应的有名管道\n);retmkfifo(fifo1,0664);if(ret-1){perror(mkfifo);exit(0);}}retaccess(fifo2,F_OK);if(ret-1){//文件不存在printf(管道不存在创建对应的有名管道\n);retmkfifo(fifo2,0664);if(ret-1){perror(mkfifo);exit(0);}}//2.以只读的方式打开管道fifo1int fdropen(fifo1,O_RDONLY);if(fdr-1){perror(open);exit(0);}printf(打开管道fifo1成功,等待读取...\n);//3.以只写的方式打开管道fifo2int fdwopen(fifo2,O_WRONLY);if(fdw-1){perror(open);exit(0);}printf(打开管道fifo2成功,等待写入...\n);char buf[128];//4.循环的写读数据while(1){//5.读管道数据memset(buf,0,128);retread(fdr,buf,128);if(ret0){perror(read);break;}printf(buf:%s\n,buf);memset(buf,0,128);//获取标准输入的数据fgets(buf,128,stdin);//写数据retwrite(fdw,buf,strlen(buf));if(ret-1){perror(write);exit(0);}}//6.关闭文件描述符close(fdr);close(fdw);return 0;
}拓展如何解决聊天过程的阻塞
读和写不能放到同一个文件中因为必然会引起阻塞每次写完都需要等待对方上一次读完才能发过去