Do You Coding?

[function] pipe ํ•จ์ˆ˜ ์•Œ์•„๋ณด๊ธฐ ๋ณธ๋ฌธ

CS & Engineering/C

[function] pipe ํ•จ์ˆ˜ ์•Œ์•„๋ณด๊ธฐ

๐Ÿ“Œ ๋งค๋‰ด์–ผ (Linux)

๋”๋ณด๊ธฐ

NAME
       pipe - create pipe

: ํŒŒ์ดํ”„ ๋งŒ๋“ค๊ธฐ


SYNOPSIS
       #include <unistd.h>
       int pipe(int pipefd[2]);

DESCRIPTION
       pipe()  creates a pipe, a unidirectional data channel that can be used for interprocess commuโ€
       nication.  The array pipefd is used to return two file descriptors referring to  the  ends  of
       the pipe.  pipefd[0] refers to the read end of the pipe.  pipefd[1] refers to the write end of
       the pipe.  Data written to the write end of the pipe is buffered by the  kernel  until  it  is
       read from the read end of the pipe.  For further details, see pipe(7).

       If  flags  is 0, then pipe2() is the same as pipe().  The following values can be bitwise ORed
       in flags to obtain different behavior:

       O_CLOEXEC
              Set the close-on-exec (FD_CLOEXEC) flag on the two new file descriptors.  See  the  deโ€
              scription of the same flag in open(2) for reasons why this may be useful.

       O_DIRECT (since Linux 3.4)
              Create  a  pipe that performs I/O in "packet" mode.  Each write(2) to the pipe is dealt
              with as a separate packet, and read(2)s from the pipe will read one packet at  a  time.
              Note the following points:

              *  Writes  of  greater  than  PIPE_BUF  bytes (see pipe(7)) will be split into multiple
                 packets.  The constant PIPE_BUF is defined in <limits.h>.

              *  If a read(2) specifies a buffer size that is smaller than the next packet, then  the
                 requested  number  of  bytes  are  read, and the excess bytes in the packet are disโ€
                 carded.  Specifying a buffer size of PIPE_BUF will be sufficient to read the largest
                 possible packets (see the previous point).

 

              *  Zero-length  packets  are not supported.  (A read(2) that specifies a buffer size of
                 zero is a no-op, and returns 0.)

              Older kernels that do not support this flag will indicate this via an EINVAL error.

              Since Linux 4.5, it is possible to change the O_DIRECT setting of a pipe file  descripโ€
              tor using fcntl(2).

       O_NONBLOCK
              Set  the  O_NONBLOCK  file status flag on the open file descriptions referred to by the
              new file descriptors.  Using this flag saves extra calls to  fcntl(2)  to  achieve  the
              same result.
: pipe()๋Š” ํ”„๋กœ์„ธ์Šค ๊ฐ„ ํ†ต์‹ ์— ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋Š” ๋‹จ๋ฐฉํ–ฅ ๋ฐ์ดํ„ฐ ์ฑ„๋„์ธ ํŒŒ์ดํ”„๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

๋ฐฐ์—ด pipefd๋Š” ํŒŒ์ดํ”„์˜ ๋์„ ์ฐธ์กฐํ•˜๋Š” ๋‘ ๊ฐœ์˜ ํŒŒ์ผ ๋””์Šคํฌ๋ฆฝํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

pipefd[0]์€ ํŒŒ์ดํ”„์˜ ์ฝ๊ธฐ ๋์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. pipefd[1]์€ ํŒŒ์ดํ”„์˜ ์“ฐ๊ธฐ ๋์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

ํŒŒ์ดํ”„์˜ ์“ฐ๊ธฐ ๋์— ์“ฐ์—ฌ์ง„ ๋ฐ์ดํ„ฐ๋Š” ํŒŒ์ดํ”„์˜ ์ฝ๊ธฐ ๋์—์„œ ์ฝ์„ ๋•Œ๊นŒ์ง€ ์ปค๋„์— ์˜ํ•ด ๋ฒ„ํผ๋ง๋ฉ๋‹ˆ๋‹ค.

