专题 11 IPC之管道
来源:互联网 发布:啊啦叮是不是网络传销 编辑:程序博客网 时间:2024/05/21 10:27
1.概述
管道通信是多个进程之间进行的通信方式之一。无名管道用于父进程与子进程之间进行通信的场合,命名管道用于任何两个进程之间的通信场合。管道仅仅是将某个进程的输出和另一个进程的输入相连接的单向通信的办法。
2.无名管道
无名管道采用pipe()函数来创建,其函数原型为:
#include<unistsd.h>
intpipe(int filedes[2]);
其中的数组filedes是一个文件描述符数组,用于保存管道返回的两个文件描述符。数组中的第一个函数是为读操作而创建和打开的,而第二个函数是为写操作而创建和打开的。直观地说,filedes[1]的输出变成了filedes[0]的输入。示例代码如下:
/*-------------------无名管道操作-------------------------------*/
#include<unistd.h>
#include<sys/types.h>
#include<stdio.h>
#include<errno.h>
#include<stdlib.h>
intmain()
{
intpipe_fd[2];
pid_tpid;
charbuf_r[100];
char*p_wbuf;
intr_num;
memset(buf_r,0, sizeof(buf_r));
if(pipe(pipe_fd)< 0)
{
printf("pipecreate error!\n");
return-1;
}
if((pid= fork()) == 0)
{
printf("\n");
close(pipe_fd[1]);//关闭读操作
sleep(2);
if((r_num= read(pipe_fd[0], buf_r, 100)) > 0)
{
printf("%dnumbers read from the pipe is %s\n", r_num, buf_r);
}
close(pipe_fd[0]);
exit(0);
}
elseif(pid > 0)
{
close(pipe_fd[0]);//关闭写操作
if(write(pipe_fd[1],"Hello", 5) != -1)
printf("parentwrite surcess\n");
if(write(pipe_fd[1],"pipe", 5) != -1)
printf("parentwrite success\n");
close(pipe_fd[1]);
sleep(2);
waitpid(pid,NULL, 0);
exit(0);
}
}
3.命名管道
命名管道的工作方式与普通的管道非常相似,但也有一些区别。
在文件系统中,命名管道是以设备特殊文件的形式存在的。
不同的进程可以通过命名管道共享数据。
可以利用makefifo函数来创建命名管道,其他的操作过程与文件操作类似。相关例子代码如下所示:
/*------------命名管道的读过程-------------*/
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<errno.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#defineFIFO "/tmp/myfifo"
int
main(intargc, char **argv)
{
charbuf_r[100];
intfd;
intnread;
if((mkfifo(FIFO,O_CREAT | O_EXCL) < 0) && (errno != EEXIST))
printf("cannotcreate fifoserver\n");
printf("Preparingfor reading bytes..\n");
memset(buf_r,0 , sizeof(buf_r));
fd= open(FIFO, O_RDONLY | O_NONBLOCK, 0);
if(fd== -1)
{
perror("openFIFO failed!\n");
exit(1);
}
while(1)
{
memset(buf_r,0, sizeof(buf_r));
if((nread= read(fd, buf_r, 100)) == -1)
{
if(errno== EAGAIN)
printf("nodata yet]n");
}
printf("read%s, from FIFO\n", buf_r);
sleep(1);
}
pause();
unlink(FIFO); //删除命名管道
}
/*-------------命名管道写进程---------------*/
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<errno.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#defineFIFO_SERVER "/tmp/myfifo"
int
main(intargc, char ** argv)
{
intfd;
charw_buf[100];
intnwrite;
fd= open(FIFO_SERVER, O_WRONLY | O_NONBLOCK, 0);
if(fd== -1)
{
if(errno== ENXIO)
printf("openerror, no reading process\n");
}
if(argc== 1)
printf("Pleasesend something\n");
strcpy(w_buf,argv[1]);
if((nwrite= write(fd, w_buf, 100)) == -1)
{
if(errno== EAGAIN)
printf("nodata yet\n");
}
else
printf("write%d characters: %s\n", nwrite, w_buf);
}
4.流管道
popen()通过创建一个管道,调用fork函数,然后启动shell来打开一个进程。
popen()函数的原型如下:
#include<stdio.h>
FILE*popen(const char *command, const char *type);
intpclose(FILE *stream);
相关的如下所示:
#include<unistd.h>
#include<sys/types.h>
#include<errno.h>
#include<stdio.h>
#include<stdlib.h>
#defineBUFSIZE 1024
intmain()
{
FILE*fp;
char*cmd = "ps -ef";
charbuf[BUFSIZE];
buf[BUFSIZE]= '\0';
if((fp=popen(cmd, "r")) ==NULL)
perror("popen");
while((fgets(buf,BUFSIZE, fp)) != NULL)
printf("%s",buf);
pclose(fp);
exit(0);
}
- 专题 11 IPC之管道
- [总结]IPC之管道
- linux IPC之管道
- IPC通信之管道
- IPC通信之管道
- linux IPC 之管道
- Linux-IPC之管道
- IPC通信之-管道
- IPC之管道详解
- IPC之管道详解
- IPC之---管道
- IPC通信之有名管道
- IPC之管道、FIFO、socketpair
- 【Linux编程】IPC之管道
- IPC之管道、FIFO、socketpair
- Linux IPC 之管道通信
- linux ipc之匿名管道
- Linux IPC 2之管道
- 工厂模式
- 如何学好C语言
- 资源URL。
- 雅虎宣布将以2.7亿美元收购InterClick
- Lion Disk Maker让你一键制作Lion系统安装U盘
- 专题 11 IPC之管道
- WIN7重装后有些文件因为权限无法打开的解决办法
- WIN7 下大小写屏幕显示迟缓的问题解决与记录
- 2011-11-1
- 用C++重写String类,实现并不是最完美的,欢迎指正补充!后续还有更多类似的实现放上来, 欢迎关注!!!
- 标记化结构初始化语法
- 强大的jQuery选择器
- 免费师范生求职记
- 二叉树判断(满二叉树, 完全二叉树, 相似, 对称同构)