Do You Coding?

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

CS & Engineering/C

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

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

๋”๋ณด๊ธฐ

NAME
       wait - wait for process to change state
: ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝํ•  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฌ๊ธฐ


SYNOPSIS
       #include <sys/types.h>
       #include <sys/wait.h>
       pid_t wait(int *wstatus);

DESCRIPTION
       All  of  these  system  calls  are  used  to  wait for state changes in a child of the calling
       process, and obtain information about the child whose state has changed.  A  state  change  is
       considered  to  be:  the child terminated; the child was stopped by a signal; or the child was
       resumed by a signal.  In the case of a terminated child, performing a wait allows  the  system
       to  release the resources associated with the child; if a wait is not performed, then the terโ€
       minated child remains in a "zombie" state (see NOTES below).

       If a child has already changed state, then these calls return  immediately.   Otherwise,  they
       block  until  either  a  child changes state or a signal handler interrupts the call (assuming
       that system calls are not automatically restarted using the SA_RESTART flag of  sigaction(2)).
       In  the  remainder  of  this  page, a child whose state has changed and which has not yet been
       waited upon by one of these system calls is termed waitable.

   wait() and waitpid()
       The wait() system call suspends execution of the calling thread until one of its children terโ€
       minates.  The call wait(&wstatus) is equivalent to:
           waitpid(-1, &wstatus, 0);

 

       The  waitpid() system call suspends execution of the calling thread until a child specified by
       pid argument has changed state.  By default, waitpid() waits only for terminated children, but
       this behavior is modifiable via the options argument, as described below.

       The value of pid can be:
       < -1   meaning  wait  for  any  child  process whose process group ID is equal to the absolute
              value of pid.
       -1     meaning wait for any child process.
       0      meaning wait for any child process whose process group ID is equal to that of the callโ€
              ing process at the time of the call to waitpid().
       > 0    meaning wait for the child whose process ID is equal to the value of pid.

: ์ด ๋ชจ๋“  ์‹œ์Šคํ…œ ํ˜ธ์ถœ์€ ํ˜ธ์ถœ ๊ณผ์ •์˜ ์ž์‹์—์„œ ์ƒํƒœ ๋ณ€๊ฒฝ์„ ๋Œ€๊ธฐํ•˜๊ณ  ์ƒํƒœ๊ฐ€ ๋ณ€๊ฒฝ๋œ child์— ๋Œ€ํ•œ

์ •๋ณด๋ฅผ ์–ป๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ƒํƒœ ๋ณ€๊ฒฝ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฐ„์ฃผ๋ฉ๋‹ˆ๋‹ค. child๊ฐ€ ์ข…๋ฃŒ๋˜๊ฑฐ๋‚˜,

์‹ ํ˜ธ์— ์˜ํ•ด child๊ฐ€ ์ค‘์ง€๋˜๊ฑฐ๋‚˜, ์‹ ํ˜ธ์— ์˜ํ•ด ๋‹ค์‹œ ์‹œ์ž‘๋ฉ๋‹ˆ๋‹ค. ์ข…๋ฃŒ๋œ child์˜ ๊ฒฝ์šฐ,

๋Œ€๊ธฐ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉด ์‹œ์Šคํ…œ์ด child๊ณผ ๊ด€๋ จ๋œ ๋ฆฌ์†Œ์Šค๋ฅผ ํ•ด์ œํ•  ์ˆ˜ ์žˆ๊ณ , ๋Œ€๊ธฐ๋ฅผ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š์œผ๋ฉด

์ข…๋ฃŒ๋œ child๋Š” "์ข€๋น„" ์ƒํƒœ๋กœ ์œ ์ง€๋ฉ๋‹ˆ๋‹ค(์•„๋ž˜ ์ฐธ๊ณ  ์ฐธ์กฐ).

child๊ฐ€ ์ด๋ฏธ ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝํ•œ ๊ฒฝ์šฐ ์ด๋Ÿฌํ•œ ํ˜ธ์ถœ์€ ์ฆ‰์‹œ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค.

๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด child์ด ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝํ•˜๊ฑฐ๋‚˜ ์‹ ํ˜ธ ์ฒ˜๋ฆฌ๊ธฐ๊ฐ€ ํ˜ธ์ถœ์„ ์ค‘๋‹จํ•  ๋•Œ๊นŒ์ง€

(์‹œ๊ทธ์•ก์…˜(2)์˜ SA_RESTART ํ”Œ๋ž˜๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‹œ์Šคํ…œ ํ˜ธ์ถœ์ด ์ž๋™์œผ๋กœ

๋‹ค์‹œ ์‹œ์ž‘๋˜์ง€ ์•Š๋Š”๋‹ค๊ณ  ๊ฐ€์ •ํ•  ๋•Œ) ์ฐจ๋‹จ๋ฉ๋‹ˆ๋‹ค.
์ด ํŽ˜์ด์ง€์˜ ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„์—์„œ๋Š” ์ƒํƒœ๊ฐ€ ๋ณ€๊ฒฝ๋˜์–ด ์•„์ง ์ด๋Ÿฌํ•œ ์‹œ์Šคํ…œ ํ˜ธ์ถœ ์ค‘ ํ•˜๋‚˜๊ฐ€

๋Œ€๊ธฐํ•˜์ง€ ์•Š์€ child๋ฅผ ๋Œ€๊ธฐ ๊ฐ€๋Šฅ ์ƒํƒœ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

wait() and waitpid()
wait() ์‹œ์Šคํ…œ ํ˜ธ์ถœ์€ ํ•˜์œ„ ํ˜ธ์ถœ ์ค‘ ํ•˜๋‚˜๊ฐ€ ์ข…๋ฃŒ๋  ๋•Œ๊นŒ์ง€ ํ˜ธ์ถœ ์Šค๋ ˆ๋“œ์˜ ์‹คํ–‰์„ ์ค‘์ง€ํ•ฉ๋‹ˆ๋‹ค.

ํ˜ธ์ถœ wait(&wstatus)๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค:
waitpid (- 1, &wstatus, 0);

waitpid() ์‹œ์Šคํ…œ ํ˜ธ์ถœ์€ pid ์ธ์ˆ˜์— ์˜ํ•ด ์ง€์ •๋œ child์ด ์ƒํƒœ๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ๊นŒ์ง€ ํ˜ธ์ถœ ์Šค๋ ˆ๋“œ์˜ ์‹คํ–‰์„ ์ค‘์ง€ํ•ฉ๋‹ˆ๋‹ค.

๊ธฐ๋ณธ์ ์œผ๋กœ waitpid()๋Š” ์ข…๋ฃŒ๋œ child๋งŒ ๋Œ€๊ธฐํ•˜์ง€๋งŒ, ์ด ๋™์ž‘์€ ์•„๋ž˜ ์„ค๋ช…๋œ ๋Œ€๋กœ ์˜ต์…˜ ์ธ์ˆ˜๋ฅผ ํ†ตํ•ด ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

pid ๊ฐ’์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค:
< -1์€ ํ”„๋กœ์„ธ์Šค ๊ทธ๋ฃน ID๊ฐ€ pid์˜ ์ ˆ๋Œ€๊ฐ’๊ณผ ๋™์ผํ•œ ํ•˜์œ„ ํ”„๋กœ์„ธ์Šค๋ฅผ ๋Œ€๊ธฐํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
-1์€ ๋ชจ๋“  ์ž์‹ ํ”„๋กœ์„ธ์Šค๋ฅผ ๊ธฐ๋‹ค๋ฆด ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
0์€ ํ˜ธ์ถœ ์‹œ ํ”„๋กœ์„ธ์Šค ๊ทธ๋ฃน ID๊ฐ€ ํ˜ธ์ถœ ํ”„๋กœ์„ธ์Šค ID์™€ ๋™์ผํ•œ ํ•˜์œ„ ํ”„๋กœ์„ธ์Šค๋ฅผ waitpid()๋กœ ๊ธฐ๋‹ค๋ฆฌ๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
> 0์€ ํ”„๋กœ์„ธ์Šค ID๊ฐ€ pid ๊ฐ’๊ณผ ๋™์ผํ•œ child๋ฅผ ๊ธฐ๋‹ค๋ฆฐ๋‹ค๋Š” ๋œป์ž…๋‹ˆ๋‹ค.

 

