Do You Coding?
[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๊ฐ ๋ฐ๊ฒ ๋๋ ๊ฒ์ด๋ค.
'CS & Engineering > C' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [function] wait ํจ์ ์์๋ณด๊ธฐ (0) | 2024.06.09 |
|---|---|
| [function] unlink ํจ์ ์์๋ณด๊ธฐ (2) | 2024.06.07 |
| [function] fork ํจ์ ์์๋ณด๊ธฐ (2) | 2024.06.04 |
| [function] exit ํจ์ ์์๋ณด๊ธฐ (0) | 2024.05.31 |
| [function] execve ํจ์ ์์๋ณด๊ธฐ (0) | 2024.05.30 |