1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.

Slides:



Advertisements
Similar presentations
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Advertisements

1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2)
Chapter 6: Process Synchronization
5.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles Seventh Edition By William Stallings.
Concurrency: mutual exclusion and synchronization Slides are mainly taken from «Operating Systems: Internals and Design Principles”, 8/E William Stallings.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles Seventh Edition By William Stallings.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 91 Today’s class Mutual exclusion and synchronization 
Computer Systems/Operating Systems - Class 8
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Enforcing Mutual Exclusion Message Passing. Peterson’s Algorithm for Processes P0 and P1 void P0() { while( true ) { flag[ 0 ] = false; /* remainder */
Concurrency: Mutual Exclusion and Synchronization Why we need Mutual Exclusion? Classical examples: Bank Transactions:Read Account (A); Compute A = A +
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization
Teaching Operating Systems With Programming and Freeware Lecture 2: Concurrency Issues, processor scheduling (Lab2 and 3) A workshop by Dr. Junaid Ahmed.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
1 Semaphores Special variable called a semaphore is used for signaling If a process is waiting for a signal, it is suspended until that signal is sent.
Chapter 2.3 : Interprocess Communication
1 Chapter 6: Concurrency: Mutual Exclusion and Synchronization Operating System Spring 2007 Chapter 6 of textbook.
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
1 Chapter 5 Concurrency. 2 Concurrency 3 4 Mutual Exclusion: Hardware Support Test and Set Instruction boolean testset (int *i) { if (*i == 0) { *i.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Operating System 5 CONCURRENCY: MUTUAL EXCLUSION AND SYNCHRONIZATION.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings 1.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Dave Bremer Otago.
Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Dave Bremer Otago Polytechnic,
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Dave Bremer Otago.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Introduction to Concurrency.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 4.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
1 Concurrent Processes. 2 Cooperating Processes  Operating systems allow for the creation and concurrent execution of multiple processes  concurrency.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-5 Process Synchronization Department of Computer Science and Software.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Operating System Chapter 5. Concurrency: Mutual Exclusion and Synchronization Lynn Choi School of Electrical Engineering.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization
G.Anuradha Reference: William Stallings
Concurrency.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization
Concurrency These slides are intended to help a teacher develop a presentation. This PowerPoint covers the entire chapter and includes too many slides.
Concurrency: Mutual Exclusion and Synchronization
Chapter 5: Process Synchronization
Concurrency: Mutual Exclusion and Synchronization
Chapter 5 Concurrency: Mutual Exclusion and Synchronization
Concurrency: Mutual Exclusion and Process Synchronization
Chapter 6: Process Synchronization
Chapter 6: Synchronization Tools
Operating System 5 CONCURRENCY: MUTUAL EXCLUSION AND SYNCHRONIZATION
Operating Systems Concepts
Chapter 5 Mutual Exclusion(互斥) and Synchronization(同步)
Presentation transcript:

1 Concurrency: Mutual Exclusion and Synchronization Chapter 5

2 Concurrency Why Important? –We have multiple programs running at the same time. Difficulties: –Shared memory access. –Managing the I/O device access

3 Operating System Concerns Keep track of various processes Allocate and deallocate resources –Processor time –Memory –Files –I/O devices Protect data and resources Output of process must be independent of the speed of execution of other concurrent processes

4 Difficulties of Concurrency Sharing of global resources Operating system managing the allocation of resources optimally Difficult to locate programming errors –Because we can’t control how everything is going to be timed can be very difficult to duplicate an error.

5 Three Main Types of Concurrent Processes Processes unaware of each other –Have no access or relation. Processes indirectly aware of each other –Share some data or an object, but don’t have direct access to each other. Process directly aware of each other –Have direct access to each other.

6

7 A Simple Example void echo() { chin = getchar(); chout = chin; putchar(chout); }

8 A Simple Example Process P1Process P2.... chin = getchar();.. chin = getchar();chout = chin; putchar(chout);..putchar(chout);....

9 Mutual Exclusion What would be some basic rules? –Make sure the processes don’t interfere with each other. (Only one process allowed in the critical section at a time.) –Finite length critical sections (can’t stay there forever).

10 Requirements for Mutual Exclusion Only one process at a time is allowed in the critical section for a resource A process that halts in its noncritical section must do so without interfering with other processes No deadlock or starvation

11 Requirements for Mutual Exclusion A process must not be delayed access to a critical section when there is no other process using it No assumptions are made about relative process speeds or number of processes A process remains inside its critical section for a finite time only

12 Mutual Exclusion: Hardware Support Interrupt Disabling –A process runs until it invokes an operating system service or until it is interrupted –Disabling interrupts guarantees mutual exclusion –Processor is limited in its ability to interleave programs –Multiprocessing disabling interrupts on one processor will not guarantee mutual exclusion

13 Mutual Exclusion: Hardware Support Special Machine Instructions –Performed in a single instruction cycle –Access to the memory location is blocked for any other instructions Why is this important? –We can’t have instructions getting inter- woven.

14 Peterson’s Algorithm Boolean flag[2]; Int turn; Void P0() { while (true) { flag[0]=true; turn = 1; while (flag[1] && turn==1) /** do nothing **/ /** crit section **/ flag [0] = false; } }

15 Peterson’s Algorithm Boolean flag[2]; Int turn; Void P1() { while (true) { flag[1]=true; turn = 0; while (flag[0] && turn==0) /** do nothing **/ /** crit section **/ flag [1] = false } }

16 Homework Extend Peterson’s Algorithm to work with 3 processes. Due Wed Feb 16 th

17 Message Passing Methods: Memory and Direct Connection Enforce mutual exclusion Exchange information send (destination, message) receive (source, message)

18 Synchronization Sender and receiver may or may not be blocking (waiting for message) Blocking send, blocking receive –Both sender and receiver are blocked until message is delivered –Called a rendezvous

19 Synchronization Nonblocking send, blocking receive –Sender continues on –Receiver is blocked until the requested message arrives Nonblocking send, nonblocking receive –Neither party is required to wait

20 Addressing Direct addressing –Send primitive includes a specific identifier of the destination process –Receive primitive could know ahead of time which process a message is expected –Receive primitive could use source parameter to return a value when the receive operation has been performed

21 Direct Addressing Piped Steams : C and Java –Used to create connections between threads.

22 Addressing Indirect addressing –Messages are sent to a shared data structure consisting of queues –Queues are called mailboxes –One process sends a message to the mailbox and the other process picks up the message from the mailbox

23

24 Message Format

25

26

27 Mutual Exclusion: Hardware Support Test and Set Instruction boolean testset (int i) { if (i == 0) { i = 1; return true; } else { return false; } }

28 Mutual Exclusion: Hardware Support Exchange Instruction void exchange(int register, int memory) { int temp; temp = memory; memory = register; register = temp; }

29 Mutual Exclusion

30 Mutual Exclusion Machine Instructions Advantages –Applicable to any number of processes on either a single processor or multiple processors sharing main memory –It is simple and therefore easy to verify –It can be used to support multiple critical sections

31 Mutual Exclusion Machine Instructions Disadvantages –Busy-waiting consumes processor time –Starvation is possible when a process leaves a critical section and more than one process is waiting. –Deadlock If a low priority process has the critical region and a higher priority process needs, the higher priority process will obtain the processor to wait for the critical region

32 Semaphores Special variable called a semaphore is used for signaling If a process is waiting for a signal, it is suspended until that signal is sent

33 Semaphores Semaphore is a variable that has an integer value –May be initialized to a nonnegative number –Wait operation decrements the semaphore value –Signal operation increments semaphore value

34

35

36 Mutual Exclusion Using Semaphores

37

38

39 Producer/Consumer Problem One or more producers are generating data and placing these in a buffer A single consumer is taking items out of the buffer one at time Only one producer or consumer may access the buffer at any one time

40 Producer producer: while (true) { /* produce item v */ b[in] = v; in++; }

41 Consumer consumer: while (true) { while (in <= out) /*do nothing */; w = b[out]; out++; /* consume item w */ }

42 Producer/Consumer Problem

43 Producer with Circular Buffer producer: while (true) { /* produce item v */ while ((in + 1) % n == out) /* do nothing */; b[in] = v; in = (in + 1) % n }

44 Consumer with Circular Buffer consumer: while (true) { while (in == out) /* do nothing */; w = b[out]; out = (out + 1) % n; /* consume item w */ }

45

46

47

48

49

50 Monitors Monitor is a software module Chief characteristics –Local data variables are accessible only by the monitor –Process enters monitor by invoking one of its procedures –Only one process may be executing in the monitor at a time

51

52

53

54

55 Readers/Writers Problem Any number of readers may simultaneously read the file Only one writer at a time may write to the file If a writer is writing to the file, no reader may read it

56

57

58 Concurrency Multiple applications –Multiprogramming Structured application –Application can be a set of concurrent processes Operating-system structure –Operating system is a set of processes or threads

59 Competition Among Processes for Resources Mutual Exclusion –Critical sections Only one program at a time is allowed in its critical section Example only one process at a time is allowed to send command to the printer Deadlock – Next Chapter Starvation – Next Chapter

60