dispatch schedule timerrunout preemption ready created exit running

  • Slides: 71
Download presentation

프로세스 상태전이도 dispatch (schedule) timerrunout (preemption) ready created exit running sleep (block) asleep (blocked)

프로세스 상태전이도 dispatch (schedule) timerrunout (preemption) ready created exit running sleep (block) asleep (blocked) wakeup swap-in (resume) swap-out (suspend) suspended ready terminated swap-in (resume) wakeup swap-out (suspend) suspended blocked 컴퓨터공학과 운영체제 (active) (suspended) Page 14

Scheduling Queues o Block queue 4 대기 상태의 프로세스 큐 4 각 프로세스가 요구한

Scheduling Queues o Block queue 4 대기 상태의 프로세스 큐 4 각 프로세스가 요구한 자원의 종류에 따라 분류 가능 4 대기-리스트(block list), 대기-큐(block queue) ë 각 자원의 종류별 관리 ë 큐의 개수 커널이 관리하는 자원의 종류의 수 대기 리스트 dispatch (schedule) ready running timerrunout (preemption) wakeup sleep (block) asleep P 11 P 12 P 13 P 21 P 22 blocked list Pk 1 Pk 2 Pk 3 컴퓨터공학과 운영체제 Page 27

Thread 개념 o Thread 정의 4 Lightweight process(LWP: 경량 프로세스) 4 CPU 스케줄의 단위

Thread 개념 o Thread 정의 4 Lightweight process(LWP: 경량 프로세스) 4 CPU 스케줄의 단위 4 구성 ë Thread ID ë Program counter ë Register set ë Stack 4 같은 프로세스에 속한 모든 thread들은 코드, 데이터, 파일 등의 자원을 공유한다 4 응용분야: web server, RPC server, web browser 4 전통적인 기존 process : single thread of control 컴퓨터공학과 운영체제 Page 28

Single/Multi-threaded processes code data registers files code data files stack registers stack thread 단일

Single/Multi-threaded processes code data registers files code data files stack registers stack thread 단일 thread 다중 thread 컴퓨터공학과 운영체제 Page 30

Three processes each with one thread One process with three threads 컴퓨터공학과 운영체제 Page

Three processes each with one thread One process with three threads 컴퓨터공학과 운영체제 Page 31

Thread 개념 o 이점 4 Responsiveness(응답성) 4 Resource sharing(자원공유) 4 Economy(경제성) 4 Utilization of

Thread 개념 o 이점 4 Responsiveness(응답성) 4 Resource sharing(자원공유) 4 Economy(경제성) 4 Utilization of multiprocessor architectures (다중처리기 구조의 활용) 컴퓨터공학과 운영체제 Page 32

A word processor with three threads 컴퓨터공학과 운영체제 Page 33

A word processor with three threads 컴퓨터공학과 운영체제 Page 33

A multithreaded Web server 컴퓨터공학과 운영체제 Page 34

A multithreaded Web server 컴퓨터공학과 운영체제 Page 34

