首页 技术 正文
技术 2022年11月18日
0 收藏 314 点赞 2,952 浏览 1831 个字
/*管道  可以把管道想象为两个实体之间的单向连接器。注意,管道是半双工的,  如果需要全双工通讯,应该转而考虑套接字。  匿名管道又称管道,提供了一个进程与它的兄弟进程通讯的方法,只存在于父进程中;  命名管道,可以存在与文件系统中,任意进程都可找到它,使得不同先祖的进程也可以通讯。  #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 80int 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;    }
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,557
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898