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. java热加载和热部署

    JAVA热部署和热加载 联系与区别 Java热部署与热加载的联系 1.不重启服务器编译/部署项目 2.基于Java的类加载器实现 区别 部署方式 热部署在服务器运行时重新部署项目 热加载在运行时重新加 ...

  2. Spark MLlib

    MLlib    数据挖掘与机器学习        数据挖掘体系 数据挖掘:也就是data mining,是一个很宽泛的概念,也是一个新兴学科,旨在如何从海量数据中挖掘出有用的信息来.         ...

  3. Python内置函数(8)——bytes

    英文文档: class bytes([source[, encoding[, errors]]]) Return a new “bytes” object, which is an immutable ...

  4. Java中的String,StringBuilder,StringBuffer三者的区别(转载)

    最近在学习Java的时候,遇到了这样一个问题,就是String,StringBuilder以及StringBuffer这三个类之间有什么区别呢,自己从网上搜索了一些资料,有所了解了之后在这里整理一下, ...

  5. AttributeError: module 'pip' has no attribute 'main'

    Pycharm在运行pip安装模块是报错如下: 解决方法: 找到安装路径下的packaging_tool.py文件修改如下: 我的路径为D:\Program Files\JetBrains\PyCha ...

  6. spring cloud 配置zuul实用

    在线演示 演示地址:http://139.196.87.48:9002/kitty 用户名:admin 密码:admin 技术背景 前面我们通过Ribbon或Feign实现了微服务之间的调用和负载均衡 ...

  7. JDBC 连接池的两种方式——dbcp & c3p0

    申明:本文对于连接资源关闭采用自定义的 JDBCUtils 工具: package com.test.utils; import java.sql.Connection; import java.sq ...

  8. 全网Star最多(近20k)的Spring Boot开源教程 2019 年要继续更新了!

    从2016年1月开始写博客,默默地更新<Spring Boot系列教程>,从无人问津到千万访问,作为一个独立站点(http://blog.didispace.com),相信只有那些跟我一样 ...

  9. VS项目发布到本地IIS

    一.项目介绍 ①web项目,开发工具Visual Studio,语言C# ②本地IIS,手机上或其他用户在和本地在同一个局域网(并不绝对是指同一个WIFI)内访问,同时要把防火墙关掉 ③IIS全名In ...

  10. C#_Lamada帮助类

    using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; us ...