RETURN VALUE
       wait(): on success, returns the process ID of the terminated child; on error, -1 is returned.
       Each of these calls sets errno to an appropriate value in the case of an error.

: ์„ฑ๊ณต ์‹œ ์ข…๋ฃŒ๋œ ์ž์‹์˜ ํ”„๋กœ์„ธ์Šค ID๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , ์˜ค๋ฅ˜ ์‹œ -1์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์ด๋Ÿฌํ•œ ํ˜ธ์ถœ์€ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•  ๊ฒฝ์šฐ errno๋ฅผ ์ ์ ˆํ•œ ๊ฐ’์œผ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

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

wait ํ•จ์ˆ˜๋Š” ๋ถ€๋ชจ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ž์‹ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ข…๋ฃŒ๋  ๋•Œ๊นŒ์ง€๋Š” ์•„๋ฌด ์ผ๋„ ํ•˜์ง€ ์•Š๊ณ  ๊ธฐ๋‹ค๋ ค์ฃผ๋„๋ก ํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค.

๋˜ํ•œ, ์ž์‹ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์™„์ „ํžˆ ์ข…๋ฃŒ๋˜๋ฉด ์ž์‹ ํ”„๋กœ์„ธ์Šค์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋ถ€๋ชจ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์–ป์„ ์ˆ˜ ์—†์œผ๋ฏ€๋กœ,

wait ํ•จ์ˆ˜์—์„œ๋Š” ์ž์‹ ํ”„๋กœ์„ธ์Šค์— ๋Œ€ํ•œ ์–ด๋– ํ•œ ์ •๋ณด๋ฅผ status ํฌ์ธํ„ฐ๋ฅผ ํ†ตํ•ด ์ €์žฅํ•˜์—ฌ์ค€๋‹ค.


[ Zombie Process ๋ž€?]

 

์ž, ์šฐ์„  ์ž์‹ ํ”„๋กœ์„ธ์Šค๊ฐ€ ๋ถ€๋ชจ ํ”„๋กœ์„ธ์Šค๋ณด๋‹ค ๋จผ์ € ์ข…๋ฃŒ๋˜์—ˆ์„ ๋•Œ,

์ปค๋„์ด ์ž์‹ ํ”„๋กœ์„ธ์Šค๋ฅผ ํŠน์ˆ˜ํ•œ ์ƒํƒœ๋กœ ๋งŒ๋“ค์–ด์ฃผ๋Š”๋ฐ, ์ด๋ฅผ 'Zombie process' ๋ผ ํ•œ๋‹ค.

๋ง ๊ทธ๋Œ€๋กœ, ์ฃฝ์ง€ ์•Š๊ณ  ์‚ด์•„์žˆ๋Š”, ์‹คํ–‰์€ ์ข…๋ฃŒ๋˜์—ˆ์ง€๋งŒ ์•„์ง ์‚ญ์ œ๋˜์ง€ ์•Š์€ ํ”„๋กœ์„ธ์Šค์ด๋‹ค.

์ด๋Š” ๋ฆฌ์†Œ์Šค๋„ ํšŒ์ˆ˜๋˜์—ˆ์ง€๋งŒ ์‹œ์Šคํ…œ ํ”„๋กœ์„ธ์Šค ํ…Œ์ด๋ธ”์—” ๋‚จ์•„ ์žˆ๋Š” ์ƒํƒœ์˜ ํ”„๋กœ์„ธ์Šค์ธ๋ฐ,

์ž์‹ ํ”„๋กœ์„ธ์Šค๊ฐ€ exit()์„ ํ†ตํ•ด ์ข…๋ฃŒํ•˜๋ฉด ์ด ์ƒํƒœ๊ฐ€ ๋œ๋‹ค.

 

์ด zombie process๋Š” ๋ถ€๋ชจ ํ”„๋กœ์„ธ์Šค๊ฐ€ wait()ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ๊นŒ์ง€ ๋‚จ์•„์žˆ๊ฒŒ ๋˜๋Š”๋ฐ,

