Skip to content

Latest commit

 

History

History
117 lines (86 loc) · 3.77 KB

47.md

File metadata and controls

117 lines (86 loc) · 3.77 KB

管道,第 1 部分:管道介绍

原文:https://github.com/angrave/SystemProgramming/wiki/Pipes%2C-Part-1%3A-Introduction-to-pipes

什么是 IPC?

进程间通信是一个进程与另一个进程通信的任何方式。你已经看过这种虚拟内存的一种形式了!一段虚拟内存可以在父母和孩子之间共享,从而实现通信。您可能希望将该内存包装在pthread_mutexattr_setpshared(&attrmutex, PTHREAD_PROCESS_SHARED);互斥锁(或进程宽互斥锁)中以防止竞争条件。

有更多标准的 IPC 方式,比如管道!考虑您是否在终端中键入以下内容

$ ls -1 | cut -d'.' -f1 | uniq | sort | tee dir_contents

以下代码的作用是什么(如果你愿意,你可以跳过这个并不重要)?那么它ls是当前目录(-1 表示它每行输出一个条目)。然后cut命令在第一个周期之前获取所有内容。 Uniq 确保所有行都是 uniq,排序将它们和 tee 输出排序到文件。

重要的是,bash 创建 5 个独立的进程,并将它们的标准出口/标准连接到管道,其中的跟踪看起来像这样。

(0)ls(1)------>(0)cut(1)------->(0)uniq(1)------>(0)排序(1)------>(0)tee(1)

管道中的数字是每个进程的文件描述符,箭头表示重定向或管道输出的位置。

什么是管道?

POSIX 管道几乎就像它的真正对应物 - 你可以在一端填充字节,它们将以相同的顺序出现在另一端。然而,与真实管道不同,进程始终在同一方向,一个文件描述符用于读取,另一个文件描述符用于写入。 pipe系统调用用于创建管道。

int filedes[2];
pipe (filedes);
printf("read from %d, write to %d\n", filedes[0], filedes[1]);

这些文件描述符可以与read一起使用 -

// To read...
char buffer[80];
int bytesread = read(filedes[0], buffer, sizeof(buffer));

write -

write(filedes[1], "Go!", 4);

如何使用管道与子进程通信?

使用管道的常用方法是在分叉之前创建管道。

int filedes[2];
pipe (filedes);
pid_t child = fork();
if (child > 0) { /* I must be the parent */
    char buffer[80];
    int bytesread = read(filedes[0], buffer, sizeof(buffer));
    // do something with the bytes read 
}

然后,孩子可以将消息发送回父母:

if (child == 0) {
   write(filedes[1], "done", 4);
}

我可以在一个过程中使用管道吗?

简短回答:是的,但我不确定你为什么要大声笑!

这是一个向自己发送消息的示例程序:

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

int main() {
    int fh[2];
    pipe(fh);
    FILE *reader = fdopen(fh[0], "r");
    FILE *writer = fdopen(fh[1], "w");
    // Hurrah now I can use printf rather than using low-level read() write()
    printf("Writing...\n");
    fprintf(writer,"%d %d %d\n", 10, 20, 30);
    fflush(writer);

    printf("Reading...\n");
    int results[3];
    int ok = fscanf(reader,"%d %d %d", results, results + 1, results + 2);
    printf("%d values parsed: %d %d %d\n", ok, results[0], results[1], results[2]);

    return 0;
}

以这种方式使用管道的问题是写入管道可能阻塞,即管道仅具有有限的缓冲容量。如果管道已满,写入过程将阻止!缓冲区的最大大小取决于系统;典型值从 4KB 到 128KB。

int main() {
    int fh[2];
    pipe(fh);
    int b = 0;
    #define MESG "..............................."
    while(1) {
        printf("%d\n",b);
        write(fh[1], MESG, sizeof(MESG))
        b+=sizeof(MESG);
    }
    return 0;
}

参见管道,第 2 部分:管道编程秘密