Docsity

Semaphores in Operating Systems: Implementation and Use Cases - Prof. Gregory D. Benson, Study notes of Operating Systems

Toggle sidebar
bg1
usf CSCS
UNIVERSITY of SAN FRANCISCO
department of computer science
Semaphores and Locks
CS 326 Operating Systems
Greg Benson
1
High-level Synchronization
!So far we have seen low-level mechanisms for
synchronization (mutual exclusion):
!Disabling interrupts on uniprocessors
!We will now look at higher-level mechanisms:
!Semaphores
!Monitors
2
Semaphores
!Special kind of variable (s)
!A non-negative integer with atomic increment and
decrement
!Two operations
!P(s) or down(s)
!Decrement counter, if counter is 0 then block
!V(s) or up(s)
!Increment counter and wake one blocked thread
3
Semaphores - Formal Def
!Let nP = number of completed P operations on s
!Let nV = number of completed V operations on s
!Let initVal = initial value of s
!Require: nP <= nV + initVal
!If s = (nV + initVal) - nP, then
!Invariant: s >= 0
4
Semaphore Implementation
!First, a busy-waiting implementation:
int sem;
down(int *s) {
int acquired = 0;
while (!acquired) {
disable_preemption();
if (*s > 0) {
*s--; acquired = 1;
}
enabled_preemption();
}
}
up(int *s) {
disable_preemption();
*s++;
enable_preemption();
}
5
Semaphore Implementation 2
!Now, a blocking implementation:
struct sem {int value, list_elem list};
down(struct sem *s) {
disable_preemption();
if (s->value == 0) {
listadd(cur, s->list);
thread_block();
} else {
s->value--;
}
enabled_preemption();
}
up(int *s) {
disable_preemption();
if (!listempty(s->list)
thread_unblock(listremove(s->list));
else
! *s++;
enable_preemption();
}
6
pf3
pf4

Partial preview of the text

Download Semaphores in Operating Systems: Implementation and Use Cases - Prof. Gregory D. Benson and more Study notes Operating Systems in PDF only on Docsity!

usfCSCS

UNIVERSITY department of computer science of SAN FRANCISCO

Semaphores and Locks

CS 326 Operating Systems Greg Benson 1

High-level Synchronization

! So far we have seen low-level mechanisms for

synchronization (mutual exclusion):

! Disabling interrupts on uniprocessors

! We will now look at higher-level mechanisms:

! Semaphores

! Monitors

Semaphores

!Special kind of variable (s)

!A non-negative integer with atomic increment and

decrement

!Two operations

!P(s) or down(s)

!Decrement counter, if counter is 0 then block

!V(s) or up(s)

!Increment counter and wake one blocked thread

Semaphores - Formal Def

! Let nP = number of completed P operations on s

! Let nV = number of completed V operations on s

! Let initVal = initial value of s

! Require: nP <= nV + initVal

! If s = (nV + initVal) - nP, then

! Invariant: s >= 0

Semaphore Implementation

! First, a busy-waiting implementation:

int sem;

down(int *s) {

int acquired = 0;

while (!acquired) {

disable_preemption();

if (*s > 0) {

*s--; acquired = 1;

enabled_preemption();

up(int *s) {

disable_preemption();

*s++;

enable_preemption();

Semaphore Implementation 2

! Now, a blocking implementation:

struct sem {int value, list_elem list}; down(struct disable_preemption(); sem *s) { if (s->value == 0) { listadd(cur, thread_block(); s->list); } else s->value--; { } }^ enabled_preemption(); up(int disable_preemption(); s) { if (!listempty(s->list) thread_unblock(listremove(s->list)); ! elses++; enable_preemption(); 6 }

Semaphore Implementation 3 ! The Pintos blocking implementation: struct sem {int value, list_elem list}; down(struct disable_preemption(); sem s) { while (s->value == 0) { / allow for higher priority threads / listadd(cur, thread_block(); / s->list); disable preemption */ ! }s->value--; enabled_preemption(); } up(struct disable_preemption(); sem s) { if (!listempty(s->list)) ! s->value++;^ thread_unblock(listremove(s->list)); }^ enable_preemption(); 7 Types of Semaphores ! Counting semaphore: ! semaphore with any value >= 0 ! Used for scheduling ! Binary semaphore: semaphore with only two values (0 or 1) ! Used for mutual exclusion and scheduling 8 Semaphores in Practice ! Not too low-level and not too high-level ! Often used in OS kernels to build even high-level synchronization ! E.g., Pintos uses semaphores to implement locks! ! Very flexible 9 Semaphore Examples ! We will look at three example uses of semaphores: ! Mutual exclusion ! Bounded buffer ! Dining philosophers 10 Semaphore Mutual Exclusion ! Use a binary semaphore as a lock variable int counter; struct semaphore c_mutex; sema_init(&c_mutex, 1); thread: ! ! sema_down(&c_mutex); / enter CS / counter++; sema_up(&c_mutex); / leave CS / Semaphore Bounded Buffer ! Also called producer-consumer problem int buf[N], front = 0; rear = 0; struct semaphore mutex, full, empty; sema_init(&mutex, 1); sema_init(&full, 0), sema_init(&empty, N); Producer Thread: / produce item / / wait for empty slot / ! sema_down(&empty); sema_down(&mutex); / put item in buffer / buf[rear] = item; rear = (rear + 1) % N; sema_up(&mutex); / announce item in buf / sema_up(&full); Consumer Thread: / wait for item / ! sema_down(&full); sema_down(&mutex); / put item in buffer / item = buf[front]; front = (front + 1) % N; sema_up(&mutex); / announce free slot / sema_up(&empty); / consume item */

Pintos Scheduling ! Two parts: thread_block() and schedule() schedule (void) { struct thread *cur = running_thread (); struct thread struct thread nextprev == next_thread_to_runNULL; (); ASSERT (intr_get_level ASSERT (cur->status != ()THREAD_RUNNING); == INTR_OFF); ASSERT (is_thread (next)); if (cur prev = switch_threads (cur, != next) next); schedule_tail (prev); } thread_block (void) { ASSERT (!intr_context ()); ASSERT (intr_get_level () == INTR_OFF); thread_current ()->status = THREAD_BLOCKED; }^ schedule (); next_thread_to_run { (void) if (list_empty return idle_thread; (&ready_list)) else return list_entry (list_pop_front (&ready_list), struct thread, elem); } From: sema_down() Priority Scheduling ! Assign priority values to threads ! PRI_MIN(0), PRI_MAX(63), PRI_DEFAULT(31) ! Ensure that newly created higher-priority threads run instead of current lower-priority threads ! Need to look at places where threads are added ! Need to ensure that high priority threads are woken up first on semaphores, locks, and condition queues