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;
}

github源代码

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;
}

github源代码

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;
}

github源代码

c/c++ 学习互助QQ群:877684253

本人微信:xiaoshitou5854

c/c++ linux 进程间通信系列3,使用socketpair,pipe的更多相关文章

  1. c/c++ linux 进程间通信系列7,使用pthread mutex

    linux 进程间通信系列7,使用pthread mutex #include <stdio.h> #include <stdlib.h> #include <unist ...

  2. c/c++ linux 进程间通信系列6,使用消息队列(message queue)

    linux 进程间通信系列6,使用消息队列(message queue) 概念:消息排队,先进先出(FIFO),消息一旦出队,就从队列里消失了. 1,创建消息队列(message queue) 2,写 ...

  3. c/c++ linux 进程间通信系列5,使用信号量

    linux 进程间通信系列5,使用信号量 信号量的工作原理: 由于信号量只能进行两种操作等待和发送信号,即P(sv)和V(sv),他们的行为是这样的: P(sv):如果sv的值大于零,就给它减1:如果 ...

  4. c/c++ linux 进程间通信系列4,使用共享内存

    linux 进程间通信系列4,使用共享内存 1,创建共享内存,用到的函数shmget, shmat, shmdt 函数名 功能描述 shmget 创建共享内存,返回pic key shmat 第一次创 ...

  5. c/c++ linux 进程间通信系列2,使用UNIX_SOCKET

    linux 进程间通信系列2,使用UNIX_SOCKET 1,使用stream,实现进程间通信 2,使用DGRAM,实现进程间通信 关键点:使用一个临时的文件,进行信息的互传. s_un.sun_fa ...

  6. c/c++ linux 进程间通信系列1,使用signal,kill

    linux 进程间通信系列1,使用signal,kill 信号基本概念:  软中断信号(signal,又简称为信号)用来通知进程发生了异步事件.进程之间可以互相通过系统调用kill发送软中断信号.内核 ...

  7. Linux 进程间通信系列之 信号

    信号(Signal) 信号是比较复杂的通信方式,用于通知接受进程有某种事件发生,除了用于进程间通信外,进程还可以发送信号给进程本身:Linux除了支持Unix早期信号语义函数sigal外,还支持语义符 ...

  8. 进程间通信系列 之 socket套接字及其实例

    进程间通信系列 之 概述与对比   http://blog.csdn.net/younger_china/article/details/15808685  进程间通信系列 之 共享内存及其实例   ...

  9. 练习--LINUX进程间通信之消息队列MSG

    https://www.ibm.com/developerworks/cn/linux/l-ipc/part3/ 继续坚持,或许不能深刻理解,但至少要保证有印象. ~~~~~~~~~~~~~~ 消息队 ...

随机推荐

  1. 配置vscode同步大神玺哥的配置

    1.应用商店下载settings  sync 2.三键 ctrl + shift + p   对话框中输入sync:点击重置 3.ctrl + shift + p  点击下载 4.然后会自动的调整到g ...

  2. 记录一次安装OpenGL的漫长过程

    尝试codeblock和Dev-C++ 这学期新开了一门计算机图形图像的课,里面涉及到openGL,中午跑到图书馆开始倒腾OpenGL. 因为电脑里本来有codeblock,于是就想不用教材里面所说的 ...

  3. JVM基础系列第13讲:JVM参数之追踪类信息

    我们都知道 JVM 在启动的时候会去加载类信息,那么我们怎么得知他加载了哪些类,又卸载了哪些类呢?我们这一节就来介绍四个 JVM 参数,使用它们我们就可以清晰地知道 JVM 的类加载信息. 为了方便演 ...

  4. 《HelloGitHub月刊》第 10 期

    前言 这一年感谢大家的支持,小弟这里给大家拜年了! <HelloGitHub月刊>会一直做下去,欢迎大家加入进来提供更多的好的项目. 最后,祝愿大家:鸡年大吉- <HelloGitH ...

  5. Java基础15:深入剖析Java枚举类

    更多内容请关注微信公众号[Java技术江湖] 这是一位阿里 Java 工程师的技术小站,作者黄小斜,专注 Java 相关技术:SSM.SpringBoot.MySQL.分布式.中间件.集群.Linux ...

  6. Chapter 5 Blood Type——12

    I blinked, my mind going blank. Holy crow, how did he do that? 我眨着眼睛,心里一片空白.天哪,他是怎么做到的? "Er, wh ...

  7. RabbitMQ消息队列(六)-消息任务分发与消息ACK确认机制(.Net Core版)

    在前面一章介绍了在.Net Core中如何使用RabbitMQ,至此入门的的部分就完成了,我们内心中一定还有很多疑问:如果多个消费者消费同一个队列怎么办?如果这几个消费者分任务的权重不同怎么办?怎么把 ...

  8. SLAM入门之视觉里程计(1):特征点的匹配

    SLAM 主要分为两个部分:前端和后端,前端也就是视觉里程计(VO),它根据相邻图像的信息粗略的估计出相机的运动,给后端提供较好的初始值.VO的实现方法可以根据是否需要提取特征分为两类:基于特征点的方 ...

  9. Docker系列01—容器的发展历程---Docker的生态圈

    本文收录在容器技术学习系列文章总目录 Docker 和容器技术的发展可谓是日新月异,本文试图以全局的视角来梳理一下 docker 目前的生态圈.既然是概览,所以不会涉及具体的技术细节. Docker ...

  10. [十六]JavaIO之InputStreamReader 与 OutputStreamWriter

      简介 InputStreamReader OutputStreamWriter是转换流 InputStreamReader 是字节流通向字符流的桥梁,它将字节流转换为字符流. OutputStre ...