


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



CS 326 Operating Systems Greg Benson 1
synchronization (mutual exclusion):
decrement
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