Concept of semaphore was devised by Dijkastra in 1965. Semaphore is an integer variable that is used to record number of weakups and had been saved. If it is equal to zero it indicates that no wakeup's are saved. A positive value shows that one or more wakeup's are pending. The DOWN operation (sleep) checks the semaphore to see if it is greater than zero. If it is, it decrements the value (UP) and continues. If the semaphore is zero the process sleeps. The UP operation (weakup) increments the value of the semaphore. If one or more proces were sleeping on that semaphore then one of the process is chosen and allowed to compute its DOWN. Checking and updating the semaphore must be done as an atomic action to avoid race conditions. Product Customer problem can be solved using semaphore as below.
#define N 100 /* number of slots in the buffer */ typedef int semaphore; /* semaphores are a special kind of int */ semaphore mutex = 1; /* controls access to critical region */ semaphore empty = N; /* counts empty buffer slots */ semaphore null = 0; /* counts full buffer slots */ void producer(void){ int item; message m; // message buffer. while(TRUE){ item = produce_item(); // generate something to put in buffer receive(consumer, &m); // Receive an empty message, if any build_message(&m, item); // construct a message to send send(consumer, &m); // send item to consumer } } void consumer(void){ int item, i; message m; for(i=0; i<N; i++) send(producer, &m); // send N empty messages while(TRUE){ receive(produce, &m); // get message containing item item = extract_item(&m); // extract item from message send(producer, &m); //send back empty replay consumer_item(term); //do something with the item } }
Post a Comment
Casino review by real players - DrmCD
Casino games. · Casino games. 안동 출장마사지 · Casino games. · Casino players · Casino games. 김해 출장마사지 · 성남 출장마사지 Casino games. 군포 출장안마 · Casino games. · Casino games. · Casino 서귀포 출장안마 games. · Casino games. · Casino games.