



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!




2
3
4
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(); }
struct sem {int value, list_elem list}; down(struct sem *s) { disable_preemption(); if (s->value == 0) { listadd(cur, s->list); } else {^ thread_block(); }^ s->value--; enabled_preemption(); } up(int disable_preemption(); s) { if (!listempty(s->list) else^ thread_unblock(listremove(s->list)); ! (^) enable_preemption();s++; }
Semaphore Implementation 3
Monitor Implementation
Monitor Dining Philosophers 25 monitor! int forks[N] = dp { {2, 2, ..., 2}; ! condition both_free[N]; ! /* right[i] is i’s right neighbor / ! int right[N] = {1, ..., N-1, 0}; ! / left[i] is i’s left neighbor / ! int left[N] = {N-1, 0, 1, ..., N-2}; ! void getforks(int i) { !! !! if (forks[i]forks[right[i]]--; < 2) wait(both_free); !! forks[left[i]]--; ! } ! void relforks(int i) { !! forks[right[i]]++; !! forks[left[i]]++; !! if (forks[right[i]] == 2) !!! signal(both_free[right[i]]); !! !! if (forks[left[i]]! signal(both_free[left[i]]); == 2) ! } } Philosopher! Thread i: ! while(1) { !! / Think / !! ... !! dp.getforks(i); !! / Eat */ !! dp.relforks(i); ! } Readers-Writers Problem