양방향 파이프를 사용하려면 무조건 파이프를 두번 생성해야 하나요?

lumium의 이미지

pipe() 함수가 읽기 및 쓰기 파이프를 열고 파일 서술자를 기록해준다는 것은 알고 있습니다. 그러나 책에서는 파이프는 단방향으로만 사용 가능하니 반드시 사용하지 않는 쪽은 파일을 닫아주라고 되어있습니다. 그래서 양방향 통신이 필요하다면 파이프를 두개 생성하라고 합니다. 제가 실행해본 결과로는 하나만 생성해도 양방향이 가능한 것으로 보이나 매뉴얼에서는 단방향으로 생성된다고 나와있네요. 아마 제가 사용한 코드는 이론상 읽고 쓰는 파일 두개를 열기 때문에 우연히 작동은 했을지도 모르겠습니다. https://man7.org/linux/man-pages/man2/pipe.2.html

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/wait.h>
 
int main() {
        int fds[2];
        int stat;
        pid_t pid;
        char buf[BUFSIZ];
 
        if (pipe(fds)) {
                perror("pipe");
                return -1;
        }
 
        switch (pid = fork()) {
                case -1:
                        perror("fork");
                        return -1;
                case 0:
                        ppid = getppid();
                        read(fds[0], buf, BUFSIZ);
                        puts(buf);
                        write(fds[1], "World\n", strlen("World\n")+1);
                        break;
                default:
                        write(fds[1], "Hello\n", strlen("Hello\n")+1);
                        sleep(1);
                        read(fds[0], buf, BUFSIZ);
                        puts(buf);
                        waitpid(pid, &stat, 0);
                        break;
        }
        return 0;
}

익명 사용자의 이미지

여전히 단방향 파이프입니다.

두 프로세스가 보내는 쪽과 받는 쪽 모두를 가진 상태에서 번갈아 가면서 주고 받았을 뿐이지요.

숫제 반이중 통신(Half Duplex)을 연상케 하는군요. 무전기 사용해 보셨나요?

===

당연히 이런 형태의 통신은 타이밍에 매우 취약합니다.

sleep(1);으로 대충 막아놓긴 했습니다만, 부모 프로세스가 자신이 쓴 "Hello\n"를 자기가 다시 읽지 않으리라는 보장은 어디에도 없어요.

시스템이 너무 바빠서 그 1초 사이에 자식 프로세스가 실행되지 못하고, 그 뒤에 다시 부모 프로세스가 실행되면 어쩌죠?

lumium의 이미지

pipe 함수로 얻은 두개의 파일 서술자는 둘다 같은 파일을 가리키고 값만 다른것으로 이해하면 될까요? 만약 시스템이 너무 바빠서 자식 프로세스가 실행되지 못하고 부모 프로세스가 다시 시작하면 자식 프로세스에서 보내는 데이터가 들어있는 파일을 읽는 것이 아니라 자신이 데이터를 전송했던 파일을 그대로 다시 읽는 것이군요.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.