Do You Coding?
[function] fork ํจ์ ์์๋ณด๊ธฐ ๋ณธ๋ฌธ

๐ ๋งค๋ด์ผ (Linux)
NAME
fork - create a child process
: ํ์ ํ๋ก์ธ์ค ์์ฑํ๊ธฐ
SYNOPSIS
#include <sys/types.h>
#include <unistd.h>
pid_t fork(void);
DESCRIPTION
fork() creates a new process by duplicating the calling process. The new process is referred
to as the child process. The calling process is referred to as the parent process.
The child process and the parent process run in separate memory spaces. At the time of fork()
both memory spaces have the same content. Memory writes, file mappings (mmap(2)), and unmapโ
pings (munmap(2)) performed by one of the processes do not affect the other.
The child process is an exact duplicate of the parent process except for the following points:
* The child has its own unique process ID, and this PID does not match the ID of any existing
process group (setpgid(2)) or session.
* The child's parent process ID is the same as the parent's process ID.
* The child does not inherit its parent's memory locks (mlock(2), mlockall(2)).
* Process resource utilizations (getrusage(2)) and CPU time counters (times(2)) are reset to
zero in the child.
* The child's set of pending signals is initially empty (sigpending(2)).
* The child does not inherit semaphore adjustments from its parent (semop(2)).
* The child does not inherit process-associated record locks from its parent (fcntl(2)). (On
the other hand, it does inherit fcntl(2) open file description locks and flock(2) locks
from its parent.)
* The child does not inherit timers from its parent (setitimer(2), alarm(2), timer_creโ
ate(2)).
* The child does not inherit outstanding asynchronous I/O operations from its parent
(aio_read(3), aio_write(3)), nor does it inherit any asynchronous I/O contexts from its
parent (see io_setup(2)).
The process attributes in the preceding list are all specified in POSIX.1. The parent and
child also differ with respect to the following Linux-specific process attributes:
* The child does not inherit directory change notifications (dnotify) from its parent (see
the description of F_NOTIFY in fcntl(2)).
* The prctl(2) PR_SET_PDEATHSIG setting is reset so that the child does not receive a signal
when its parent terminates.
* The default timer slack value is set to the parent's current timer slack value. See the
description of PR_SET_TIMERSLACK in prctl(2).
* Memory mappings that have been marked with the madvise(2) MADV_DONTFORK flag are not inherโ
ited across a fork().
* Memory in address ranges that have been marked with the madvise(2) MADV_WIPEONFORK flag is
zeroed in the child after a fork(). (The MADV_WIPEONFORK setting remains in place for
those address ranges in the child.)
* The termination signal of the child is always SIGCHLD (see clone(2)).
* The port access permission bits set by ioperm(2) are not inherited by the child; the child
must turn on any bits that it requires using ioperm(2).
Note the following further points:
* The child process is created with a single thread—the one that called fork(). The entire
virtual address space of the parent is replicated in the child, including the states of muโ
texes, condition variables, and other pthreads objects; the use of pthread_atfork(3) may be
helpful for dealing with problems that this can cause.
* After a fork() in a multithreaded program, the child can safely call only async-signal-safe
functions (see signal-safety(7)) until such time as it calls execve(2).
* The child inherits copies of the parent's set of open file descriptors. Each file descripโ
tor in the child refers to the same open file description (see open(2)) as the correspondโ
ing file descriptor in the parent. This means that the two file descriptors share open
file status flags, file offset, and signal-driven I/O attributes (see the description of
F_SETOWN and F_SETSIG in fcntl(2)).
* The child inherits copies of the parent's set of open message queue descriptors (see
mq_overview(7)). Each file descriptor in the child refers to the same open message queue
description as the corresponding file descriptor in the parent. This means that the two
file descriptors share the same flags (mq_flags).
* The child inherits copies of the parent's set of open directory streams (see opendir(3)).
POSIX.1 says that the corresponding directory streams in the parent and child may share the
directory stream positioning; on Linux/glibc they do not.
: fork()๋ ํธ์ถ ํ๋ก์ธ์ค๋ฅผ ๋ณต์ ํ์ฌ ์ ํ๋ก์ธ์ค๋ฅผ ์์ฑํฉ๋๋ค.
์ ํ๋ก์ธ์ค๋ฅผ ์์ ํ๋ก์ธ์ค(child process)๋ผ๊ณ ํ๋ฉฐ
ํธ์ถ ํ๋ก์ธ์ค๋ฅผ ๋ถ๋ชจ ํ๋ก์ธ์ค(parent process)๋ผ๊ณ ํฉ๋๋ค.
์์ ํ๋ก์ธ์ค์ ๋ถ๋ชจ ํ๋ก์ธ์ค๋ ๋ณ๋์ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์์ ์คํ๋ฉ๋๋ค.
fork() ์์ ์์ ๋ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ๋ชจ๋ ๋์ผํ ๋ด์ฉ์ ๊ฐ์ต๋๋ค.
ํ๋ก์ธ์ค ์ค ํ๋์ ์ํด ์ํ๋๋ ๋ฉ๋ชจ๋ฆฌ ์ฐ๊ธฐ, ํ์ผ ๋งคํ(mmap(2)) ๋ฐ ๋งคํ ํด์ (munmap(2))๋
๋ค๋ฅธ ํ๋ก์ธ์ค์ ์ํฅ์ ๋ฏธ์น์ง ์์ต๋๋ค.
์์ ํ๋ก์ธ์ค๋ ๋ค์ ์ฌํญ์ ์ ์ธํ๊ณ ๋ถ๋ชจ ํ๋ก์ธ์ค๋ฅผ ์ ํํ ๋ณต์ ํ ๊ฒ์
๋๋ค:
* ์์์๋ ๊ณ ์ ํ ํ๋ก์ธ์ค ID๊ฐ ์์ผ๋ฉฐ ์ด PID๋ ๊ธฐ์กด ํ๋ก์ธ์ค ๊ทธ๋ฃน(setpgid(2)) ๋๋ ์ธ์
์ ID์ ์ผ์นํ์ง ์์ต๋๋ค.
* ์๋
์ ๋ถ๋ชจ ํ๋ก์ธ์ค ID๋ ๋ถ๋ชจ ํ๋ก์ธ์ค ID์ ๋์ผํฉ๋๋ค.
* ์์์ ๋ถ๋ชจ์ ๋ฉ๋ชจ๋ฆฌ ์ ๊ธ(mlock(2), mlockall(2))์ ์์๋ฐ์ง ์์ต๋๋ค.
* ํ๋ก์ธ์ค ๋ฆฌ์์ค ์ฌ์ฉ๋ฅ (getrassage(2)) ๋ฐ CPU ์๊ฐ ์นด์ดํฐ(time(2))๋ ํ์ ํญ๋ชฉ์์ 0์ผ๋ก ์ฌ์ค์ ๋ฉ๋๋ค.
* ์์์ ๋ณด๋ฅ ์ ํธ ์ธํธ๊ฐ ์ฒ์์๋ ๋น์ด ์์ต๋๋ค(sigpending(2)).
* ์์์ ๋ถ๋ชจ๋ก๋ถํฐ ์ธ๋งํฌ์ด ์กฐ์ ์ ์์๋ฐ์ง ์์ต๋๋ค(semop(2)).
* ์์์ ๋ถ๋ชจ๋ก๋ถํฐ ํ๋ก์ธ์ค ๊ด๋ จ ๋ ์ฝ๋ ์ ๊ธ์ ์์๋ฐ์ง ์์ต๋๋ค(fcntl(2)).
(๋ฐ๋ฉด ๋ถ๋ชจ๋ก๋ถํฐ fcntl(2) ์ด๋ฆฐ ํ์ผ ์ค๋ช
์ ๊ธ๊ณผ flock(2) ์ ๊ธ์ ์์๋ฐ์ต๋๋ค.)
* ์์์ ๋ถ๋ชจ๋ก๋ถํฐ ํ์ด๋จธ๋ฅผ ์์๋ฐ์ง ์์ต๋๋ค(setitimer(2), alarm(2), timer_create(2)).
* ์์์ ์์(aio_read(3), aio_write(3))์์ ๋ฏธ๊ฒฐ ๋น๋๊ธฐ I/O ์์
์ ์์ํ์ง ์์ผ๋ฉฐ,
์์์์ ๋น๋๊ธฐ I/O ์ปจํ
์คํธ๋ฅผ ์์ํ์ง๋ ์์ต๋๋ค(io_setup(2) ์ฐธ์กฐ).
์ด์ ๋ชฉ๋ก์ ํ๋ก์ธ์ค ํน์ฑ์ ๋ชจ๋ POSIX.1์ ์ง์ ๋์ด ์์ต๋๋ค.
๋ถ๋ชจ์ ์์์ ๋ํ ๋ค์๊ณผ ๊ฐ์ ๋ฆฌ๋
์ค ๊ณ ์ ์ ํ๋ก์ธ์ค ํน์ฑ๊ณผ ๊ด๋ จํ์ฌ ๋ค๋ฆ
๋๋ค:
* ์์์ด ๋ถ๋ชจ๋ก๋ถํฐ ๋๋ ํ ๋ฆฌ ๋ณ๊ฒฝ ์๋ฆผ(dnotify)์ ์์๋ฐ์ง ์์ต๋๋ค(fcntl(2)์ F_NOTIFY ์ค๋ช
์ฐธ์กฐ).
* prctl(2) PR_SET_PDESSIG ์ค์ ์ ๋ถ๋ชจ๊ฐ ์ข
๋ฃ๋ ๋ ์์์ด ์ ํธ๋ฅผ ๋ฐ์ง ์๋๋ก ์ฌ์ค์ ๋ฉ๋๋ค.
* ๊ธฐ๋ณธ ํ์ด๋จธ ์ฌ๋ ๊ฐ์ ๋ถ๋ชจ์ ํ์ฌ ํ์ด๋จธ ์ฌ๋ ๊ฐ์ผ๋ก ์ค์ ๋ฉ๋๋ค.
prctl(2)์ PR_SET_TIMERSLACK์ ๋ํ ์ค๋ช
์ ์ฐธ์กฐํ์ญ์์ค.
* madvise(2) MADV_DONTFORK ํ๋๊ทธ๋ก ํ์๋ ๋ฉ๋ชจ๋ฆฌ ๋งคํ์ ํฌํฌ()์์ ์์๋์ง ์์ต๋๋ค.
* madvise(2) MADV_WIPEONFORK ํ๋๊ทธ๋ก ํ์๋ ์ฃผ์ ๋ฒ์์ ๋ฉ๋ชจ๋ฆฌ๋ ํฌํฌ() ์ดํ ์์์์ ์์ ์ผ๋ก ํ์๋ฉ๋๋ค.(MADV_WIPEONFORK ์ค์ ์ ์์์ ํด๋น ์ฃผ์ ๋ฒ์์ ๋ํด ๊ทธ๋๋ก ์ ์ง๋ฉ๋๋ค.)
* ์์์ ์ข
๋ฃ ์ ํธ๋ ํญ์ SIGCHLD์
๋๋ค(clone(2) ์ฐธ์กฐ).
* ioperm(2)์์ ์ค์ ํ ํฌํธ ์ก์ธ์ค ๊ถํ ๋นํธ๋ ์์์๊ฒ ์์๋์ง ์์ผ๋ฉฐ,
์์์ ioperm(2)์ ์ฌ์ฉํ์ฌ ํ์ํ ๋ชจ๋ ๋นํธ๋ฅผ ์ผ์ผ ํฉ๋๋ค.
๋ค์ ์ฌํญ์ ์ฃผ์ํ์ญ์์ค:
* ์์ ํ๋ก์ธ์ค๋ fork()๋ผ๋ ๋จ์ผ ์ค๋ ๋๋ฅผ ์ฌ์ฉํ์ฌ ์์ฑ๋ฉ๋๋ค. ๋ถ๋ชจ์ ์ ์ฒด ๊ฐ์ ์ฃผ์ ๊ณต๊ฐ์ ์์๊ฑฐ ์ํ,
์กฐ๊ฑด ๋ณ์ ๋ฐ ๊ธฐํ pthread ๊ฐ์ฒด๋ฅผ ํฌํจํ์ฌ ์์์์ ๋ณต์ ๋ฉ๋๋ค. pthread_atfork(3)์ ์ฌ์ฉํ๋ฉด
์ด๋ฌํ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ๋ฐ ๋์์ด ๋ ์ ์์ต๋๋ค.
* ๋ฉํฐ ์ค๋ ๋ ํ๋ก๊ทธ๋จ์์ ํฌํฌ()๋ฅผ ์ฌ์ฉํ ํ, ์์ด๋ execve(2)๋ฅผ ํธ์ถํ ๋๊น์ง
๋น๋๊ธฐ ์ ํธ ์์ ๊ธฐ๋ฅ(signal-safety(7))๋ง ์์ ํ๊ฒ ํธ์ถํ ์ ์์ต๋๋ค.
* ์์์ ๋ถ๋ชจ์ ์คํ ํ์ผ ๋์คํฌ๋ฆฝํฐ ์งํฉ์ ๋ณต์ฌ๋ณธ์ ์์ํฉ๋๋ค. ์์์ ๊ฐ ํ์ผ ๋์คํฌ๋ฆฝํฐ๋
๋ถ๋ชจ์ ํด๋น ํ์ผ ๋์คํฌ๋ฆฝํฐ์ ๋์ผํ ์คํ ํ์ผ ๋์คํฌ๋ฆฝํฐ(open(2))๋ฅผ ์ฐธ์กฐํฉ๋๋ค.
์ด๋ ๋ ํ์ผ ๋์คํฌ๋ฆฝํฐ๊ฐ ์คํ ํ์ผ ์ํ ํ๋๊ทธ, ํ์ผ ์คํ์ ๋ฐ ์ ํธ ๊ธฐ๋ฐ I/O ์์ฑ์
๊ณต์ ํ๋ค๋ ๊ฒ์ ์๋ฏธํฉ๋๋ค(fcntl(2)์ F_SETOWN ๋ฐ F_SETSIG ์ค๋ช
์ฐธ์กฐ).
* ์์์ ๋ถ๋ชจ์ ์ด๋ฆฐ ๋ฉ์์ง ํ ๋์คํฌ๋ฆฝํฐ ์งํฉ์ ๋ณต์ฌ๋ณธ์ ์์ํฉ๋๋ค(mq_overview(7)).
์์์ ๊ฐ ํ์ผ ๋์คํฌ๋ฆฝํฐ๋ ๋ถ๋ชจ์ ํด๋น ํ์ผ ๋์คํฌ๋ฆฝํฐ์ ๋์ผํ ์ด๋ฆฐ ๋ฉ์์ง ํ ๋์คํฌ๋ฆฝํฐ๋ฅผ ๋ํ๋ ๋๋ค.
์ด๋ ๋ ํ์ผ ๋์คํฌ๋ฆฝํฐ๊ฐ ๋์ผํ ํ๋๊ทธ(mq_flags)๋ฅผ ๊ณต์ ํ๋ค๋ ๊ฒ์ ์๋ฏธํฉ๋๋ค.
* ์์์ ๋ถ๋ชจ์ ์ด๋ฆฐ ๋๋ ํฐ๋ฆฌ ์คํธ๋ฆผ ์งํฉ์ ๋ณต์ฌ๋ณธ์ ์์ํฉ๋๋ค(opendir(3) ์ฐธ์กฐ).
POSIX.1์ ๋ถ๋ชจ ๋ฐ ์์์ ํด๋น ๋๋ ํ ๋ฆฌ ์คํธ๋ฆผ์ด ๋๋ ํ ๋ฆฌ ์คํธ๋ฆผ ํฌ์ง์
๋์ ๊ณต์ ํ ์ ์์ง๋ง
Linux/glibc์์๋ ๊ณต์ ํ์ง ์๋๋ค๊ณ ๋งํฉ๋๋ค.
RETURN VALUE
On success, the PID of the child process is returned in the parent, and 0 is returned in the
child. On failure, -1 is returned in the parent, no child process is created, and errno is
set appropriately.
: ์ฑ๊ณต ์ ์์ ํ๋ก์ธ์ค์ PID๋ ๋ถ๋ชจ์์ ๋ฐํ๋๊ณ 0์ ์์์์ ๋ฐํ๋ฉ๋๋ค.
์คํจ ์ -1์ ๋ถ๋ชจ์์ ๋ฐํ๋๊ณ ์์ ํ๋ก์ธ์ค๋ ์์ฑ๋์ง ์์ผ๋ฉฐ errno๋ ์ ์ ํ๊ฒ ์ค์ ๋ฉ๋๋ค.
๐ ํจ์ ์ค๋ช
fork ํจ์๋ ํจ์๋ฅผ ํธ์ถํ ํ๋ก์ธ์ค๋ฅผ ๋ณต์ฌํ๋ ๊ธฐ๋ฅ์ ๊ฐ์ง๋ฉฐ,
์๋ ์งํ๋๋ ํ๋ก์ธ์ค๋ฅผ '๋ถ๋ชจ ํ๋ก์ธ์ค' (parent), ๋ณต์ฌ๋ ํ๋ก์ธ์ค๋ฅผ '์์ ํ๋ก์ธ์ค' (child) ๋ผ ํ๋ค.
(ํ๋ก์ธ์ค์ ๋ํ ์ถ๊ฐ ๋ด์ฉ : 2024.06.02 - [ํ๋ก๊ทธ๋๋ฐ/C] - [study] ํ๋ก์ธ์ค (Process))
ํ๋ก์ธ์ค์ id๋ฅผ pid๋ผ ํ๋ฉฐ, fork ํจ์๊ฐ ๋ฐํํ๋ ๊ฐ์ด ์ด pid์ด๋ค.
๋ถ๋ชจ ํ๋ก์ธ์ค๋ ์์ pid๋ฅผ ๋ฐํํ๊ณ , ์์ ํ๋ก์ธ์ค๋ 0 ๋ฐํ, ํจ์ ์คํ ์คํจ ์ -1 ๋ฐํ์ด๋ค.

