Systems programming Thread management (Cont.) Synchronization using - - PowerPoint PPT Presentation

systems programming
SMART_READER_LITE
LIVE PREVIEW

Systems programming Thread management (Cont.) Synchronization using - - PowerPoint PPT Presentation

Systems programming Thread management (Cont.) Synchronization using Semaphores Most of the slides in this lecture are either from or adapted from the slides provided by Dr. Ahmad Barghash Semaphores Introduced in 1965 by E. Dijkstra It


slide-1
SLIDE 1

Systems programming

Thread management (Cont.) Synchronization using Semaphores

Most of the slides in this lecture are either from or adapted from the slides provided by Dr. Ahmad Barghash

slide-2
SLIDE 2

Semaphores

  • Introduced in 1965 by E. Dijkstra
  • It is an abstract data type (a special integer variable) used to access

shared data/resources.

  • Semaphores are special integer variables that can be initialized to any

value ≥ 0 and can only be manipulated through two atomic

  • perations: P( ) and V( )
  • Also called wait()and signal()
slide-3
SLIDE 3

The P( ) operation

◼ If semaphore value is zero,

Wait until value become positive

◼ Once value of semaphore is greater

than zero,

Decrement it

Wait()

slide-4
SLIDE 4

The V( ) operation

◼ Increment the value of the

semaphore

Signal()

slide-5
SLIDE 5

sem_wait() () and sem_post()

◼ sem_t *mysem; sem_wait(mysem);

Implements the P() operation

◼ sem_t *mysem; sem_post(mysem);

Implements the V() operation

Semaphores in Pthreads Library

slide-6
SLIDE 6

An analogy

◼ Paula and Victor work in a restaurant: ◼ Paula handles customer arrivals:

Prevents people from entering the restaurant when all tables are

busy.

◼ Victor handles departures

 Notifies people waiting for a table when one becomes available

slide-7
SLIDE 7

An analogy (II)

◼ The semaphore represents the number of available tables

Initialized with the total number of tables in restaurant

slide-8
SLIDE 8

An analogy (III)

Paula V ictor Restaurant

slide-9
SLIDE 9

An analogy (IV)

◼ When people come to the restaurant, they wait for Paula to direct

them:

If a table is available, she let them in and decrements the table

count

slide-10
SLIDE 10

An analogy (V)

X

X X X

Paula V ictor Restaurant

slide-11
SLIDE 11

An analogy (VI)

◼ When people leave, they tell Victor:

Victor increments the semaphore and checks the waiting area: If there is anyone in there, he lets one group in and decrements the

semaphore

◼ Paula and Victor have worked long enough together and don't

interfere with each other

slide-12
SLIDE 12

CLASSICAL PROBLEMS: : Applications for Semaphores

◼ Bounded Buffer ◼ Readers and Writes ◼ Dining Philosophers ◼ Sleeping barber

slide-13
SLIDE 13

Bounded buffer (I)

◼ Producer: produce one “item” each iteration. ◼ Consumer: consume one “item” each iteration. ◼ Restriction: a buffer holds N items! ◼ One or more producer processes put their output in a bounded

buffer

Must wait when buffer is full

◼ One or more consumer processes take items from the buffer

Must wait when buffer is empty

slide-14
SLIDE 14

Bounded buffer (II)

Producer Producer Consumer Consumer Consumer

slide-15
SLIDE 15

The rules

◼ Three rules

Producers cannot store items in the buffer when the buffer is full. Consumers cannot take items from the buffer when the buffer is

empty.

Producers and consumers must access the buffer one at a time

slide-16
SLIDE 16

Two analogies

The supermarket

Supermarket is the buffer We are the consumers Suppliers are the producers

Our garbage

Our garbage can is the buffer We are the producers Garbage truck is the consumer

slide-17
SLIDE 17

Bounded Buffer – Solution Pseudocode

producer() { struct x item; do { produce(&item); wait(empty); wait(mutex); put(item); signal(mutex); signal(full); } while(true); } // producer consumer() { struct x item; do { wait(full); wait(mutex); take(item); signal(mutex); signal(empty); consume(item); } while(true); } // consumer Semaphore mutex = 1; Semaphore empty = Buffer_Size; Semaphore full = 0; int buffer[Buffer_Size];

