现在的位置: 首页 > 综合 > 正文

Linux管道编程实例

2013年11月04日 ⁄ 综合 ⁄ 共 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 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;
        
    }

抱歉!评论已关闭.