Real-Time Computing and Communications Lab., Hanyang University Real-Time Computing and Communications Lab., Hanyang University.

Slides:



Advertisements
Similar presentations
1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
Advertisements

Operating Systems Mehdi Naghavi Winter 1385.
7장 프로세스 동기화 II
Chapter 6: Process Synchronization
6.5 Semaphore Can only be accessed via two indivisible (atomic) operations wait (S) { while S
Synchronization Principles Gordon College Stephen Brinton.
Enforcing Mutual Exclusion, Semaphores. Four different approaches Hardware support Disable interrupts Special instructions Software-defined approaches.
Classical Problems of Concurrency
Concurrency, Race Conditions, Mutual Exclusion, Semaphores, Monitors, Deadlocks Chapters 2 and 6 Tanenbaum’s Modern OS.
02/19/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Objectives Understand.
Process Synchronization
IPC and Classical Synchronization Problems
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 6: Process Synchronization Dr. Mohamed Hefeeda.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Module 6: Process Synchronization.
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail,
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Background Concurrent.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
6.3 Peterson’s Solution The two processes share two variables: Int turn; Boolean flag[2] The variable turn indicates whose turn it is to enter the critical.
Critical Problem Revisit. Critical Sections Mutual exclusion Only one process can be in the critical section at a time Without mutual exclusion, results.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 14: October 14, 2010 Instructor: Bhuvan Urgaonkar.
Synchronization II: CPE Operating Systems
Chapter 6 Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Module 6: Process Synchronization.
1 Chapter 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Special Machine Instructions for Synchronization Semaphores.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Chap 6 Synchronization. Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms.
Chapter 6: Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Synchronization Background The Critical-Section.
Chapter 6: Process Synchronization
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Chapter 6: Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 11: Synchronization (Chapter 6, cont)
Real-Time Computing and Communications Lab., Hanyang University Real-Time Computing and Communications Lab., Hanyang University.
Monitor 제 06 강 : Monitor. 세마포어의 문제점 코딩, 이해, 검증이 어렵다. 여러 사람의 자발적인 협조에 의존할 수 밖에 한 개의 오류가 전체 시스템에 악영향을 미친다. P(S); critical section V(S); 일반 code P(M); critical.
CSC 360 Instructor: Kui Wu More on Process Synchronization Semaphore, Monitor, Condition Variables.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Module 6: Process Synchronization.
ITEC 502 컴퓨터 시스템 및 실습 Chapter 3-3: Process Synchronization Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
CS4315A. Berrached:CMS:UHD1 Process Synchronization Chapter 8.
Process Synchronization CS 360. Slide 2 CS 360, WSU Vancouver Process Synchronization Background The Critical-Section Problem Synchronization Hardware.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 22 Semaphores Classic.
Process Synchronization. Objectives To introduce the critical-section problem, whose solutions can be used to ensure the consistency of shared data To.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Web Server Architecture Client Main Thread for(j=0;j
Rensselaer Polytechnic Institute CSCI-4210 – Operating Systems David Goldschmidt, Ph.D.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Synchronization Background The Critical-Section Problem Peterson’s.
CS 311/350/550 Semaphores. Semaphores – General Idea Allows two or more concurrent threads to coordinate through signaling/waiting Has four main operations.
6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.5 Semaphore Less complicated than the hardware-based solutions Semaphore S – integer.
Semaphore Synchronization tool that provides more sophisticated ways (than Mutex locks) for process to synchronize their activities. Semaphore S – integer.
Process Synchronization: Semaphores
Chapter 5: Process Synchronization – Part 3
Auburn University COMP 3500 Introduction to Operating Systems Synchronization: Part 4 Classical Synchronization Problems.
Process Synchronization
Process Synchronization
Chapter 5: Process Synchronization – Part II
Chapter 5: Process Synchronization
סמפורים.
Chapter 5: Process Synchronization (Con’t)
Lecture 25 Syed Mansoor Sarwar
Process Synchronization
Module 7a: Classic Synchronization
Synchronization.
Presentation transcript:

Real-Time Computing and Communications Lab., Hanyang University Real-Time Computing and Communications Lab., Hanyang University Operating System 13 주차 - Process Synchronization (2)- Real-Time Computing and Communications Lab. Hanyang University

2 Real-Time Computing and Communications Lab., Hanyang University 2 Real-Time Computing and Communications Lab., Hanyang University Contents  Process Synchronization  Bounded-Buffer Problem  The Dining-Philosophers problem  Quiz #1

3 Real-Time Computing and Communications Lab., Hanyang University 3 Real-Time Computing and Communications Lab., Hanyang University Process Synchronization  데이터의 일관성을 위해 shared data 에 대한 동시 접근을 제어하는 것  일관성을 유지하기 위해서는 관련된 프로세스들이 순차적으로 실행되 는 보장하는 방법이 요구됨

4 Real-Time Computing and Communications Lab., Hanyang University 4 Real-Time Computing and Communications Lab., Hanyang University The Critical-Section Problem  N 개의 프로세스들은 서로 shared data 를 사용하기 위해 경쟁한다.  shared data 에 접근하는 코드 부분을 critical section 이라고 한다.  critical section 은 한번에 하나의 프로세스만 접근 가능하다.

5 Real-Time Computing and Communications Lab., Hanyang University 5 Real-Time Computing and Communications Lab., Hanyang University The Critical-Section Problem  시스템은 동시에 두 개의 프로세스가 critical section 코드 부분을 수 행하지 않게 해야 함.  각각의 프로세스들은 동기화를 위해 사용하기 위한 변수를 공유한다.  General process P i  entry section : critical section 에 들어가기 위한 요청을 수행  exit section : critical section 의 수 행이 끝난 후 출구 부분  remainder section : 코드의 나머 지 부분

6 Real-Time Computing and Communications Lab., Hanyang University 6 Real-Time Computing and Communications Lab., Hanyang University Semaphore  Mutex 와 같이 critical section 에 대한 접근을 제한하는 키 로 활용됨  integer variable S 를 가지며, 2 개의 atomic operation 을 통해 접근 제어가 수행됨  P(S) : wait function  V(S) : signal function

7 Real-Time Computing and Communications Lab., Hanyang University 7 Real-Time Computing and Communications Lab., Hanyang University Semaphore functions  Header file : semaphore.h  Semaphore 초기화  int sem_init(sem_t *sem, int pshared, unsigned int value);  sem : 초기화할 semaphore 객체  pshared : 0 이 아니면 프로세스들 간에 semaphore 를 공유. 0 이면 프로세스 내부 에서만 semaphore 를 사용  value : semaphore 초기값  Semaphore value 감소  int sem_wait(sem_t *sem);  int sem_trywait(sem_t *sem);  sem_wait : semaphore 값이 0 보다 크면 프로세스는 semaphore 를 얻은 뒤 감소 시키고 즉시 리턴값을 반환한다. semaphore 값이 0 이면 0 보다 커지거나 signal 이 발생할 때까지 대기한다.  sem_trywait : sem_wait 과 기본적으로 같지만, 즉시 semaphore 를 감소시키고 리 턴값을 반환한다는 점이 다르다.

8 Real-Time Computing and Communications Lab., Hanyang University 8 Real-Time Computing and Communications Lab., Hanyang University Semaphore functions  Semaphore value 증가  int sem_post(sem_t *sem);  Semaphore value 저장  int sem_getvalue(sem_t *sem, int *sval);  sval 이 가리키는 위치에 sem 의 현재값을 저장  Semaphore 삭제  int sem_destory(sem_t *sem);  semaphore 를 포함해 관련된 resource 를 모두 해제

9 Real-Time Computing and Communications Lab., Hanyang University 9 Real-Time Computing and Communications Lab., Hanyang University Bounded-Buffer Problem  임시 저장공간인 Buffer 에 여러 명의 생산자와 소비자들이 접근하는 동기화 문제  Producer-Consumer Problem 이라고도 함  Producer 는 Buffer 에 데이터 하나를 저장하고, Consumer 는 Buffer 의 데이터를 하나 불러온다.

10 Real-Time Computing and Communications Lab., Hanyang University 10 Real-Time Computing and Communications Lab., Hanyang University Bounded-Buffer Problem  Variables  Shared data : Binary semaphore 를 통해 데이터에 대한 동기화 를 제어 Mutex : Buffer 에 대한 접근을 통제 ( 초기값 1)  Resource count : Buffer 에 있는 data 의 개수를 표시하기 위해 Integer semaphore 를 사용 S_empty : Buffer 에 저장할 공간이 있는지를 나타내는 semaphore ( 초 기값 n) S_full : Buffer 에 가져올 데이터가 있는지를 나타내는 semaphore ( 초 기값 0)

11 Real-Time Computing and Communications Lab., Hanyang University 11 Real-Time Computing and Communications Lab., Hanyang University Bounded-Buffer Problem  Producer 프로세스  Consumer 프로세스

12 Real-Time Computing and Communications Lab., Hanyang University 12 Real-Time Computing and Communications Lab., Hanyang University Bounded-Buffer Problem  Example  Program : N 명의 producer 와 consumer 가 BUFFER_SIZE 의 크 기를 가지는 buffer 를 공유하는 프로그램  Shared data : 서로가 공유하는 buffer 와 critical section 인 gitem  Producer : 1 초 간격으로 gitem 을 1 증가시키고 buffer 에 삽입  Consumer : 2 초 간격으로 buffer 의 데이터를 하나 가져옴

13 Real-Time Computing and Communications Lab., Hanyang University 13 Real-Time Computing and Communications Lab., Hanyang University Bounded-Buffer Problem  Example

14 Real-Time Computing and Communications Lab., Hanyang University 14 Real-Time Computing and Communications Lab., Hanyang University Bounded-Buffer Problem  Example

15 Real-Time Computing and Communications Lab., Hanyang University 15 Real-Time Computing and Communications Lab., Hanyang University Bounded-Buffer Problem  Example

16 Real-Time Computing and Communications Lab., Hanyang University 16 Real-Time Computing and Communications Lab., Hanyang University Bounded-Buffer Problem  Example 결과

17 Real-Time Computing and Communications Lab., Hanyang University 17 Real-Time Computing and Communications Lab., Hanyang University The Dining Philosopher Problem  여러 프로세스가 동시에 돌아갈 때 교착 상태가 나타나는 원 인을 직관적으로 보여주는 문제  다섯 명의 철학자가 앉아 있고, 철학자의 양 옆에는 젓가락이 하나씩 놓여져 있음  철학자가 식사를 하기 위해서는 양 옆의 2 개의 젓가락을 동시에 들고 있어야 함

18 Real-Time Computing and Communications Lab., Hanyang University 18 Real-Time Computing and Communications Lab., Hanyang University The Dining Philosopher Problem  Possible solutions  Allow at most four philosophers to be sitting simultaneously at the table  Allow a philosopher to pick up her chop sticks only if both chopsticks are available  Use an asymmetric solution: an odd philosophers picks up first her left chopstick and then her right chopstick, whereas an even philosopher picks up her right chopstick and then her left chopstick

19 Real-Time Computing and Communications Lab., Hanyang University 19 Real-Time Computing and Communications Lab., Hanyang University Quiz #1  주어진 Skeleton Code 와 Semaphore API 를 이용하여, Dining Philosopher Problem 을 해결해보자.  앞에 나온 해결책들 이외에 Deadlock 을 해결하면서 더 효율적인 알고리즘 이 있다면 그것을 사용해도 된다.  철학자들이 균등하게 식사한다.  각각의 철학자는 HUNGRY, EATING, THINKING 상태를 가진다.  초기 상태는 THINKING 이다.  젓가락을 놓는 순간, THINKING 상태로 돌입한다.  THINKING -> HUNGRY, EATING -> THINKING 의 상태변화는 10~100msec 후 일어난다.

20 Real-Time Computing and Communications Lab., Hanyang University 20 Real-Time Computing and Communications Lab., Hanyang University Quiz #1  Skeleton Code>

21 Real-Time Computing and Communications Lab., Hanyang University 21 Real-Time Computing and Communications Lab., Hanyang University Quiz #1  Skeleton Code>

22 Real-Time Computing and Communications Lab., Hanyang University 22 Real-Time Computing and Communications Lab., Hanyang University Quiz #1  잘못된 알고리즘으로 이 문제를 해결하려고 하면 아래와 같이 Deadlock 이 발생할 수 있습니다.

23 Real-Time Computing and Communications Lab., Hanyang University 23 Real-Time Computing and Communications Lab., Hanyang University Quiz #1  Example>  오른쪽 그림은 10 초 동안 동작시킨 모습입니다.  각각의 철학자들은 현재 자신의 상 태를 출력합니다.  최종적으로 주어진 동작시간 동안 식사한 총 횟수를 출력해 줍니다.