Linux管道编程实例
/*管道
可以把管道想象为两个实体之间的单向连接器。注意,管道是半双工的,
如果需要全双工通讯,应该转而考虑套接字。
匿名管道又称管道,提供了一个进程与它的兄弟进程通讯的方法,只存在于父进程中;
命名管道,可以存在与文件系统中,任意进程都可找到它,使得不同先祖的进程也可以通讯。
#include <unistd.h>
int pipe( int dfs[ 2 ] );创建匿名管道
int dup(int oldfd );创建一个文件描述符的副本
int dup2(int oldfd, int targetfd);
dup/dup2提供了复制文件描述符的功能。他们常用于stdin(0)、stdout(1)、stderr(2)的重定向;
#include <sys/types.h>
#include <sys/stat.h>
int mkfifo(const char* pathname,mode_t mode );创建一个命名管道
记住:管道只不过是一对文件描述符因此所有能够操作文件描述符的函数都可用于管道。这些函数
包括但不限于select,read,write,fcntl,freopen。
*/
/**********1、简单匿名管道应用************/
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#define MAX_LINE 80
#define PIPE_STDIN 0
#define PIPE_STDOUT 1
/*
myPipe[ 1 ]向管道写入数据;myPipe[ 0 ]从管道读取数据。
*/
int main( )
{
const char* string={"A simple message."};
int ret,myPipe[ 2 ];
char buffer[ MAX_LINE+1 ];
//create the pipe
ret=pipe( myPipe ); //pipe( )创建一个匿名管道
if( ret==0 )
{
//write the message into the pipe
write( myPipe[ PIPE_STDOUT ],string,strlen( string ) );
//read the message from the pipe
ret=read( myPipe[ PIPE_STDIN ],buffer,MAX_LINE );
//NULL terminate the string
buffer[ ret ]=0;
printf( "%s/n",buffer );
}
close( thePipe[ 0 ] );
close( thePipe[ 1 ] );
return 0;
}
//父子进程间利用管道通讯实例
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <wait.h>
#define MAX_LINE 80
int main( )
{
int thePipe[ 2 ],ret;
char buf[ MAX_LINE+1 ];
const char* testbuf={"a test string."};
if( pipe( thePipe )==0 )
{
if( fork( )==0 )
{
printf( "You have enter the child process/n" );
ret=read( thePipe[ 0 ],buf,MAX_LINE );
buf[ ret ]=0;
printf( "Child read info: %s/n",buf );
}
else
{
ret=write( thePipe[ 1 ],testbuf,strlen( testbuf ) );
ret=wait( NULL );
}
}
close( thePipe[ 0 ] );
close( thePipe[ 1 ] );
return 0;
}
/*值得注意的是:
把子进程的输出重定向到管道的输入,父进程的输入重定向到管道的输出。
--这是一个很值得记住的有用技术
*/
//使用C实现管道链接
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main( )
{
int pfds[ 2 ];
if( pipe( pfds )==0 )
{
if( fork( )==0 )
{
close( 1 );
dup2( pfds[ 1 ],1 );
close( pfds[ 0 ] );
execlp( "ls","ls","-l",NULL );
}
else
{
close( 0 );
dup2( pfds[ 0 ],0 );
close( pfds[ 1 ] );
execlp( "wc","wc","-l",NULL );
}
}
return 0;
}
Linux管道编程实例的更多相关文章
- linux内核模块编程实例
linux内核模块编程实例 学号:201400814125 班级:计科141 姓名:刘建伟 1.确定本机虚拟机中的Ubuntu下Linux的版本 通过使用命令uname -a/uname -r/una ...
- Linux c编程实例_例子
例一:字符与整型变量的实现 #include <stdio.h> int main() { int c1,c2; char c3; c1='a'-'A'; c2='b'-'B'; c3=; ...
- Linux多线程编程实例解析
Linux系统下的多线程遵循POSIX线程接口,称为 pthread.编写Linux下的多线程程序,需要使用头文件pthread.h,连接时需要使用库libpthread.a.顺便说一下,Linux ...
- Linux网络编程实例解析
**************************************************************************************************** ...
- linux 定时器编程实例(完善中).....
最近在写linux 下的定时器编程实验,测试发现 usleep函数在 x86 架构下的定时还是比较准确的,在arm9下 就不太准了. 今天用linux 下的setitimer()函数进行了定时 器的测 ...
- Linux文件编程实例
//捕获fopen调用中的错误 #include <stdio.h> #include <errno.h> #include <string.h> #define ...
- Linux多进程编程实例
前言:编写多进程程序时,我们应该了解一下,创建一个子进程时,操作系统内核是怎样做的.当通过fork函数创建新的子进程时,内核将父进程的用户地址空间的内容复制给子进程,这样父子进程拥有各自独立的用户空间 ...
- Linux 多线程编程实例
一.多线程 VS 多进程 和进程相比,线程有很多优势.在Linux系统下,启动一个新的进程必须分配给它独立的地址空间,建立众多的数据表来维护代码段和数据.而运行于一个进程中的多个线程,他们之间使用相同 ...
- linux socket编程实例
/* ============================================================================ Name : client.c Auth ...
随机推荐
- Rails报找不到sanitize和raw方法的解决
以下一段代码作用是对html字符串做过滤作用: sanitize(raw(content.split.map{ |s| wrap_long_string(s) }.join(' '))) 不过实际会报 ...
- Bootstrap3 表格-带边框的表格
添加 .table-bordered 类为表格和其中的每个单元格增加边框. <table class="table table-bordered"> ... </ ...
- [extjs5学习笔记]第三十八节 sencha CMD 6.0.0.220版本安装
本文地址:http://blog.csdn.net/sushengmiyan/article/details/46740381 简介 sencha cmd 6安装过程不需要UAC控制了对于widnow ...
- 一个整数数组,有n个整数,如何找其中m个数的和等于另外n-m个数的和?
int getSum(int* arr, int len) { int sum = 0; for (int i = 0; i < len; ++i) { sum += arr[i]; } ret ...
- Servlet - Listener、Filter、Decorator
Servlet 标签 : Java与Web Listener-监听器 Listener为在Java Web中进行事件驱动编程提供了一整套事件类和监听器接口.Listener监听的事件源分为Servle ...
- Java进阶(四十四)线程与进程的特征及区别
线程与进程的特征及区别 定义及特征 进程 指在系统中能独立运行并作为资源分配的基本单位,它是由一组机器指令.数据和堆栈等组成的,是一个能独立运行的活动实体. 进程的特征: 1.动态性:进程的实质是 ...
- [EXTJS5学习笔记]第二十六节 在eclipse/myeclipse中使用sencha extjs的插件
本文地址:http://blog.csdn.net/sushengmiyan/article/details/40507383 插件下载: http://download.csdn.net/detai ...
- spring 的OpenSessionInViewFilter简介
假设在你的应用中Hibernate是通过spring 来管理它的session.如果在你的应用中没有使用OpenSessionInViewFilter或者OpenSessionInViewInterc ...
- ubunut系统清理系统根目录下缓存文件夹.cache超大导致磁盘不足
在使用中突然发现系统超慢,没有做什么特别的操作. 只好重启下电脑,重启后提示系统空间不足1G.挨个查看文件夹大小,没有发现问题,然后就用Ctrl + H显示隐藏文件夹后再继续逐个查看大小,发现.cac ...
- Linux内核线程
内核线程是直接由内核本身启动的进程.内核线程实际上是将内核函数委托给独立的进程,与系统中其他进程"并行"执行(实际上,也并行于内核自身的执行),内核线程经常被称为内核"守 ...