Docsity

Synchronization Primitives and Semaphores in Operating Systems - Prof. Jonathan Walpole, Study notes of Operating Systems

Partial preview of the text

Download Synchronization Primitives and Semaphores in Operating Systems - Prof. Jonathan Walpole and more Study notes Operating Systems in PDF only on Docsity!

CS 333

Introduction to Operating SystemsClass 4 – Synchronization Primitives

Semaphores^ Jonathan WalpoleComputer SciencePortland State University

Synchronization primitives^ ^ Sleep

^

Put a thread to sleep

^

Thread becomes BLOCKed

^ Wakeup

^

Move a BLOCKed thread back onto “Ready List”

^

Thread becomes READY (or RUNNING)

^ Yield

^

Move to another thread

^

Does not BLOCK thread

^

Just gives up the current time-slice

Concurrency control in the kernel^ ^ Different threads call Yield, Sleep, ...^ ^ Scheduler routines manipulate the “ready list”^ ^ The ready list is shared data!^ ^ Problem:

^ How can scheduler routines be programmedcorrectly?

^ Solution:

^ Scheduler can

disable interrupts

, or

^ Scheduler can use the

TSL instruction

Concurrency in the kernel^ ^ The kernel can avoid performing context switcheswhile manipulating the ready list

^ prevents concurrent execution of system call code ^ … but what about interrupts? ^ … what if interrupt handlers touch the ready list?

^ Disabling interrupts during critical sections

^ Ensures that interrupt handling code will not run

^ Using TSL for critical sections

^ Ensures mutual exclusion for all code

that follows

that convention

Disabling interrupts in the kernel^ Scenario 1:A thread is running; wants to access shared data

Disable interruptsAccess shared data (“critical section”)Enable interrupts

Disabling interrupts in the kernel^ Scenario 2:Interrupts are already disabled and a secondthread wants to access the critical section

...using the above sequence...

Classical Synchronization Problems^ Producer-Consumer

^ One thread produces data items ^ Another thread consumes them ^ Use a bounded buffer / queue between the threads ^ The buffer is a shared resource

-^ Must control access to it!!!

^ Must suspend the producer thread if buffer is full ^ Must suspend the consumer thread if buffer isempty

Producer/Consumer with Busy Waiting thread producer {while(1){

// Produce char c while (count==n) {no_op} buf[InP] = cInP = InP + 1 mod ncount++} }

thread consumer {

while(1){while (count==0) {

no_op} c = buf[OutP]OutP = OutP + 1 mod ncount-- // Consume char } }

n-1 …

Global variables:

char buf[n]int InP = 0

// place to add

int OutP = 0

// place to get

int count

0 thread consumer { 1

while(1) { 2

while (count==0) { 3

sleep(empty) 4

c = buf[OutP] 6

OutP = OutP + 1 mod n 7

count--; 8

if (count == n-1) 9

wakeup(full) 10

// Consume char 11

Producer/Consumer with Blocking 0 thread producer { 1

while(1) { 2

// Produce char c 3

if (count==n) { 4

sleep(full) 5

buf[InP] = c; 7

InP = InP + 1 mod n 8

count++ 9

if (count == 1) 10

wakeup(empty) 11

n-1 …

Global variables:

char buf[n]int InP = 0

// place to add

int OutP = 0

// place to get

int count

This code is still incorrect!^ ^ The “count” variable can be corrupted:

^ Increments or decrements may be lost! ^ Possible Consequences:

-^ Both threads may sleep forever •^ Buffer contents may be over-written ^ What is this problem called?

This code is still incorrect!^ ^ The “count” variable can be corrupted:

^ Increments or decrements may be lost! ^ Possible Consequences:

-^ Both threads may sleep forever •^ Buffer contents may be over-written ^ What is this problem called?

Race Condition

^ Code that manipulates count must be made intoa

critical section

and protected using

mutual

exclusion!

Semaphores^ ^ An abstract data type that can be used forcondition synchronization and mutual exclusion^ What is the difference between mutualexclusion and condition synchronization?

Semaphores^ ^ An abstract data type

^ containing an integer variable (S) ^ Two operations: Down (S) and Up (S)

^ Alternative names for the two operations

^ Down(S)

= Wait(S)

=^

P(S)

^ Up(S)

=^

Signal(S) = V(S)

Semaphores^ ^ Down

(S)

… called “Wait” in SPANK

^ decrement S by 1 ^ if S would go negative, wait/sleep until signaled

^ Up

(S)

…^

called “Signal” in SPANK

^ increment S by 1 ^ signal/wakeup a waiting thread

^ S will always be >= 0.

^ Both

Up

()^ and

Down

()^ are assumed to be

atomic!!!

^ A kernel implementation must ensure atomicity