๊ทธ๋ฆผ์ ํตํด ๋ณด๋ฉด, fork๋ ๊ธฐ์กด Parent Process๋ฅผ Parent Process์ ์งํ๊ณผ ํจ๊ป
ํด๋น ํ๋ก์ธ์ค๋ฅผ ๋ณต์ฌํ Child Process๋ฅผ ๋ง๋ค์ด์ฃผ๋ ํจ์๋ก ์ฐ์ธ๋ค.
์ฝ๋๋ฅผ ํตํด ํ์ธํด๋ณด๋ฉด ๋์ฑ ์ฝ๊ฒ ์ ์ ์๋ค.
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
fork();
printf("hello\n");
return 0;
}
์, fork()๋ฅผ ํ๋ฒ ์คํํ๋ค๊ณ ํ์ ๋, ์งํ์ํฉ์ ์ดํด๋ณด๋๋ก ํ๊ฒ ๋ค.
์ด ํ๋ก๊ทธ๋จ์ด ์์ํ์ฌ ํ๋ก์ธ์ค๊ฐ ๋ง๋ค์ด์ง๋ฉด, main๋ฌธ์ด ์คํ์ด ๋๋ค.
๊ทธ๋ฌํ์ฌ, fork()๋ฅผ ์ฒ์ ๋ง๋๊ฒ ๋๋๋ฐ, ์ด ์ํฉ์์ ๊ธฐ์กด ํ๋ก์ธ์ค๋ ๋ถ๋ชจ ํ๋ก์ธ์ค๊ฐ ๋์ด, ์์ ํ๋ก์ธ์ค๋ฅผ ๋ณต์ฌํ๋ค.
๊ทธ๋ผ fork() ์ดํ์ ์คํ๋ ์ฝ๋๋ค์ ๊ฐ์ง ๋ ๊ฐ์ ํ๋ก์ธ์ค๊ฐ ์๊ธฐ๊ฒ ๋ ๊ฒ์ด๊ณ , printf()๊ฐ ๊ฐ๊ฐ ํ๋ฒ์ฉ ์คํ ๋๋ ๊ฒ์ด๋ค.