์ž์„ธํ•œ ๋‚ด์šฉ์€ ํŒŒ์ดํ”„(7)๋ฅผ ์ฐธ์กฐํ•˜์‹ญ์‹œ์˜ค.

 

ํ”Œ๋ž˜๊ทธ๊ฐ€ 0์ด๋ฉด pipe2()๋Š” pipe()์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ ๊ฐ’์€ ํ”Œ๋ž˜๊ทธ์—์„œ ๋น„ํŠธ ๋‹จ์œ„๋กœ ORed๋˜์–ด ์„œ๋กœ ๋‹ค๋ฅธ ๋™์ž‘์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

O_CLOEXEC
: ๋‘ ๊ฐœ์˜ ์ƒˆ ํŒŒ์ผ ๋””์Šคํฌ๋ฆฝํ„ฐ์— FD_CLOEXEC(Close on exec) ํ”Œ๋ž˜๊ทธ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

์ด๊ฒƒ์ด ์œ ์šฉํ•œ ์ด์œ ๋Š” open(2)์˜ ๋™์ผํ•œ ํ”Œ๋ž˜๊ทธ์— ๋Œ€ํ•œ ์„ค๋ช…์„ ์ฐธ์กฐํ•˜์‹ญ์‹œ์˜ค.

O_DIRECT(Linux 3.4 ์ดํ›„)

: "ํŒจํ‚ท" ๋ชจ๋“œ์—์„œ I/O๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ํŒŒ์ดํ”„๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ํŒŒ์ดํ”„์— ๋Œ€ํ•œ ๊ฐ ์“ฐ๊ธฐ(2)๋Š” ๋ณ„๋„์˜ ํŒจํ‚ท์œผ๋กœ

์ฒ˜๋ฆฌ๋˜๋ฉฐ, ํŒŒ์ดํ”„์˜ ์ฝ๊ธฐ(2)๋Š” ํ•œ ๋ฒˆ์— ํ•˜๋‚˜์˜ ํŒจํ‚ท์„ ์ฝ์Šต๋‹ˆ๋‹ค.

 

๋‹ค์Œ ์‚ฌํ•ญ์— ์ฃผ์˜ํ•˜์‹ญ์‹œ์˜ค:
*  PIPE_BUF ๋ฐ”์ดํŠธ๋ณด๋‹ค ํฐ ์“ฐ๊ธฐ(PIPE(7))๋Š” ์—ฌ๋Ÿฌ ํŒจํ‚ท์œผ๋กœ ๋ถ„ํ• ๋ฉ๋‹ˆ๋‹ค.

์ƒ์ˆ˜ PIPE_BUF๋Š” <limits.h>์— ์ •์˜๋ฉ๋‹ˆ๋‹ค.
*  read(2)๊ฐ€ ๋‹ค์Œ ํŒจํ‚ท๋ณด๋‹ค ์ž‘์€ ๋ฒ„ํผ ํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•˜๋ฉด ์š”์ฒญ๋œ ๋ฐ”์ดํŠธ ์ˆ˜๊ฐ€ ์ฝํ˜€์ง€๊ณ 

ํŒจํ‚ท์˜ ์ดˆ๊ณผ ๋ฐ”์ดํŠธ๋Š” ๋ฒ„๋ ค์ง‘๋‹ˆ๋‹ค. PIPE_BUF์˜ ๋ฒ„ํผ ํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•˜๋ฉด ๊ฐ€๋Šฅํ•œ

๊ฐ€์žฅ ํฐ ํŒจํ‚ท์„ ์ฝ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค(์•ž์˜ ์„ค๋ช… ์ฐธ์กฐ).

*  ๊ธธ์ด๊ฐ€ 0์ธ ํŒจํ‚ท์€ ์ง€์›๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. (๋ฒ„ํผ ํฌ๊ธฐ๋ฅผ 0์œผ๋กœ ์ง€์ •ํ•˜๋Š” ์ฝ๊ธฐ(2)๋Š” no-op์ด๊ณ  0์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.)

