管道通信之无名管道---pipe()
pipe()函数在子进程产生之前就应该存在。
- 父子进程之间只进行一次传递
/*============================================
> Copyright (C) 2014 All rights reserved.
> FileName:onepipe.c
> author:donald
> details:
==============================================*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 512
int main(int argc, const char *argv[])
{
int pipefd[];
pid_t pid; if(pipe(pipefd) == -){
perror("pipe failed");
exit(-);
}
printf("%u\n",pid);
pid == fork();//这里一个个大大的bug,自己的误操作,debug了很久才搞定了
printf("%u\n",pid);
if( == pid){
close(pipefd[]);//0 read 1 write
//一个约定,父子进程都需遵守 char buf[N];
memset(buf,,N);
read(pipefd[],buf,N);
printf("child read:%s\n",buf); printf("child exit\n");
exit();
}else{
close(pipefd[]);//0 read
char line[N];
printf("parent begin\n"); memset(line,,N);
fgets(line,N,stdin); write(pipefd[],line,strlen(line));
printf("parent exit\n");
wait(NULL);//等待子进程的结束
}
return ;
} 父子进程通过管道,进行多次读写操作,先贴上一个比较奇葩的方法(就是一个错误):
/*============================================
> Copyright (C) 2014 All rights reserved.
> FileName:my_pipe.c
> author:donald
> details:
==============================================*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 1024
int main(int argc, const char *argv[])
{
int fds[];
if(pipe(fds) == -){//只能有一对进行读写
perror("failed");
exit();
}
pid_t pid = fork();
if(pid == -){
perror("error");
exit();
} while(){
if(pid == ){//child read
close(fds[]);//1 write
char buf[] = "";
read(fds[],buf,);//只能有一个读,
printf("child read:%s\n",buf);
//exit(1);
}else{//parent write close(fds[]);//0 read
// char *p = "hello,donald";
char line[N];
// memset(line,0,N);
fgets(line,N,stdin);
write(fds[],line,strlen(line));
//wait(NULL);
}
}
return ;
}再贴上正确的方法:
/*============================================
> Copyright (C) 2014 All rights reserved.
> FileName:twopipe.c
> author:donald
> details:
==============================================*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 512
int main(int argc, const char *argv[])
{
int pipefd[];
pid_t pid;
//pid = fork(); if(pipe(pipefd) == -){
perror("pipe failed");
exit(-);
}
pid = fork();
if(pid == ){
close(pipefd[]);//0 read
char buf[N];
while(){
memset(buf,,N);
if(read(pipefd[],buf,N) == ){
break;
}
printf("child read:%s\n",buf);
}
printf("child exit\n");
exit();
}else{
close(pipefd[]);
char line[N];
while(memset(line,,N),fgets(line,N,stdin) != NULL ){
write(pipefd[],line,strlen(line));
}
close(pipefd[]);
printf("parent exit\n");
wait(NULL);
}
return ;
}
管道通信之无名管道---pipe()的更多相关文章
- Linux 进程通信(无名管道)
无名管道 无名管道是半双工的,就是对于一个管道来讲,只能读,或者写. 无名管道只能在相关的,有共同祖先的进程间使用(即一般用户父子进程). 一个fork或者execve调用创建的子进程继承了父进程的文 ...
- linux进程篇 (三) 进程间的通信1 管道通信
通信方式分4大类: 管道通信:无名管道 有名管道 信号通信:发送 接收 和 处理 IPC通信:共享内存 消息队列 信号灯 socke 网络通信 用户空间 进程A <----无法通信----> ...
- Linux 进程间通信 无名管道(pipe)
无名管道: 1)只能用于具有亲缘关系的进程之间的通信(无名管道是某一个进程创建的,不像普通文件有路径,在文件系统中是不可见的,其他进程要想打开,只能通过继承的方式去打开) 2)半双工的通信模式,具有固 ...
- linux命名管道通信过程
前一个道,这节学习命名管道. 二命名管道 无名管道仅仅能用来在父子进程或兄弟进程之间进行通信,这就给没有亲缘关系的进程之间数据的交换带来了麻烦.解决问题就是本节要学习的还有一种管道通信:命名管道. 命 ...
- PHP多进程编程(2):管道通信
一个进程如果是个人英雄主义,那么多进程就是集体主义.(不严格区分多进程 和 多线程的差别) 你不再是一个独行侠,而是一个指挥家. 独来独往,非常自由自在,但是,很多时候,不如众人拾柴火焰高. 这就是我 ...
- c# c++通信--命名管道通信
进程间通信有很多种,windows上面比较简单的有管道通信(匿名管道及命名管道) 最近做个本机c#界面与c++服务进行通信的一个需求.简单用命名管道通信.msdn都直接有demo,详见下方参考. c+ ...
- linux进程的管道通信
linux进程的管道通信 要求 编程实现进程的管道通信,掌握管道通信的同步和互斥机制. 相关函数 pipe管道 指用于连接一个读进程和一个写进程以实现他们之间通信的一个共享文件,又名pipe文件.向管 ...
- Linux简单程序实例(GNU工具链,进程,线程,无名管道pipe,基于fd的文件操作,信号,scoket)
一, GNU工具链简介: (1)编译代码步骤: 预处理 -> 编译 -> 汇编 -> 链接: 预处理:去掉注释,进行宏替换,头文件包含等工作: gcc -E test.c -o te ...
- 进程间通信IPC之--无名管道(pipe)和有名管道(fifo)(转)
进程间通信IPC之--无名管道(pipe)和有名管道(fifo) 2012-01-17 22:41:20 分类: C/C++ 每个进程各自有不同的用户地址空间,任何一个进 程的全局变量在另一个进程中 ...
随机推荐
- 【搜索引擎Jediael开发笔记】v0.1完整代码
详细代码请见 E:\Project\[重要]归档代码\SearchEngine归档代码 或 https://code.csdn.net/jediael_lu/jediael/tree/10991c83 ...
- jq插件第二款来袭 图片滚动
这第二款也是非常实用的插件,也是与图片相关,关于图片的需求太多了,这个是图片滚动哦,不过不是无缝滚动,是左像右滚动,到头的话再往回滚动,利用scrollLeft实现的,支持自动滚动和每次滚动的个数默认 ...
- svg 文字
<text>标签 在svg中用使用<text>标签去定义一段文字.如 Example 1 在svg中写下 在平坦的路上曲折前行 Example 1 Dome <svg h ...
- Immediate Decodability(字典树)
Immediate Decodability Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- std::map的insert和下标[]访问
在map中插入元素 改变map中的条目非常简单,因为map类已经对[]操作符进行了重载 enumMap[1] = "One";enumMap[2] = "Two" ...
- Inno Setup 安装inf文件的一个例子
原文 http://zwkufo.blog.163.com/blog/static/2588251201063033524889/ ; INF安装例子; [Setup]; 注意: AppId 的值是唯 ...
- Geoserver基本使用、WMS服务发布与OpenLayers测试
1.Geoserver与OpenLayers的下载 Geoserver:http://geoserver.org/ OpenLayers:http://openlayers.org/ 2.安装部署Ge ...
- Sereja and Coat Rack(水)
Sereja and Coat Rack Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I6 ...
- Pet(dfs+vector)
Pet Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- mysql jdbc 查询连接问题
做了一个测试,mysql jdbc 链接A调用setAutoCommit,设置false,查询指定数据,可以查询出来,另个一链接把指定的数据给删除了,第一个链接在此查询的时候,仍然可以查询出来,使用的 ...