๊ฒฐ๋ก ์ ์œผ๋กœ '์ข…๋ฃŒ๋˜์—ˆ์ง€๋งŒ ๋ถ€๋ชจ ํ”„๋กœ์„ธ์Šค๊ฐ€ wait()๋ฅผ ํ˜ธ์ถœํ•˜์ง€ ์•Š์•„ ๋‚จ์•„์žˆ๋Š” ํ”„๋กœ์„ธ์Šค'์ธ

zombie process๋Š” ๋ถ€๋ชจ๊ฐ€ wait()๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ํ…Œ์ด๋ธ”์— ์žˆ๋˜ ๋ฐ์ดํ„ฐ์™€ PID๋ฅผ ์šด์˜์ฒด์ œ์— ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

+) zombie process๋Š” ์‹œ์Šคํ…œ์— ๋ฌธ์ œ๋ฅผ ์ฃผ๋‚˜์š”?

: ์ด๋ฏธ ๋ฆฌ์†Œ์Šค๊ฐ€ ๋ฐ˜๋‚ฉ๋˜์—ˆ๊ธฐ์—, ์‹œ์Šคํ…œ ๋ฆฌ์†Œ์Šค๋ฅผ ์†Œ๋ชจํ•˜์ง€ ์•Š์•„ ๋ฌธ์ œ๊ฐ€ ๋˜์ง„ ์•Š์ง€๋งŒ,

ํ• ๋‹น๋ฐ›์€ PID๋Š” ๋ฐ˜๋‚ฉํ•˜์ง€ ์•Š์•˜๊ธฐ์—, 2์˜ 15์Šน์ธ 32768๊ฐœ์˜ PID (๋ฆฌ๋ˆ…์Šค ๊ธฐ์ค€)๋ฅผ ์ข€๋น„ ํ”„๋กœ์„ธ์Šค๊ฐ€

๋Œ€๋ถ€๋ถ„ ์ฐจ์ง€ํ•˜๊ฒŒ ๋˜๋Š” ์ƒํ™ฉ์ด ์˜ค๋ฉด ์ผ๋ฐ˜ ํ”„๋กœ์„ธ์Šค์— ์‹คํ–‰์„ ๋ฐฉํ•ดํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

 

< Zombie Process ์ฐพ๋Š” ๋ช…๋ น์–ด >

ps -ef | grep defunct | grep -v grep

ps aux | egrep "Z|defunct"

top -b -n 1 | grep zombie

 

์œ„ ์„ธ๊ฐ€์ง€ shell ๋ช…๋ น์–ด๋กœ zombie process๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ๋Š”๋ฐ,

1, 2๋ฒˆ์งธ ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” ํ•ด๋‹น ํ”„๋กœ์„ธ์Šค ์ž์ฒด๋ฅผ ์ฐพ๊ณ , 3๋ฒˆ์งธ ๋ฐฉ๋ฒ•์œผ๋กœ ๊ฐœ์ˆ˜๋งŒ ํŒŒ์•…ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.


ํ•จ์ˆ˜ ์›ํ˜•์„ ๋ณด๋ฉด, pid_t wait(int *status); ์ด๋‹ค. 

๋ฐ˜ํ™˜๊ฐ’์€ pid_t ์ด๊ณ , ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ๋Š” ์ •์ˆ˜ ํฌ์ธํ„ฐ๋ฅผ ๋ฐ›๋Š”๋‹ค.

์šฐ์„ , ๋ฐ˜ํ™˜๊ฐ’๋ถ€ํ„ฐ ๋ณด์ž๋ฉด, ์ข…๋ฃŒ๋œ ์ž์‹ ํ”„๋กœ์„ธ์Šค์˜ ํ”„๋กœ์„ธ์Šค ID๋ฅผ return ํ•˜๊ณ , ์‹คํŒจํ•˜๋ฉด -1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

