์ค์ต ์ ํ์ํ Process ๊ด๋ จ ๋ฐฐ๊ฒฝ์ง์
โ
Process์ ๋ํ ๊น์ ์ดํด๋ฅผ ์ํด ๊ด๋ จ ์ค์ต์ ์งํํ๋ค.
โ
์ค์ต์ ํ์ํ ๋ฐฐ๊ฒฝ์ง์์ ๊ฐ๋จํ ์ ๋ฆฌํ๋ฉด,
โ
- Process: RAM์ ์ ์ฅ๋์ด ์๋ ํ๋ก๊ทธ๋จ์ด CPU์ load ๋ ์ํ
- PCB(Process Counter Block): ๋ ์ง์คํฐ ์ ๋ณด, ํ์ฌ process ์ ๋ณด ๋ฑ์ด ๋ด๊ธด ๊ณณ
- Context-switch(๋ฌธ๋งฅ ๊ตํ): ํ๋์ PCB loadํ๊ณ ๋ค์ PCB๋ฅผ store. ์ด๋ ๊ฒ PCB ๋ค์ ๋ค์ ํด์ฃผ๋ ๋ฌธ๋งฅ ๊ตํ์ ํตํด time sharing ํ๋ฉฐ concurrent ํ๊ฒ ์คํ๋จ
โ
UNIX์ ๊ฐ์ O/S์์ ์๋ก์ด process๋ fork( )๋ผ๋ ์์คํ ์ฝ์ ํตํด ๋ง๋ค์ด์ง๋ค.
์ด๋ ํ์ฌ process์ ์ฃผ์ ๊ณต๊ฐ์ ๋ณต์ฌํด์ child process ๋ง๋ ๋ค.
โ
๊ทธ๋ผ ์ด์ ๋ณธ๊ฒฉ์ ์ผ๋ก ์ค์ต์ ํตํด process์ ๋ํด ๊น์ด ์๊ฒ ์ดํดํด๋ณด์.
์ค์ต1.
#include <stdio.h>
#include <unistd.h>
int main()
{
pid_t pid;
pid = fork();
printf("Hello, Process!\n");
return 0;
}
Hello, Process!
Hello, Process!
๐น fork()๊ฐ ๋์ํ๋ ๋ฐฉ์
โ
- fork()๋ฅผ ํธ์ถํ๋ฉด ํ์ฌ ํ๋ก์ธ์ค๋ฅผ ๊ทธ๋๋ก ๋ณต์ ํ ์๋ก์ด ํ๋ก์ธ์ค(์์ ํ๋ก์ธ์ค)๊ฐ ์์ฑ๋จ
- ์๋ก ์์ฑ๋ ์์ ํ๋ก์ธ์ค๋ ๋ถ๋ชจ ํ๋ก์ธ์ค(P0)์ ๋ชจ๋ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ๊ทธ๋๋ก ๋ณต์ฌํด์ ๊ฐ์ง.์ฆ, ๋ถ๋ชจ ํ๋ก์ธ์ค๊ฐ ๊ฐ์ง๊ณ ์๋ ์ฝ๋, ๋ณ์ ๊ฐ, ํ๋ก๊ทธ๋จ ์ํ ๋ฑ์ด ๋๊ฐ์ด ๋ณต์ฌ๋ ์๋ก์ด ํ๋ก์ธ์ค(P1)๊ฐ ๋ง๋ค์ด์ง
- ํ์ง๋ง! ๋ถ๋ชจ์ ์์ ํ๋ก์ธ์ค๋ ๋ณ๊ฐ๋ก ์คํ๋จ.์ฆ, fork() ์ดํ๋ถํฐ ๋ถ๋ชจ์ ์์์ด ๊ฐ์ ๋ ๋ฆฝ์ ์ผ๋ก ์คํ๋จ
โ
๐น fork() ์ดํ์ ์คํ ํ๋ฆ
#include <stdio.h>#include <unistd.h>int main()
{
pid_t pid;
pid = fork(); // ์๋ก์ด ํ๋ก์ธ์ค(P1) ์์ฑ
printf("Hello, Process!\n");
return 0;
}
๐ fork() ์ดํ์ ํ๋ฆ:
โ
- fork()๊ฐ ์คํ๋๋ฉด ํ์ฌ ์คํ ์ค์ธ ํ๋ก์ธ์ค(P0)๋ฅผ ๊ทธ๋๋ก ๋ณต์ ํ ์์ ํ๋ก์ธ์ค(P1)๊ฐ ์์ฑ๋จ.
- ๊ทธ ํ fork() ๋ค์ ์ค๋ถํฐ ๋ถ๋ชจ(P0)์ ์์(P1) ๋ชจ๋ ์คํ์ ๊ณ์ํจ.
- ์ฆ, printf("Hello, Process!\n"); ์ด ์ฝ๋๋ ๋ถ๋ชจ(P0)์์๋ ์คํ๋๊ณ , ์์(P1)์์๋ ์คํ๋จ. ๊ทธ ๊ฒฐ๊ณผ, "Hello, Process!\\n"๊ฐ ๋ ๋ฒ ์ถ๋ ฅ๋จ
๐น fork() ๋ฐํ๊ฐ์ด ์ค์ํ ์ด์
โ
fork() ํจ์๋ ์คํ๋ ๋ ๋ถ๋ชจ์ ์์์์ ๊ฐ๊ฐ ๋ค๋ฅธ ๊ฐ์ ๋ฐํํจ
์คํ ์์น
|
fork( ) ๋ฐํ๊ฐ
|
๋ถ๋ชจ ํ๋ก์ธ์ค (P0)
|
์์ ํ๋ก์ธ์ค์ PID (์์ ์ ์)
|
์์ ํ๋ก์ธ์ค (P1)
|
0
|
์ฆ,
- ๋ถ๋ชจ(P0)์์๋ pid = fork(); ์คํ ํ pid์๋ ์์ ํ๋ก์ธ์ค์ PID(์์) ๊ฐ ์ ์ฅ๋จ.
- ์์(P1)์์๋ pid = fork(); ์คํ ํ pid์๋ 0์ด ์ ์ฅ๋จ.
โ
์ด๋ฅผ ์ด์ฉํด์ ๋ถ๋ชจ์ ์์์ ๋ถ๋ฆฌ๋ ์คํ ํ๋ฆ์ผ๋ก ์ ์ดํ ์ ์์
โ
์๋ฅผ ๋ค์ด:
#include <stdio.h>#include <unistd.h>int main()
{
pid_t pid;
pid = fork();
if (pid > 0)
{
printf("๋ถ๋ชจ ํ๋ก์ธ์ค: PID = %d\n", getpid());
}
else if (pid == 0)
{
printf("์์ ํ๋ก์ธ์ค: PID = %d\n", getpid());
}
return 0;
}
๋ถ๋ชจ ํ๋ก์ธ์ค: PID = 1234
์์ ํ๋ก์ธ์ค: PID = 5678
์ฒ๋ผ ๋ถ๋ชจ์ ์์ ํ๋ก์ธ์ค๋ฅผ ๊ตฌ๋ถํ ์ ์์
๐น ๊ฒฐ๋ก
โ
- fork()๋ฅผ ์คํํ๋ฉด ํ์ฌ ํ๋ก์ธ์ค(P0)๋ฅผ ๊ทธ๋๋ก ๋ณต์ฌํ ์๋ก์ด ํ๋ก์ธ์ค(P1)๊ฐ ์์ฑ๋จ.
- fork() ์ดํ์๋ ๋ถ๋ชจ(P0)์ ์์(P1)์ด ๋ ๋ฆฝ์ ์ผ๋ก ์คํ๋๋ฏ๋ก printf("Hello, Process!\n");๋ ๋ ๋ฒ ์คํ๋จ.
- fork()์ ๋ฐํ๊ฐ์ ์ฌ์ฉํ๋ฉด ๋ถ๋ชจ์ ์์ ํ๋ก์ธ์ค๋ฅผ ๊ตฌ๋ถํ ์ ์์.
์ค์ต2.
#include <stdio.h>
#include <unistd.h>
int main()
{
pid_t pid;
pid = fork();
printf("Hello, Process! %d\n", pid);
}
Hello, Process! 140755
Hello, Process! 0
โ
์ฆ, ์ ๋ฆฌํ๋ฉด fork( ) system call ์ดํ,
- parent process์ address space๋ฅผ ๊ทธ๋๋ก ๋ณต์
- parent process๋ ๊ณ์ ์คํ๋จ
- ๋๋ child process๊ฐ ์คํ๋๋ ๋์ wait( ) system call์ ํ ์ ์์. ready queue์์ wait queue๋ก ๋น ์ ธ์ child๊ฐ ์ข ๋ฃ๋ ๋๊น์ง ๊ธฐ๋ค๋ฆผ.
์ค์ต3.
#include <unistd.h>
int main()
{
pid_t pid;
pid = fork();
if (pid > 0)
wait(NULL);
printf("Hello, Process! %d\n", pid);
}
Hello, Process! 0
Hello, Process! 141055
(์์๊ฐ ๋ฐ๋)
parent๊ฐ wait queue์ ๋ค์ด๊ฐ ์๋ค๊ฐ child ์คํ ์ดํ parent๊ฐ ์คํ๋จ
์ฐธ๊ณ : fork ๋ ์ดํ parent์ child๋ ๋ ๋ฆฝ์ . ์ฆ ๊ฐ์๊ฐ ๋ฐ๋๋ ๊ฒ์ด ์๋ก์๊ฒ ์ํฅ์ ๋ฏธ์น์ง ์์.
์ค์ต4.
#include <stdio.h>
#include <unistd.h>
int main()
{
fork();
fork();
fork();
fork();
printf("Hello, fork\n");
return 0;
}
Hello, fork
Hello, fork
Hello, fork
Hello, fork
Hello, fork
Hello, fork
Hello, fork
Hello, fork
Hello, fork
Hello, fork
Hello, fork
Hello, fork
Hello, fork
Hello, fork
Hello, fork
Hello, fork
hello fork 16๋ฒ ์ถ๋ ฅ. forkํ๋ฉด ๋ง๋ค์ด์ง ์์๋ ์์์ ๋ง๋ค๊ธฐ ๋๋ฌธ์ 2์ 4์น์ด ๋จ
์ค์ต5
#include <stdio.h>
#include <unistd.h>
int main()
{
pid_t pid;
pid = fork();
if(pid == 0) {
execlp("/bin/ls", "ls", NULL);
printf("LINE J\n");
}
else if (pid > 0) {
wait(NULL);
printf("Child Complete\n");
}
return 0;
}
execlp๋ fork๋ก ๋ณต์ฌ๋ child process์ ๋ด์ฉ์ด ์์ ๋ค๋ฅธ ๋ด์ฉ์ผ๋ก ๋ฎ์ด์ง๋ ๊ฒ. ์ ์์ ์์ “Line J”๋ ์คํ๋์ง ์์. execlp๋ก ๋ฎ์ด์ ธ execlp ๊ทธ ๋ค์ ๋ด์ฉ์ child process์ ์กด์ฌํ์ง ์๊ธฐ ๋๋ฌธ.
์ค์ต6
#include <stdio.h>
#include <unistd.h>
int main()
{
pid_t pid, pid1;
pid = fork();
if(pid == 0) { //child process
pid1 = getpid();
printf("child: pid = %d\n", pid);
printf("child: pid1 = %d\n", pid1);
}
else if (pid > 0) { //parent process
pid1 = getpid();
printf("parent: pid = %d\n", pid);
printf("parent: pid1 = %d\n", pid1);
wait(NULL);
}
return 0;
}
parent: pid = 142323
parent: pid1 = 142322
child: pid = 0
child: pid1 = 142323
์ค์ต7.
#include <stdio.h>
#include <unistd.h>
#define SIZE 5
int nums[SIZE] = {0, 1, 2, 3, 4};
int main()
{
pid_t pid;
int i;
pid = fork();
if(pid == 0) { //child process
for ( i = 0 ; i < SIZE ; i++) {
nums[i] *= i;
printf("CHILD: %d \n", nums[i]);
}
}
else if (pid > 0) { //parent process
wait(NULL);
for( i = 0 ; i < SIZE ; i++) {
printf("PARENT: %d \n", nums[i]);
}
}
return 0;
}
CHILD: 0
CHILD: 1
CHILD: 4
CHILD: 9
CHILD: 16
PARENT: 0
PARENT: 1
PARENT: 2
PARENT: 3
PARENT: 4
๐๐ ๋จธ๋ฆฌ๊ฐ ์ํ๋ ์ค์ต์ ํด๋ณด๋ฉด ํ์คํ ์๋ฆฌ๊ฐ ์๋ฟ๊ธด ํ๋ค. ์ฌ๋ฌ๋ถ๋ค๋ ๋์ ํด๋ณด์๊ธธ...!
โ
์์ ์์ ๊ฐ ๋ชจ๋ ์ดํด๋๋ฉด ์๋์ ๊ฐ์ ๋ฌธ์ ๋ฅผ ํ์ด๋ณด๋ ๊ฒ๋ ๋์์ด ๋ง์ด ๋๋ค.