linux pipe 管道简单例子

本文通过一个简单的C语言程序示例介绍了Linux系统中进程间通信的一种方式:管道。该示例展示了父进程如何通过管道向子进程发送字符,并且子进程如何从管道中读取这些字符。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

linux pipe 管道简单例子

//
// Created by liyihang on 17-9-25.
//

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

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


    int pipe_fd[2];
    pid_t  child_pid;
    char pipe_buf;

    memset(pipe_fd, 0, sizeof(int)*2);

    if (pipe(pipe_fd)==-1){
        printf("open err\n");
        return -1;
    }

    child_pid=fork();
    if (child_pid==-1)
    {
        printf("thead err\n");
    }

    if (child_pid==0)
    {
        close(pipe_fd[1]);
        printf("while start\n");
        while (read(pipe_fd[0], &pipe_buf, 1)>0)
        {
//            write(STDOUT_FILENO, &pipe_buf, 1);
            printf("read content outs:%s\n", &pipe_buf);
        }
        close(pipe_fd[0]);
        printf("child thead ok\n");
        return 0;
    } else{
        close(pipe_fd[0]);
        write(pipe_fd[1], "h", 1);
        close(pipe_fd[1]);
        printf("father thaed ok\n");
//        wait(NULL);
        wait(&child_pid);
        printf("wait thread \n");
        return 0;
    }


}
### LinuxPipe 管道的工作原理与使用方法 #### 工作原理 Pipe 是一种单向的数据通道,主要用于进程间的通信 (Inter-Process Communication, IPC)[^2]。在 Linux 系统中,管道是一种简单的 IPC 机制,允许具有亲缘关系的进程(通常是父子进程或兄弟进程)之间交换数据[^3]。 当调用 `pipe()` 函数时,系统会创建一对文件描述符:`pipefd[0]` 和 `pipefd[1]`。其中: - `pipefd[0]` 表示管道的读端; - `pipefd[1]` 表示管道的写端。 任何写入到 `pipefd[1]` 的数据都可以从 `pipefd[0]` 被读取出来。这种设计使得管道成为了一种半双工通信工具——同一时间只能在一个方向上传输数据[^4]。 此外,匿名管道仅限于在同一台机器上的相关进程中使用,因为它不支持跨主机通信,也不具备持久化特性[^5]。 --- #### 使用方法 以下是关于如何在 C 或其他兼容的语言中使用 `pipe()` 创建并操作管道的具体说明: 1. **创建管道** 需要先定义一个整型数组来存储两个文件描述符,然后调用 `pipe()` 函数初始化它们。 ```c int pipefd[2]; if (pipe(pipefd) == -1) { perror("pipe"); exit(EXIT_FAILURE); } ``` 2. **派生子进程** 利用 `fork()` 创建一个新的子进程。这样可以确保父进程和子进程都能访问同一个管道实例。 ```c pid_t pid = fork(); if (pid == -1) { perror("fork"); close(pipefd[0]); close(pipefd[1]); exit(EXIT_FAILURE); } ``` 3. **关闭不必要的文件描述符** 在父进程和子进程中分别保留所需的文件描述符,并关闭不需要的那一部分以节省资源。 如果当前是父进程,则应关闭写端;如果是子进程,则需关闭读端。 ```c if (pid > 0) { // Parent process close(pipefd[1]); // Close write end char buffer[BUFSIZ]; ssize_t bytes_read; while ((bytes_read = read(pipefd[0], buffer, BUFSIZ)) > 0) { printf("Parent received: %.*s\n", (int)bytes_read, buffer); } close(pipefd[0]); } else { // Child process close(pipefd[0]); // Close read end const char* message = "Hello from child!"; write(pipefd[1], message, strlen(message)); close(pipefd[1]); } ``` 4. **清理资源** 完成数据传输后,记得释放所有打开的文件描述符以防泄漏。 --- #### 示例代码 下面是一个完整的例子展示如何利用管道完成基本的父子进程间消息传递功能。 ```c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #define BUFFER_SIZE 100 int main(void) { int pipefd[2]; if (pipe(pipefd) == -1) { perror("Failed to create pipe."); return EXIT_FAILURE; } pid_t pid = fork(); if (pid == -1) { perror("Fork failed."); return EXIT_FAILURE; } if (pid > 0) { // Parent Process close(pipefd[1]); char buffer[BUFFER_SIZE]; ssize_t bytesRead; bytesRead = read(pipefd[0], buffer, sizeof(buffer)); if (bytesRead != -1) { buffer[bytesRead] = '\0'; printf("Message from child: %s\n", buffer); } else { perror("Error reading from pipe."); } close(pipefd[0]); } else { // Child Process close(pipefd[0]); const char *message = "Greetings from the other side!"; write(pipefd[1], message, strlen(message)); close(pipefd[1]); } return EXIT_SUCCESS; } ``` 此程序展示了如何设置管道以及让父进程接收来自子进程的消息[^1]。 --- 相关问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值