spatiotemporal database laboratory em pusan national university korea 1 operating systems chapter 2:...

57
1 Spatiotemporal Database Laboratory em Pusan National University Korea Operating Systems Chapter 2: Process Ki-Joune Li http://quantos.cs.pusan.ac.kr/html/li k.html

Upload: willis-evan-arnold

Post on 28-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

1

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

Operating SystemsChapter 2: Process

Ki-Joune Lihttp://quantos.cs.pusan.ac.kr/html/lik.html

Page 2: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

2

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

What is Process ?

Process An Abstraction of Running Program OS 에서 가장 핵심적인 내용 OS 가 관리하는 프로그램의 기본 단위

각종 Resource 를 할당하고 관리하는 단위 독립적으로 실행 가능한 Sequential Program

Multiprogramming 여러 개의 Program 이 하나의 Processor 를 공유 Pseudo Parallelism (cf. Multiprocessor)

Page 3: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

3

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

Multiprogramming

Pro

cess

A

B

C

DTime

Operating System

A

B

C

D

PC for A

PC for B

PC for C

PC for D

Page 4: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

4

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

Multiprogramming

Pro

cess

A

B

C

DTime

Operating System

A

B

C

D

PC for A

PC for B

PC for C

PC for D

Page 5: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

5

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

Multiprogramming

Pro

cess

A

B

C

DTime

Operating System

A

B

C

D

PC for A

PC for B

PC for C

PC for D

Page 6: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

6

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

Multiprogramming

Pro

cess

A

B

C

DTime

Operating System

A

B

C

D

PC for A

PC for B

PC for C

PC for D

Page 7: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

7

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

Multiprogramming

Pro

cess

A

B

C

DTime

Operating System

A

B

C

D

PC for A

PC for B

PC for C

PC for D

Page 8: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

8

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

Process States – 5 States

Running

Ready

Blocked

End

StartProcess Started

Page 9: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

9

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

Process States – 5 States

Running

Ready

Blocked

End

Process Scheduler picks it

Start

Page 10: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

10

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

Process States – 5 States

Running

Ready

Blocked

End

Timeout

Start

Page 11: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

11

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

Process States – 5 States

Running

Ready

Blocked

End

Blocked (e.g. I/O request)

Start

Page 12: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

12

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

Process States – 5 States

Running

Ready

Blocked

End

I/O Completed

Start

Page 13: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

13

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

Process States – 5 States

Running

Ready

Blocked

End

Process Completed

Start

Page 14: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

14

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

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

Page 15: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

15

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

Process States – 7 States

Running

Ready

Blocked

End

Process Completed

StartReady /Suspended

Blocked /Suspended

Page 16: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

16

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

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

Page 17: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

17

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

Child Process

각 Process 는 Child Process 를 생성 가능 독립적인 Process Control Block 독립적인 Process Image를 소유

Process A

Memory Space

Page 18: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

18

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

Child Process

각 Process 는 Child Process 를 생성 가능 독립적인 Process Control Block 독립적인 Process Image를 소유

Process A

Process A1

Memory Space

Page 19: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

19

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

Child Process

각 Process 는 Child Process 를 생성 가능 독립적인 Process Control Block 독립적인 Process Image를 소유

Process A

Process A1

Process A2

Process A11

Memory Space

Page 20: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

20

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

Thread : Example

Process A

Process B

Process C

CPU

Ready

Running

Page 21: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

21

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

Thread : Example

Process A

Process B

Process C

CPU

Ready

Running

I/O request

Page 22: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

22

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

Thread : Example

Process A

Process BProcess C

CPU

Ready

Blocked

Disk

Running

Context Switching and Swapping if necessary

Page 23: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

23

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

Thread : Example

Process A

Process BProcess C

CPU

Ready

Blocked

Disk

Running

Context Switching and Swapping if necessary

Expensive

Page 24: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

24

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

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 A Process B

Resource Resource

Thread A Thread B

Resource

Page 25: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

25

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

Thread

Thread A

Thread B

Thread C

CPU

Ready

Running

I/O request

Process

Page 26: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

26

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

Thread

Thread C

Thread A

Thread B

CPU

Ready

Running

I/O request

Process

Blocked

Page 27: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

27

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

Thread

Thread C

Thread A

Thread B

CPU

Ready

Running

I/O request

Process

Blocked

Context Switching

Not Very Expensive

Page 28: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

28

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

Interprocess Communication (IPC)

여러 개의 Process 가 동시에 작동될 때 , Process 사이의 효과적인 협력이 필요 필요한 정보를 전달 적절한 방법으로 협력

Page 29: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

29

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

Interprocess Communication (IPC)

Race Condition

좌석 예약 DBShared 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; } }

Page 30: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

30

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

Interprocess Communication (IPC)

Race Condition

좌석 예약 DBShared 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; } }