(์ž์‹์ด ์ฃฝ์—ˆ๋Š”๋ฐ ์ข€๋น„๊ฐ€ ๋˜๊ณ  ์ž์‹ ์‹๋ณ„๋ฒˆํ˜ธ๋ผ๋„ ๋ถ€๋ชจ๊ฐ€ ์•Œ์•„๊ฐ€์•ผ ์ž์‹์ด ๋ˆˆ๊ฐ๋Š”  ์Šคํ† ๋ฆฌ๊ฐ€... ใ… ใ… )

๊ทธ๋ฆฌ๊ณ , ๋งค๊ฐœ๋ณ€์ˆ˜์ธ status๋Š” ํฌ์ธํ„ฐ๋กœ ์ „๋‹ฌ๋ฐ›๋Š”๋ฐ, ์ด ๋ถ€๋ถ„์ด NULL์ด ์•„๋‹ˆ๋ฉด

ํ•ด๋‹น ํฌ์ธํ„ฐ์— ์ž์‹ ํ”„๋กœ์„ธ์Šค์˜ ์ถ”๊ฐ€ ์ •๋ณด๊ฐ€ ์ €์žฅ๋œ๋‹ค. (์ถ”๊ฐ€ ์ •๋ณด๊ฐ€ ํ•„์š”์—†๋‹ค๋ฉด NULL ์‚ฌ์šฉ)

 

Status์— ์ €์žฅ๋œ ๋น„ํŠธ๋ฅผ ํ•ด์„ํ•˜๊ธฐ ์œ„ํ•œ ๋งคํฌ๋กœ๋“ค์ด ์กด์žฌํ•˜๊ณ  ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

WIFEXITED / WIFSIGNALED / WIFSTOPPED / WIFCONTINUED /

WEXITSTATUS / WTERMSIG / WSTOPSIG / WCOREDUMP

-> ์ •์ƒ ์ข…๋ฃŒ์‹œ, status์— ์žˆ๋Š” ํ•˜์œ„ 8๋น„ํŠธ์—๋Š” 0์ด ์ €์žฅ + ์ƒ์œ„ 8๋น„ํŠธ์—๋Š” ํ”„๋กœ์„ธ์Šค ์ข…๋ฃŒ ์‹œ๊ทธ๋„ ๋ฒˆํ˜ธ ์ €์žฅ

 ๋น„์ •์ƒ ์ข…๋ฃŒ์‹œ, status์— ์žˆ๋Š” ํ•˜์œ„ 8๋น„ํŠธ์—๋Š” ํ”„๋กœ์„ธ์Šค ์ข…๋ฃŒ ์‹œ๊ทธ๋„ ๋ฒˆํ˜ธ ์ €์žฅ + ์ƒ์œ„ 8๋น„ํŠธ์—๋Š” 0์ด ์ €์žฅ

์ด ์ €์žฅ๋œ ๋น„ํŠธ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์œ„์˜ ๋งคํฌ๋กœ๋“ค์ด ํ•ด์„ํ•ด์ค€๋‹ค๋Š” ์ ์„ ์•Œ์•„๋‘๋ฉด ๋œ๋‹ค.


#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(void)
{
    int status;
    
    if(fork() == 0)
    {
        printf("I'm child\n");
    }
    else
    {
        printf("I'm parent\n");
        wait(&status);
        printf("CHILD ENDED\n");
    }
    printf("**********\n");
    return (0);
}

 

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

์šฐ์„ , parent๋Š” else์˜ ๋ช…๋ น์„ ์‹คํ–‰ํ•˜๊ณ , I'm parent๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  wait๋ฅผ ๋งŒ๋‚˜๋ฉด, ์ž์‹ ํ”„๋กœ์„ธ์Šค๊ฐ€ return (0)์„ ๋งŒ๋‚˜

๋๋‚  ๋•Œ๊นŒ์ง€, ๊ธฐ๋‹ค๋ ค์ค€๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ž์‹ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ถœ๋ ฅํ•  ๋‘์ค„์„ ์ถœ๋ ฅํ•œ ๋’ค์—์„œ์•ผ ๋ถ€๋ชจ ํ”„๋กœ์„ธ์Šค์˜ ๋‚˜๋จธ์ง€ ์ค„์ด ์ถœ๋ ฅ๋œ๋‹ค.

 

