operating systems
DESCRIPTION
Operating Systems, first presentationTRANSCRIPT
Operating Systems
Team:Jonathan Arturo Alvarado Mata 1441616Obed David Guevara Ibarra 1447478
Carlos Eduardo Triana Sarmiento 1412573
Blog:http://os-ocj.blogspot.com/
First Point
Explain how to solve unbounded-buffer producer-consumer with locks. Discuss the quality of the proposed solution.
what we did?...
We use an array as global variable, this array contain the number of items. We use this array to control the producer and the consumer, because if the array is full the producer stops producing and if the array is empty the consumer stops consuming
Also we use two threads, one for the consumer and the other for the producer.
what we did?...
The assignment consists in implementing locks to the producer consumer problem. So we have to declare a lock as a global variable, which controls the threads activity.This is important to remember.... Only the thread that acquired the lock can release it
CODELOCK *L1;CONDITION *C
PRODUCER(){ WHILE(1){// try to aquire the lockIF(L1->TRYAQUIRE()){ // if the array is full, lets wait // if it isn’t, continue... WHILE(ARRAY == FULL){ C->WAIT(L1) } // increase the number of items in the array C-> SIGNAL(); // Enables the waiting threads L1->RELEASE(); } // end of if } // end of while } // end of the function producer
CONSUMER(){while(1){// try to aquire the lockif(l1->TRYAQUIRE()){// if the array is empty, wait // if it isn’t, continue... WHILE(ARRAY == EMPTY){ C->WAIT(L1); } // decrease the number of items in the array CONSUME(); C-> SIGNALl(); // Enables the waiting thread L1->RELEASE(); } // end of if } // end of while} //end of function consumer
void main(){L1 = new lock(“L1”);C = new condition(“C”);Thread *t = new Thread(“producer”); t->Fork(producer, 1);Thread * t= new Thread(“consumer”);t->Fork(consumer, 1);}end main
Second Point
Explain how to detect and eliminate deadlock in the dinning philosophers problem.
The solution...
* This is the solution
When a philosopher has a fork waits a random time to get the second fork. If in that time is not free the second fork, release the one he has and go back to the queue
If a philosopher "A" release a fork (because he has eaten or because he waited too long with a fork in hand) but still want to eat, getting back on line to the fork. Basically we follow this concept and that's how we make the code
CODEPhilosophers[N]; //Array to store the philosophersLock *L1 // Only two philosophers can try to get 2 forksLock *L2
void philosopher (i){ WHILE(true){ think(); //All the philosophers start waiting take_forks(i); } //End of while} //End of function
void take_forks(int i){ Look_for_Locks(); Get_Forks(); Eat(); Drop_Forks(); L1->release() || L2 ->release(); //release the lock that the philosophers has philosopher return to the queue } //End of function
void Look_for_Locks(){ IF(L1->tryaquire() || L2 -> tryaquire()){ if one is available return to take_forks }ELSE{ // if not wait some time look if one lock is available and try to get it if no lock is available L1-> release() || L2 -> release() philosopher return to the queue }//end else}
void Get_Forks(){ Look for forks; if(fork == 1 || fork == 0) // if no Lock is available or if only got one wait some time look if forks are available and try to get one or two if (fork == 2){ return to take_forks }else{ // if doesn’t get the fork it need drop fork L1-> release() || L2 -> release() // release the lock that the philosophers has philosopher return to the queue }else{return to take_forks}}
Bibliography
*Operating Systems-Design and Implementation, Second Edition Author: Andrew S. Tanenbaum and Albert S. Woodhull * http://www.isi.edu/~faber/cs402/notes/lecture7.html *http://www.4coders.com/index.php?modulo=contenido&op=detalle&tipo=c&id=83http://www.4coders.com/index.php?modulo=contenido&op=detalle&tipo=c&id=83 *http://es.wikipedia.org/wiki/Problema_de_la_cena_de_los_fil%C3%B3sofos