9
Theoretical Assignment 1

Assigntment 1 Theoretical

Embed Size (px)

Citation preview

Page 1: Assigntment 1 Theoretical

TheoreticalA s s i g n m e n t 1

Page 2: Assigntment 1 Theoretical

Unbounded-buffer producer-consumerProblem:

#define N 100 /*number of slots in the buffer*/int count = 0;/*number of items in the buffer*/

void producer(void){ int item; while(TRUE){ /*repeated indefinitely*/ item = produce_item(); /*generates the next item*/ if(count == N)sleep(); /*If the buffer is full, becomes inactive*/ insert_item(item);/*put the item in the buffer*/ count = count + 1; /*increases the count of items in the buffer*/ if(count == 1) wakeup(consumer);/*checks whether the buffer was empty*/ }}

void consumer(void){ int item;

while(TRUE) { /*checks whether the buffer was empty*/ if(count == 0)sleep(); /*if buffer is empty, it goes to inactive state*/ item = remove_item();/*removes the element from the buffer*/ count = count - 1; /*Decreases the count of items in the buffer*/ if(count == N-1)wakeup(producer); /*checks whether the buffer was empty*/ consumer_item(item); /*print item*/ } }

The problem we have here is a race condition.

Page 3: Assigntment 1 Theoretical

Solution:

Unbounded-buffer producer-consumer

#define N 100 /*number of slots in the buffer*/typedef int semaphore; /*semaphores are a kind of int*/semaforo mutual = 1;/*controls access to the critical region*/semaforo empty = N;/*count the empty slots*/semaforo full = 0;/*count the full slots*/

void producer(void){ int item; while(TRUE){ /*TRUE is constant 1*/ item = produce_item(); /* generates something to put in the buffer*/ down(&empty); down(&mutual); insert_element(element); up(&mutual); up(&full); }}

void consumer(void){ int item;

while(TRUE) { down(&full); down(&mutual); item = remove_consumer(); up(&mutual); up(&full); consumer_item(item); } }

Solving this problem by semaphores.

Page 4: Assigntment 1 Theoretical

Unbounded-buffer producer-consumer with locks

Lock *l;Condition *c;int avail = 0;void consumer(int dummy) { while (1) { l->Acquire(); if (avail == 0) { c->Wait(l); } consume the next unit of data avail--; l->Release(); } }void producer(int dummy) { while (1) { l->Acquire(); produce the next unit of data avail++; c->Signal(l); l->Release(); }}void main() { l = new Lock("l"); c = new Condition("c"); Thread *t = new Thread("consumer"); t->Fork(consumer, 1); Thread *t = new Thread("consumer"); t->Fork(consumer, 2); t = new Thread("producer"); t->Fork(producer, 1);}

This one solution from some ways to solve consumer-producer problem.

Page 5: Assigntment 1 Theoretical

Eliminate deadlock

l1->Acquire();l2->Acquire();l3->Acquire();l4->Acquire();l5->Acquire();

void do() { l2->Acquire(); if (l1->TryAcquire()) { do something... } else { l2->Release();l1->Acquire(); l2->Acquire(); do something... l1->Release(); } l2->Release(); }

Page 6: Assigntment 1 Theoretical

How to detect and eliminate deadlock in the dining philosophers problem.

Problem:

Five philosophers are seated around a circular table, each philosopher has a plate of spaghetti. The spaghetti is so slippery that a philosopher needs twoforks to eat. Between each pair of dishes are a fork, when a philosopher is hungry, try to buy their left and right forks, one at a time, in any order. If successful in acquiring two forks, eating for a moment, then let the forks andcontinue to think.

Page 7: Assigntment 1 Theoretical

Solution:

How to detect and eliminate deadlock in the dining philosophers problem.

#define N 5 /*number of philosophers*/#define LEFT (i+N-1)%N /*number of left neighbor*/#define RIGHT (i+1)%N /*number of right neighbor*/#define THINKING 0 /*Philosopher is thinking*/#define HUNGRY 1 /* Philosopher is hungry*/#define EATING 2 /*Philosopher is eating*/typedef int semaphore; /*semaphores are a kind of int*/int state[N]; /*array that keeps track of the status of all*/semaphore mutex = 1;/*mutual exclusion for critical regions following*/semaphore s[N];/*a semaphore by philosopher*/

void philosopher(int i)/*i number of philosopher from 0 to N-1*/{ while(TRUE){/*repeated indefinitely*/ THINKING();/*the philosophers is thinking*/ TAKE_FORK(i);/*the philosopher takes the fork*/ EAT();/*the philosopher is eating*/ PUT_FORK(i);/*philosopher put the fork*/ } }

void TAKE_FORK(int i) /*i number of philosopher from 0 to N-1*/{ down(&mutex);/*enters the critical region*/ state[i] = HUNGRY;/*records the philosopher who is eating*/ TRY(i);/*attempts to acquire 2 forks*/ up(&mutex)/*leaves the critical region*/; down(&s[i]);/*crashes if the holders were not acquired*/}void PUT_FORK(int i) /*i number of philosopher from 0 to N-1*/{ down(&mutex);/*enters the critical region*/ state[i] = THINKING;/*records the philsopher who is eating*/ TRY(LEFT);/*attempts to acquire the left fork*/ TRY(RIGHT);/*attempts to acquire the right fork*/ up(&mutex);leaves the critical region*/}void TRY(i) /*i number of philosopher from 0 to N-1*/{if(state[i] == HUNGRY && state[LEFT] != EATING && state[RIGHT] != EATING){state[i] = EATING;up(&s[i]); }}

Page 8: Assigntment 1 Theoretical

Suspend and Resume algorithm

The worst case is when the lock is unlocked just after the thread starts the suspend.

The optimal algorithm just spins until the lock is unlocked, taking the suspend and resume time to acquire the lock.

The SR algorithm costs twice the suspend and resume time. It first spins for the suspend and resume time, then suspends, then gets the lock, then resume.

Page 9: Assigntment 1 Theoretical

Thanks!

Our blog:http://operatingsystems-fime.blogspot.com/