์ด ํ”Œ๋ž˜๊ทธ๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š๋Š” ์˜ค๋ž˜๋œ ์ปค๋„์€ EINVAL ์˜ค๋ฅ˜๋ฅผ ํ†ตํ•ด ์ด ํ”Œ๋ž˜๊ทธ๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
Linux 4.5 ์ดํ›„ fcntl(2)์„ ์‚ฌ์šฉํ•˜์—ฌ ํŒŒ์ดํ”„ ํŒŒ์ผ ๋””์Šคํฌ๋ฆฝํ„ฐ์˜ O_DIRECT ์„ค์ •์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

O_NONBLOCK
: ์ƒˆ ํŒŒ์ผ ๋””์Šคํฌ๋ฆฝํ„ฐ๊ฐ€ ์ฐธ์กฐํ•˜๋Š” ์—ด๋ฆฐ ํŒŒ์ผ ์„ค๋ช…์— O_NONBLOCK ํŒŒ์ผ ์ƒํƒœ ํ”Œ๋ž˜๊ทธ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

์ด ํ”Œ๋ž˜๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด fcntl(2)์— ์ถ”๊ฐ€ ํ˜ธ์ถœ์ด ์ €์žฅ๋˜์–ด ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


RETURN VALUE
       On  success,  zero  is  returned.   On  error, -1 is returned, errno is set appropriately, and
       pipefd is left unchanged.

       On Linux (and other systems), pipe() does not modify pipefd on failure.  A  requirement  stanโ€
       dardizing this behavior was added in POSIX.1-2008 TC2.

: ์„ฑ๊ณต ์‹œ 0์ด ๋ฐ˜ํ™˜๋˜๊ณ  ์˜ค๋ฅ˜ ์‹œ -1์ด ๋ฐ˜ํ™˜๋˜๊ณ  errno๊ฐ€ ์ ์ ˆํ•˜๊ฒŒ ์„ค์ •๋˜๋ฉฐ

pipefd๊ฐ€ ๋ณ€๊ฒฝ๋˜์ง€ ์•Š์€ ์ƒํƒœ๋กœ ์œ ์ง€๋ฉ๋‹ˆ๋‹ค.

Linux(๋ฐ ๊ธฐํƒ€ ์‹œ์Šคํ…œ)์—์„œ pipe()๋Š” pipefd on failure๋ฅผ ์ˆ˜์ •ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์ด ๋™์ž‘์„ ํ‘œ์ค€ํ™”ํ•˜๋Š” ์š”๊ตฌ ์‚ฌํ•ญ์ด POSIX.1-2008 TC2์— ์ถ”๊ฐ€๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ“Œ ํ•จ์ˆ˜ ์„ค๋ช…

pipe ํ•จ์ˆ˜๋Š” ํ”„๋กœ์„ธ์Šค ๊ฐ„ ํ†ต์‹ ์„ ํ• ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜์˜ ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ, ๋ชจ๋“  ์œ ๋‹‰์Šค ์‹œ์Šคํ…œ์ด ์ œ๊ณตํ•œ๋‹ค.

ํ”„๋กœ์„ธ์Šค ๊ฐ„ ํ†ต์‹ ์€ IPC(Inter Process Communication)์ด๋ผ ํ•˜๋ฉฐ, ์ปค๋„์ด ์ œ๊ณตํ•˜๋Š” IPC ์„ค๋น„๋ฅผ ํ†ตํ•ด ํ”„๋กœ์„ธ์Šค๋“ค์€

์„œ๋กœ ํ†ต์‹ ์„ ํ•˜๊ฒŒ ๋œ๋‹ค. ์ง€๊ธˆ ๋‹ค๋ฃจ๊ฒŒ ๋  ๊ฒƒ์€ ๊ฐ€์žฅ ์˜ค๋ž˜๋œ IPC์ธ pipe์ด๋‹ค.

 

[ ๊ธฐ๋ณธ ํŠน์ง• ]