์ฝ๋์ ์งํ์ ๊ทธ๋ฆผ์ผ๋ก ๋ณด๋ฉด ์ด๋ฐํํ๋ฅผ ๋ ์ ์๋ค.
fork ์ดํ ๋ถํฐ์ ์ฝ๋๊ฐ ์งํ๋๋ ํ๋ก์ธ์ค๊ฐ 2๊ฐ์ง๊ฐ ์๊ฒจ๋ ๊ฒ์ด๋ค.
(ํ๋๋ parent process์ ๊ธฐ์กด ์งํ, ํ๋๋ ๋ณต์ฌ๋ Child Process์ ์งํ)
๊ฒฐ๊ณผ - std_output)
| hello hello |
์ด๋ ๊ฒ ๋ ํ๋ก์ธ์ค๊ฐ ์คํ๋์ด ๊ฒฐ๊ณผ๊ฐ 2๊ฐ์ hello๋ฅผ ์ถ๋ ฅํ๋ ์ํฉ์ด ๋์จ๋ค.
์ด ๋ ํ๋ก์ธ์ค๋ pid๋ก ๊ตฌ๋ถ์ด ๊ฐ๋ฅํ๊ณ (๋ถ๋ชจ๋ ์์์ pid, ์์์ 0) pid์ ๊ฐ์ ์ ์ฅํ์ฌ forkํ๋ค๋ฉด,
๊ทธ ๊ฐ์ ๋ฐ๋ผ ๊ตฌ๋ถํ์ฌ ์ํ๋ ์คํ ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์๋ ์๋ค. ๋ค์ ์ฝ๋๊ฐ ๊ทธ๋ฌํ ์ํฉ์ ๋ณด์ฌ์ค๋ค.
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main(void)
{
pid_t pid = fork();
if (pid == -1) {
perror("fork failed");
return (0);
}
else if (pid == 0) {
printf("Hello from the child process!\n");
return (0);
}
else {
printf("Hello from the parent process!\n");
return (0);
}
return (0);
}
๊ฒฐ๊ณผ - std_output)
| Hello from the parent process! Hello from the child process! |
pid์ ๊ฐ์ด 0์ด ๋ ์ํฉ์์๋ child ํ๋ก์ธ์ค์ด๋ฏ๋ก, child์ ๊ฒฐ๊ณผ๊ฐ์ด ๋์ค๋๋ก ํ๊ณ ,
pid์ ๊ฐ์ด 0์ด๋ -1์ด ์๋๋ฉด, child ํ๋ก์ธ์ค์ pid๋ฅผ ๊ฐ์ง๊ณ ์๋ parent ํ๋ก์ธ์ค์ด๋ฏ๋ก parent์ ๊ฒฐ๊ณผ๊ฐ์ด ๋์ค๋๋ก ํ๋ค.
์ด ์คํ ์์์ ๊ดํด์๋ '์ค์ผ์ค๋ง'์ ๋ฐ๋ผ OS๊ฐ ํ๋ณํ์ฌ ๋ ์ค ํ๋๋ ๋จผ์ ์คํ๋ ์ ์๋ค.
๊ทธ๋ ๋ค๋ฉด ์ข ๋ฃ ์์๋? ๋ง์ฐฌ๊ฐ์ง๋ก '์ค์ผ์ค๋ง'์ ๋ฐ๋ฅด๊ฒ ์ง๋ง ์ผ๋ฐ์ ์ผ๋ก๋ ์์ ํ๋ก์ธ์ค๊ฐ ์ข ๋ฃ๋ ๋๊น์ง ๋ถ๋ชจ๊ฐ ๊ธฐ๋ค๋ฆฐ๋ค.
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
printf("Line 0\n");
fork();
printf("Line 1\n");
fork();
printf("Line 2\n");
fork();
printf("Line 3\n");
return 0;
}
๋ ์ฌ์ด ์ดํด๋ฅผ ์ํด, ์ด๋ฒ์๋ fork๋ฅผ ๋ฌด๋ ค 3๋ฒ ์ด ์ฝ๋๊ฐ ๋์๋ค.
๊ทธ๋ฆฌ๊ณ ์ค๊ฐ์ค๊ฐ printf๊ฐ ์กด์ฌํ๋๋ฐ ์ด๋ป๊ฒ ๋์ํ ์ง ์์ธกํด๋ณด์.

