尧图网站设计 尧图网站设计YAOTU DESIGN
ARTICLE DETAIL

资讯详情

深耕网站设计与一线实操的经验洞察。

OS50.【Linux】进程间通信 命名(有名)管道 (2)

OS50.【Linux】进程间通信 命名(有名)管道 (2) 目录1.知识回顾2.open的阻塞3.优化4.管道创始人Malcolm Douglas McIlroy的管道原稿5.复习Linux进程中一些细碎的概念1.知识回顾参见OS49.【Linux】进程间通信 命名(有名)管道(1)文章2.open的阻塞对OS49.【Linux】进程间通信 命名(有名)管道(1)文章的read.cpp和write.cpp稍作修改: 调用open成功后分别打印read.out: 成功打开命名管道文件和write.out: 成功打开命名管道文件运行结果:可以发现read.out执行后并没有立刻打印read.out: 成功打开命名管道文件,而是等write.out执行后才打印原因: 这是合情合理的,read.out执行但write.out没有执行,这说明写端没有打开,是没有必要打开管道的读端的(open阻塞),显然管道内肯定没有数据,打开管道没有意义,反过来也一样: 读端没有打开,是没有必要打开管道的读写端的(open阻塞),显然管道内肯定没有数据,打开管道没有意义3.优化现优化OS49.【Linux】进程间通信 命名(有名)管道(1)文章的代码:将OS49.【Linux】进程间通信 命名(有名)管道(1)文章命名管道的创建和删除写到一个类中:将命名管道的创建写到构造函数中,命名管道的删除写到析构函数中:class named_pipe { public: named_pipe() { if (mkfifo(PIPE_FILE_PATH,MODE)) { perror(mkfifo failed); exit(FIFO_CREATE_ERR); } } ~named_pipe() { if (unlink(PIPE_FILE_PATH)) { perror(unlink failed); exit(FIFO_DELETE_ERR); } } };那么main函数中这样使用就行:不需要手动调用构造和析构函数int main() { named_pipe p; int fdopen(PIPE_FILE_PATH,O_RDONLY); if (fd0) { perror(open failed); exit(FIFO_OPEN_ERR); } std::coutread.out: 成功打开命名管道文件std::endl; for(;;) { char buffer[ONE_KB]; ssize_t nread(fd,buffer,ONE_KB); if (n0) { //接收到的消息当成C语言的字符串 buffer[n]\0; std::coutread.out接收到字符串bufferstd::endl; } else if (n0) { perror(read failed); exit(FIFO_READ_ERR); } else //n0 { std::cout写端关闭,停止读取std::endl; break; } } close(fd); std::cout读端关闭,read.out退出std::endl; return 0; }运行结果:4.管道创始人Malcolm Douglas McIlroy的管道原稿来自: https://doc.cat-v.org/unix/pipes/OCR识别结果:Summary--whats most important.To put my strongest concerns into a nutshell:1. We should have some ways of coupling programs likegarden hose--screw in another segment when it becomes whenit becomes necessary to massage data in another way.This is the way of IO also.2. Our loader should be able to do link-loading andcontrolled establishment.3. Our library filing scheme should allow for rathergeneral indexing, responsibility, generations, data pathswitching.4. It should be possible to get private system components(all routines are system components) for buggering around with.M. D. McIlroyOctober 11, 1964这里附上陈皓大佬的解读链接: https://coolshell.cn/articles/1351.html有兴趣的读者可以看看tmpout杂志社对Doug McIlroy的访谈: Interview: Doug McIlroy ~ tmp.0ut Staff5.复习Linux进程中一些细碎的概念豆瓣书友马牛的Understanding The Linux Kernel的书评对Linux进程的理解非常形象,可以看看https://book.douban.com/review/2921983/
返回列表