- Uni-directional byte stream (๋‹จ๋ฐฉํ–ฅ) -> pipe๋Š” ๋‹จ๋ฐฉํ–ฅ์œผ๋กœ ๋™์ž‘ํ•œ๋‹ค.

- name or ID ์—†์Œ. ๊ทธ๋ฆฌํ•˜์—ฌ ๋‘ ํ”„๋กœ์„ธ์Šค ๊ฐ„์— ๊ด€๊ณ„๊ฐ€ ํ•ญ์ƒ ์žˆ์–ด์•ผ ํ•จ. ์ฆ‰, related process ๊ฐ„์— ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ๋ฐ,

  fork ๋ฅผ ํ†ตํ•ด ๋งŒ๋“ค์–ด์ง„ ๋ถ€๋ชจ-์ž์‹ ๊ด€๊ณ„์˜ ํ”„๋กœ์„ธ์Šค์ผ ๋•Œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค. (๋˜๋Š” ๋ถ€๋ชจ๋ฅผ ํ†ตํ•ด ๋งŒ๋“ค์–ด์ง„ ์ž์‹ ๊ฐ„ ํ†ต์‹ )

- ์œ„ ๊ทธ๋ฆผ์—์„œ์ฒ˜๋Ÿผ fd[1]์„ ํ†ตํ•ด pipe๊ฐ€ ์•ž์˜ ํ”„๋กœ์„ธ์Šค๋กœ๋ถ€ํ„ฐ write๋ฅผ ๋ฐ›๊ณ , fd[0]์„ ํ†ตํ•ด read ์‹œ์ผœ์ค€๋‹ค.

 

 

 

 

 

[ 1. pipe ์ƒ์„ฑ ์ดํ›„ fork ํ•œ ์ƒํ™ฉ ]

- ์œ„ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด pipe๋ฅผ ์ƒ์„ฑํ•œ ๋’ค, fork() ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๋ถ€๋ชจ-์ž์‹ ํ”„๋กœ์„ธ์Šค๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.

์—ฌ๊ธฐ์„œ fork ํ•จ์ˆ˜๋Š” ๊ธฐ์กด์— ํ”„๋กœ์„ธ์Šค๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋˜ ํŒŒ์ผ ๋””์Šคํฌ๋ฆฝํ„ฐ๋ฅผ ๋ณต์ œํ•ด์„œ ๊ทธ๋Œ€๋กœ ๊ณต์œ ํ•˜๊ฒŒ ๋˜๋Š”๋ฐ,

๊ธฐ์กด Parent๊ฐ€ write ํ•˜์—ฌ pipe์— fd[1]๋กœ ์ „๋‹ฌ๋˜๋˜ ๊ฒƒ์„ child๋„ ๋˜‘๊ฐ™์ด fd[1]๋กœ ์ „๋‹ฌํ•˜๋ฉฐ, read ๊ณผ์ •๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋‹ค.

 

ํ•˜์ง€๋งŒ, ์ด ์ƒํ™ฉ์—๋Š” ๋ฌธ์ œ์  ํ•œ๊ฐ€์ง€๊ฐ€ ์กด์žฌํ•˜๋Š”๋ฐ,

Parent๊ฐ€ Child ์—๊ฒŒ ์–ด๋– ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ž‘์„ฑํ•˜์—ฌ Child ๊ฐ€ ๋ฐ›๋Š” ๋™์ž‘์„ ์›ํ•  ๋•Œ,

Parent๊ฐ€ write ํ•˜๊ณ  Child๊ฐ€ read ํ•˜๋Š” ์ด์ƒ์ ์ธ ๋™์ž‘์„ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์œผ๋‚˜

fd๊ฐ€ ์ง€๊ธˆ ์ค‘๋ณต๋˜์–ด ์‚ฌ์šฉ๋˜๊ณ  ์žˆ์œผ๋ฏ€๋กœ, Parent๊ฐ€ writeํ•œ ๊ฒƒ์ด ๋‹ค์‹œ Parent๋กœ read ๋  ์ˆ˜ ์žˆ๋Š” ์ƒํƒœ์ด๋‹ค.