๊ทธ๋ฆผ์ผ๋ก ๋ณด๋ฉด, printf("Line 0")์ ์ฒซ parent process์์ ํ๋ฒ ์คํ๋๊ณ ๊ทธ ์ดํ์ fork๋ฅผ ํ๋ฉด
ํ์์ ์ฝ๋๋ค๋ก ์ด๋ค์ง process๊ฐ ๋์ด ์๊ธฐ๊ณ , ๊ทธ๋ผ printf("Line 1") ์ ์ด ๋ ๋ฒ ์คํ์ด ๋๋ค. ๊ทธ ์ดํ ๊ฐ์ ๊ณผ์ ์ผ๋ก
fork ๋๊ณ printf("Line 2")๊ฐ ์๋ 4๊ฐ์ ํ๋ก์ธ์ค๊ฐ ์๊ธฐ๊ณ , ๊ทธ๋ ๊ฒ ๊ผฌ๋ฆฌ์ ๊ผฌ๋ฆฌ๋ฅผ ๋ฌผ์ด, 1 + 2 + 4 + 8, 15๊ฐ์ ์ค์ด ์๊ธธ ๊ฒ์ด๋ค.
๊ฒฐ๊ณผ - std_output)

์ธ๊ธํ๋ ๋๋ก, ์ค์ผ์ค๋ง์ ์ํด ์คํ ๋๋ง๋ค ํ๋ก์ธ์ค ์คํ ์์๊ฐ ๋ฐ๋ ์ํฉ์ด๋ค.
ํ์ง๋ง ๋น์ฐํ๊ฒ๋, Line 2๊ฐ ํ๋ฒ๋ ๋์ค์ง ์์๋๋ฐ Line 3์ด ๋์ค๋ ๊ฒฝ์ฐ๋ ๋ฐ์ํ์ง ์๋๋ค.
์ด์ ๊ฐ์ด ๊ฒฐ๊ณผ๋ก ๋์ฌ ์ ์๋ ๋ถ๊ฐ๋ฅํ ๊ฒฝ์ฐ๋ 'Infeasible output' ์ด๋ผ ํ๊ณ ,
์ฌ์ง๊ณผ ๊ฐ์ด ์ ์์ ์ผ๋ก ๋์ฌ ์ ์๋ ๊ฐ๋ฅํ ๊ฒฝ์ฐ๋ 'Feasible output' ์ด๋ผ ํ๋ค.
์ด fork์ ๋ํด ์ถ๊ฐ์ ์ธ ์ค๋ช ์ ๋ง๋ถ์ด๋๋ก ํ๊ฒ ๋ค.
[ fork์ ์ฉ๋ ]
1. ๊ฐ์ ํ๋ก๊ทธ๋จ์์ ์๋ก์ด ์ ์ด ํ๋ฆ์ ๋ง๋๋ ์ฉ๋
2. ๋ค๋ฅธ ํ๋ก๊ทธ๋จ์ ์คํํ๋ ์ ํ๋ก์ธ์ค๋ฅผ ๋ง๋๋ ์ฉ๋
๋๊ฐ 2๋ฒ์ ์ฉ๋๋ก ์ฌ์ฉํ๋๋ฐ, ์์์ ๋ช ๋ น์ด๋ฅผ ํตํด ํ๋ก์ธ์ค๋ฅผ ์คํํ๋ ๊ฒฝ์ฐ์ด๋ค.
๊ทธ๋ฆฌ๊ณ , ์์ ํ๋ก์ธ์ค๊ฐ ์์ฑ๋๋ฉด ๋ถ๋ชจ ํ๋ก์ธ์ค์ Stack, Heap, Data ๋ฅผ ๋ณต์ฌํ๊ณ ,
๋ถ๋ชจ ํ๋ก์ธ์ค์ PCB(Process Control Block)๋ ๋ณต์ฌํ๋ค. Text ์์ญ์ ๋ณต์ฌ๋์ ๊ณต์ ํ๋ค.
์ด PCB์๋ CPU์์ ์ํ๋๋ ๋ ์ง์คํฐ ๊ฐ๋ค๊ณผ Program Counter ์ ๋ณด๊ฐ ์กด์ฌํ๋ฏ๋ก
fork ์ดํ์ ์คํ์ํ๋ฅผ ๊ทธ๋๋ก ๋ณต์ฌํ์ฌ ์คํ๋๋ค.
๊ทธ๋ฆฌ๊ณ , COW(Copy On Write) ์ ์ฑ ์ด๋ผ๋ '์ฐ๊ธฐ ์์ ์ ๋ณต์ฌ'๊ฐ ์ผ์ด๋๋ ์ต์ ํ ๊ธฐ์ ์ ์ฌ์ฉํ๋๋ฐ,
๋ง ๊ทธ๋๋ก ์ด Resource๋ฅผ ์์ ํ ๋๋ง ์ค์ ๋ก Copy๋ฅผ ํ๊ณ ,
์์ ํ์ง ์๊ณ ์ฌ์ฉํ ๋๋ ๊ทธ๋ฅ ๊ณต์ ํด์ ์ฐ๋ ๊ฒ์ ์๋ฏธํ๋ค.
๊ทธ๋ฆฌํ์ฌ ๋ณต์ฌํ ์์ ํ๋ก์ธ์ค์ ๋ฉ๋ชจ๋ฆฌ ๊ฐ์ ์ฃผ์๊ฐ ๋์ผํ๋ฉฐ,
๋ด์ฉ์ด ๋ณ๊ฒฝ๋ ์ ์๋ก์ด ๋ฌผ๋ฆฌ ๋ฉ๋ชจ๋ฆฌ ์ฃผ์๋ฅผ ๊ฐ๋ฆฌํค๋ ์ํฉ์ด ์๊ธด๋ค.
๊ฒฐ๊ตญ, ๋ชจ๋ ๋ฐ์ดํฐ๋ฅผ ์๋กญ๊ฒ ๋ณต์ฌํ๋ ๊ฒ์ด ์๋๊ธฐ ๋๋ฌธ์ ๋ณต์ ๋น์ฉ์ด ๊ฑฐ์ ๋ค์ง ์๊ณ , ๊ด๋ฆฌ๊ฐ ์ฉ์ดํ๋ค.
ํ๋ก์ธ์ค๋ฅผ ์์ฑํ๋ ๊ฒ๋ณด๋ค fork๋ฅผ ํตํด ๋ณต์ฌํ๋ ๊ฒ์ ๋ํ ์์๊ฐ ๋ฐ๋ก ์ด๋ฐ ๊ฒ์ด๋ค.
์ฐธ๊ณ )
https://velog.io/@jungbumwoo/fork-%EB%A5%BC-%EC%95%8C%EC%95%84%EB%B3%B4%EC%9E%90
'CS & Engineering > C' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [function] unlink ํจ์ ์์๋ณด๊ธฐ (2) | 2024.06.07 |
|---|---|
| [function] pipe ํจ์ ์์๋ณด๊ธฐ (0) | 2024.06.06 |
| [function] exit ํจ์ ์์๋ณด๊ธฐ (0) | 2024.05.31 |
| [function] execve ํจ์ ์์๋ณด๊ธฐ (0) | 2024.05.30 |
| [function] dup2 ํจ์ ์์๋ณด๊ธฐ (2) | 2024.05.28 |