美文网首页
linux无名管道

linux无名管道

作者: 嵌入式工作 | 来源:发表于2018-07-13 19:39 被阅读0次
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#if 0
int pipe(int pipefd[2]);

       #define _GNU_SOURCE             /* See feature_test_macros(7) */
       #include <unistd.h>

       int pipe2(int pipefd[2], int flags);

DESCRIPTION
       pipe()  creates  a pipe, a unidirectional data channel that can be used
       for interprocess communication.  The array pipefd is used to return two
       file  descriptors  referring to the ends of the pipe.  pipefd[0] refers
       to the read end of the pipe.  pipefd[1] refers to the write end of  the
       pipe.   Data  written  to  the write end of the pipe is buffered by the
       kernel until it is read from the read end of  the  pipe.   For  further
       details, see pipe(7).
#endif

int main(int argc ,char*argv[])
{
    int buf[1024];
    int  len;
    int pip[2],rc,c;
    int *readfd;
    int *writefd;
    readfd=&pip[0];
    writefd=&pip[1];
    
    rc = pipe(pip);
    printf("start\n");
    if(rc == -1)
    {
        printf("\ncreat pipe fail \n");
        
    }
    
    rc = fork();
    switch(rc)
    {
       case -1:
        printf("fork err \n");
        exit(1);
        case 0:
        close(*readfd);
        while( (c =getchar())>0)
        {
            write(*writefd,&c,1);
        }
        
        break;
        default:
        close(*writefd);
         while(read(*readfd,&c,1)>0)
        {
              putchar(c);
        }
        break;
        
        
    }
    
    
    
}


相关文章

  • linux无名管道

  • linux无名管道

    1管道(无名管道)介绍 int pipe(int fd[2]); //成功返回0,失败返回-1;...

  • Linux 进程之间的通信方式

    linux使用的进程间通信方式 管道(pipe)、流管道(s_pipe)、无名管道(FIFO)、 套接字 sock...

  • Android 进程通信--Binder机制

    一、起源——为什么在Android中使用binder通信机制? linux中的进程通信 管道包含无名管道和有名管道...

  • linux 中的奇妙错误(持续更新)

    linux 中的奇妙错误 1/无名管道 当打开一个管道之后,将管道的读端关闭,然后在向其中写入内容,进程会被信号S...

  • 14.进程间通信:管道

    1. 管道,有名管道和无名管道。 1.1 无名管道主要用于父子进程或者兄弟关系的进程间的通信。通过pipe创建无名...

  • 管道---无名管道

    无名管道的特点: 1:无名管道只能用于具有亲缘关系的进程之间的通信(这里理解为,二叉树中只有具有同一...

  • 58三面(5.17)

    25分钟。面试官很好地提前约了时间。 自我介绍 Linux常用命令 进程通信方式 比较有名管道和无名管道。 比较J...

  • 无名管道

    无名管道是父子进程通信的手段,没有关系的进程是不能使用无名管道的 int fd[2];if (pipe(fd) =...

  • Linux系统调用pipe(无名/匿名管道)

    执行结果:

网友评论

      本文标题:linux无名管道

      本文链接:https://www.haomeiwen.com/subject/lkpspftx.html