Do You Coding?
[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
'CS & Engineering > C' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [library] substr ๊ตฌํํ๊ธฐ (0) | 2024.06.18 |
|---|---|
| [function] waitpid ํจ์ ์์๋ณด๊ธฐ (2) | 2024.06.13 |
| [function] unlink ํจ์ ์์๋ณด๊ธฐ (2) | 2024.06.07 |
| [function] pipe ํจ์ ์์๋ณด๊ธฐ (0) | 2024.06.06 |
| [function] fork ํจ์ ์์๋ณด๊ธฐ (2) | 2024.06.04 |