itec 502 컴퓨터 시스템 및 실습 chapter 3-2: process synchronization mi-jung choi...

15
ITEC 502 컴컴컴 컴컴컴 컴 컴컴 Chapter 3-2: Process Synchronizatio n Mi-Jung Choi [email protected] DPNM Lab. Dept. of CSE, POSTECH

Upload: flora-bond

Post on 21-Jan-2016

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ITEC 502 컴퓨터 시스템 및 실습 Chapter 3-2: Process Synchronization Mi-Jung Choi mjchoi@postech.ac.kr DPNM Lab. Dept. of CSE, POSTECH

ITEC 502 컴퓨터 시스템 및 실습

Chapter 3-2:Process Synchronization

Mi-Jung [email protected]

DPNM Lab. Dept. of CSE, POSTECH

Page 2: ITEC 502 컴퓨터 시스템 및 실습 Chapter 3-2: Process Synchronization Mi-Jung Choi mjchoi@postech.ac.kr DPNM Lab. Dept. of CSE, POSTECH

ITEC502 컴퓨터 시스템 및 실습

2

Contents

1. Monitors2. Message Passing3. Barriers

Page 3: ITEC 502 컴퓨터 시스템 및 실습 Chapter 3-2: Process Synchronization Mi-Jung Choi mjchoi@postech.ac.kr DPNM Lab. Dept. of CSE, POSTECH

ITEC502 컴퓨터 시스템 및 실습

3

Problems with Semaphores Be careful not to misuse the semaphore Examples of misuse:

– mutex is a semaphore which is initialized to 1

– signal (mutex) …. wait (mutex)• results in a violation of the mutual exclusion requirement

– wait (mutex) … wait (mutex)• results in deadlock

– Omitting of wait (mutex) or signal (mutex) (or both)• Either mutual exclusion is violated or a deadlock will occur

To deal with such errors, the monitor may be used

Page 4: ITEC 502 컴퓨터 시스템 및 실습 Chapter 3-2: Process Synchronization Mi-Jung Choi mjchoi@postech.ac.kr DPNM Lab. Dept. of CSE, POSTECH

ITEC502 컴퓨터 시스템 및 실습

4

Monitors - Concept A high-level abstraction that provides a

convenient and effective mechanism for process synchronization

Monitor is defined as an abstract data type like class in object-oriented language (C++, Java)

A monitor instance is shared among multiple processes

Only one process may be active within the monitor at a time

monitor monitor-name

{

shared variable declarations

procedure P1 (…) { …. }

procedure Pn (…) {……}

Initialization code ( ….) { … }

}

Private Data

Public Methods

Initialization Method

Page 5: ITEC 502 컴퓨터 시스템 및 실습 Chapter 3-2: Process Synchronization Mi-Jung Choi mjchoi@postech.ac.kr DPNM Lab. Dept. of CSE, POSTECH

ITEC502 컴퓨터 시스템 및 실습

5

Schematic view of a Monitor Shared data Operations Initialization code

The shared data can be accessed by operations

Only one process is active within a monitor at a time

Programmer don’t need to code this synchronization

Page 6: ITEC 502 컴퓨터 시스템 및 실습 Chapter 3-2: Process Synchronization Mi-Jung Choi mjchoi@postech.ac.kr DPNM Lab. Dept. of CSE, POSTECH

ITEC502 컴퓨터 시스템 및 실습

6

Condition Variables

Basic monitor is not sufficiently powerful for modeling some synchronization schemes

To solve, condition variables are introduced– condition x, y;– A programmer can define one or more variables of type conditio

n Two operations on a condition variable:

– x.wait () – a process that invokes the operation is suspended– x.signal () – resumes one of processes (if any) that invoked x.w

ait () – x.signal() resumes exactly one suspended process– If no process is suspended, then nothing happens

Page 7: ITEC 502 컴퓨터 시스템 및 실습 Chapter 3-2: Process Synchronization Mi-Jung Choi mjchoi@postech.ac.kr DPNM Lab. Dept. of CSE, POSTECH

ITEC502 컴퓨터 시스템 및 실습

7

Monitor with Condition Variables

Page 8: ITEC 502 컴퓨터 시스템 및 실습 Chapter 3-2: Process Synchronization Mi-Jung Choi mjchoi@postech.ac.kr DPNM Lab. Dept. of CSE, POSTECH

ITEC502 컴퓨터 시스템 및 실습

8

Condition Variables – issues

Suppose that, when the x.signal() is invoked by a process P, there is a suspended process Q with condition x

If the suspended process Q is allowed to resume its execution, the signaling process P must wait – Otherwise both Q and P would be active simultaneousl

y Two possibilities:

– Signal and Wait: P either waits until Q leaves the monitor or waits for another condition

– Signal and Continue: Q either waits until P leaves the monitor or waits for another condition

Page 9: ITEC 502 컴퓨터 시스템 및 실습 Chapter 3-2: Process Synchronization Mi-Jung Choi mjchoi@postech.ac.kr DPNM Lab. Dept. of CSE, POSTECH

ITEC502 컴퓨터 시스템 및 실습

9

Monitors (1)

Example of a monitor

Page 10: ITEC 502 컴퓨터 시스템 및 실습 Chapter 3-2: Process Synchronization Mi-Jung Choi mjchoi@postech.ac.kr DPNM Lab. Dept. of CSE, POSTECH

ITEC502 컴퓨터 시스템 및 실습

10

Monitors (2)

Outline of producer-consumer problem with monitors– only one monitor procedure active at one time– buffer has N slots

Page 11: ITEC 502 컴퓨터 시스템 및 실습 Chapter 3-2: Process Synchronization Mi-Jung Choi mjchoi@postech.ac.kr DPNM Lab. Dept. of CSE, POSTECH

ITEC502 컴퓨터 시스템 및 실습

11

Monitors (3)

Solution to producer-consumer problem in Java (part 1)

Page 12: ITEC 502 컴퓨터 시스템 및 실습 Chapter 3-2: Process Synchronization Mi-Jung Choi mjchoi@postech.ac.kr DPNM Lab. Dept. of CSE, POSTECH

ITEC502 컴퓨터 시스템 및 실습

12

Monitors (4)

Solution to producer-consumer problem in Java (part 2)

Page 13: ITEC 502 컴퓨터 시스템 및 실습 Chapter 3-2: Process Synchronization Mi-Jung Choi mjchoi@postech.ac.kr DPNM Lab. Dept. of CSE, POSTECH

ITEC502 컴퓨터 시스템 및 실습

13

Message Passing

The producer-consumer problem with N messages

Page 14: ITEC 502 컴퓨터 시스템 및 실습 Chapter 3-2: Process Synchronization Mi-Jung Choi mjchoi@postech.ac.kr DPNM Lab. Dept. of CSE, POSTECH

ITEC502 컴퓨터 시스템 및 실습

14

Barriers

Use of a barrier– processes approaching a barrier– all processes but one blocked at barrier– last process arrives, all are let through

Page 15: ITEC 502 컴퓨터 시스템 및 실습 Chapter 3-2: Process Synchronization Mi-Jung Choi mjchoi@postech.ac.kr DPNM Lab. Dept. of CSE, POSTECH

ITEC502 컴퓨터 시스템 및 실습

15

Summary

To deal with errors of semaphores, the monitor may be used

Monitors provides the synchronization mechanism for sharing abstract data types

– Condition variables provides a method by which a monitor procedure can block its execution until it is signaled to continue