๊ทธ๋ ‡๋‹ค๋ฉด ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์–ด๋–ป๊ฒŒ ํ•˜๋ฉด ์ข‹์„๊นŒ?

 

 

 

 

 

 

[ 2. ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” fd๋“ค์„ ๋‹ซ๊ธฐ ]

Parent์™€ Child๋ฅผ ๋‹จ๋ฐฉํ–ฅ์œผ๋กœ ์ด์–ด์ฃผ๊ธฐ ์œ„ํ•ด์„œ๋Š” Parent์˜ read ๋ถ€๋ถ„๊ณผ, Child์˜ write ๋ถ€๋ถ„์„ ๋‹ซ์•„์ค€๋‹ค.

์ด๋ ‡๊ฒŒ ๋ณ€๊ฒฝ๋˜๋ฉด Parent๊ฐ€ writeํ•˜๋Š” ๋ฐ์ดํ„ฐ๋Š” Child๋งŒ read๋กœ ์ˆ˜์‹ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

 

์ด ๋ฐฉ๋ฒ•์œผ๋กœ fork๋ฅผ ์ด์šฉํ•ด pipe๋กœ Parent-Child๊ฐ„ ํ†ต์‹ ์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

 


์ด์ œ, pipe() ํ•จ์ˆ˜์˜ ์ธ์ž๋“ค์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ๋‹ค.

int pipe(int pipefd[2]); ๋ผ๋Š” ๋ชจ์–‘์ธ๋ฐ, ์ธ์ž๊ฐ€ ๋…ํŠนํ•˜๊ฒŒ 2๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด๋Š” int ๋ฐฐ์—ด์ด ๋œ๋‹ค.

์ด๋Š” '2๊ฐœ์˜ ํŒŒ์ผ ๋””์Šคํฌ๋ฆฝํ„ฐ๋ฅผ ์ €์žฅํ•  ๋ฐฐ์—ด'์ด๊ณ  ๊ฐ๊ฐ ์ฝ๊ณ  ์“ฐ๋Š” ์—ญํ• ์˜ ํŒŒ์ผ ๋””์Šคํฌ๋ฆฝํ„ฐ๊ฐ€ ๋œ๋‹ค.

์ด๋Š” ์œ„์˜ ๊ทธ๋ฆผ๋“ค์—์„œ ๋‚˜์˜จ fd[0], fd[1] ์ด๋ผ๋Š” ๊ฒƒ์„ ๊ฐ„์ ‘์ ์œผ๋กœ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 

pipefd[2] : ์ƒ์„ฑ๋  pipe fd[2]๋ฅผ ์ €์žฅํ•  ๋ฒ„ํผ

-> pipefd[0] = reader-side fd (read๋ฅผ ์‚ฌ์šฉํ•  fd ๋ถ€๋ถ„)

-> pipefd[1] = writer-side fd (write๋ฅผ ์‚ฌ์šฉํ•  fd ๋ถ€๋ถ„)


์ถ”๊ฐ€์ ์œผ๋กœ, pipe๋„ ํ•˜๋‚˜์˜ '๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด๋Š” ๋ฒ„ํผ ๊ณต๊ฐ„'์ด๋ฏ€๋กœ, ์ด ๊ณต๊ฐ„์˜ ํฌ๊ธฐ๋ฅผ ๋„˜๊ฒŒ ๋˜๋ฉด full ์ด ๋œ๋‹ค.

๊ทธ๋Ÿผ pipe๊ฐ€ full์ด ๋˜๋ฉด write ์‹œ๋„ ์‹œ ๋” ์ด์ƒ ๋„ฃ์ง€ ๋ชปํ•˜๋„๋ก blocking์ด ๋˜์–ด ๊ธฐ๋‹ค๋ฆฐ๋‹ค.