์ถœ๋ ฅ - std_output)

I'm parent
I'm child
**********
CHILD ENDED
**********

 

ํ”„๋กœ์„ธ์Šค์˜ ํ๋ฆ„์„ ์ถœ๋ ฅ์„ ํ†ตํ•ด ์‰ฝ๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค.


#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(void)
{
    int status;
    pid_t pid, result_pid;
    if((pid = fork()) == 0)
    {
        exit(7);
    }
    printf("fork๋œ child์˜ pid : %d\n", pid);
    result_pid = wait(&status);
    if(WIFEXITED(status))
    {
        printf("exit๋œ child์˜ pid : %d\n", result_pid);
        printf("status ๊ฐ’ : %x\n", status);
        printf("๋งคํฌ๋กœ๋ฅผ ํ†ตํ•œ exit status : %d\n", WEXITSTATUS(status));
    }
    else
    {
    	printf("๋น„์ •์ƒ์  ์ข…๋ฃŒ\n");
    }
    return (0);
}

 

์ถœ๋ ฅ - std_output)

fork๋œ child์˜ pid : 8143
exit๋œ child์˜ pid : 8143
status ๊ฐ’ : 700
๋งคํฌ๋กœ๋ฅผ ํ†ตํ•œ exit status : 7

 

fork ์˜ ๊ฒฐ๊ณผ๋„ pid์— ์ €์žฅํ•ด์ฃผ๊ณ , wait์˜ ๊ฒฐ๊ณผ๋„ result_pid์— ์ €์žฅํ•˜์—ฌ ์„œ๋กœ ๋น„๊ตํ•ด์ฃผ๋ฉด ๋™์ผํ•จ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ , ์ž์‹ ํ”„๋กœ์„ธ์Šค๋Š” exit(7)์œผ๋กœ ์ข…๋ฃŒ์‹œ์ผœ์ฃผ๋Š”๋ฐ, ์ด์— ๋”ฐ๋ผ status์—๋Š” ์ƒ์œ„ 8๋น„ํŠธ์— 7์ด ์ €์žฅ๋˜๊ณ 

ํ•˜์œ„ 8๋น„ํŠธ์— 0์ด ์ €์žฅ๋˜์—ˆ๋‹ค. 

 

๊ทธ๋Ÿผ ๊ทธ status ๊ฐ’์„ ํ™•์ธํ•ด๋ณด์ž. 

 

if๋ฌธ ์กฐ๊ฑด์œผ๋กœ WIFEXITED๋ฅผ ๋„ฃ์—ˆ๋Š”๋ฐ, ์ด๋Š” exit๋กœ ์ •์ƒ ์ข…๋ฃŒ๋˜๋ฉด ์ฐธ, ๋น„์ •์ƒ ์ข…๋ฃŒ๋ฉด ๊ฑฐ์ง“์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋งคํฌ๋กœ๋‹ค.

๋”ฐ๋ผ์„œ ์ •์ƒ์ข…๋ฃŒ ์‹œ, ํ•ด๋‹น ๋ฌธ๊ตฌ๋“ค์„ printf๋กœ ์ถœ๋ ฅํ•˜๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ status ๊ฐ’๋“ค์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์šฐ์„  %x๋ฅผ ํ†ตํ•ด 16์ง„์ˆ˜๋กœ ์ถœ๋ ฅํ•ด๋ณด๋ฉด, 700์ด ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. (์ƒ์œ„ 8๋น„ํŠธ : 7 / ํ•˜์œ„ 8๋น„ํŠธ : 0)

๊ทธ ํ›„, WEXITSTATUS ๋งคํฌ๋กœ๋กœ ์ข…๋ฃŒ ์ฝ”๋“œ๋ฅผ ๋‹ด์•„ ์ œ๊ณตํ•˜๋ฉด 7์ด ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.


์ฐธ๊ณ )

https://blog.naver.com/skout123/50133478563

https://jobdong7757.tistory.com/102

https://velog.io/@shinkoh98/wait-%ED%95%A8%EC%88%98

https://blogshine.tistory.com/98

https://wildeveloperetrain.tistory.com/180