While (1) { client. Fd = accept(server. Fd, client. Sock. Addr. Ptr, &client. Len);

While (1) { client. Fd = accept(server. Fd, client. Sock. Addr. Ptr, &client. Len); // if (fork() == 0) // { write. Recipe(client. Fd); close(client. Fd); // exit(0); // } // else // close(client. Fd); } //clone(write. Recipe 2, child_stack, CLONE_VM|CLONE_FILES, arg); 컴퓨터공학과 운영체제 Page 35

Implementing Threads in User Space A user-level threads package 컴퓨터공학과 운영체제 Page 36

Implementing Threads in User Space A user-level threads package 컴퓨터공학과 운영체제 Page 36

Implementing Threads in the Kernel A threads package managed by the kernel 컴퓨터공학과 운영체제

Implementing Threads in the Kernel A threads package managed by the kernel 컴퓨터공학과 운영체제 Page 37

Hybrid Implementations Multiplexing user-level threads onto kernel- level threads 컴퓨터공학과 운영체제 Page 38

Hybrid Implementations Multiplexing user-level threads onto kernel- level threads 컴퓨터공학과 운영체제 Page 38

Pthread example(1) #include <stdio. h> #include <pthread. h> void consumer (void); // thread function

Pthread example(1) #include <stdio. h> #include <pthread. h> void consumer (void); // thread function prototype char buffer[n]; int n, in = 0, out = 0; int main () { char nextp; int i; pthread_t tid; pthread_create (&tid, NULL, consumer, NULL); // consumer thread 의 생성 컴퓨터공학과 운영체제 Page 39

Pthread example(2) void consumer(void) { char nextc; for (i = 0; i < 500;

Pthread example(2) void consumer(void) { char nextc; for (i = 0; i < 500; i++) { while (in == out) ; // 대기 loop nextc = buff[out]; out++; out %= n; . . . consume the item in nextc; } } Producer Process Consumer Process Producer Process Circular Buffer 컴퓨터공학과 운영체제 Page 40

인터럽트 처리 과정 t up r r g e Int ndlin ha pt Interru

인터럽트 처리 과정 t up r r g e Int ndlin ha pt Interru service 프로세서 Pi Context 인터럽트 발생 Con te xt re saving Interrupt handler Kernel ISR-1 ISR-2 ISR-n PCBi stor ing 컴퓨터공학과 운영체제 PCBj 주기억장치 Page 43

UNIX KERNEL o 5 - 10 % of total UNIX OS 4 memory 상주

UNIX KERNEL o 5 - 10 % of total UNIX OS 4 memory 상주 - 90% C - 10% Assembly 효율성, 하드웨어 제어부분 o 3 부분 4 프로세스 관리 4 파일 시스템 4 I/O 시스템 o 2 modes of machine operations 4 kernel mode 4 user mode ë for protection (memory space, privileged instruction 등) 컴퓨터공학과 운영체제 Page 48

UNIX KERNEL o 모드 전환 user's source program : " read next word from

UNIX KERNEL o 모드 전환 user's source program : " read next word from file X into variable y " object program : 시스템 호출을 위한 매개변수 준비 인터럽트 실행 CPU : save state vector(문맥) CPU mode ← kernel mode로 전환 인터럽트 서비스 루틴으로 이동 OS : (interrupt handler 는 커널 영역) interrupt 원인 파악 (system call, illegal code, address fault, . . ) 해당 kernel procedure로 이동 | system call service | OS가 CPU mode ← user mode로 전환 restore state vector(문맥) Now, back to user program. 컴퓨터공학과 운영체제 Page 49

UNIX PROCESS MANAGEMENT o PCB(process control block) o In UNIX, PCB is divided into

UNIX PROCESS MANAGEMENT o PCB(process control block) o In UNIX, PCB is divided into 3 parts 4 u 4 proc 4 text o All in the kernel address space o used by kernel for system call services 컴퓨터공학과 운영체제 Page 50

UNIX PROCESS MANAGEMENT o Proc Table 4 4 4 process마다 하나씩 할당됨 항상 memory에

UNIX PROCESS MANAGEMENT o Proc Table 4 4 4 process마다 하나씩 할당됨 항상 memory에 상주 allocated when process is created freed when process is terminated 저장 내용 ë 프로세스가 디스크로 swap-out되었을지라도 메모리에 남아있어야 하는 부분들 ë process id, 상태, 우선순위, waiting event, location of "image" (text segment, data segment, . . . ) 4 프로세스가 실행중이 아닌 중에도 시스템에 의해 필요한 정보들 컴퓨터공학과 운영체제 Page 51

UNIX PROCESS MANAGEMENT o U block (1024 bytes) 4 프로세스가 실행중일 때만 시스템에 의해

UNIX PROCESS MANAGEMENT o U block (1024 bytes) 4 프로세스가 실행중일 때만 시스템에 의해 필요한 정보들 4 프로세스와 함께 swap-out 가능 4 low overhead for suspended process (only proc table entry, which is few words !) 4 CONTENT of u block ë ë ë ë ë open files descriptor directory(current, root) command argument state vector save area offset execution time (for scheduling) signal parent process id pointer to proc table entry others 컴퓨터공학과 운영체제 Page 52

UNIX PROCESS MANAGEMENT main() { int pid; printf("Just one process so farn"); printf("Calling fork

UNIX PROCESS MANAGEMENT main() { int pid; printf("Just one process so farn"); printf("Calling fork …n"); pid = fork(); if(pid == 0) printf("I'm the child n "); else if (pid > 0) printf("I'm the parent, child has pid %d n ", pid); else printf("Fork returned error code, no child n "); } 컴퓨터공학과 운영체제 Page 55

fork에 의한 프로세스 생성 int a = 3; int main() { int pid; :

fork에 의한 프로세스 생성 int a = 3; int main() { int pid; : do something; if ((pid = fork()) == 0) { //child a = 4; printf ("child's a = %dn", a); exit(0); } else // end of child { // parent a = 5; printf ("parent's a = %dn", a); wait(); } do something; } // child’s variable a // child 종료 // parent’s variable a // parent는 child의 exit 까지 대기 컴퓨터공학과 운영체제 Page 56

UNIX PROCESS MANAGEMENT /* run ls in a subprocess */ main() { int pid;

UNIX PROCESS MANAGEMENT /* run ls in a subprocess */ main() { int pid; pid = fork(); } if (pid > 0) { wait((int *) 0); printf("ls completed n "); exit(0); } if (pid == 0) { execl("/bin/ls", "-l", (char *)0); perror("execl failed"); exec 호출이 성공하면 실행되지 않는다. exit(1); } perror ("fork failed"); 컴퓨터공학과 운영체제 Page 61

UNIX 계열 프로세스 tree init fork, exec getty exec login fork + exec shell

UNIX 계열 프로세스 tree init fork, exec getty exec login fork + exec shell a. out wait exit 컴퓨터공학과 운영체제 Page 62

축약된 shell의 코드 while (!logout) { print prompt sign; accept command line string; parse

축약된 shell의 코드 while (!logout) { print prompt sign; accept command line string; parse the command line; // 이때 command는 “a. out" 또는 // “a. out&” 이라 가정 if ((pid = fork()) == 0) { // child shell execve ("a. out", , , ); } // parent shell if (foreground) while (pid != wait()); } 컴퓨터공학과 운영체제 Page 63

UNIX PROCESS MANAGEMENT o PROCESS 종료 4 "exit" system call : 자발적 종료 4

UNIX PROCESS MANAGEMENT o PROCESS 종료 4 "exit" system call : 자발적 종료 4 "signal" 이용(user kills, quit, break 등. . . ) : 비자발적 종료 4 swappable image(text, data segment, u)는 즉시 반납 ë proc table entry 는 존재 → "zombie" 상태 4 parent가 wait중이면 parent에게 signal이 감 4 추후 parent가 wakeup, run하면 종료된 child의 proc table entry를 제거해줌 컴퓨터공학과 운영체제 Page 64