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. Python - 命令式编程与符号编程

    原文链接:https://zh.d2l.ai/chapter_computational-performance/hybridize.html本文是对原文内容的摘取和扩展. 命令式编程(imperat ...

  2. H5 页面调试小帮手-UC 开发者工具

    调试web页面难 接触过app自动化的同学都比较清楚,如果想要对web应用或者Hybrid应用进行调试时(获取页面元素信息等等),必定绕不开的就是chrome浏览器的inspect功能了:在chrom ...

  3. Python Django(WEB电商项目构建)

    (坚持每一天,就是成功) Python Django Web框架,Django是一个开放源代码的Web应用框架,由Python写成.采用了MTV的框架模式,即模型M,模板T和视图V组成. 安装Pyth ...

  4. python编译pyc工程--导包问题解决

    利用python 编译工程,生产pyc文件 pyc文件好处:是一种二进制机器码,并且隐藏了源文件代码,但是有和py文件一样的功能(可以理解为效果一样) 所以可以将代码隐藏,便于商业价值,保护代码隐私还 ...

  5. mac openresty 源码安装 坑

    下载openresty源码安装 下载页面http://openresty.org/cn/download.html 下载上一个版本的稳定版 https://openresty.org/download ...

  6. 【Maven】---Linux搭建Nexus3.X私服

    Linux搭建Nexus3.X私服 备注:linux版本: ubuntu 同时已经部署好JDK8环境 一.linux安装nexus 1.创建文件夹并进入该目录 cd /usr/local && ...

  7. Docker实用技巧之更改软件包源提升构建速度

    一.开篇 地球,中国,成都市,某小区的阳台上,一青年负手而立,闭目沉思,阵阵的凉风吹得他衣衫呼呼的飘.忽然,他抬起头,刹那间,睁开了双眼,好似一到精光射向星空,只见这夜空......一颗星星都没有.他 ...

  8. .NET应用加载容器Glue4Net

    在写ASP.NET的应用的时候我们只需要把写好的WEB程序放到IIS即可,在更新的时候只需要把文件复制过去IIS就可以自动重新加载相关WEB程序.如果是写一些win服务程序或一个服务端应用程序就没有这 ...

  9. Leetcode:338. Bit位计数

    Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the ...

  10. 线程安全(上)--彻底搞懂volatile关键字

    对于volatile这个关键字,相信很多朋友都听说过,甚至使用过,这个关键字虽然字面上理解起来比较简单,但是要用好起来却不是一件容易的事.这篇文章将从多个方面来讲解volatile,让你对它更加理解. ...