A program loaded in the memory.
process = body + process descriptor
body = code + data + stack
ps -ef
๋ช
๋ น์ด๋ ํ์ฌ ๋ฉ๋ชจ๋ฆฌ์ ๋ก๋๋ ๋ชจ๋ ํ๋ก์ธ์ค๋ฅผ ๋ณด์ฌ์ค๋ค.ps -f
๋ช
๋ น์ด๋ ํ์ฌ ํฐ๋ฏธ๋์์ ์คํ ์ค์ธ ํ๋ก์ธ์ค๋ฅผ ๋ํ๋ธ๋ค.fork
: ํ๋ก์ธ์ค๋ฅผ ๋ณต์ exec
: ํ๋ก์ธ์ค๋ฅผ ๋ค๋ฅธ ํ๋ก์ธ์ค๋ก ๋ณํexit
: ํ๋ก์ธ์ค ์ค์งwait
: ์์ ํ๋ก์ธ์ค(child process)๊ฐ ์ข
๋ฃ๋ ๋๊น์ง ๋๊ธฐgetpid
: process ID๋ฅผ getgetppid
: parent process ID๋ฅผ getfork()
:fork()
ex0.c:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(){
int x,y;
x=getpid();
y=getppid();
printf("PID:%d PPID:%d\n", x, y);
for(;;); // to make this process alive
}
$ gcc โo ex0 ex0.c
Run โex0โ with &
. &
puts process in the background so that you can issue next command to the shell.
$ ex0&
[1] 2950
PID:2950 PPID:2219
Now confirm with ps โf
.
$ ps โf
UID PID PPID C STIME TTY TIME CMD
502 1358 4160 0 9:19AM ttys000 0:00.37 /bin/zsh -l
502 1405 1 0 9:19AM ttys000 0:00.01 /bin/zsh -l
502 2219 2217 0 9:24AM ttys001 0:00.61 -zsh
502 2258 1 0 9:24AM ttys001 0:00.01 -zsh
502 2950 2219 0 9:28AM ttys001 0:15.66 ./ex0
Now kill โex0โ.
$ kill 2950
[1] + 2950 terminated ./ex0
,where xxxx is the pid of โex0โ.
์ ์ฌ์ง์ฒ๋ผ โex0โ๋ฅผ kill
ํ ํ, ps โf
๋ฅผ ํตํด ํ์ธํด๋ณด๋ฉด ์ ์์ ์ผ๋ก ์ข
๋ฃ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
ex1.c:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(){
int x;
x=fork();
printf("hello\n");
for(;;);
}
$ gcc โo ex1 ex1.c
$ ex1&
hello
hello
$ ps -f
502 1358 4160 0 9:19AM ttys000 0:00.37 /bin/zsh -l
502 1405 1 0 9:19AM ttys000 0:00.01 /bin/zsh -l
502 2219 2217 0 9:24AM ttys001 0:00.74 -zsh
502 2258 1 0 9:24AM ttys001 0:00.01 -zsh
502 4653 2219 0 9:39AM ttys001 0:08.18 ./ex1
502 4662 4653 0 9:39AM ttys001 0:08.07 ./ex1
$ kill xxxx(pid of ex1) yyyy(pid of ex1's child)
fork
๋ฅผ ํตํด process๋ฅผ ๋ณต์ ํ์ฌ printf("hello\n");
๊ฐ 2๋ฒ ์คํ๋์๊ธฐ ๋๋ฌธ์ด๋ค.PID
of โex1โ and โex1โโs child?
PID
of โex1โ : 4653PID
of โex1โโs child : 4662
PID
๊ฐ 4662์ธ ๊ฒ์ PPID
๊ฐ 4653์ด๊ธฐ ๋๋ฌธ. ์ฆ, PID
๊ฐ 4662์ธ ๊ฒ์ด PID
๊ฐ 4653์ธ ๊ฒ์ผ๋ก๋ถํฐ ๋ณต์ ๋ ๊ฒ์ด๋ค.PPID
๊ฐ 2219์ด๋ฏ๋ก โex1โ์ parent๋ โ-zshโ์ด๋ค.ps -ef
.#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(){
int x;
x=fork();
printf("hello. my pid is %d\n", getpid());
printf("and my parent pid is %d\n", getppid());
for(;;);
}
$ ps -ef
UID PID PPID C STIME TTY TIME CMD
0 1 0 0 ํ 10AM ?? 6:26.11 /sbin/launchd
...
502 23834 1 0 2:08PM ?? 3:05.88 /Applications/iTerm 2.app/Contents/MacOS/iTerm2
502 23835 23834 0 2:08PM ?? 0:00.02 /Users/oneonlee/Library/Application Support/iTerm2/iTermServer-3.4.15 /Users/oneonlee/Library/Application Support/iTerm2/iterm2-daemon-1.socket
...
502 1358 4160 0 9:19AM ttys000 0:00.37 /bin/zsh -l
502 1405 1 0 9:19AM ttys000 0:00.01 /bin/zsh -l
0 2217 23835 0 9:24AM ttys001 0:00.36 login -fp oneonlee
502 2219 2217 0 9:24AM ttys001 0:00.97 -zsh
502 2258 1 0 9:24AM ttys001 0:00.01 -zsh
502 7759 2219 0 9:57AM ttys001 0:10.25 ./ex2
502 7760 7759 0 9:57AM ttys001 0:09.97 ./ex2
0 8254 2219 0 10:00AM ttys001 0:00.01 ps -ef
-zsh
(2219)-zsh
(2219)์ parent : login -fp oneonlee
(2217)login -fp oneonlee
(2217)์ parent : /Users/oneonlee/Library/Application Support/iTerm2/iTermServer-3.4.15 /Users/oneonlee/Library/Application Support/iTerm2/iterm2-daemon-1.socket
(23835)PID 0
and show all of them.
/Users/oneonlee/Library/Application Support/iTerm2/iTermServer-3.4.15 /Users/oneonlee/Library/Application Support/iTerm2/iterm2-daemon-1.socket
(23835)์ parent : /Applications/iTerm 2.app/Contents/MacOS/iTerm2
(23834)/Applications/iTerm 2.app/Contents/MacOS/iTerm2
(23834)์ parent : /sbin/launchd
(1)#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(){
int x;
x=fork();
printf("hello: %d\n", x);
}
parent์ child์ PID๋ฅผ ํ์ธํ๊ธฐ ์ํด ์ฃผ์ด์ง ์ฝ๋์ for(;;);
๋ฌดํ๋ฃจํ๋ฅผ ์ถ๊ฐํ์ฌ ํ๋ก๊ทธ๋จ์ด ์ข
๋ฃ๋์ง ์๊ฒ ํ์๋ค.
$ gcc โo ex2 ex2.c
$ ex2
hello: 36711
hello: 0
fork()
๋ ์ฑ๊ณตํ ๊ฒฝ์ฐ ์์์ PID๋ฅผ ๋ถ๋ชจ์๊ฒ ์ ๋ฌํ๋ฉฐ, ์์์ 0์ ๋ฐํ ๋ฐ๋๋ค.
๋ฐ๋ผ์ โhello: 36711โ๋ฅผ ์ถ๋ ฅํ ๊ฒ์ด parent์ด๋ฉฐ(PID:36710), โhello: 0โ๋ฅผ ์ถ๋ ฅํ ๊ฒ์ด child์ด๋ค. (PID:36711)
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(){
int x;
x=fork();
printf("hello: %d\n", x);
for(;;); // make the parent and child alive
}
$ gcc โo ex3 ex3.c
$ ex3 &
hello: 36711
hello: 0
$ ps -ef
UID PID PPID C STIME TTY TIME CMD
502 35338 35336 0 10:32AM ttys001 0:00.47 -zsh
502 36710 35338 0 10:39AM ttys001 0:06.08 ./ex3
502 36711 36710 0 10:39AM ttys001 0:06.00 ./ex3
PPID๋ฅผ ๋ฐ๋ผ๊ฐ๋ฉฐ ps์ ์ถ๋ ฅ๊ฐ์ ์ฝ์ด๋ณด๋ฉด, zsh ํ๋ก์ธ์ค(35338)๋ก๋ถํฐ โex3(36710)โ ํ๋ก์ธ์ค๊ฐ ์์ฑ๋์์ผ๋ฉฐ โex3(36710)โ๋ถํฐ โex3(36711)โ ํ๋ก์ธ์ค๊ฐ ์์ฑ๋์๋ค.
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(){
int x;
x=fork();
if (x==0){
printf("hello: %d\n", x);
}else{
printf("hi: %d \n", x);
}
}
$ gcc โo ex4 ex4.c
$ ex4
x
๋ fork()
๋ก๋ถํฐ ๋ฐํ๋ ๊ฐ์ด๊ธฐ ๋๋ฌธ์ x
๊ฐ 0
์ด๋ฉด ํ์ฌ ํ๋ก์ธ์ค๋ ์์ ํ๋ก์ธ์ค์ด๊ณ , 0
์ด ์๋๋ฉด ๋ถ๋ชจ ํ๋ก์ธ์ค์ด๋ค. ๋ฐ๋ผ์ โhiโ๋ ๋ถ๋ชจ ํ๋ก์ธ์ค์์ ์ถ๋ ฅ๋ ๊ฐ์ด๊ณ , โhelloโ๋ ๋ณต์ ๋ ์์ ํ๋ก์ธ์ค์์ ์ถ๋ ฅ๋ ๊ฐ์ด๋ค.
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(){
int x,y;
x=fork();
y=fork();
printf("hello: %d %d\n", x, y);
}
$ gcc โo ex5 ex5.c
$ ex5
hello: 41139 41140
hello: 41139 0
hello: 0 41141
hello: 0 0
fork()
๋ฅผ ํ ๋ฒํ๋ฉด ์ด 2๊ฐ์ ํ๋ก์ธ์ค๊ฐ ์์ฑ๋๋ค.fork()
๋ฅผ ๋ ๋ฒ ํ๊ฒ ๋๋ฉด, ์ฒ์์ ์๊ธด ํ๋ก์ธ์ค ๊ฐ๊ฐ fork()
๋ฅผ ์ํํ๊ฒ ๋๋ฏ๋ก โ4 (= 2^2)โ๊ฐ์ ํ๋ก์ธ์ค๊ฐ ์์ฑ๋๋ค.x
, y
๋ชจ๋ 0
์ด ์๋๊ธฐ ๋๋ฌธ์ ์ต์ด์ ๋ถ๋ชจ ํ๋ก์ธ์ค๋ก๋ถํฐ ์์ฑ๋ ๊ฒ์ด๋ค.x
๊ฐ 0
์ด๊ธฐ ๋๋ฌธ์ ์ฒซ ๋ฒ์งธ fork()
๋ก ๋ณต์ฌ๋ ํ๋ก์ธ์ค์์ ์์ฑ๋ ๊ฒ์ผ๋ก, ๋ ๋ฒ์งธ ์ถ๋ ฅ์ 1๋ฒ๋ง ๋ณต์ฌ๋ ๊ฒ, ์ธ ๋ฒ์งธ๋ 2๋ฒ ๋ณต์ฌ๋ ํ๋ก์ธ์ค์ด๋ค.fork()
์์ ๋ณต์ฌ๋ ํ๋ก์ธ์ค์์ ์ถ๋ ฅ๋ ๊ฒ์ x
๋ 0
์ด ์๋์ง๋ง y
๋ 0
์์์ ์ ์ ์๋ค.#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(){
int x,y;
x=fork();
printf("hello: %d\n", x);
y=fork();
printf("hello: %d\n", y);
}
$ gcc โo ex6 ex6.c
$ ex6
hello: 44973
hello: 44974
hello: 0
hello: 0
hello: 44975
hello: 0
fork()
๋ก๋ถํฐ ์์ฑ๋ ์์ ํ๋ก์ธ์ค๋ก๋ถํฐ ์ถ๋ ฅ๋์๋ค.#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main(){
int x, i;
for(i=0;i<5;i++){
x=fork();
if (x==0){ // child
int k;
for(k=0;k<10;k++){
printf("%d-th child running %d-th iteration \n", i, k);
fflush(stdout); // to make printf work immediately
// sleep(1); // sleep 1 second
}
printf("\n\n")
exit(0); // child exits after 10 iterations
}
}
// now parent
printf("parent exits\n");
}
$ ./ex9
0-th child running 0-th iteration
1-th child running 0-th iteration
3-th child running 0-th iteration
parent exits
4-th child running 0-th iteration
2-th child running 0-th iteration
0-th child running 1-th iteration
1-th child running 1-th iteration
3-th child running 1-th iteration
4-th child running 1-th iteration
2-th child running 1-th iteration
0-th child running 2-th iteration
1-th child running 2-th iteration
3-th child running 2-th iteration
4-th child running 2-th iteration
2-th child running 2-th iteration
0-th child running 3-th iteration
1-th child running 3-th iteration
3-th child running 3-th iteration
4-th child running 3-th iteration
2-th child running 3-th iteration
0-th child running 4-th iteration
1-th child running 4-th iteration
3-th child running 4-th iteration
4-th child running 4-th iteration
2-th child running 4-th iteration
0-th child running 5-th iteration
1-th child running 5-th iteration
3-th child running 5-th iteration
4-th child running 5-th iteration
2-th child running 5-th iteration
0-th child running 6-th iteration
1-th child running 6-th iteration
3-th child running 6-th iteration
4-th child running 6-th iteration
2-th child running 6-th iteration
0-th child running 7-th iteration
1-th child running 7-th iteration
3-th child running 7-th iteration
4-th child running 7-th iteration
2-th child running 7-th iteration
0-th child running 8-th iteration
1-th child running 8-th iteration
3-th child running 8-th iteration
4-th child running 8-th iteration
2-th child running 8-th iteration
0-th child running 9-th iteration
1-th child running 9-th iteration
3-th child running 9-th iteration
4-th child running 9-th iteration
2-th child running 9-th iteration
fork()
๋ฅผ ์ด 5๋ฒ ์ํํ๋ฏ๋ก ์ต์ข
์ ์ผ๋ก๋ ํ๋์ ๋ถ๋ชจ ํ๋ก์ธ์ค์ 5๊ฐ์ ์์ ํ๋ก์ธ์ค๊ฐ ์์ฑ๋๋ค. ๋ถ๋ชจ ํ๋ก์ธ์ค๋ โparent exitsโ๋ฅผ ์ถ๋ ฅํ๊ณ ์์ ํ๋ก์ธ์ค๋ณด๋ค ๋จผ์ ์ข
๋ฃ๋๋ค. ์ด๋ ์์ ํ๋ก์ธ์ค๋ ๋ชจ๋ ๋ถ๋ชจ๋ฅผ ์์ด๋ฒ๋ฆฐ ๊ณ ์ ํ๋ก์ธ์ค๊ฐ ๋์ด PPID๊ฐ init ํ๋ก์ธ์ค์ PID์ธ 1
์ ๊ฐ๋ฆฌํค๊ฒ ๋๋ค.
์์ ํ๋ก์ธ์ค๋ 0๋ถํฐ 9๊น์ง ์ํํ๋ฉฐ printf
๋ก ๋ฌธ์์ด์ ์ถ๋ ฅํ๋ค.
์ด๋ fflush(stdout)
๋ ํ์ค์ถ๋ ฅ์คํธ๋ฆผ์ ๋น์์ฃผ๋ ์ญํ ๋ก, ๋ฒํผ์ ๋ฌธ์์ด์ ๋ฃ๊ณ ๊ธฐ๋ค๋ฆฌ์ง ๋ง๊ณ ๋ฐ๋ก ์ถ๋ ฅํ๊ธฐ ์ํด ์ฌ์ฉํ๋ค.
sleep(1)
์์ ํ๋ก์ธ์ค๊ฐ ๋ถ๋ชจ ํ๋ก์ธ์ค๋ณด๋ค ๋์ค์ ์ข
๋ฃ๋จ์ ๋ณด์ฅํ๊ธฐ ์ํด ํ๋ก์ธ์ค ์คํ ์๊ฐ์ ๋๋ ์ด๋ฅผ ๋ฃ์ ๊ฒ์ด๋ค.
ํ๋ก์ธ์ค๊ฐ ๋ฐ๋ณต๋ฌธ ์ํ๋ฅผ ๋ง์ณค๋ค๋ฉด exit(0)
๋ฅผ ํตํด ์์ ํ๋ก์ธ์ค๋ฅผ ์ข
๋ฃํ๋ค.
๋ฐ๋ผ์ ํ๋ก๊ทธ๋จ์ ์ด 5๊ฐ์ ์์ ํ๋ก์ธ์ค๋ก๋ถํฐ 5*10๋ฒ printf
๋ฅผ ํธ์ถํ ๊ฒ์ด๋ค.
exit(0)
in โex7.cโ, how many processes will be created? Confirm your answer by modifying the code such that each process displays its own pid
.$ ./ex7-1
0-th child running 0-th iteration(pid: 79605)
1-th child running 0-th iteration(pid: 79606)
2-th child running 0-th iteration(pid: 79607)
parent exits
3-th child running 0-th iteration(pid: 79608)
4-th child running 0-th iteration(pid: 79609)
0-th child running 1-th iteration(pid: 79605)
2-th child running 1-th iteration(pid: 79607)
3-th child running 1-th iteration(pid: 79608)
4-th child running 1-th iteration(pid: 79609)
1-th child running 1-th iteration(pid: 79606)
0-th child running 2-th iteration(pid: 79605)
3-th child running 2-th iteration(pid: 79608)
2-th child running 2-th iteration(pid: 79607)
4-th child running 2-th iteration(pid: 79609)
1-th child running 2-th iteration(pid: 79606)
0-th child running 3-th iteration(pid: 79605)
2-th child running 3-th iteration(pid: 79607)
4-th child running 3-th iteration(pid: 79609)
1-th child running 3-th iteration(pid: 79606)
3-th child running 3-th iteration(pid: 79608)
2-th child running 4-th iteration(pid: 79607)
4-th child running 4-th iteration(pid: 79609)
1-th child running 4-th iteration(pid: 79606)
0-th child running 4-th iteration(pid: 79605)
3-th child running 4-th iteration(pid: 79608)
4-th child running 5-th iteration(pid: 79609)
1-th child running 5-th iteration(pid: 79606)
3-th child running 5-th iteration(pid: 79608)
0-th child running 5-th iteration(pid: 79605)
2-th child running 5-th iteration(pid: 79607)
1-th child running 6-th iteration(pid: 79606)
3-th child running 6-th iteration(pid: 79608)
0-th child running 6-th iteration(pid: 79605)
4-th child running 6-th iteration(pid: 79609)
2-th child running 6-th iteration(pid: 79607)
0-th child running 7-th iteration(pid: 79605)
3-th child running 7-th iteration(pid: 79608)
2-th child running 7-th iteration(pid: 79607)
4-th child running 7-th iteration(pid: 79609)
1-th child running 7-th iteration(pid: 79606)
3-th child running 8-th iteration(pid: 79608)
2-th child running 8-th iteration(pid: 79607)
4-th child running 8-th iteration(pid: 79609)
1-th child running 8-th iteration(pid: 79606)
0-th child running 8-th iteration(pid: 79605)
0-th child running 9-th iteration(pid: 79605)
3-th child running 9-th iteration(pid: 79608)
1-th child running 9-th iteration(pid: 79606)
4-th child running 9-th iteration(pid: 79609)
2-th child running 9-th iteration(pid: 79607)
parent exits
parent exits
4-th child running 0-th iteration(pid: 79635)
2-th child running 0-th iteration(pid: 79638)
1-th child running 0-th iteration(pid: 79636)
3-th child running 0-th iteration(pid: 79637)
parent exits
parent exits
3-th child running 0-th iteration(pid: 79639)
4-th child running 0-th iteration(pid: 79640)
2-th child running 0-th iteration(pid: 79641)
4-th child running 0-th iteration(pid: 79642)
3-th child running 0-th iteration(pid: 79643)
parent exits
...
$ ./ex7-1 | grep -c "iteration"
310
๋ง์ฝ exit(0))
๋ฅผ ์ ๊ฑฐํ๋ค๋ฉด ์์ ํ๋ก์ธ์ค๊ฐ 10๋ฒ printf
๋ฅผ ํ ์ดํ์ ๋ค์ ์์ ๋ฐ๋ณต๋ฌธ์ผ๋ก ๋์์ ์๋ก์ด ์์ ํ๋ก์ธ์ค๋ฅผ ์์ฑํ๋ค.
๊ฐ์ฅ ๋ง๋จ์ ์์๋ง printf
๋ฅผ ํ ์ ์์ง๋ง, ๊ฐ์ฅ ๋ง๋จ์ ์์ ํ๋ก์ธ์ค๊ฐ ์๋๋๋ผ๋ fork()
๋ฅผ ํตํด ์๋ก์ด ํ๋ก์ธ์ค๋ฅผ ๊ณ์ ์์ฑํ ์ ์๋ค.
์ด๋ ์ด๋ถ๋ฒ์ผ๋ก ์ฆ์ํ๋ ์ธํฌ ๊ฐ์ด ์๊ฐํ ์ ์๋ค. 5ํ ๋ฐ๋ณต ์ 31(= 2^0+2^1+2^2+2^3+2^4)๊ฐ์ ์์ ํ๋ก์ธ์ค๊ฐ ์์ฑ๋๋ฉฐ, ๋ฐ๋ผ์ 31๊ฐ์ ์์ ํ๋ก์ธ์ค๋ก๋ถํฐ 310๋ฒ printf
๊ฐ ํธ์ถ๋ ๊ฒ์ด๋ค.
$ ./child_first
child: 19019
parent: 19018
See child_first.c (Ref Exercise 3)
$ ./pid_of_children
Child(0)'s PID: 28899
Child(1)'s PID: 28900
Child(2)'s PID: 28901