๋ฐ˜๋Œ€๋กœ, ์ด ๋ฒ„ํผ ๊ณต๊ฐ„์— ์•„๋ฌด๊ฒƒ๋„ ์—†์œผ๋ฉด read ์‹œ๋„ ์‹œ blocking ๋˜์–ด ๊ธฐ๋‹ค๋ฆฐ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ , ์ •ํ•ด์ง„ PIPE_BUF๋ผ๋Š” ํŠน์ • ์‚ฌ์ด์ฆˆ๊ฐ€ ์žˆ๋Š”๋ฐ (4KB),

write size๊ฐ€ ์ด PIPE_BUF๋ณด๋‹ค ์ž‘์œผ๋ฉด atomic, ์ฆ‰, ๋ฒ„ํผ์— ๋‹ด๊ฒจ ์ •์ƒ์ ์œผ๋กœ write ๋˜๋ฉฐ,

write size๊ฐ€ ์ด PIPE_BUF๋ณด๋‹ค ํฌ๋ฉด interleaved, ์ฆ‰, ๋ฒ„ํผ์— ๋‹ค ๋‹ด๊ธฐ์ง€ ๋ชปํ•˜๊ณ  ์ชผ๊ฐœ์ ธ์„œ write ๋  ์ˆ˜ ์žˆ๋‹ค.


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

#define MAX_BUF 1024

int main(){
    int fd[2];
    pid_t pid;
    char buf[MAX_BUF];

    if(pipe(fd) < 0){
        printf("pipe error\n");
        exit(1);
    }
    if((pid=fork()) < 0){
        printf("fork error\n");
        exit(1);
    }
    if(pid > 0){
        close(fd[0]);
        strcpy(buf, "hello from parent");
        write(fd[1], buf, strlen(buf));
    }
    else {
        close(fd[1]);
        read(fd[0], buf, MAX_BUF);
        printf("child received message : %s\n", buf);
    }
    return (0);
}

 

์ฝ”๋“œ๋ฅผ ํ†ตํ•ด ๊ฐ€๋ณ๊ฒŒ pipe์˜ ๋™์ž‘์„ ๋”ฐ๋ผ๊ฐ€๋ณด๋„๋ก ํ•˜๊ฒ ๋‹ค.

pipe ํ•จ์ˆ˜๋Š” ์„ฑ๊ณตํ•˜๋ฉด 0์„ ๋ฐ˜ํ™˜ํ•˜๊ณ , ์‹คํŒจํ•˜๋ฉด -1์„ ๋ฐ˜ํ™˜ํ•˜๋ฏ€๋กœ, if๋ฌธ์œผ๋กœ pipe์˜ error๋ฅผ ์žก์•„์ค€๋‹ค.

๋‹ค์Œ์œผ๋กœ, fork๋ฅผ ํ†ตํ•ด pid์— ๋„ฃ์–ด์ฃผ๋Š” ๊ณผ์ •์„ ๋”ฐ๋ฅด๋Š”๋ฐ, parent process๋Š” child process์˜ pid๋ฅผ,

child process๋Š” 0์„ pid์— ๋„ฃ๊ฒŒ ๋œ๋‹ค. fork๊ฐ€ ์‹คํŒจํ•  ๊ฒฝ์šฐ -1์„ ๋ฐ˜ํ™˜ํ•˜๋ฏ€๋กœ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ if๋ฌธ์œผ๋กœ fork error๋ฅผ ์žก์•„์ค€๋‹ค.

 

๊ทธ๋ฆฌ๊ณ , fork๋ฅผ ํ–ˆ์œผ๋ฏ€๋กœ pid๊ฐ€ ๊ฐ๊ฐ ๋‹ค๋ฅธ parent process, child process๊ฐ€ ์ƒ๊ธฐ๊ณ , ํ•˜์œ„ ์ฝ”๋“œ๋ฅผ ๊ฐ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์‹คํ–‰ํ•œ๋‹ค.

์ด ์ƒํ™ฉ์—์„œ๋Š” parent์˜ fd๋ฅผ ๊ทธ๋Œ€๋กœ child๊ฐ€ ๋ฐ›์•„ ๊ฐ™์€ read, write fd๋ฅผ ๊ฐ€์ง€๊ฒŒ ๋œ๋‹ค.

 

