Docsity

Process Synchronization - Lecture Notes | EECS 678, Study notes of Operating Systems

Partial preview of the text

Download Process Synchronization - Lecture Notes | EECS 678 and more Study notes Operating Systems in PDF only on Docsity!

Process Synchronization – Outline

n Why do processes need synchronization?

n What is the critical-section problem?

n Describe solutions to the critical-section problem

l (^) Peterson’s solution l (^) using synchronization hardware l (^) semaphores l (^) monitors

n Classic Problems of Synchronization

n What are atomic transactions?

Why Process Synchronization?

n Processes may cooperate with each other

l (^) producer-consumer and service-oriented system models l (^) exploit concurrent execution on multiprocessors

n Cooperating processes may share data (globals, files, etc)

l (^) imperative to maintain data correctness

n Why is data correctness in danger?

l (^) process run asynchronously, context switches can happen at any time l (^) processes may run concurrently l (^) different orders of updating shared data may produce different values

n Process synchronization

l (^) to coordinate updates to shared data l (^) order of process execution should not affect shared data

n Only needed when processes share data!

Producer-Consumer Data Sharing

while (true){ /* wait if buffer full / while (counter == 10) ; / do nothing / / produce data / buffer[in] = sdata; in = (in + 1) % 10; / update number of items in buffer / R1 = load (counter); R1 = R1 + 1; counter = store (R1); } while (true){ / wait if buffer empty / while (counter == 0) ; / do nothing / / consume data / sdata = buffer[out]; out = (out + 1) % 10; / update number of items in buffer */ R2 = load (counter); R2 = R2 – 1; counter = store (R2); }

Producer Consumer

n Suppose counter = 5

n Race condition is a situation where

l (^) several processes concurrently manipulate shared data, and l (^) shared data value depends on the order of execution

Race Condition

R1 = load (counter); R1 = R1 + 1; R2 = load (counter); R2 = R2 – 1; counter = store (R1); counter = store (R2); Final Value in counter = 4! R1 = load (counter); R1 = R1 + 1; R2 = load (counter); R2 = R2 – 1; counter = store (R2); counter = store (R1); Final Value in counter = 6! Incorrect Sequence 1 Incorrect Sequence 2

Solution to the Critical Section Problem

n Formally states, each solution should ensure

l (^) mutual exclusion: only a single process can execute in its critical section at a time l (^) progress: selection of a process to enter its critical section should be fair, and the decision cannot be postponed indefinitely. l (^) bounded waiting: there should be a fixed bound on how long it takes for the system to grant a process's request to enter its critical section

n Other than satisfying these requirements, the system should

also guard against deadlocks.

Preemptive Vs. Non-preemptive Kernels

n Several kernel processes share data

l (^) structures for maintaining file systems, memory allocation, interrupt handling, etc.

n How to ensure OSes are free from race conditions?

n Non–preemptive kernels

l (^) process executing in kernel mode cannot be preempted l (^) disable interrupts when process is in kernel mode l (^) what about multiprocessor systems?

n Preemptive kernels

l (^) process executing in kernel mode can be preempted l (^) suitable for real-time programming l (^) more responsive

n Solution meets all three requirements

l (^) P0 and P1 can never be in the critical section at the same time l (^) if P0 does not want to enter critical region, P1 does no waiting l (^) process waits for at most one turn of the other to progress

Peterson's Solution

do { flag[0] = TRUE; turn = 1; while (flag[1] && turn==1) ; // critical section flag[0] = FALSE; // remainder section } while (TRUE) do { flag[1] = TRUE; turn = 0; while (flag[0] && turn==0) ; // critical section flag[1] = FALSE; // remainder section } while (TRUE)

Process 0 Process 1

Peterson's Solution – Notes

n Only supports two processes

l (^) generalizing for more than two processes has been achieved

n Assumes that the LOAD and STORE instructions are atomic

n Assumes that memory accesses are not reordered

n May be less efficient than a hardware approach

l (^) particularly for >2 processes

Hardware Support for Lock-Based

Solutions – Uniprocessors

n For uniprocessor systems

l (^) concurrent processes cannot be overlapped, only interleaved l (^) process runs until it invokes system call, or is interrupted

n Disable interrupts!

l (^) active process will run without preemption do { disable interrupts ; critical section enable interrupts ; remainder section } while(TRUE);

Hardware Support for Lock-Based

Solutions – Multiprocessors

n In multiprocessors

l (^) several processes share memory l (^) processors behave independently in a peer manner

n Disabling interrupt based solution will not work

l (^) too inefficient l (^) OS using this not broadly scalable

n Provide hardware support in the form of atomic instructions

l (^) atomic test-and-set instruction l (^) atomic swap instruction l (^) atomic compare-and-swap instruction

n Atomic execution of a set of instructions means that the

instructions are treated as a single step that cannot be

interrupted.

Mutual Exclusion using TestAndSet

int mutex; init_lock (&mutex) ; do { lock (&mutex); critical section unlock (&mutex) ; remainder section } while(TRUE); void init_lock (int *mutex) { *mutex = 0; } void lock (int *mutex) { while(TestAndSet(mutex)) ; } void unlock (int *mutex) { *mutex = 0; }

Swap Instruction

n Psuedo code definition of swap instruction

void Swap (boolean *a, boolean *b)

boolean temp = *a;

*a = *b;

*b = temp:

Bounded Waiting Solution

do{ waiting[i] = TRUE; key = TRUE; while(waiting[i] && key) key = TestAndSet(&lock); waiting[i] = FALSE; // Critical Section j = (i + 1) % n; while ((j != i) && !waiting[j]) j = (j+1) % n; if (j == i ) lock = FALSE; else waiting[j] = FALSE; // Remainder Section } while (TRUE); do{ waiting[i] = TRUE; key = TRUE; while(waiting[i] && key) key = TestAndSet(&lock); waiting[i] = FALSE; // Critical Section j = (i + 1) % n; while ((j != I) && !waiting[j]) j = (j+1) % n; if (j == i ) lock = FALSE; else waiting[j] = FALSE; // Remainder Section } while (TRUE);

Process i = 0 Process i = 1

Cycle = 0

Process i = 0

lock=FALSE, key=FALSE, waiting[0]=0, waiting[1]=

Bounded Waiting Solution

do{ waiting[i] = TRUE; key = TRUE; while(waiting[i] && key) key = TestAndSet(&lock); waiting[i] = FALSE; // Critical Section j = (i + 1) % n; while ((j != i) && !waiting[j]) j = (j+1) % n; if (j == i ) lock = FALSE; else waiting[j] = FALSE; // Remainder Section } while (TRUE); do{ waiting[i] = TRUE; key = TRUE; while(waiting[i] && key) key = TestAndSet(&lock); waiting[i] = FALSE; // Critical Section j = (i + 1) % n; while ((j != I) && !waiting[j]) j = (j+1) % n; if (j == i ) lock = FALSE; else waiting[j] = FALSE; // Remainder Section } while (TRUE);

Process i = 0 Process i = 1

Cycle = 1

Process i = 0

lock=FALSE, key=FALSE, waiting[0]=1, waiting[1]=