Page 31: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

31

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

Interprocess Communication (IPC)

Race Condition

좌석 예약 DBShared 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; } }

Page 32: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

32

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

Interprocess Communication (IPC)

Race Condition

좌석 예약 DBShared 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; } }

Page 33: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

33

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

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; }

}

바로 이부분

Page 34: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

34

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

Disabling Interrupt

Disabling Interrupt Critical Section 에 들어가기 전 Clock Interrupt 를 Disable

다른 Process 로 Context Switching 이 일어나지 않음 Mutual Exclusion 이 절대적으로 보장

Critical Section 을 나온 후 바로 Interrupt 를 Enable 별로 안 좋은 방법

위험한 방법 : 만일 사용의 프로그램이 Enable Interrupt 를 생략하면 ?

Multiprocessor : 모든 Processor 의 Interrupt 를 Disable.

사용자 프로그램이 아닌 OS 내의 프로그램에서는 적절한 통제 하에 사용될 수도 있음 .

Page 35: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

35

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

Lock Variable : Busy Waiting

좌석 예약 DBShared 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 ?

Page 36: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

36

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

Strict Alternation : Busy Waiting

좌석 예약 DBShared 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

Page 37: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

37

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

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

Page 38: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

38

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

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

Page 39: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

39

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

Sleep and Wakeup

Busy Waiting 의 문제점

Low PriorityProcess A

CSHigh Priority

Process B

Busy Waiting: Holds CPU A 는 B 가 timeout 될 때까지 계속 ready queue 그 만큼 CPU Waste

Sleep and Wakeup

Page 40: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

40

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

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);

}

}

Page 41: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

41

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

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);

}

}

Page 42: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

42

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

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);

}

}

Page 43: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

43

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

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

Page 44: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

44

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

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

Page 45: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

45

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

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

Page 46: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

46

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

그러면 Semaphore 는 어떻게 ?

Semaphore 의 Down Operationif 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

Page 47: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

47

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

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

Page 48: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

48

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

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);

}

};

Monitor PCMonitor;

. . .

void producer() {ProduceItem();PCMonitor.enter();

}

void consumer() {PCMonitor.remove();consumeItem();

}

Page 49: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

49

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

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);

}

}

takenrequesting

• What will happen ? Infinite waiting

• How to solve it ?

Page 50: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

50

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

Process Scheduling

Scheduling Service 를 받을 Process 의 순서를 결정 Service 를 받고 있는 Process 를 어떤 원칙으로 Ready Queu

e 로 보내는가를 결정

Good Scheduling Fairness : 각 Process 에 공평한 시간 ( 편애 , 왕따 방지 ) Efficiency : CPU utilization Response Time : Interactive User Turnaround Time : Batch User Throughput : # of processes/Time

Page 51: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

51

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

Preemptive Scheduling

Preemption 현재 수행중인 Process 를 중단하고 , 다른 Process 를 수행 Preemptive Resource 에 대해서만 가능

예 . CPU, Memory

Non-Preemptive Scheduling Process 가 끝나야지만 다른 Process 를 처리 Non-Preemptive Resource 에 대하여

예 , Printer, Critical Section 이 있는 경우 전체적인 시스템의 성능을 저하 대부분의 OS 에서는 사용하지 않는 방법

Page 52: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

52

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

Fairness

모든 Process 에게 균등한 기회를 제공

CPU P1:1

P2:2 P3:2 P3:2 P4:5

Process 4 에게는 CPU 를 사용할 기회가 주어지지 않음 다른 Process 가 I/O 등으로 Blocked 될 때만 기회가 주어짐 Starvation Priority Promotion

Page 53: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

53

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

Different Scheduling Policies

Job A

Job B

Job C

40 sec20 sec

10 sec

• Case 1 : 각 Job 의 Execution Time 을 미리 알고 있을 때 1. Schedule (A,B,C) : 1/3{40+(40+20)+(40+20+10)}=67 sec 2. Schedule (C,B,A) : 1/3{10+(10+20)+(10+20+40)}=37 sec

• Case 2 : 각 Job 의 수행시간을 모를 때는 ?

• 일반적인 Scheduling 방법 1. Round Robin Scheduling 2. Priority Scheduling 3. Shortest Job First 4. Real-Time Scheduling 5. Guaranteed Scheduling

Page 54: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

54

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

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. 가장 간단하면서 가장 널리 사용되는 방법

Page 55: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

55

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

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 를 재조정

Page 56: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

56

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

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

Page 57: Spatiotemporal Database Laboratory em Pusan National University Korea 1 Operating Systems Chapter 2: Process Ki-Joune Li

57

Spatiotemporal Database Laboratory emP

usa

n N

atio

nal

Un

iver

sity

Ko

rea

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 : 초과 예상시간이 작은 것 부터