Chapter 05 in os
-
Upload
ahmad-suliman -
Category
Documents
-
view
231 -
download
5
Transcript of Chapter 05 in os
![Page 1: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/1.jpg)
Chapter 5Concurrency: Mutual
Exclusion and Synchronization
Operating Systems:Internals and Design Principles, 6/E
William Stallings
Patricia RoyManatee Community College, Venice, FL©2008, Prentice Hall
![Page 2: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/2.jpg)
Concurrency Problem: A Simple Example
char chin, chout;void echo(){
chin = getchar();chout = chin;putchar(chout);
}
![Page 3: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/3.jpg)
Concurrency Problem: A Simple Example
Process P1 Process P2. .
chin = getchar(); .. chin =
getchar();chout = chin; chout = chin;putchar(chout); .
.putchar(chout);
. .
![Page 4: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/4.jpg)
Multiprogramming Concerns
• Output of process must be independent of the speed of execution of other concurrent processes
![Page 5: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/5.jpg)
Competition among Processes for Resources
• Mutual Exclusion–Critical sections
• Deadlock• Starvation
![Page 6: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/6.jpg)
Key Terms
![Page 7: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/7.jpg)
Race Condition Example
Process P1 Process P2. .
chin = getchar(); .. chin =
getchar();chout = chin; chout = chin;putchar(chout); .
.putchar(chout);
. .
![Page 8: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/8.jpg)
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
![Page 9: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/9.jpg)
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
![Page 10: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/10.jpg)
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
![Page 11: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/11.jpg)
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
![Page 12: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/12.jpg)
Mutual Exclusion: Hardware Support
• Compare&Swap Instructionint compare_and_swap (int *word, int testval, int newval){
int oldval;oldval = *word;if (oldval == testval) *word = newval;return oldval;
}
![Page 13: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/13.jpg)
Mutual Exclusion
![Page 14: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/14.jpg)
Mutual Exclusion: Hardware Support
• Exchange instructionvoid exchange (int *register, int *memory){
int temp;temp = *memory;*memory = *register;*register = temp;
}
![Page 15: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/15.jpg)
Mutual Exclusion
![Page 16: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/16.jpg)
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.)
![Page 17: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/17.jpg)
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?
![Page 18: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/18.jpg)
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
![Page 19: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/19.jpg)
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
![Page 20: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/20.jpg)
Semaphore Primitives
![Page 21: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/21.jpg)
Example of Semaphore Mechanism
![Page 22: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/22.jpg)
Example of Semaphore Mechanism
![Page 23: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/23.jpg)
Mutual Exclusion Using Semaphores
![Page 24: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/24.jpg)
Mutual Exclusion Using Semaphores
![Page 25: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/25.jpg)
Processes Using Semaphore
![Page 26: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/26.jpg)
Binary Semaphore Primitives
![Page 27: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/27.jpg)
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
![Page 28: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/28.jpg)
Buffer
![Page 29: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/29.jpg)
Producer
• producer:while (true) {
/* produce item v */b[in] = v;in++;
}
![Page 30: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/30.jpg)
Consumer
• consumer:while (true) { while (in <= out)
/*do nothing */;w = b[out];out++; /* consume item w */
}
![Page 31: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/31.jpg)
Incorrect Solution
![Page 32: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/32.jpg)
32
![Page 33: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/33.jpg)
Correct Solution
![Page 34: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/34.jpg)
Semaphores
![Page 35: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/35.jpg)
Circular Buffer
![Page 36: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/36.jpg)
Producer with Circular Buffer
• producer:while (true) {
/* produce item v */while ((in + 1) % n == out) /* do
nothing */;b[in] = v;in = (in + 1) % n
}
![Page 37: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/37.jpg)
Consumer with Circular Buffer
• consumer:while (true) {
while (in == out)/* do nothing */;
w = b[out];out = (out + 1) % n;/* consume item w */
}
![Page 38: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/38.jpg)
Semaphores
![Page 39: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/39.jpg)
Semaphores
![Page 40: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/40.jpg)
Semaphore Implementation: Atomic Primitives
![Page 41: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/41.jpg)
![Page 42: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/42.jpg)
Mutual Exclusion Machine-Instruction vs. Semaphore
![Page 43: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/43.jpg)
Mutual Exclusion Machine-Instruction vs. Semaphore
![Page 44: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/44.jpg)
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
![Page 46: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/46.jpg)
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
![Page 47: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/47.jpg)
Message Passing
• Enforce mutual exclusion• Exchange information
• send (destination, message)• receive (source, message)
![Page 48: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/48.jpg)
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)
![Page 49: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/49.jpg)
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
![Page 50: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/50.jpg)
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
![Page 51: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/51.jpg)
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
![Page 52: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/52.jpg)
Indirect Process Communication
![Page 53: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/53.jpg)
General Message Format
![Page 54: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/54.jpg)
Mutual Exclusion Using Messages (1)
• Assume–blocking receive primitive
&–nonblocking send primitive
![Page 55: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/55.jpg)
Mutual Exclusion Using Messages (2)
![Page 56: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/56.jpg)
Producer/Consumer Messages
![Page 57: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/57.jpg)
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?
![Page 58: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/58.jpg)
Any Problem with This Solution?
![Page 59: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/59.jpg)
Readers have Priority
![Page 60: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/60.jpg)
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
![Page 61: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/61.jpg)
Writers have Priority
![Page 62: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/62.jpg)
Writers have Priority
![Page 63: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/63.jpg)
Writers have Priority
![Page 64: Chapter 05 in os](https://reader034.fdocuments.in/reader034/viewer/2022052313/577cce281a28ab9e788d782d/html5/thumbnails/64.jpg)
Message Passing
Note: count is initialized to the maximum possible number of readers, e.g., 100.