一、PIPE(无名管道)

函数原型:

#include <unistd.h>

int pipe(int fd[]);

通常,进程会先调用pipe,接着调用fork,从而创建从父进程到子进程的IPC通道。

父进程和子进程之间也可用通过pipe通信。

例子,父进程到子进程hello world:

 #include "apue.h"

 int main(void)
{
int n;
int fd[];
pid_t pid;
char line[MAXLINE]; if(pipe(fd) < )
err_sys("pipe error");
if((pid = fork()) < ) {
err_sys("fork error");
} else if (pid > ) {
close(fd[]);
write(fd[], "hello world\n", );
} else {
close(fd[]);
  n = read(fd[], line, MAXLINE);
write(STDOUT_FILENO, line, n);
}
exit();
}

二、函数popen和pclose

#include <stdio.h>

FILE *open(const char *cmdstring, const char *type);

int pclose(FILE *fp);

创建一个管道,fork一个子进程,关闭未使用的管道端,执行一个shell运行命令,然后等待命令终止。

#include "apue.h"
#include <stdio.h> int main()
{
FILE *fp;
char ch, line[]; fp = popen("ls *.c", "r");
if(fp != NULL)
{
while(fgets(line, , fp))
{
printf("line=%s\n", line);
}
} pclose(fp); return ;
}

linux IPC的PIPE的更多相关文章

  1. Linux IPC BSD Pipe

    mkfifo() //创建有名管道(FIFO special file),创建完了就像普通文件一样open(),再读写,成功返回0,失败返回-1设errno.VS$man 3 mkfifo #incl ...

  2. Linux IPC实践(1) -- 概述

    进程的同步与互斥 进程同步: 多个进程需要相互配合共同完成一项任务. 进程互斥: 由于各进程要求共享资源,而且有些资源需要互斥使用,因此各进程间竞争使用这些资源,进程的这种关系为进程的互斥;系统中某些 ...

  3. CVE-2017-7494 Linux Samba named pipe file Open Vul Lead to DLL Execution

    catalogue . 漏洞复现 . 漏洞代码原理分析 . 漏洞利用前提 . 临时缓解 && 修复手段 1. 漏洞复现 . SMB登录上去 . 枚举共享目录,得到共享目录/文件列表,匿 ...

  4. linux ipc/its

    linux进程间双向消息队列 server.c #include <stdio.h> #include <stdlib.h> #include <string.h> ...

  5. Linux IPC之共享内存C 事例

    Linux IPC之共享内存 标签: linuxrandomnull工作 2011-08-25 11:52 4123人阅读 评论(0) 收藏 举报  分类: Linux(3)  读书札记(3)  版权 ...

  6. Linux IPC Pipe

    mkfifo() //创建有名管道(FIFO special file),创建完了就像普通文件一样open(),再读写,成功返回0,失败返回-1设errno.VS$man 3 mkfifo #incl ...

  7. Linux IPC实践(2) --匿名PIPE

    管道概念 管道是Unix中最古老的进程间通信的形式,我们把从一个进程连接到另一个进程的一个数据流称为一个"管道", 管道的本质是固定大小的内核缓冲区; 如:ps aux | gre ...

  8. linux IPC总结——管道

    管道 管道是unix ipc的最古老形式,是一种在内存中的特殊文件,只能在具有公共祖先的进程之间使用(即父子进程,兄弟进程). 管道由pipe函数创建 #include <unistd.h> ...

  9. Linux中的pipe(管道)与named pipe(FIFO 命名管道)

    catalogue . pipe匿名管道 . named pipe(FIFO)有名管道 1. pipe匿名管道 管道是Linux中很重要的一种通信方式,是把一个程序的输出直接连接到另一个程序的输入,常 ...

随机推荐

  1. 【leetcode】1003. Check If Word Is Valid After Substitutions

    题目如下: We are given that the string "abc" is valid. From any valid string V, we may split V ...

  2. PHP chdir() 函数

    实例 改变当前的目录: <?php// Get current directoryecho getcwd() . "<br>"; // Change direct ...

  3. POJ 2955 Brackets (区间dp入门)

    Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...

  4. 匹配Luhn算法:可用于检测银行卡卡号

    匹配Luhn算法:可用于检测银行卡卡号 /** * http://www.cnblogs.com/JnKindle/p/5798974.html * * 匹配Luhn算法:可用于检测银行卡卡号 * * ...

  5. Oracle中start with...connect by (prior)子句的用法

    connect by 是结构化查询中用到的,基本语法是:select … from tablenamestart with 条件1connect by 条件2where 条件3; 例:select * ...

  6. 探索Redis设计与实现1:Redis 的基础数据结构概览

    本文转自互联网 本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https://github.com/h2pl/Java-Tutorial ...

  7. oracle 批处理 bulk collect 带来的性能优势

    create table -- drop table tmp_20190706_220000-- truncate table tmp_20190706_220000 create table tmp ...

  8. php开发面试题---PHP为什么不安全,主要有那些安全问题(整理)

    php开发面试题---PHP为什么不安全及常见的攻击方式(整理) 一.总结 一句话总结: 其实安全和语言关系不大,主要和程序员关系比较大,php也就是因为是弱类型语言,所以不如java健壮,php会遇 ...

  9. 8、数值分析与matlab

    1.今天要拷matlab代码了,而且是很恶心的算法,估计也没几个人能看得懂,就连我自己都看不懂. 我也不知道这样做的意义何在,可能只是证明我在这世上曾经学过那么那么难的东西吧 首先是一个matlab版 ...

  10. 18. HTTP协议一:概述、原理、版本、请求方法

    HTTP协议概述 HTTP协议就是我们常说的超文本协议(HyperText Transfer Protocol).HTTP协议是互联网上应用最为广泛的一种网络协议.所有的WWW文件都必须遵守这个标准. ...