IPC之PIPE
管道是一种只允许用在有亲属关系的进程间通信的方式,由函数pipe创建一个管道,read,write进行读写操作。
#include <unistd.h>
int pipe(int pipefd[]);
参数pipefd[2]数组返回打开的读写描述符,pipefd[0]为读,pipefd[1]为写。
第一个问题:文件描述符怎么会出现一个只能读,一个只能写呢?猜想是对一个文件打开了2次,一个以只读打开,一个以只写打开。使用fcntl来验证下:
#include <unistd.h>
#include <fcntl.h> int fcntl(int fd, int cmd, ... /* arg */ );
F_GETFL (void)
Get the file access mode and the file status flags; arg is ignored.
cmd为F_GETFL时,最后一个参数arg被忽略。测试代码:
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h> int main(void)
{
int flags;
int fd[]; if (pipe(fd) < )
{
perror("pipe error");
} flags = fcntl(fd[], F_GETFL,);
if ( flags < )
{
perror("fcntl");
close(fd[]);
close(fd[]);
}
switch (flags & O_ACCMODE)
{
case O_RDONLY:
printf("read only\n");
break; case O_WRONLY:
printf("write only\n");
break; case O_RDWR:
printf("read write\n");
break; default:
printf("unknown access mode\n");
} flags = fcntl(fd[], F_GETFL,);
if ( flags < )
{
perror("fcntl");
close(fd[]);
close(fd[]);
}
switch (flags & O_ACCMODE)
{
case O_RDONLY:
printf("read only\n");
break; case O_WRONLY:
printf("write only\n");
break; case O_RDWR:
printf("read write\n");
break; default:
printf("unknown access mode\n");
}
close(fd[]);
close(fd[]);
exit();
}
运行结果:
read only
write only
与猜想相符。
数据的流向:

从图中可以看出,进程可以以pipefd[1]写完,然后以pipefd[0]读,自己写自己读,这条数据流是通的。 验证:
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h> #define MAXLINE 4096
int main(void)
{
int flags;
int fd[], n;
char buf[MAXLINE];
if (pipe(fd) < )
{
perror("pipe error");
} n = write(fd[], "hello world\n", MAXLINE);
if ( n < )
{
perror("write");
goto end;
}
n = read(fd[],buf, n);
if ( n < )
{
perror("read");
goto end;
}
printf("read:%s\n",buf); end:
close(fd[]);
close(fd[]);
exit();
}
输出:
read:hello world
既然是进程间通信,那么管道在同一个进程中读写基本是没什么意义的,管道常用的方式是,先创建一个管道,然后fork,父子进程就共享了这个管道了。数据流向如图:

这样,管道的写端有2个进程操作,读端有2个进程操作。但是这样一来就出现了一个问题,假设父进程读,那么这个数据是它自己写进去的呢?还是子进程写进去的?无法区分。通常一个进程关闭它的读,另一个进程关闭它的写,这样,数据流向就只有一个方向了,数据来自谁就显而易见了。如图:

测试代码:
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h> #define MAXLINE 4096 int main(void)
{
int n;
int fd[];
pid_t pid;
char line[MAXLINE]; if (pipe(fd) < )
perror("pipe error");
if ((pid = fork()) < )
{
perror("fork error");
}
else if (pid > ) /* parent */
{
close(fd[]);
write(fd[], "hello world\n", );
}
else /* child */
{
close(fd[]);
n = read(fd[], line, MAXLINE);
write(STDOUT_FILENO, line, n);
}
exit();
}
结果:
hello world
读一个空的管道或者写一个满的管道都将导致阻塞,不过可以通过fcntl的F_SETFL设置为O_NONBLOCK,从而不阻塞。
当管道一端被关闭后,有下列2条规则:
1.当读一个写端所有文件描述符引用都已被关闭的管道时,在所有数据被读完后,read将返回0。表示无数据可读。
2.当写一个读端所有文件描述符引用都已被关闭的管道时,将产生SIGPIPE信号,write返回-1。
混淆的东西,管道的容量和管道的缓冲区大小。
管道的容量:指管道满时装的字节数,自2.6.11内核后,容量为64k。管道满了就会导致写操作产生阻塞。
管道缓冲区大小:由PIPE_BUF指定,指的是保证管道写操作为原子操作的最大值,如果一次写入的内容超过这个值,那么这次的写操作就不是原子的。什么意思呢?就是指,可能存在多个进程写同一个管道,如果一次写入的字节数大于缓冲区大小,则可能会出现A进程写入的内容中插入了B进程写入的内容。
下面是出现这种情况的代码:
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h> #define MAXLINE 4096+100 int main(void)
{
int n;
int fd[];
pid_t pid;
char line[MAXLINE]; if (pipe(fd) < )
{
perror("pipe error");
} if ((pid = fork()) < )
{
perror("fork error");
}
else if (pid > ) /* parent */
{
close(fd[]);
while ( )
{
n = read(fd[], line, MAXLINE);
write(STDOUT_FILENO, line, n);
write(STDOUT_FILENO, "\n\n\n", );
}
}
else /* child */
{
if ((pid = fork()) < )
{
perror("fork error");
}
else if (pid > )
{
close(fd[]); while ()
{
memset(line, 'a',MAXLINE);
write(fd[], line, MAXLINE);
}
}
else
{
close(fd[]); while ( )
{
memset(line, 'b',MAXLINE);
write(fd[], line, MAXLINE);
}
}
} exit();
}
IPC之PIPE的更多相关文章
- Linux IPC BSD Pipe
mkfifo() //创建有名管道(FIFO special file),创建完了就像普通文件一样open(),再读写,成功返回0,失败返回-1设errno.VS$man 3 mkfifo #incl ...
- Unix IPC之pipe
pipe创建函数: #include <unistd.h> /* Create a one-way communication channel (pipe). If successful, ...
- linux IPC的PIPE
一.PIPE(无名管道) 函数原型: #include <unistd.h> ]); 通常,进程会先调用pipe,接着调用fork,从而创建从父进程到子进程的IPC通道. 父进程和子进程之 ...
- CVE-2017-7494 Linux Samba named pipe file Open Vul Lead to DLL Execution
catalogue . 漏洞复现 . 漏洞代码原理分析 . 漏洞利用前提 . 临时缓解 && 修复手段 1. 漏洞复现 . SMB登录上去 . 枚举共享目录,得到共享目录/文件列表,匿 ...
- 【APUE】Chapter16 Network IPC: Sockets & makefile写法学习
16.1 Introduction Chapter15讲的是同一个machine之间不同进程的通信,这一章内容是不同machine之间通过network通信,切入点是socket. 16.2 Sock ...
- 高级IPC DBus
What is IPC IPC [Inter-Process Communication] 进程间通信,指至少两个进程或线程间传送数据或信号的一些技术或方法.在Linux/Unix中,提供了许多IPC ...
- (转)解决 ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务
下面操作默认在安装Oralce数据库的服务器上运行. 1)确保Oracle 基本服务都已启动 OracleDBConsoleorcl OracleOraDb11g_home1TNSListener O ...
- python高级之多进程
python高级之多进程 本节内容 多进程概念 Process类 进程间通讯 进程同步 进程池 1.多进程概念 multiprocessing is a package that supports s ...
- oracle 监听启动、停止、查看命令
1.su oracle 然后启动监听器 1.lsnrctl start 会看到启动成功的界面; 1.lsnrctl stop 停止监听器命令. 1.lsnrctl status 查看监听器命令. ...
随机推荐
- 【C#进阶系列】04 类型基础
关于System.Object 所有类型都从System.Object派生而来. System.Object的公共方法中ToString()一般是返回对象的类型的全名,只有Int32这些类型将其重写后 ...
- JavaMail入门第二篇 创建邮件
JavaMail API使用javax.mail.Message类来表示一封邮件,Message类是一个抽象类,所以我们需要使用其子类javax.mail.internet.MimeMessage类来 ...
- ahjesus如何在windows下制作适用于mac的u盘启动盘
先用macdrive把U盘格式化成hfs+格式,然后下载原版dmg格式系统,再用ultraISO将dmg转成ISO格式(也可以不用转换),最后用ultraISO里面“启动”--->“写入硬盘映像 ...
- 【GPU编解码】GPU硬编码
一.OpenCV中的硬编码 OpenCV2.4.6中,已实现利用GPU进行写视频,编码过程由cv::gpu::VideoWriter_GPU完成,其示例程序如下. int main(int argc, ...
- linux多线程-互斥&条件变量与同步
多线程代码问题描述 我们都知道,进程是操作系统对运行程序资源分配的基本单位,而线程是程序逻辑,调用的基本单位.在多线程的程序中,多个线程共享临界区资源,那么就会有问题: 比如 #include < ...
- C# 分层 三层架构
Hello! 三层架构↓↓↓↓↓↓ 三层架构分为:表现层(UI(User Interface)).业务逻辑层(BLL(Business Logic Layer)).数据访问层(DAL(Data Acc ...
- Vue列表渲染
gitHub地址:https://github.com/lily1010/vue_learn/tree/master/lesson09 一 for循环数组 <!DOCTYPE html> ...
- xampp 访问出现New XAMPP security concept 解决办法
最近通过手机访问本地服务器时出现以下问题: Access forbidden! New XAMPP security concept: Access to the requested director ...
- Atitit.视频文件加密的方法大的总结 java c# php
Atitit.视频文件加密的方法大的总结 java c# php 1. 加密的算法 aes 3des des xor等.1 2. 性能1 3. 解密1 4. 播放器的事件扩展1 5. 自定义格式 ...
- android 数据文件存取至储存卡
来自:http://blog.csdn.net/jianghuiquan/article/details/8569233 <?xml version="1.0" encodi ...