Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li
Spatiotemporal Database Laboratory em Pusan National University Korea 2 What is Process ? Process An Abstraction of Running Program OS 에서 가장 핵심적인 내용 OS 가 관리하는 프로그램의 기본 단위 각종 Resource 를 할당하고 관리하는 단위 독립적으로 실행 가능한 Sequential Program Multiprogramming 여러 개의 Program 이 하나의 Processor 를 공유 Pseudo Parallelism (cf. Multiprocessor)
Spatiotemporal Database Laboratory em Pusan National University Korea 3 Multiprogramming Process A B C D Time Operating System A B C D PC for A PC for B PC for C PC for D
Spatiotemporal Database Laboratory em Pusan National University Korea 4 Multiprogramming Process A B C D Time Operating System A B C D PC for A PC for B PC for C PC for D
Spatiotemporal Database Laboratory em Pusan National University Korea 5 Multiprogramming Process A B C D Time Operating System A B C D PC for A PC for B PC for C PC for D
Spatiotemporal Database Laboratory em Pusan National University Korea 6 Multiprogramming Process A B C D Time Operating System A B C D PC for A PC for B PC for C PC for D
Spatiotemporal Database Laboratory em Pusan National University Korea 7 Multiprogramming Process A B C D Time Operating System A B C D PC for A PC for B PC for C PC for D
Spatiotemporal Database Laboratory em Pusan National University Korea 8 Process States – 5 States RunningReadyBlockedEndStart Process Started
Spatiotemporal Database Laboratory em Pusan National University Korea 9 Process States – 5 States RunningReadyBlockedEnd Process Scheduler picks it Start
Spatiotemporal Database Laboratory em Pusan National University Korea 10 Process States – 5 States RunningReadyBlockedEnd Timeout Start
Spatiotemporal Database Laboratory em Pusan National University Korea 11 Process States – 5 States RunningReadyBlockedEnd Blocked (e.g. I/O request) Start
Spatiotemporal Database Laboratory em Pusan National University Korea 12 Process States – 5 States RunningReadyBlockedEnd I/O Completed Start
Spatiotemporal Database Laboratory em Pusan National University Korea 13 Process States – 5 States RunningReadyBlockedEnd Process Completed Start
Spatiotemporal Database Laboratory em Pusan National University Korea 14 Suspended State I/O speed vs. CPU speed 대부분의 process 가 I/O blocked 될 가능성 존재 다른 process 를 service 할 수 없음 Main Memory 가 이미 모두 사용 Swapping Blocked 된 Process 의 Memory Image 를 Disk 에 보관 경우에 따라, Ready 된 Process 도 Disk 에 보관 Suspended Process Main Memory 에 있는 Process 와 구별 Disk 에 Swap 되어 있는 Process
Spatiotemporal Database Laboratory em Pusan National University Korea 15 Process States – 7 States RunningReadyBlockedEnd Process Completed Start Ready /Suspended Blocked /Suspended
Spatiotemporal Database Laboratory em Pusan National University Korea 16 PCB (Process Control Block) OS 는 각 Process 와 Resource 를 효과적으로 관리 각 Process 에 대한 관리정보 : Process Control Block 각 Resource 마다 관리정보 Memory Table I/O Table Process Control Block Process ID, Process 의 상태, PC, SP, PSW(Program Status Word) Time 에 관한 정보 Main Memory 의 위치 Process Image
Spatiotemporal Database Laboratory em Pusan National University Korea 17 Child Process 각 Process 는 Child Process 를 생성 가능 독립적인 Process Control Block 독립적인 Process Image 를 소유 Process A Memory Space
Spatiotemporal Database Laboratory em Pusan National University Korea 18 Child Process 각 Process 는 Child Process 를 생성 가능 독립적인 Process Control Block 독립적인 Process Image 를 소유 Process AProcess A1 Memory Space
Spatiotemporal Database Laboratory em Pusan National University Korea 19 Child Process 각 Process 는 Child Process 를 생성 가능 독립적인 Process Control Block 독립적인 Process Image 를 소유 Process AProcess A1Process A2Process A11 Memory Space
Spatiotemporal Database Laboratory em Pusan National University Korea 20 Thread : Example Process A Process B Process C CPU Ready Running
Spatiotemporal Database Laboratory em Pusan National University Korea 21 Thread : Example Process A Process B Process C CPU Ready Running I/O request
Spatiotemporal Database Laboratory em Pusan National University Korea 22 Thread : Example Process A Process B Process C CPU Ready Blocked Disk Running Context Switching and Swapping if necessary
Spatiotemporal Database Laboratory em Pusan National University Korea 23 Thread : Example Process A Process B Process C CPU Ready Blocked Disk Running Context Switching and Swapping if necessary Expensive
Spatiotemporal Database Laboratory em Pusan National University Korea 24 Thread Process 의 문제점 : Context Switching Overhead 예. 많은 I/O 를 요청 : 잦은 Context Switching Context Switching 시 Swapping 발생 : Disk I/O 유발 Lightweight Process 일반 Process 보다 제한된 Resource 를 소유 가능 Context Switching 의 Overhead 를 최소화 같은 Process 의 Resource 를 공유 Process AProcess B Resource Thread AThread B Resource
Spatiotemporal Database Laboratory em Pusan National University Korea 25 Thread Thread A Thread B Thread C CPU Ready Running I/O request Process
Spatiotemporal Database Laboratory em Pusan National University Korea 26 Thread Thread C Thread A Thread B CPU Ready Running I/O request Process Blocked
Spatiotemporal Database Laboratory em Pusan National University Korea 27 Thread Thread C Thread A Thread B CPU Ready Running I/O request Process Blocked Context Switching Not Very Expensive
Spatiotemporal Database Laboratory em Pusan National University Korea 28 Interprocess Communication (IPC) 여러 개의 Process 가 동시에 작동될 때, Process 사이의 효과적인 협력이 필요 필요한 정보를 전달 적절한 방법으로 협력
Spatiotemporal Database Laboratory em Pusan National University Korea 29 Interprocess Communication (IPC) Race Condition 좌석 예약 DB Shared Memory 또는 공유되는 부분 Process A for(I=0;I<NumSeats;I++) { if(seat[I].reserved==No) { strcpy(seat[I].name, ” abc ” ); seat[I].reserved=Yes; } } Process B for(I=0;I<NumSeats;I++) { if(seat[I].reserved==No) { strcpy(seat[I].name, ” deg ” ); seat[I].reserved=Yes; } }
Spatiotemporal Database Laboratory em Pusan National University Korea 30 Interprocess Communication (IPC) Race Condition 좌석 예약 DB Shared Memory 또는 공유되는 부분 Process A for(I=0;I<NumSeats;I++) { if(seat[I].reserved==No) { strcpy(seat[I].name, ” abc ” ); seat[I].reserved=Yes; } } Process B for(I=0;I<NumSeats;I++) { if(seat[I].reserved==No) { strcpy(seat[I].name, ” deg ” ); seat[I].reserved=Yes; } }
Spatiotemporal Database Laboratory em Pusan National University Korea 31 Interprocess Communication (IPC) Race Condition 좌석 예약 DB Shared Memory 또는 공유되는 부분 Process A for(I=0;I<NumSeats;I++) { if(seat[I].reserved==No) { strcpy(seat[I].name, ” abc ” ); seat[I].reserved=Yes; } } Process B for(I=0;I<NumSeats;I++) { if(seat[I].reserved==No) { strcpy(seat[I].name, ” deg ” ); seat[I].reserved=Yes; } }
Spatiotemporal Database Laboratory em Pusan National University Korea 32 Interprocess Communication (IPC) Race Condition 좌석 예약 DB Shared Memory 또는 공유되는 부분 Process A for(I=0;I<NumSeats;I++) { if(seat[I].reserved==No) { strcpy(seat[I].name, ” abc ” ); seat[I].reserved=Yes; } } Process B for(I=0;I<NumSeats;I++) { if(seat[I].reserved==No) { strcpy(seat[I].name, ” deg ” ); seat[I].reserved=Yes; } }
Spatiotemporal Database Laboratory em Pusan National University Korea 33 Mutual Exclusion 과 Critical Section Critical Section 여러 개의 Process 가 동시에 수행될 때, Mutual Exclusions 으로 보호 받아야 되는 부분 Mutual Exclusion : 한번에 하나의 Process 만 접근 가능 Process A for(I=0;I<NumSeats;I++){ if(seat[I].reserved==No) { strcpy(seat[I].name, ” abc ” ); seat[I].reserved=Yes; } } 바로 이부분
Spatiotemporal Database Laboratory em Pusan National University Korea 34 Disabling Interrupt Critical Section 에 들어가기 전 Clock Interrupt 를 Disable 다른 Process 로 Context Switching 이 일어나지 않음 Mutual Exclusion 이 절대적으로 보장 Critical Section 을 나온 후 바로 Interrupt 를 Enable 별로 안 좋은 방법 위험한 방법 : 만일 사용의 프로그램이 Enable Interrupt 를 생략하면 ? Multiprocessor : 모든 Processor 의 Interrupt 를 Disable. 사용자 프로그램이 아닌 OS 내의 프로그램에서는 적절한 통제 하에 사용될 수도 있음.
Spatiotemporal Database Laboratory em Pusan National University Korea 35 Lock Variable : Busy Waiting 좌석 예약 DB Shared Memory 또는 공유되는 부분 Process A while(lock==YES) {} ; lock=YES; enterCS() ; lock=NO; Process B while(lock==YES) {} ; //wait lock=YES; enterCS() ; lock=NO; Lock lock=NO; YES NO Mutual Exclusion Wait But does it work well ?
Spatiotemporal Database Laboratory em Pusan National University Korea 36 Strict Alternation : Busy Waiting 좌석 예약 DB Shared Memory 또는 공유되는 부분 Process A while(True) { while(Turn==B} {} enterrCS() ; Turn=B; } Process B while(True) { while(Turn==A} {} enterrCS() ; Turn=A; } Turn Turn=A; B MU Wait But what is wrong ? A
Spatiotemporal Database Laboratory em Pusan National University Korea 37 Peterson’s Algorithm Process A other=1-process; // process=0 for A interested[process]=True; turn=process; while(turn==process and interested[other]==True) ; // busy waiting enterrCS() ; interested[process]=False; Interested[0]=True Interested[1]=False A B Turn
Spatiotemporal Database Laboratory em Pusan National University Korea 38 Peterson’s Algorithm Process A other=1-process; // process=0 for A interested[process]=True; turn=process; while(turn==process and interested[other]==True) ; // busy waiting enterrCS() ; interested[process]=False; Interested[0]=False Interested[1]=True A B Turn
Spatiotemporal Database Laboratory em Pusan National University Korea 39 Sleep and Wakeup Busy Waiting 의 문제점 Low Priority Process A CS High Priority Process B Busy Waiting: Holds CPU A 는 B 가 timeout 될 때까지 계속 ready queue 그 만큼 CPU Waste Sleep and Wakeup
Spatiotemporal Database Laboratory em Pusan National University Korea 40 Sleep and Wakeup Producer and Consumer Problem void Producer(void) { while(1) { produceItem(); if(count==N) sleep(); //buffer full enterItem(); ++count; if(count==1) wakeup(consumer); } void Consumer(void) { while(1) { if(count==0) sleep(); //buffer empty removeItem(); consumeItem(); --count; if(count==N-1) wakeup(producer); }
Spatiotemporal Database Laboratory em Pusan National University Korea 41 Sleep and Wakeup Producer and Consumer Problem void Producer(void) { while(1) { produceItem(); if(count==N) sleep(); //buffer full enterItem(); ++count; if(count==1) wakeup(consumer); } void Consumer(void) { while(1) { if(count==0) sleep(); //buffer empty removeItem(); consumeItem(); --count; if(count==N-1) wakeup(producer); }
Spatiotemporal Database Laboratory em Pusan National University Korea 42 Sleep and Wakeup Producer and Consumer Problem void Producer(void) { while(1) { produceItem(); if(count==N) sleep(); //buffer full enterItem(); ++count; if(count==1) wakeup(consumer); } void Consumer(void) { while(1) { if(count==0) sleep(); //buffer empty removeItem(); consumeItem(); --count; if(count==N-1) wakeup(producer); }
Spatiotemporal Database Laboratory em Pusan National University Korea 43 Sleep and Wakeup Producer and Consumer Problem void Producer(void) { while(1) { produceItem(); if(count==N) sleep(); //buffer full enterItem(); ++count; if(count==1) wakeup(consumer); } void Consumer(void) { while(1) { if(count==0) sleep(); //buffer empty removeItem(); consumeItem(); --count; if(count==N-1) wakeup(producer); } ? : wakeup signal is Lost
Spatiotemporal Database Laboratory em Pusan National University Korea 44 Semaphore A Variable (Nonnegative) Only Two Atomic Operations Down(X) (or Sleep(X), or P(X)) { if X > 0, X=X-1; else Sleep_Until_ X_GreaterThan_0() ; } Up(X) (or Wakeup(X) or V(X)) { X=X+1; } Semaphore : An Atomic Signal Variable
Spatiotemporal Database Laboratory em Pusan National University Korea 45 Producer and Consumer with Semaphores void Producer(void) { while(1) { produceItem(); down(full); down(mutex); enterItem(); up(mutex); up(empty); } } void Consumer(void) { while(1) { down(empty); down(mutex); removeItem(); consumeItem(); up(mutex); up(full); } } Semaphore mutex=1; Semaphore full=100; Semaphore empty=0; if(count==N) sleep(); if(count==0) sleep(); Atomic Operation No Lost
Spatiotemporal Database Laboratory em Pusan National University Korea 46 그러면 Semaphore 는 어떻게 ? Semaphore 의 Down Operation if X > 0, then X=X-1; else Sleep_Until_ X_GreaterThan_0() ; 중간에 다른 Process 가 끼어 들어오면 ? Atomic 한 특성을 어떻게 유지 ? Machine Instruction 의 TSL : Test and Set Lock Test and Set if Zero 예를 들어, TSL X, SleepLock
Spatiotemporal Database Laboratory em Pusan National University Korea 47 Monitor To ensure the safety(Deadlock Free) : Monitor An Abstraction of Procedure (Black Box) Guarantee Mutual Exclusion Deadlock Free Monitor Process A Process B Process C Process D How to implement monitor ? Compiler Semaphore
Spatiotemporal Database Laboratory em Pusan National University Korea 48 Monitor : Example Class Monitor { conditions full,empty; integer count; void enter() { if(count==N) wait(full); enterItem();++count; if(count==1) signal(empty); } void remove() { if(count==0) wait(empty); removeItem();--count: if(count==N-1) signal(full); } }; MonitorPCMonitor;... void producer() { ProduceItem(); PCMonitor.enter(); } void consumer() { PCMonitor.remove(); consumeItem(); }
Spatiotemporal Database Laboratory em Pusan National University Korea 49 Dining Philosophers #define N 5 void Philosopher(int I){ while(TRUE) { think(); takeFork(I); takeFork((I+1)%N); eat(); putFork(I); putFork((I+1)%N); } taken requesting What will happen ? Infinite waiting How to solve it ?
Spatiotemporal Database Laboratory em Pusan National University Korea 50 Process Scheduling Scheduling Service 를 받을 Process 의 순서를 결정 Service 를 받고 있는 Process 를 어떤 원칙으로 Ready Queue 로 보내는가를 결정 Good Scheduling Fairness : 각 Process 에 공평한 시간 ( 편애, 왕따 방지 ) Efficiency : CPU utilization Response Time : Interactive User Turnaround Time : Batch User Throughput : # of processes/Time
Spatiotemporal Database Laboratory em Pusan National University Korea 51 Preemptive Scheduling Preemption 현재 수행중인 Process 를 중단하고, 다른 Process 를 수행 Preemptive Resource 에 대해서만 가능 예. CPU, Memory Non-Preemptive Scheduling Process 가 끝나야지만 다른 Process 를 처리 Non-Preemptive Resource 에 대하여 예, Printer, Critical Section 이 있는 경우 전체적인 시스템의 성능을 저하 대부분의 OS 에서는 사용하지 않는 방법
Spatiotemporal Database Laboratory em Pusan National University Korea 52 Fairness 모든 Process 에게 균등한 기회를 제공 CPUP1:1 P2:2P3:2 P4:5 Process 4 에게는 CPU 를 사용할 기회가 주어지지 않음 다른 Process 가 I/O 등으로 Blocked 될 때만 기회가 주어짐 Starvation Priority Promotion
Spatiotemporal Database Laboratory em Pusan National University Korea 53 Different Scheduling Policies Job A Job B Job C 40 sec 20 sec 10 sec Case 1 : 각 Job 의 Execution Time 을 미리 알고 있을 때 1. Schedule (A,B,C) : 1/3{40+(40+20)+( )}=67 sec 2. Schedule (C,B,A) : 1/3{10+(10+20)+( )}=37 sec Case 2 : 각 Job 의 수행시간을 모를 때는 ? 일반적인 Scheduling 방법 1. Round Robin Scheduling 2. Priority Scheduling 3. Shortest Job First 4. Real-Time Scheduling 5. Guaranteed Scheduling
Spatiotemporal Database Laboratory em Pusan National University Korea 54 Round Robin 각 Process 에 Time Quantum 을 할당 적당한 Time Quantum 너무 긴 Time Quantum : Response Time 이 길어짐 예. 10 개의 Process 가 동시에 시작, 각 Process 는 0.1sec 만에 수행완료. TimeQuantum=1sec 이면, 평균 response time=5sec 너무 짧은 Time Quantum 잦은 context switching 으로 인한 overhead 대체로 100msec. 가장 간단하면서 가장 널리 사용되는 방법
Spatiotemporal Database Laboratory em Pusan National University Korea 55 Priority Scheduling 각 Process 에게 다른 Priority 를 부여 높은 Priority 의 Process 에게 CPU 를 사용할 우선권을 부여 Starvation I/O 가 많은 Process 는 가능한 높은 Priority 를 부여 금방 Blocked 되므로 다른 Process 에게 기회가 돌아감. Dynamic Priority vs. Static Priority Static Priority : 일정한 Priority 를 계속 유지 Dynamic Priority : 전체적인 수행시간과 상황을 고려하여 Priority 를 재조정
Spatiotemporal Database Laboratory em Pusan National University Korea 56 Shortest Job First 앞의 예와 같이 가장 빨리 끝나는 Process 를 먼저 처리 전체적인 Response Time 이 향상 가장 빨리 끝나는 Process 가 무엇인지 어떻게 판단 ? 각 Job 의 수행시간이 얼마가 될 지를 예상 그러나 불행히도, 수행 전에는 수행시간을 정확하게 알 수 없음 Aging 방법 이전에 수행된 횟수로 예측 T = a { T(0)+ T(1) + …+ T(K-1)} + (1-a)T(K) 예. a=1/2
Spatiotemporal Database Laboratory em Pusan National University Korea 57 Real-Time Scheduling Soft Real-Time 조건 주어진 Process 를 가능한 빨리 끝내야 되는 조건 예. 10 msec. Hard Real-Time 조건 주어진 Process 를 주어진 시간 내에 반드시 끝내야 하는 조건 Rate Monotonic Algorithm : 발생하는 비율로 시간을 할당 매 50 msec : 20 msec, 100 msec : 10 msec Earliest Deadline First Least Laxity : 초과 예상시간이 작은 것 부터