linux进程通信--管道(pipe)(含实例代码)
来源:互联网 发布:甄嬛传 槿汐 知乎 编辑:程序博客网 时间:2024/04/30 07:36
管道(pipe):用于亲缘关系进程间(如父子进程,兄弟进程)的通信
一个进程写管道:写入字节数小于PIPE_BUF是原子操作,写操作在管道缓冲区没有及时读走时发生阻塞。
一个进程读管道:读操作在管道缓冲区没有数据时发生阻塞。
主要函数:
int pipe(int filedes[2]);
代码实例创建了父子进程,父进程写管道,子进程读管道。
代码验证了写阻塞:子进程读一次管道就休眠1秒,父进程一次写操作后将阻塞,直到子进程取走数据。
代码验证了读阻塞:父进程的写一次管道后休眠1秒,子进程一次读操作后将阻塞,直到父进程再次写数据。
如果管道内的实际数据比请求读的要少,读不阻塞。
但是无法验证写入字节数大于PIPE_BUF不是原子操作,我想不出一个方法来验证它。
附代码
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include<stdlib.h>
main()
...{
int pipe_fd[2];
pid_t pid;
int len = 4096*2;
char r_buf[len];
char w_buf[len];
char* p_wbuf;
int r_num;
int w_num;
int cmd;
memset(r_buf,0,sizeof(r_buf));
memset(w_buf,0,sizeof(r_buf));
p_wbuf=w_buf;
if(pipe(pipe_fd)<0)
...{
printf("pipe create error ");
return -1;
}
if((pid=fork())==0)
...{
close(pipe_fd[1]);
while(1)
...{
r_num=read(pipe_fd[0],r_buf,sizeof(r_buf));
if(r_num==0)
break;
printf( "读字节数:%d ",r_num);
//sleep(1);//验证写阻塞
}
close(pipe_fd[0]);
printf( "子进程退出... ");
}
else if(pid>0)
...{
close(pipe_fd[0]);//关闭读
//sleep(5);
while(1)
...{
//w_num = write(pipe_fd[1],w_buf,sizeof(w_buf));
w_num = write(pipe_fd[1],w_buf,111);
if(w_num!=-1)
printf( "写字节数:%d ",w_num);
else
printf( "error ");
sleep(1);//验证读阻塞
}
close(pipe_fd[1]);//关闭写
printf( "父进程退出... ");
}
}
#include <sys/types.h>
#include <errno.h>
#include<stdlib.h>
main()
...{
int pipe_fd[2];
pid_t pid;
int len = 4096*2;
char r_buf[len];
char w_buf[len];
char* p_wbuf;
int r_num;
int w_num;
int cmd;
memset(r_buf,0,sizeof(r_buf));
memset(w_buf,0,sizeof(r_buf));
p_wbuf=w_buf;
if(pipe(pipe_fd)<0)
...{
printf("pipe create error ");
return -1;
}
if((pid=fork())==0)
...{
close(pipe_fd[1]);
while(1)
...{
r_num=read(pipe_fd[0],r_buf,sizeof(r_buf));
if(r_num==0)
break;
printf( "读字节数:%d ",r_num);
//sleep(1);//验证写阻塞
}
close(pipe_fd[0]);
printf( "子进程退出... ");
}
else if(pid>0)
...{
close(pipe_fd[0]);//关闭读
//sleep(5);
while(1)
...{
//w_num = write(pipe_fd[1],w_buf,sizeof(w_buf));
w_num = write(pipe_fd[1],w_buf,111);
if(w_num!=-1)
printf( "写字节数:%d ",w_num);
else
printf( "error ");
sleep(1);//验证读阻塞
}
close(pipe_fd[1]);//关闭写
printf( "父进程退出... ");
}
}
- linux进程通信--管道(pipe)(含实例代码)
- linux进程通信--有名管道(FIFO)(含实例代码)
- Linux进程间通信之管道(pipe)
- Linux环境进程间通信 --- 管道Pipe
- Linux 进程间管道pipe通信
- Linux进程间通信之管道(pipe)
- Linux进程间通信之管道(pipe)
- Linux进程通信-管道(pipe)
- 进程通信----管道(pipe)
- Linux进程间通信二之管道(含代码)
- Linux进程通信IPC--管道Pipe/Named Pipe
- Linux--进程间通信(一)-管道(pipe)通信
- 进程通信的实例--管道(pipe)
- 进程间通信-管道pipe
- 进程间通信 管道pipe
- 进程间通信--管道(pipe)
- 进程通信值pipe管道
- 进程通信-无名管道PIPE
- 8.1.1 String类和对象
- 中移动对垃圾短信表态:有不可推卸的责任
- 8.1 不变字符串String处理
- 第8章 字符串处理
- 项目管理(1) - 项目的计划
- linux进程通信--管道(pipe)(含实例代码)
- 没人能完全幸免的美国次贷危机
- 多线程调用DataGridView
- 如何让你的发言语出惊人
- 业务实体对象(Business Entity Object)的序列化
- Java开发中的几个概念介绍
- linux进程通信--有名管道(FIFO)(含实例代码)
- 使用Ext.Window时出现No center region defined in BorderLayout错误的愿原因
- 7.3.2 时间和字符串之间的转换