Upcoming SlideShare
×

# Assigntment 1 Theoretical

2,508 views

Published on

Operative Systems

Published in: Technology, Self Improvement
1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
2,508
On SlideShare
0
From Embeds
0
Number of Embeds
397
Actions
Shares
0
17
0
Likes
1
Embeds 0
No embeds

No notes for slide
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• ### Assigntment 1 Theoretical

1. 1. Theoretical A s s i g n m e n t 1
2. 2. Unbounded-buffer producer-consumerProblem: #deﬁne N 100 /*number of slots in the buffer*/ int count = 0;/*number of items in the buffer*/ void producer(void) The problem we have here is a race condition. { int item; while(TRUE){ /*repeated indeﬁnitely*/ 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*/ } }
3. 3. Unbounded-buffer producer-consumerSolution: #deﬁne 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*/ Solving this problem by semaphores. 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); } }
4. 4. Unbounded-buffer producer-consumer withlocks Lock *l; Condition *c; int avail = 0; void consumer(int dummy) { while (1) { This one solution from some ways to l->Acquire(); if (avail == 0) { solve consumer-producer problem. 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); }
5. 5. Eliminate deadlock void do() { l2->Acquire(); if (l1->TryAcquire()) l1->Acquire(); { l2->Acquire(); do something... l3->Acquire(); } else { l4->Acquire(); l2->Release(); l5->Acquire(); l1->Acquire(); l2->Acquire(); do something... l1->Release(); } l2->Release(); }
6. 6. How to detect and eliminate deadlock in the dining philosophers problem. Problem:Five philosophers are seated around a circular table, eachphilosopher has a plate of spaghetti. The spaghetti is soslippery that a philosopher needs twoforks to eat. Betweeneach pair of dishes are a fork, when a philosopher is hungry, tryto buy their left and right forks, one at a time, in any order. Ifsuccessful in acquiring two forks, eating for a moment, then letthe forks andcontinue to think.
7. 7. How to detect and eliminate deadlock in the dining philosophers problem. Solution: void TAKE_FORK(int i) /*i number of philosopher from 0 to N-1*/#deﬁne N 5 /*number of philosophers*/ {#deﬁne LEFT (i+N-1)%N /*number of left neighbor*/ down(&mutex);/*enters the critical region*/#deﬁne RIGHT (i+1)%N /*number of right neighbor*/ state[i] = HUNGRY;/*records the philosopher who is eating*/#deﬁne THINKING 0 /*Philosopher is thinking*/ TRY(i);/*attempts to acquire 2 forks*/#deﬁne HUNGRY 1 /* Philosopher is hungry*/ up(&mutex)/*leaves the critical region*/;#deﬁne EATING 2 /*Philosopher is eating*/ down(&s[i]);/*crashes if the holders were not acquired*/typedef int semaphore; /*semaphores are a kind of int*/ }int state[N]; /*array that keeps track of the status of all*/ void PUT_FORK(int i) /*i number of philosopher from 0 to N-1*/semaphore mutex = 1;/*mutual exclusion for critical regions following*/ {semaphore s[N];/*a semaphore by philosopher*/ down(&mutex);/*enters the critical region*/ state[i] = THINKING;/*records the philsopher who isvoid philosopher(int i)/*i number of philosopher from 0 to N-1*/ eating*/{ TRY(LEFT);/*attempts to acquire the left fork*/ while(TRUE){/*repeated indeﬁnitely*/ TRY(RIGHT);/*attempts to acquire the right fork*/ THINKING();/*the philosophers is thinking*/ up(&mutex);leaves the critical region*/ TAKE_FORK(i);/*the philosopher takes the fork*/ } EAT();/*the philosopher is eating*/ void TRY(i) /*i number of philosopher from 0 to N-1*/ PUT_FORK(i);/*philosopher put the fork*/ { } if(state[i] == HUNGRY && state[LEFT] != EATING && } state[RIGHT] != EATING){ state[i] = EATING; up(&s[i]); } }
8. 8. Suspend and Resume algorithmThe worst case is when the lock is The SR algorithm costs twice theunlocked just after the thread starts the suspend and resume time. It ﬁrst spinssuspend. for the suspend and resume time, then suspends, then gets the lock, then resume. The optimal algorithm just spins untilthe lock is unlocked, taking the suspendand resume time to acquire the lock.
9. 9. Thanks! Our blog:http://operatingsystems-ﬁme.blogspot.com/