成都高新区建设厅网站,天津西青网站建设公司,wordpress api定制,用织梦做的网站怎样看专栏内容#xff1a;linux下并发编程个人主页#xff1a;我的主页座右铭#xff1a;天行健#xff0c;君子以自强不息#xff1b;地势坤#xff0c;君子以厚德载物#xff0e;目录
前言
概述
原理介绍
接口说明
代码演示
结尾 前言
本专栏主要分享linux下并发编程… 专栏内容linux下并发编程个人主页我的主页座右铭天行健君子以自强不息地势坤君子以厚德载物目录
前言
概述
原理介绍
接口说明
代码演示
结尾 前言
本专栏主要分享linux下并发编程相关知识包括多进程多线程进程/线程间通信并发同步控制以及高并发下性能提升请大家多多留言。 概述
在不同进程和程序间如何传递少量数据且能阻塞形式使用这就用到了管道。管道分为两种无名管道和命名管道因为子进程可以复制父进程的进程上下文所以无名管道用在父子进程间而命名管道在不同进程间通过路径名进行共享更加通用。 原理介绍 命名管道是一种在进程间进行通信的机制它可以用于在两个进程间进行数据传输。具体来说命名管道是一种特殊类型的文件它可以在不同的进程中被打开和读写从而实现了进程间的通信。一般情况下在Linux系统中命名管道的创建和使用是通过C语言的相关API实现的。 建立管道文件后可以在路径下看到它的文件类型为 p 说明它是管道文件。管道文件不同于一般文件的是写时会阻塞至到数据被读出而在读时无数据的话会阻塞等待直到有数据被写入。 通过系统命令也可以创建命名管道如下 [senllanglocalhost pipetest]$ mkfifo /tmp/myfilo [senllanglocalhost pipetest]$ ll /tmp/ total 0 prw-r--r--. 1 senllang develops 0 Apr 8 15:52 myfilo 可以看到管道文件的类型为 p我们打开两个终端进行读写测试。
终端一写入 [senllanglocalhost pipetest]$ echo hello /tmp/myfifo 因为没有读此时会卡住 终端二读取 [senllanglocalhost ~]$ cat /tmp/myfifo hello 可以看到读出了hello此时终端一也会解除阻塞。同样先读取也会阻塞直到写入数据为至。 接口说明
1管道文件创建
#include sys/types.h#include sys/stat.hint mkfifo(const char *pathname, mode_t mode);
通过传递路径pathname和mode权限来创建管道文件。
#include fcntl.h /* Definition of AT_* constants */#include sys/stat.hint mkfifoat(int dirfd, const char *pathname, mode_t mode);
这是另一个版本当pathname是相对路径时创建路径是在dirfd指定的目录下。当dirfd为 AT_FDCWD时用mkfifo效果一样。 2管道文件打开/读写/关闭
常用的文件操作 open/read/write/close 3删除管道文件
unlink(fd)
在用完管道时需要删除管道文件和删除文件类似。 代码演示
#include stdio.h
#include ctype.h
#include fcntl.h
#include sys/stat.h
#include unistd.hint main()
{int fd;char *myfifo /tmp/myfifo;char input[256];char output[256];/* create named pipe */mkfifo(myfifo, 0666);/* open named pipe for reading */fd open(myfifo, O_RDWR);while(1) {/* read input from named pipe */read(fd, input, 256);/* convert input to uppercase */int i;for (i 0; input[i]; i) {output[i] toupper(input[i]);}/* write output to named pipe */int len i;write(fd, output, len);}/* close named pipe */close(fd);unlink(myfifo);return 0;
}编译后运行管道里的字符串被程序转为大写后又写入管道。 [senllanglocalhost pipetest]$ gcc mkfifo_ex01.c -o mkfifo_ex01 [senllanglocalhost pipetest]$ ./mkfifo_ex01 在另一个终端写入字符串 [senllanglocalhost ~]$ echo hello /tmp/myfifo [senllanglocalhost ~]$ cat /tmp/myfifo HELLO 结尾
作者邮箱studysenllang.onaliyun.com 如有错误或者疏漏欢迎指出互相学习。另外有什么想要了解的内容也可以给我发邮件互相谈讨定知无不言。
注未经同意不得转载