/*管道
  可以把管道想象为两个实体之间的单向连接器。注意,管道是半双工的,
  如果需要全双工通讯,应该转而考虑套接字。
  匿名管道又称管道,提供了一个进程与它的兄弟进程通讯的方法,只存在于父进程中;
  命名管道,可以存在与文件系统中,任意进程都可找到它,使得不同先祖的进程也可以通讯。
  #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管道编程实例的更多相关文章

  1. linux内核模块编程实例

    linux内核模块编程实例 学号:201400814125 班级:计科141 姓名:刘建伟 1.确定本机虚拟机中的Ubuntu下Linux的版本 通过使用命令uname -a/uname -r/una ...

  2. Linux c编程实例_例子

    例一:字符与整型变量的实现 #include <stdio.h> int main() { int c1,c2; char c3; c1='a'-'A'; c2='b'-'B'; c3=; ...

  3. Linux多线程编程实例解析

    Linux系统下的多线程遵循POSIX线程接口,称为 pthread.编写Linux下的多线程程序,需要使用头文件pthread.h,连接时需要使用库libpthread.a.顺便说一下,Linux ...

  4. Linux网络编程实例解析

    **************************************************************************************************** ...

  5. linux 定时器编程实例(完善中).....

    最近在写linux 下的定时器编程实验,测试发现 usleep函数在 x86 架构下的定时还是比较准确的,在arm9下 就不太准了. 今天用linux 下的setitimer()函数进行了定时 器的测 ...

  6. Linux文件编程实例

    //捕获fopen调用中的错误 #include <stdio.h> #include <errno.h> #include <string.h> #define  ...

  7. Linux多进程编程实例

    前言:编写多进程程序时,我们应该了解一下,创建一个子进程时,操作系统内核是怎样做的.当通过fork函数创建新的子进程时,内核将父进程的用户地址空间的内容复制给子进程,这样父子进程拥有各自独立的用户空间 ...

  8. Linux 多线程编程实例

    一.多线程 VS 多进程 和进程相比,线程有很多优势.在Linux系统下,启动一个新的进程必须分配给它独立的地址空间,建立众多的数据表来维护代码段和数据.而运行于一个进程中的多个线程,他们之间使用相同 ...

  9. linux socket编程实例

    /* ============================================================================ Name : client.c Auth ...

随机推荐

  1. Android需求之点击跳转至市场评价

    相信大家都看过APP上有一个选项"喜欢此APP?还希望您评价一下吧!",然后点击弹出选择框让你选择一个市场如: 安智市场,百度应用,豌豆荚-.然后选择其中一个后就跳转至此市场你的A ...

  2. 项目分享:通过使用SSH框架的公司-学员关系管理系统(CRM)

    ----------------------------------------------------------------------------------------------[版权申明: ...

  3. How to code like a hacker

    We are coding. Are we engineers? Are we programmers? Are we coder? No, I want to be a hacker! Many g ...

  4. GirlFriendNotFoundException异常是怎样处理的?

    GirlFriendNotFoundException异常是怎样处理的? 如果说要去创造这个异常,那么我们的JAVA程序里,肯定是继承Exception去处理,所有我们可以先实现一个自己的Except ...

  5. 使用QGIS将文本坐标转换为矢量文件

    本文主要是说明如果使用QGIS将文本格式的点坐标转换为矢量文件(如shapefile格式). 所需工具:QGIS 所需数据:文本格式的点文件 所需要处理的点坐标文件如下所示, 114.2 22.15 ...

  6. Swift中方法闭包参数不能省略括号的一种情况

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 我们知道在swift中,如果方法的最后一个参数是一个闭包类型, ...

  7. lucene内存索引库、分词器

    内存索引库 特点 在内存中开辟一块空间,专门为索引库存放.这样有以下几个特征: 1)    因为索引库在内存中,所以访问速度更快. 2)    在程序退出时,索引库中的文件也相应的消失了. 3)    ...

  8. JavaEE介绍

    相关术语 为什么需要JavaEE 我们编写的JSP代码中,由于大量的显示代码和业务逻辑混淆在一起,彼此嵌套,不利于程序的维护和扩展.当业务需求发生变化的时候,对于程序员和美工都是一个很重的负担.为了程 ...

  9. 在代码中写view 的长宽高等

    获得资源的id的另一种方法 int layoutRes = getResources().getIdentifier("pager_view" + i, "layout& ...

  10. cassandra 如何写数据以及放置副本

    application发送数据到server application 发送请求到server 根据设置的load balance 规则从cluster中挑选一个coordinator,一般使用轮询即可 ...