Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Semaphores - Slides for Operating Systems | COMP 310, Study notes of Operating Systems

Material Type: Notes; Class: Operating Systems; Subject: Computer Science; University: University of San Diego; Term: Fall 2004;

Typology: Study notes

Pre 2010

Uploaded on 08/18/2009

koofers-user-6b5
koofers-user-6b5 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
COMP 310:
Operating Systems
Lecture 6: Semaphores
September 17, 2004
Christine Alvarado
Today’s Topics
OS Support for Synchronization
Semaphores review
Monitors
Last time
Semaphores
Review: Topics so Far
Process 1 Process 2
S1 S2 S3
Thread 1
Thread 2
Thread 3
Thread 1
Thread 2
Readers/Writers
An object is shared across many threads.
Some threads want to read it, others want to write to it
Any number of readers can read at the same time
When a writer writes, there can be no other readers or
writers accessing the object
What do we need to keep track of?
how many are reading
who wants to write
who wants to read
Readers/Writers
// keep track of how many
// reading
int read_count = 0;
// keep track of writing or
// reading and who wants to
// read/write
Semaphore w_or_r = 1;
Semaphore mutex = 1;
writer {
wait(w_or_r);
write
signal(w_or_r);
}
reader {
wait(mutex);
read_count++;
if (read_count == 1) {
wait(r_or_w);
}
signal(mutex);
Read
wait( mutex );
read_count--;
if (read_count == 0 ) {
signal(w_or_r);
}
}
pf2

Partial preview of the text

Download Semaphores - Slides for Operating Systems | COMP 310 and more Study notes Operating Systems in PDF only on Docsity!

COMP 310:

Operating Systems

Lecture 6: Semaphores

September 17, 2004

Christine Alvarado

Today’s Topics

 OS Support for Synchronization

 Semaphores review

 Monitors

Last time

 Semaphores

Review: Topics so Far

Process 1 Process 2 S1 S2^ S Thread 1 Thread 2 Thread 3 Thread 1 Thread 2

Readers/Writers

 An object is shared across many threads.

 Some threads want to read it, others want to write to it

 Any number of readers can read at the same time

 When a writer writes, there can be no other readers or

writers accessing the object

 What do we need to keep track of?

how many are reading who wants to write who wants to read

Readers/Writers

// keep track of how many // reading int read_count = 0; // keep track of writing or // reading and who wants to // read/write Semaphore w_or_r = 1; Semaphore mutex = 1; writer { wait(w_or_r); write signal(w_or_r); } reader { wait(mutex); read_count++; if (read_count == 1) { wait(r_or_w); } signal(mutex); Read wait( mutex ); read_count--; if (read_count == 0 ) { signal(w_or_r); } }

2 Bounded Buffer  There is a set of resource buffers shared by producer and consumer threads  Producer inserts resources into buffer set if not full  Consumer removes resources if buffer set not empty  They execute at different rates

 They execute independently

 Consumer just looking in buffer to consume and producer

just writes to buffer

What semaphores will we need?  What do we need to keep track of? make sure not writing at same time make sure there’s something to read make sure there’s room to write Leads to one mutex and 2 counting Bounded Buffer Implementation  Pair up and work on this problem  Three semaphores:

 mutex

 empty (counting, starts at N)

 full (counting, starts at 0)

 You need two threads:

 Producer and consumer

 Producer adds to buffer if not full, otherwise waits

 Consumer takes from buffer if empty, otherwise waits

 Remember you can’t modify the buffer at the same time!

do { produce an item wait(empty); wait(mutex); add nextp to buffer signal( mutex ); signal( full ); } Semaphore mutex = 1; Semaphore full = 0; Semaphore empty = N; do { wait(full); wait(mutex); remove an item signal(mutex); wait(empty); consume the item } What if there were more than one producer or consumer What if we switch around mutex and full? (Deadlock) Semaphores Summary  Semaphores can be used to solve any of the traditional synchronization problems  They have some drawbacks:

 They are shared global variables

 No connection between semaphore and data

 Used both for CS and coordinating… confusing

 No control! Can we use them wrong? Yes!

 Sometimes hard to use and prone to bugs.

 Programming language support for synchronization