Docsity

Advanced OS - Synchronization, Mutual Exclusion, and Distributed Coordination, Slides of Advanced Operating Systems

Partial preview of the text

Download Advanced OS - Synchronization, Mutual Exclusion, and Distributed Coordination and more Slides Advanced Operating Systems in PDF only on Docsity!

CIS 620

Advanced Operating Systems

Lecture 9 – Synchronization

Synchronization

• We will be looking at synchronization in two

kinds of systems:

– Shared memory systems

• Mutual exclusion

• Semaphores

– Message passing systems

• Time in distributed systems

• Mutual exclusion

• Election in distributed systems

Shared Memory Coordination

– Context switching primitives normally use busy

waiting in their implementation.

• Mutual Exclusion

– Consider adding one to a shared variable V.

– When compiled onto many machines get three

instructions

• load r1 ← V

• add r1 ← r1+

• store r1 → V

Mutual Exclusion

– Assume V is initially 10 and one process begins

the 3 instruction sequence.

• After the first instruction context switch to another

process.

  • Registers are of course saved.

• New process does all three instructions.

• Context switch back.

  • Registers are of course restored.

• First process finishes.

• V has been incremented twice but has only reached 11.

Mutual Exclusion

– The portion of code that requires mutual

exclusion is often called a critical section.

• One approach is to prevent context switching.

– We can do this for the kernel of a uniprocessor.

• Mask interrupts.

– Not feasible for user mode processes.

– Not feasible for multiprocessors.

Mutual Exclusion

– Critical Section Problem is to implement:

loop

trying-part

critical-section

releasing-part

non-critical section

end loop

– So that when many processes execute this you never have

more than one in the critical section.

– That is you must write the trying-part and the releasing-

part.

Mutual Exclusion

• Then the requirement is "If a process is executing its

trying part, then some process will eventually enter the

critical section".

• Software-only solutions to CS problem.

– We assume the existence of atomic loads and

stores.

• Only up to word length (i.e. not a whole page).

– We start with the case of two processes.

– Easy if we want tasks to alternate in CS and you

know which one goes first in CS.

Mutual Exclusion

shared int turn = 1

loop loop

while (turn=2) while (turn=1)

CS CS

turn=2 turn=

NCS NCS

Mutual Exclusion

• The first solution that worked was discovered

by a mathematician named Dekker.

– Now we will use turn only to resolve disputes.

Dekker’s Algorithm

/* Variables are global and shared / for (; ;) { // process 1 - an infinite loop to show it enters // CS more than once. Turn is initially 1. p1wants = 1; while (p2wants == 1) { if (turn == 2) { p1wants = 0; while (turn == 2) {/ Empty loop */} p1wants = 1; } } critical_section(); turn = 2; p1wants = 0; noncritical_section(); }

Mutual Exclusion

– The winner-to-be just loops waiting for the loser

to give up and then goes into the CS.

– The loser-to-be:

• Gives up.

• Waits to see that the winner has finished.

• Starts over (knowing it will win).

– Dijkstra extended Dekker's solution for > 2

processes.

– Others improved the fairness of Dijkstra's

algorithm.

Mutual Exclusion

– These complicated methods remained the

simplest known until 1981 when Peterson found a

much simpler method.

– Keep Dekker's idea of using turn only to resolve

disputes, but drop the complicated then body of

the if.

Peterson’s Algorithm

/* Variables are global and shared / for (; ;) { // process 2 - an infinite loop to show it enters // CS more than once. p2wants = 1; turn = 1; while (p1wants && turn == 1) {/ empty loop */} critical_section(); p2wants = 0; noncritical_section(); }

Semaphores

– Trying and release often called entry and exit , or

wait and signal , or down and up , or P and V (the

latter are from Dutch words since Dijkstra is

Dutch).

– Let’s try to formalize the entry and exit parts.

– To get mutual exclusion we need to ensure that no

more than one task can pass through P until a V

has occurred. The idea is to keep trying to walk

through the gate and when you succeed

atomically close the gate behind you so that no

one else can enter.