pipe管道

pipe()学习

例1: 进程内读写管道,代码如下:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <signal.h>

int main(int argc, char *argv[])
{
    int fd[2];

    pipe(fd);
    char s[100] = "hello pipe....";

    while(1)
    {
        write(fd[1],s,100);
        bzero(s,100);
        read(fd[0],s,100);
        puts(s);
        sleep(1);
    }

    return 0;
}

编译/链接执行后的结果如下:

例2: 进程间读写管道,代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <signal.h>
void child_rw_pipe(int rfd,int wfd)
{
    char msg[] = "from child process!!!\n";
    write(wfd,msg,sizeof(msg));

    char msg2[100];
    read(rfd,msg2,100);
    printf("child read from pipe:  %s\n",msg2);
}

void parent_rw_pipe(int rfd,int wfd)
{
    char msg[] = "from parent process!!!\n";
    write(wfd,msg,sizeof(msg));

    char msg2[100];
    read(rfd,msg2,100);
    printf("parent read from pipe:  %s\n",msg2);
}

int main(int argc, char *argv[])
{
    int fd1[2];
    int fd2[2];

    pipe(fd1);
    pipe(fd2);

    pid_t pid = fork();
    if(pid == 0)
    {
        close(fd1[1]);
        close(fd2[0]);
        child_rw_pipe(fd1[0],fd2[1]);
        exit(0);
    }

    close(fd1[0]);
    close(fd2[1]);
    parent_rw_pipe(fd2[0],fd1[1]);
    wait(NULL);

    return 0;
}

父子进程分别向管道写数据, 从管道读数据. 编译/链接/执行,结果如下:

例3: 父子进程间读写管道:

父进程将命令行数据写入管道, 代码如下:

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <termios.h>
#include <signal.h>
int main(int argc,char *argv[])
{
    int fd[2];
    pid_t pid;

    pipe(fd);
    pid = fork();
    if(pid == 0)
    {
        close(fd[1]);
        close(0);
        dup(fd[0]);

        execl("process","process",NULL);
        exit(0);
    }

    close(fd[0]);
    write(fd[1],argv[1],strlen(argv[1]));
    wait(NULL);

    return 0;
}

子进程读出管道内容(前面代码dup(fd[0])将文件描述符STDIN_FILENO重定向到管道的读端), 代码如下:

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <termios.h>
#include <signal.h>
int main(int argc,char *argv[])
{
    while(1)
    {
        char buf[100];
        int ret = read(STDIN_FILENO,buf,100);
        if(ret > 0)
        {
            buf[ret] = 0;
            printf("process receive:  %s\n",buf);
            if(strcmp(buf,"exit") == 0)
                exit(0);
            if(strcmp(buf,"getpid") == 0)
            {
                printf("child pid = %d\n",getpid());
                exit(0);
            }
        }
    }

    return 0;
}

编译/链接/执行, 结果如下:

时间: 2024-12-14 04:14:03

pipe管道的相关文章

redis pipe管道

redis命令在从提交到返回处理结果的过程中,消耗的时间我们称之为RTT(往返时间). 在需要批量执行redis 命令的场景下,如果命令单条逐个执行,那么总共花费的时间是命令条数 N * RTT. redis 提供了管道技术来提高批量执行效率,即将多个命令打包发送给redis服务端,所有命令执行完后,再将所有结果打包返回. 在所有命令执行结束前,redis服务器会缓存已执行结束的结果. 在redis-cli命令行中, 使用redis管道技术时,我们通常将待执行的命令放到一个文本里,比如comma

pipe()管道最基本的IPC机制

<h4>进程间通信 fork pipe pie_t 等用法(管道机制 通信)</h4>每个进程各自有不同的用户地址空间,任何一个进程的全局变量在另一个进程中都看不到,所以进程之间要交换数据必须通过内核,在内核中开辟一块缓冲 区,进程1把数据从用户空间拷到内核缓冲区,进程2再从内核缓冲区把数据读走,内核提供的这种机制称为进程间通信(IPC,InterProcess Communication).如下图所示. <a href="http://www.emacsvi.co

fork产生子进程利用pipe管道通信

http://siqun.blog.163.com/blog/static/213496001201341231121720/ 转载链接:http://hi.baidu.com/hj11yc/item/9a2ea30cca773077bfe97efc注:加了一点内容 进程间通信 fork pipe pie_t 等用法(管道机制 通信) 每个进程各自有不同的用户地址空间,任 何一个进程的全局变量在另一个进程中都看不到,所以进程之间要交换数据必须通过内核,在内核中开辟一块缓冲 区,进程1把数据从用户

python线程,pipe管道通信原理

Pipe管道: * 管道实例化后会产生两个通道,分别交给两个进程* 通过send和recv来交互数据,这是一个双向的管道,child和parent可以互相收发 from multiprocessing import Process, Pipe def f(conn): conn.send([42, None, 'hello']) conn.send([43,32]) print(conn.recv()) conn.close() if __name__ == '__main__': #Pipe实

Python multiprocessing模块中的Pipe管道

multiprocessing.Pipe([duplex]) 返回2个连接对象(conn1, conn2),代表管道的两端,默认是双向通信.如果duplex=False,conn1只能用来接收消息,conn2只能用来发送消息.不同于os.open之处在于os.pipe()返回2个文件描述符(r, w),表示可读的和可写的 实例如下: #!/usr/bin/python #coding=utf-8 import os from multiprocessing import Process, Pip

关于pipe管道的读写端关闭问题

知识概述 通过pipe在内核中创建一个文件,然后可以实现两个进程通信 管道是一种最基本的IPC机制,由 pipe 函数创建: 1 #include <unistd.h> 2 int pipe(int filedes[2]); 调用 pipe 函数时在内核中开辟一块缓冲区(称为管道)用于通信,它有一个读端一个写端,然后通过 filedes 参数传出给用户程序两个文件描述符, filedes[0] 指向管道的读端, filedes[1] 指向管道的写端(很好记,就像0是标准输入1是标准输出一样).

python中multiprocessing模块之Pipe管道

multiprocessing.Pipe([duplex]) 返回2个连接对象(conn1, conn2),代表管道的两端,默认是双向通信.如果duplex=False,conn1只能用来接收消息,conn2只能用来发送消息.不同于os.open之处在于os.pipe()返回2个文件描述符(r, w),表示可读的和可写的 实例如下: #!/usr/bin/python #coding=utf-8 import os from multiprocessing import Process, Pip

angular6之pipe管道

作为前端开发人员,我们在网站开发时,需要读取后端的接口进行视图层的数据展示.我们经常会遇到接口给予我们的值不是最终展现的数据,例如:后端返回的金额是number类型额数据,我们需要遇到千分位用逗号隔开(10000 -> 10,000),重量10000g转成10kg等,这些需要前端把数据进行简单的转换成对用户友好的格式.在angular6中pipe便发挥这样的作用,熟悉vue的同学会感觉非常类似vue中的computed计算属性. 下面介绍angular6中pipe的具体用法 1.ng g pip

pipe管道——进程通信