Shared Data 1 2 3 Critical Section!

The “empty” semaphore is about the available places in the buffer. The “full” semaphore is about the available items in the buffer.

slide-18
SLIDE 18
slide-19
SLIDE 19
slide-20
SLIDE 20

Order matters (Note about the pseudocode in the previous slide)

◼ The order of the two wait( ) operations is very important

Neither the producer or the consumer should request exclusive

access to the buffer before being sure they can perform the

  • peration they have to perform.

◼ The order of the two signal( ) operations does not matter

slide-21
SLIDE 21

The readers-writers problem (I)

◼ We have a file (or a database) and two types of processes:

Readers that need to access the file Writers that need to update it.

◼ A real problem

slide-22
SLIDE 22

The readers-writers problem (II)

◼ Readers must be prevented from accessing the file while a writer

updates it.

◼ Writers must be prevented from accessing the file while any other

process accesses it

They require mutual exclusion

slide-23
SLIDE 23

An analogy

◼ Sharing a classroom between teachers and students

Teachers use it to lecture

◼ They cannot share the room

Students use it for quiet study

◼ They can share the room with other students

◼ Classroom is assumed to be in use if the light is turned on

slide-24
SLIDE 24

Rules for teachers

◼ Do not enter a classroom if its light is turned on ◼ Otherwise

Turn the light on when you come in Turn the light off when you leave

slide-25
SLIDE 25

Rules for students

◼ If the light is on and you see students but no teacher

Enter the room

◼ If the light is off, you are the first student to enter the room

Turn the light on and enter the room

◼ If you are the last student to leave the room

Turn the light off after leaving

slide-26
SLIDE 26

The readers-writers problem (III)

Shared variables and semaphores

  • int readers_count = 0;
  • semaphore mutex = 1;
  • semaphore access = 1;
slide-27
SLIDE 27

The readers-writers problem (IV)

write_to_file() { P(&access); . . . V(&access); } // write_to_file

slide-28
SLIDE 28

The readers-writers problem (VI)

read_the_file(){ sem_P(&mutex); readers_count++; if(readers_count == 1) sem_P(&access); sem_V(&mutex); . . . sem_P(&mutex); readers_count‐‐; if(readers_count == 0) sem_V(&access); sem_V(&mutex); } // read_the_file

slide-29
SLIDE 29

Starvation

◼ Solution favors the readers over the writers

A continuous stream of incoming readers could block writers forever

◼ Result would be writers' starvation.

A suggested solution is to schedule threads in a round- robin manner pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); pthread_attr_setschedpolicy(&attr, SCHED_RR);

slide-30
SLIDE 30

The sleeping barber (I)

◼ Proposed by Andrew Tanenbaum in his textbook “Modern

Operating Systems”.

◼ A barber shop has several chairs for waiting

customers and one barber who sleeps when there are no customers.

◼ Customers don't wait if the shop is full and there are no free chairs to

sit upon.

Must keep track of the number of customers in the shop

slide-31
SLIDE 31

Global declarations

// number of chairs #define NCHAIRS 4 semaphore mutex = 1; semaphore ready_barber = 0; semaphore waiting_customers = 0; // tracks value of waiting_customers int nwaiting = 0;

slide-32
SLIDE 32

The barber function

◼ barber() {

for(;;) { sem_P(&waiting_customers); sem_P(&mutex); nwaiting‐‐; sem_V(&ready_barber); sem_V(&mutex); cut_hair(); } // for } // barber

The customer function

◼ customer() {

P(&mutex); if (nwaiting < NCHAIRS) { nwaiting++; sem_V(&waiting_customers); V(&mutex); sem_P(&ready_barber); get_haircut(); } // if sem_V(&mutex); } // customer

slide-33
SLIDE 33

◼ Pthreads offer three synchronization primitives

POSIX semaphores Mutexes Condition variables

For Reference Only! (Not Covered in this course)

Pt Pthread synchronization

slide-34
SLIDE 34

Homework!

  • 1. Write a C code using the Pthread library to implement the readers-

writers problem.

  • 2. Write a C code using the Pthread library to implement the sleeping

barber problem.