Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee."— Presentation transcript:

1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL ©2008, Prentice Hall

2 Concurrency Problem: A Simple Example char chin, chout; void echo() { chin = getchar(); chout = chin; putchar(chout); }

3 Concurrency Problem: A Simple Example Process P1Process P2. chin = getchar();..chin = getchar();chout = chin; putchar(chout);..putchar(chout);.

4 Multiprogramming Concerns Output of process must be independent of the speed of execution of other concurrent processes

5 Competition among Processes for Resources Mutual Exclusion –Critical sections Deadlock Starvation

6 Key Terms

7 Race Condition Example Process P1Process P2. chin = getchar();..chin = getchar();chout = chin; putchar(chout);..putchar(chout);.

8 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

9 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 processors A process remains inside its critical section for a finite time only

10 Mutual Exclusion Approaches Software approach Appendix A Hardware support, using special-purpose machine instructions Support within OS or a programming language –Semaphore, Monitors, Message Passing

11 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 Disadvantages: –Processor is limited in its ability to interleave programs –Will not work in multiprocessor architecture

12 Mutual Exclusion: Hardware Support Compare&Swap Instruction int compare_and_swap (int *word, int testval, int newval) { int oldval; oldval = *word; if (oldval == testval) *word = newval; return oldval; }

13 Mutual Exclusion

14 Mutual Exclusion: Hardware Support Exchange instruction void exchange (int *register, int *memory) { int temp; temp = *memory; *memory = *register; *register = temp; }

15 Mutual Exclusion

16 Mutual Exclusion Machine- Instruction Advantages –Applicable to any number of processes on a single processor –Processes on multiple processors? as long as processors share main memory –It is simple and therefore easy to verify –It can be used to support multiple critical sections; each critical section can be defined by its own variable (e.g. bolt1, bolt2, etc.)

17 Mutual Exclusion Machine- Instruction Disadvantages –Busy-waiting consumes processor time –Starvation? Starvation is possible when a process leaves a critical section and more than one process is waiting –Deadlock?

18 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

19 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

20 Semaphore Primitives

21 Example of Semaphore Mechanism

22

23 Mutual Exclusion Using Semaphores

24

25 Processes Using Semaphore

26 Binary Semaphore Primitives

27 Producer/Consumer Problem One or more producers are generating data and placing these in a buffer One or more consumers are taking items out of the buffer one at time Only one producer or consumer may access the buffer at any one time Producer can’t add data into full buffer and consumer can’t remove data from empty buffer

28 Buffer

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

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

31 Incorrect Solution

32 32

33 Correct Solution

34 Semaphores

35 Circular Buffer

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

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

38 Semaphores

39

40 Semaphore Implementation: Atomic Primitives

41

42 Mutual Exclusion Machine- Instruction vs. Semaphore

43

44 Mutual Exclusion Approaches Software approach Appendix A Hardware support, using special-purpose machine instructions Support within OS or a programming language –Semaphore, Monitors, Message Passing

45 Monitor

46 Mutual Exclusion Approaches Software approach Appendix A Hardware support, using special-purpose machine instructions Support within OS or a programming language –Semaphore, Monitors, Message Passing

47 Message Passing Enforce mutual exclusion Exchange information send (destination, message) receive (source, message)

48 Synchronization The receiver cannot receive a message until it has been sent by another process What happens to a process after it issues a send or receive primitive –Sender and receiver may or may not be blocked (waiting for message)

49 Synchronization Blocking send, blocking receive –Both sender and receiver are blocked until message is delivered 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

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

51 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

52 Indirect Process Communication

53 General Message Format

54 Mutual Exclusion Using Messages (1) Assume –blocking receive primitive & –nonblocking send primitive

55 Mutual Exclusion Using Messages (2)

56 Producer/Consumer Messages

57 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 How is this problem different from mutual exclusion problem? Cast it to a mutual exclusion problem?

58 Any Problem with This Solution?

59 Readers have Priority

60 Writers Have Priority The following semaphores and variables are added: –A semaphore rsem that inhibits all readers while there is at least one writer desiring access to the data area –A variable writecount that controls the setting of rsem –A semaphore y that controls the updating of writecount –A semaphore z that prevents a long queue of readers to build up on rsem

61 Writers have Priority

62

63

64 Message Passing Note: count is initialized to the maximum possible number of readers, e.g., 100.


Download ppt "Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee."

Similar presentations


Ads by Google