c/c++ linux 进程间通信系列3,使用socketpair,pipe
linux 进程间通信系列3,使用socketpair,pipe
1,使用socketpair,实现进程间通信,是双向的。
2,使用pipe,实现进程间通信
使用pipe关键点:fd[0]只能用于接收,fd[1]只能用于发送,是单向的。
3,使用pipe,用标准输入往里写。
疑问:在代码2里不写wait函数的话,父进程不能结束,但是在代码3里也没有写wait函数,父进程却可以结束???
1,使用socketpair:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <wait.h>
int main(){
int sv[2];
pid_t pid;
char buf[128];
memset(buf, 0, sizeof(buf));
if(socketpair(AF_UNIX, SOCK_STREAM, 0, sv) != 0){
perror("socketpair");
return 1;
}
pid = fork();
if(pid < 0){
perror("fork");
return 1;
}
if(pid == 0){
close(sv[0]);
read(sv[1], buf, sizeof(buf));
printf("child process : data from parant process [%s]\n", buf);
exit(0);
}
else {
int status;
close(sv[1]);
write(sv[0], "HELLO", 5);
printf("parent process : child process id %d\n", pid);
wait(&status);
}
return 0;
}
2,使用pipe:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wait.h>
int main(){
int p[2];
pid_t pid;
char buf[128];
memset(buf, 0, sizeof(buf));
if(pipe(p) != 0){
perror("pipe");
return 1;
}
pid = fork();
if(pid < 0){
perror("fork");
return 1;
}
if(pid == 0){
close(p[1]);
read(p[0], buf, sizeof(buf));
printf("child process : data form parent process [%s]\n", buf);
exit(0);
}
else{
close(p[0]);
write(p[1], "aaaa", 4);
printf("parent process : child process is %d\n", pid);
int status;
wait(&status);
}
return 0;
}
3,使用pipe,用标准输入往里写。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wait.h>
int main(){
int p[2];
pid_t pid;
char buf[1024];
memset(buf, 0, sizeof(buf));
if(pipe(p) != 0){
perror("pipe");
return 1;
}
pid = fork();
if(pid < 0){
perror("fork");
return 1;
}
if(pid == 0){
printf("child process : my_id=%d\n",getpid());
close(p[0]);
//把标准输出给管道1了
dup2(p[1], fileno(stdout));
char *argv[ ]={"ls", "/home/ys/cpp/network"};
//利用ls命令,往标准输出里,输入文件夹里文件的的名字,标准输出又连接到了上面开的管道1里。
if(execve("/bin/ls", argv, NULL) < 0){
perror("exec");
return 1;
}
exit(0);
}else{
int n;
FILE* filep;
close(p[1]);
printf("parent process : child process id=%d\n", pid);
//先打开管道1
filep = fdopen(p[0], "r");
if(filep == NULL){
perror("fdopen");
return 1;
}
//再从管道1里读取数据
while(fgets(buf, sizeof(buf), filep) != NULL){
printf("get:%s\n", buf);
}
int status;
wait(&status);
}
return 0;
}
c/c++ 学习互助QQ群:877684253

