Download Synchronization in Operating Systems: Critical Section Problem and Solutions and more Schemes and Mind Maps Operating Systems in PDF only on Docsity!
Chapter 6:
Synchronization
Objectives and Outline
Objectives
- (^) To introduce the critical-section problem
- (^) Critical section solutions can be used to ensure the consistency of shared data
- To present both software and hardware solutions of the critical- section problem Outline - (^) Background - (^) The Critical-Section Problem - (^) Peterson’s Solution - (^) Synchronization Hardware - (^) Semaphores - (^) Classic Problems of Synchronization - (^) Monitors - (^) Synchronization Examples from operating systems
Producer Consumer Problem Revisited
- (^) Below is a solution to the consumer-producer problem that fills all the slots of the shared buffer.
- (^) Use an integer count to keep track of the number of full slots.
- (^) Initially, count is set to 0. It is incremented by the producer after it puts a new item and is decremented by the consumer after it retrieves an item from the buffer Shared Buffer Producer Consumer count also a shared variable at most BUFFER_SIZE items
Producer and Consumer Code
while (true) { /produce an item/ nextProduced = …. while (count == BUFFER_SIZE) ; // do nothing buffer [in] = nextProduced; in = (in + 1) % BUFFER_SIZE; count++; } while (true) { while (count == 0) ; // do nothing nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; count--; /consume item nextConsumed/ } Producer ConSUMER
Producer Consumer Producer Consumer or
Race Condition
- (^) count++ could be implemented as register1 = count register1 = register1 + 1 count = register
- (^) count-- could be implemented as register2 = count register2 = register2 - 1 count = register
Interleaved Execution sequence
- (^) Consider this execution interleaving with “count = 5” initially: S0: producer execute register1 = count {register1 = 5} S1: producer execute register1 = register1 + 1 {register1 = 6} S2: consumer execute register2 = count {register2 = 5} S3: consumer execute register2 = register2 - 1 {register2 = 4} S4: producer execute count = register1 {count = 6 } S5: consumer execute count = register2 {count = 4}
Programs and critical sections
- (^) The part of the program (process) that is accessing and changing shared data is called its critical section Change X Change X Change Y Change Y Change Y Change X Process 1 Code (^) Process 2 Code Process 3 Code Assuming X and Y are shared data.
Structuring Programs
- (^) The general way to do that is: do { critical section remainder section } while (TRUE) The general structure of a program do { entry section critical section exit section remainder } while (TRUE) Entry section will allow only one process to enter and execute critical section code.
Solution to Critical-Section Problem
1. Mutual Exclusion - If process Pi is executing in its critical section, then no other processes can be executing in their critical sections 2. Progress - If no process is executing in its critical section and there exist some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely **// no deadlock
- Bounded Waiting** - A bound must exist on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted // no starvation of a process
Assume that each process executes at a nonzero speed
No assumption concerning relative speed of the N processes
Kernel Critical Sections
- While kernel is executing a function x(), a hardware interrupt may arrive and interrupt handler h() can be run. Make sure that interrupt handler h() and x() do not access the same kernel global variable. Otherwise race condition may happen.
- (^) While a process is running in user mode, it may call a system call s(). Then kernel starts running function s(). CPU is executing in kernel mode now. We say the process is now running in kernel mode (even though kernel code is running).
- (^) While a process X is running in kernel mode, it may or may not be pre- empted. It preemptive kernels , the process running in kernel mode can be preempted and a new process may start running. In non- preemptive kernels , the process running in kernel mode is not preempted unless it blocks or returns to user mode.
Kernel Critical Sections
- (^) In a preemptive kernel, a process X running in kernel mode may be suspended (preempted) at an arbitrary (unsafe) time. It may be in the middle of updating a kernel variable or data structure at that moment. Then a new process Y may run and it may also call a system call. Then, process Y starts running in kernel mode and may also try update the same kernel variable or data structure (execute the critical section code of kernel). We can have a race condition if kernel is not synchronized.
- (^) Therefore, we need solve synchronization and critical section problem for the kernel itself as well. The same problem appears there as well.
Algorithm for Process P
i do { flag[i] = TRUE; turn = j; while (flag[j] && turn == j); critical section flag[i] = FALSE; remainder section } while (1) entry section exit section
Two processes executing concurrently
do { flag1 = TRUE; turn = 2; while (flag2 && turn == 2); critical section….. flag1 = FALSE; remainder section….. } while (1) do { flag2 = TRUE; turn = 1; while (flag1 && turn == 1); critical section….. flag2 = FALSE; remainder section….. } while (1)
PROCESS 1 PROCESS 2
Shared Variables flag1, flag turn