์šฐ์„ , pid > 0 ์ด if๋ฌธ์˜ ์กฐ๊ฑด์ธ๋ฐ ์ด๋Š” parent process๊ฐ€ ์‹คํ–‰ํ•  ์ฝ”๋“œ๊ฐ€ ๋˜๊ณ ,

else ๋ถ€๋ถ„์ด child process์˜ ์‹คํ–‰ ์ฝ”๋“œ๊ฐ€ ๋  ๊ฒƒ์ด๋‹ค.

 

parent process๋Š” read ๋ถ€๋ถ„์„ ๋‹ซ์•„์ฃผ์–ด writeํ•  ๋‚ด์šฉ์ด child์—๊ฒŒ๋งŒ ๊ฐ€๋„๋ก ํ•ด์ค€๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋ฌธ์ž์—ด ํ•˜๋‚˜๋ฅผ ๋งŒ๋“ค์–ด, buf์— ๋‹ด๊ณ  ์ด๋ฅผ fd[1]๋กœ write ํ•ด์ฃผ๋ฉด, child์—์„œ๋Š” read ๊ณผ์ •์œผ๋กœ fd[0]์—์„œ ๋ฐ›์œผ๋ฉด

ํ•ด๋‹น ๋ฌธ์ž์—ด์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

 

child process๋กœ ๊ฐ€๋ฉด, write ๋ถ€๋ถ„์„ ๋‹ซ์•„์ค€๋‹ค. ๊ทธ๋ฆฌ๊ณ , readํ•จ์ˆ˜๋กœ fd[0]์œผ๋กœ ์ฝ์–ด์˜ค๋ฉด pipe๋ฅผ ํ†ตํ•ด

parent process๋กœ๋ถ€ํ„ฐ ์ž…๋ ฅ๋ฐ›์€ ๋‚ด์šฉ์ด ๋‹ด๊ธฐ๊ฒŒ ๋˜๊ณ , buf๋ฅผ prinf๋กœ ์ถœ๋ ฅํ•˜๋ฉด ๊ทธ ๋‚ด์šฉ์ด ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค.

 

์ถœ๋ ฅ - std_output)

child received message : hello from parent

 


 

์ถ”๊ฐ€์ ์œผ๋กœ, parent ์™€ child๊ฐ€ ๋‘˜ ๋‹ค ์ฝ๊ธฐ ์“ฐ๊ธฐ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋„๋ก ํ•ด์ฃผ๋ ค๋ฉด,

ํŒŒ์ดํ”„๋ฅผ ํ•˜๋‚˜ ๋” ์ถ”๊ฐ€ํ•˜์—ฌ ๋‘๊ฐœ์˜ ํŒŒ์ดํ”„๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

 

AํŒŒ์ดํ”„๋Š” parent์˜ read ๋ถ€๋ถ„์ธ fd[0]๋ฅผ ๋‹ซ์•„์ฃผ๊ณ , child์˜ write ๋ถ€๋ถ„์ธ fd[1]์„ ๋‹ซ์•„์ค€๋‹ค.

๊ทธ๋ฆฌ๊ณ  BํŒŒ์ดํ”„๋Š” parent์˜ write ๋ถ€๋ถ„์ธ fd[1]์„ ๋‹ซ์•„์ฃผ๊ณ , child์˜ read ๋ถ€๋ถ„์ธ fd[0]์„ ๋‹ซ์•„์ค€๋‹ค.

 

๊ทธ๋Ÿผ A ํŒŒ์ดํ”„๋ฅผ ํ†ตํ•ด parent๊ฐ€ ์“ด ๋ฐ์ดํ„ฐ๋ฅผ child๊ฐ€ ๋ฐ›๊ฒŒ ๋˜๊ณ ,

B ํŒŒ์ดํ”„๋ฅผ ํ†ตํ•ด์„œ๋Š” child๊ฐ€ ์“ด ๋ฐ์ดํ„ฐ๋ฅผ parent๊ฐ€ ๋ฐ›๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด๋‹ค.