本人微信:xiaoshitou5854
c/c++ linux 进程间通信系列3,使用socketpair,pipe的更多相关文章
- c/c++ linux 进程间通信系列7,使用pthread mutex
linux 进程间通信系列7,使用pthread mutex #include <stdio.h> #include <stdlib.h> #include <unist ...
- c/c++ linux 进程间通信系列6,使用消息队列(message queue)
linux 进程间通信系列6,使用消息队列(message queue) 概念:消息排队,先进先出(FIFO),消息一旦出队,就从队列里消失了. 1,创建消息队列(message queue) 2,写 ...
- c/c++ linux 进程间通信系列5,使用信号量
linux 进程间通信系列5,使用信号量 信号量的工作原理: 由于信号量只能进行两种操作等待和发送信号,即P(sv)和V(sv),他们的行为是这样的: P(sv):如果sv的值大于零,就给它减1:如果 ...
- c/c++ linux 进程间通信系列4,使用共享内存
linux 进程间通信系列4,使用共享内存 1,创建共享内存,用到的函数shmget, shmat, shmdt 函数名 功能描述 shmget 创建共享内存,返回pic key shmat 第一次创 ...
- c/c++ linux 进程间通信系列2,使用UNIX_SOCKET
linux 进程间通信系列2,使用UNIX_SOCKET 1,使用stream,实现进程间通信 2,使用DGRAM,实现进程间通信 关键点:使用一个临时的文件,进行信息的互传. s_un.sun_fa ...
- c/c++ linux 进程间通信系列1,使用signal,kill
linux 进程间通信系列1,使用signal,kill 信号基本概念: 软中断信号(signal,又简称为信号)用来通知进程发生了异步事件.进程之间可以互相通过系统调用kill发送软中断信号.内核 ...
- Linux 进程间通信系列之 信号
信号(Signal) 信号是比较复杂的通信方式,用于通知接受进程有某种事件发生,除了用于进程间通信外,进程还可以发送信号给进程本身:Linux除了支持Unix早期信号语义函数sigal外,还支持语义符 ...
- 进程间通信系列 之 socket套接字及其实例
进程间通信系列 之 概述与对比 http://blog.csdn.net/younger_china/article/details/15808685 进程间通信系列 之 共享内存及其实例 ...
- 练习--LINUX进程间通信之消息队列MSG
https://www.ibm.com/developerworks/cn/linux/l-ipc/part3/ 继续坚持,或许不能深刻理解,但至少要保证有印象. ~~~~~~~~~~~~~~ 消息队 ...
随机推荐
- linux清空文件内容的几种方式与区别
虽然linux清空文件内容的方式有很多种,但是他们之间有着细微的差别.通过实践我将他们分为两类: 将文件清空,文件大小为0k $ : > filename $ > filename $ ...
- redis 系列11 列表对象
一. 列表对象概述 Redis列表是简单的字符串列表,按照插入顺序排序.你可以添加一个元素到列表的头部(左边)或者尾部(右边).一个列表最多可以包含 232 - 1 个元素 (4294967295, ...
- C++中int与string的转化
C++中int与string的转化 int本身也要用一串字符表示,前后没有双引号,告诉编译器把它当作一个数解释.缺省情况下,是当成10进制(dec)来解释,如果想用8进制,16进制,怎么办?加上前缀, ...
- C语言中你可能不熟悉的头文件(stdlib.h)
C语言中你可能不熟悉的头文件<cstdlib>(stdlib.h) C Standard General Utilities Library (header) C标准通用工具库(头文件) ...
- Kafka基础简介
kafka是一个分布式的,可分区的,可备份的日志提交服务,它使用独特的设计实现了一个消息系统的功能. 由于最近项目升级,需要将spring的事件机制转变为消息机制,针对后期考虑,选择了kafka作为消 ...
- 【EF6学习笔记】目录
[EF6学习笔记](一)Code First 方式生成数据库及初始化数据库实际操作 [EF6学习笔记](二)操练 CRUD 增删改查 [EF6学习笔记](三)排序.过滤查询及分页 [EF6学习笔记]( ...
- linux添加C#运行环境
linux是不带C#的运行环境的,同样的还有.NET. 有一个叫做Mono的很好用http://www.go-mono.com/,有给docker,而且有环境的选择,要注意. 安好后有给样例的程序,编 ...
- MaxCompute安全管理指南-基础篇
背景及目的 方便和辅助MaxCompute的project owner或安全管理员进行project的日常安全运维,保障数据安全. MaxCompute有安全模型,DataWorks也有安全模型,当通 ...
- 超级账本fabric原理之gossip详解
Goosip协议 去中心化.容错和最终一致性的算法 信息达到同步的最优时间:log(N). 功能: 节点发现 数据广播 gossip中有三种基本的操作: push - A节点将数据(key,value ...
- formData批量上传的多种实现
前言 最近项目需要批量上传附件,查了下资料,网上很多但看着一脸懵,只贴部分代码,介绍也不详细,这里记录一下自己的采坑与多种实现,以免以后忘记. 这里先介绍下FormData对象,以下内容摘自:http ...