首页 > php教程 > PHP开发 > 正文

Linux C编程 - 管道pipe

高洛峰
发布: 2016-12-13 11:37:19
原创
1677人浏览过

在linux中,管道也是一种文件,只不过比较特殊,我们可以用pipe函数创建一个管道,其原型声明如下:

#inlcude <unistd.h>

int pipe(int fields[2]);

其实它相当于一个通信缓冲区,fields[0]用来读,fields[1]用来写。下面的例子中,创建一个管道作为通信缓冲区,父进程创建了一个子进程,子进程通过管道的fields[1]描述符想管道中写入一个字符串,而父进程则利用管道的fields[0] 从管道中读取这个字串并显示出来:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>

#define BUF_SIZ    255     // message buffer size

int main(int argc, char **argv)
{
    char buffer[BUF_SIZ + 1];
    int fd[2];

// receive a string as parameter
    if ( argc != 2)
    {
        fprintf(stderr, "Usage: %s string\n\a", argv[0]);
        exit(1);
    }

// create pipe for communication
    if ( pipe(fd) != 0 )
    {
        fprintf(stderr, "Create pipe error: %s\n\a", strerror(errno));
        exit(1);
    }

    if ( fork() == 0 )     // in child process write msg to pipe
    {
        close(fd[0]);
        printf("Child %ld write to pipe\n\a", getpid()); 
        snprintf(buffer, BUF_SIZ, "%s", argv[1]);
        write(fd[1], buffer, strlen(buffer));
        printf("Child %ld quit.\n\a", getpid());
    }
    else     // in parent process, read msg from pipe
    {
        close(fd[1]);
        printf("Parent %ld read from pipe\n\a", getpid());
        memset(buffer, '

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>

#define BUF_SIZ    255     // message buffer size

int main(int argc, char **argv)
{
    char buffer[BUF_SIZ + 1];
    int fd[2];

// receive a string as parameter
    if ( argc != 2)
    {
        fprintf(stderr, "Usage: %s string\n\a", argv[0]);
        exit(1);
    }

// create pipe for communication
    if ( pipe(fd) != 0 )
    {
        fprintf(stderr, "Create pipe error: %s\n\a", strerror(errno));
        exit(1);
    }

    if ( fork() == 0 )     // in child process write msg to pipe
    {
        close(fd[0]);
        printf("Child %ld write to pipe\n\a", getpid()); 
        snprintf(buffer, BUF_SIZ, "%s", argv[1]);
        write(fd[1], buffer, strlen(buffer));
        printf("Child %ld quit.\n\a", getpid());
    }
    else     // in parent process, read msg from pipe
    {
        close(fd[1]);
        printf("Parent %ld read from pipe\n\a", getpid());
        memset(buffer, '\0', BUF_SIZ + 1);
        read(fd[0], buffer, BUF_SIZ);
        printf("Parent %ld read : \n%s\n", getpid(), buffer);
        exit(1);
    }
    return 0;
}

', BUF_SIZ + 1);
        read(fd[0], buffer, BUF_SIZ);
        printf("Parent %ld read : \n%s\n", getpid(), buffer);
        exit(1);
    }
    return 0;
}

豆包AI编程
豆包AI编程

豆包推出的AI编程助手

豆包AI编程 483
查看详情 豆包AI编程
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号