-
Process๋,
- ํ๋ก๊ทธ๋จ์ด ํ์ผ ํํ๋ก ํ๋๋์คํฌ์ ์ ์ฅ๋ผ์๋ค๊ฐ, ์คํ์ ์ํค๋ฉด process๊ฐ ๋๋ค
- [Program code]
- [Data : ํ๋ก๊ทธ๋จ ์ ์ญ ๋ณ์] : ์ฒ์ ์ปดํ์ผํ ๋๋ถํฐ ๋ฉ๋ชจ๋ฆฌ Data ์์ญ์ ๋ฐ๋ก ์ ์ฅ๋จ. ํ๋ก๊ทธ๋จ์ด ๋๋์ผ ์ฌ๋ผ์ง
- [Stack : ํ๋ก๊ทธ๋จ ์ง์ญ ๋ณ์] : ํจ์์์ ํธ์ถ๋ ๋ณ์๋ค ๋ฉ๋ชจ๋ฆฌ Stack ์์ญ์ ๋ฐ๋ก ์ ์ฅ๋จ. ํจ์ return ์ ์ฌ๋ผ์ง
- [PCB : Process Control Block]
- ํ๋ก์ธ์ค ์ ์ด ๋ธ๋ก
- Process๋ ์ฌ๋ฌ๊ฐ์ง ์์๋ค์ ์ํด ์ ์ผํ๊ฒ ์๋ณ๋ ์ ์๋ค (์๋ณ์, ์ ์ , ์ฐ์ ์์, ํ๋ก๊ทธ๋จ ์นด์ดํฐ, ๋ฉ๋ชจ๋ฆฌ ํฌ์ธํฐ, ๋ฌธ๋งฅ ๋ฐ์ดํฐ, ์ ์ถ๋ ฅ์ํ์ ๋ณด, ์ด์นด์ดํ ์ ๋ณด) ์ด๋ฐ๊ฒ ์๋๋ฐ, ์ด๋ฐ ์์๋ค์ด PCB์ ์ ์ฅ์ด ๋๊ณ , OS์ ์ํด ์์ฑ๋๊ณ ๊ด๋ฆฌ๋๋ค
- PCB์ด ์ฃผ์ ์ญํ ์ process๋ฅผ interruptํ ํ ๋ค์ ๊ทธ process๋ฅผ ์ํ์ฌ๊ฐ ํ ์ ์๊ฒ process์ ์ ๋ณด๋ฅผ ์ ์งํ๋ ๊ฒ. ๋ค์์ process๋ฅผ ์ง์ํ๊ณ , ๋ฉํฐํ๋ก์ธ์ฑ์ ์ ๊ณตํ ์ ์๊ฒ ํด์ค๋ค
- [Process Identification]
- Process Identifier : ํ๋ก์ธ์ค๋ค์ด ์๋ก ํต์ ํ ๋, ํต์ ํ ์๋๋ฐฉ์ OS์๊ฒ ์๋ ค์ฃผ๊ธฐ ์ํด ํ๋ก์ธ์ค ์๋ณ์ ์ฌ์ฉ
- Parent Process Identifier : ํ๋ก์ธ์ค๋ค์ด ๋ค๋ฅธ ํ๋ก์ธ์ค๋ฅผ ์์ฑํ ๋, ์๋ณ์๋ฅผ ์ด์ฉํด์ ๋ถ๋ชจ/์์ ํ๋ก์ธ์ค ๊ตฌ๋ณ ๊ฐ๋ฅ
- User Identifier : ์์ ์ ๋ํด ์ฑ ์์ง๊ณ ์๋ ์ฌ์ฉ์๋ฅผ ์ง์ํ๊ธฐ ์ํด ์ฌ์ฉ์ ์๋ณ์๊ฐ ๊ฐ ํ๋ก์ธ์ค์ ํ ๋น๋ ์ ์๋ค
- [Processor State Information]
- [Process Control Information]
-
Process์ ๊ณ์ธต ๊ตฌ์กฐ : Parent process -> Child proceses ํ๋ก๊ทธ๋จ์ ์คํํ๋ฉด, ํ๋ก์ธ์ค๋ ์ฌ๋ฌ๊ฐ์ child ํ๋ก์ธ์ค๋ฅผ ๋ง๋ค์ด์, ์ฌ๋ฌ๊ฐ์ ํ๋ก์ธ์ค๋ค๋ก ์คํ์ด ๋๋ค
-
UNIX ์์คํ ์ ๋ชจ๋ process๋ [init] ์ด๋ผ๋ 1๋ฒ process์ descendent process์ด๋ค [init] ํ๋ก์ธ์ค๊ฐ [shell] process๋ฅผ ๋ง๋ค๊ณ , shell์ด [child]๋ฅผ ๋ง๋ค์ด์ ํ๋ก๊ทธ๋จ์ ์คํํ๋ค
-
์๋ฅผ ๋ค์ด์ a.out ํ๋ก๊ทธ๋จ์ ์คํํ๋ฉด, init ํ๋ก์ธ์ค๊ฐ shell ํ๋ก์ธ์ค๋ฅผ ๋ง๋ค๊ณ , shell ํ๋ก์ธ์ค๊ฐ a.out์ด๋ผ๋ child ํ๋ก์ธ์ค๋ฅผ ๋ง๋ค์ด์ ์คํํ๋ค
-
Shell์์์ ํ๋ก์ธ์ค ๋ชฉ๋ก ํ์ธ
$ ps
์คํ ๊ฒฐ๊ณผ
PID TTY TIME CMD 16945 pts/7 00:00:00 bash 20763 pts/7 00:00:00 ps -
์คํ ์ค์ธ ํ๋ก์ธ์ค ์ข ๋ฃ ์ํค๊ธฐ
PID๋ฅผ ์์์ผ ์ข ๋ฃ์ํฌ ์ ์๊ธฐ ๋๋ฌธ์ ps๋ก ํ์ธํ๊ณ ํ๋ก์ธ์ค๋ฅผ ์ข ๋ฃ์ํจ๋ค
$ kill -9 [ํ๋ก์ธ์ค ๋ฒํธ : PID] -
Process Identifier
- ์์ด ์๋ ์ ์
- 0 : swapper
-
1 : init
- getpid() : ์ง๊ธ ์คํ ์ค์ธ process ID
-
getppid() : ์ง๊ธ ์คํ ์ค์ธ process์ parent ID
include<unistd.h> pid_t getpid(void); pid_t getppid(void); getpid(); getppid();
-
Process Group
- ํน์ ํ๋ก์ธ์ค๋ค์ ๋ฌถ์ด์ ํ๋์ group์ผ๋ก ๋ง๋ค์ด์, ๊ฐ์ group์ ์ํ process๋ค์๊ฒ ๋์์ signal์ ๋ณด๋ผ ์ ์๋ค ์ด๊ธฐ์ fork๋ exec์ ์ํด group id ๊ณ์น(์์) : ๋ฑํ ๋ช ์ ์ ํด์ฃผ๋ฉด, parent ์๋ ๋ชจ๋ process๋ ๊ฐ์ group์ ๊ทธ๋์ group id๋ฅผ ๋ฐ๊ฟ์ฃผ๋ฉด ๊ทธ ์๋ gid๋ ๋ค ์์์ ๋ฐ์ผ๋ ๋ฐ๋๊ฒ ๋๋ค
-
Group leader : group leader๋ pid์ pgid๋ฅผ ๊ฐ๊ฒ ํด์ค์ผํจ. ๋ฐ๋ผ์, ์์ ์ pid๊ฐ group id์ด๋ฉด group leader์ด๋ค.
#include <sys/types.h> #include <unistd.h> //๋์ group id ์์๋ณด๊ธฐ pid_t getpgrp(void); //๋ค๋ฅธ child์ id๋ฅผ ์ธ์๋ก ๋๊ฒจ์ฃผ๋ฉด, ๊ทธ process์ gid ๋ฐํ //์ธ์๊ฐ 0์ด๋ฉด, ํธ์ถ ํ๋ก์ธ์ค ์์ ์ group id๋ฅผ ๋ฐํ pid_t getpgid(pid_t pid); getpgrp(); getpgid(0);
-
process group ๋ณ๊ฒฝ / setpgid(id, gid);
์ง๊ธ๋ถํฐ ์๋ก์ด group leader๊ฐ ๋๊ฒ ๋ค๋ ๋ช ๋ น
#include <sys/types.h> #include <unistd.h> //์ธ์(๋ด id, ๋ด group id) int setpgid(pid_t pid, pid_t pgid);
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <dirent.h> #include <string.h> #include <ftw.h> int main(int argc, char **argv) { pid_t pid; printf("%ld : %ld\n", getpid(), getppid()); printf("%ld : %ld\n", getpgrp(), getpgid(0)); pid = fork(); //child์ธ ๊ฒฝ์ฐ์ 0 ๋ฐํ if(pid == 0) { printf("%ld : %ld\n", getpid(), getppid()); printf("%ld : %ld\n", getpgrp(), getpgid(0)); //group id ๋ณ๊ฒฝ setpgid(getpid(), getpid()); printf("%ld : %ld\n", getpid(), getppid()); printf("%ld : %ld\n", getpgrp(), getpgid(0)); } wait(0); return 0; }
-
์คํ๊ฒฐ๊ณผ
- ./example.out ์คํํ๋ฉด, shell ํ๋ก์ธ์ค๊ฐ example ํ๋ก์ธ์ค๋ฅผ ์คํ. example์ group์ ๋ฆฌ๋๋ก ์คํ์ํด
29816 : 16945 //example ํ๋ก์ธ์ค id : shell ํ๋ก์ธ์ค id 29816 : 29816 //example ํ๋ก์ธ์ค gid 29817 : 29816 //child ํ๋ก์ธ์ค id : parent์ธ example ํ๋ก์ธ์ค id 29816 : 29816 //child ํ๋ก์ธ์ค gid. example ํ๋ก์ธ์ค์ child์ด๋๊น parent์ gid์ ๊ฐ์ 29817 : 29816 //child ํ๋ก์ธ์ค id : parent์ธ example ํ๋ก์ธ์ค id 29817 : 29817 //child ํ๋ก์ธ์ค๋ฅผ group leader๋ก ๋ง๋ค์ด์ pid์ pgid ๊ฐ์์ง
-
-
Session
-
ํ session์ ํ ๋จ๋ง๊ธฐ๋ฅผ ์ฌ์ฉํ๋ foreground process group๊ณผ background process group์ ์งํฉ์ฒด์ด๋ค
-
๊ฐ process group์ ํ๋์ session์ ์ํ๋ค A [process group] is a collection of related processes which can all be signalled at once.
A session is a collection of process groups, which are either attached to a single terminal device (known as the controlling terminal) or not attached to any terminal.
Sessions are used for job control: one of the process groups in the session is the foreground process group, and can be sent signals by terminal control characters. You can think of a session with a controlling terminal as corresponding to a "login" on that terminal. (Daemons normally disassociate themselves from any controlling terminal by creating a new session without one.)
e.g. if you run
some_appfrom the shell, the shell creates a new process group for it, and makes that the foreground process group of the session. (some_appmight create some child processes; by default they will be part of the same process group.) If you then press^Z,some_app's process group is signalled to stop it; and the shell's process group is switched to be the foreground process group again. Then e.g.bg %1would startsome_app's process group again, but keep it running in the background. -
getsid ์์คํ ํธ์ถ : session id๋ฅผ ํ๋ / ์ด๋ค session์ ์ํด์๋์ง ํ์ธ
#include <sys/types.h> #include <unistd.h> pid_t getsid(pid_t pid);
-
setsid() ์์คํ ํธ์ถ
#include <sys/types.h> #include <unistd.h> pid_t setsid(void);
-
์ ์ด ๋จ๋ง๊ธฐ๋ฅผ ๊ฐ์ง ์๋ ์๋ก์ด session๊ณผ group ์์ฑ
ํธ์ถ ํ๋ก์ธ์ค์ id๊ฐ session๊ณผ group์ id๊ฐ ๋๋ค
๋ง์ฝ, ํธ์ถ process๊ฐ ํ์ฌ group์ leader์ด๋ฉด -1์ returnํ๋ค
-
session์ ์ข ๋ฃํด๋ ๊ณ์ ์คํ ๋๊ฒ ๋ง๋ค ๋ session์ ๋ณ๊ฒฝํ๋ค
์ด session๊ณผ ๊ด๊ณ์์ด ์คํํ๊ฒ ๋ค๋ผ๋ ๋ช ๋ น์ด๋, shell ํ๋ก๊ทธ๋จ ์ข ๋ฃ๋ผ๋ ์ข ๋ฃ๋ฅผ ๋ชป์ํค๊ฒ ๋ผ์ ์ฌ์ฉํ์ง ๋ง ๊ฒ
-
-
-
Mainํจ์ ์ธ์ ์ฌ์ฉ. ์ด์ ๋ ์ด๋ ๊ฒ ์ธ์๋ฅผ ์ ๋ฌ๋ฐ์์ ํ๋ก๊ทธ๋จ์ ๋ง๋ค ๊ฒ
//argc ๋ฌธ์์ด ๋ช ๊ฐ๋ฅผ ๋๊ฒจ๋ฐ์๋์ง ์๋ ค์ค //argv ํฌ์ธํฐ ๋ฐฐ์ด๋ก, ์ด๋ค ๊ฐ์ ๋๊ฒจ๋ฐ์๋์ง ์ ์ ์์ int main(int argc, char **argv) { int i = 0; printf("%d\n", argc); while(argv[i] != NULL) { printf("%s\n", argv[i]); i++; } return 0; }
-
๋ชจ๋ ์ ์ถ๋ ฅ์ OS๊ฐ ๋ด๋นํจ. program์ด I/O ์์ ์ OS์๊ฒ ๋ถํ์ ํ๋ ๊ฒ์. ๊ทธ๋ฌ๋ฉด ์ ๋ ฅ๋ฐ์ ๊ฐ๋ค์ OS๋ mainํจ์ ์ธ์๋ก ๋๊ฒจ์ฃผ๋ ๊ฒ์
//ls ํ์ฌ์์น ํ๋ก๊ทธ๋จ ์ด๋ฆ //-l ํ๋ก๊ทธ๋จ ์คํ์ ์ํ ์ธ์ $ ls -l //chmod ํ์ฌ์์น ํ๋ก๊ทธ๋จ ์ด๋ฆ //0777 ํ๋ก๊ทธ๋จ ์คํ์ ์ํ ์ธ์ //a.c ํ๋ก๊ทธ๋จ ์คํ์ ์ํ ์ธ์ $ chmod 0777 a.c -
getpid() / getppid() / getpgrp() / getpgid(0) / setpgid(pid, gid) / getsid(pid) / setsid()