Presentation is loading. Please wait.

Presentation is loading. Please wait.

FIRST SOLUTION Producer Process Consumer Process

Similar presentations


Presentation on theme: "FIRST SOLUTION Producer Process Consumer Process"— Presentation transcript:

1 FIRST SOLUTION Producer Process Consumer Process
#define N // Queue Size #define NUM_REPEAT 1,000, // Number of items to be generated semaphore MUTEX // binary semaphore SM: a pointer to the queue in shared memory SMnum_element = 0; // # of the random # produced to the queue Producer Process Consumer Process int i; // loop counter int new_item; // the new random # int tail = 0; // the position for the new # for (i=0; i < NUM_REPEAT; i++) { new_item = rand( ); if(SMnum_element < N) wait(MUTEX); SM[tail] = new_item; SMnum_element ++; signal(MUTEX); tail ++; if (tail  N) tail = 0; } else { i--; } int top = 0; // the position for the new # if(SMnum_element > 0) new_item = SM[top]; SMnum_element --; top ++; if (top  N) top = 0; FIRST SOLUTION

2 SECOND SOLUTION Producer Process Consumer Process
#define N // Queue Size #define NUM_REPEAT 1,000, // Number of items to be generated semaphore MUTEX // binary semaphore semaphore EMPTY N // counting semaphore semaphore FULL // counting semaphore SM: a pointer to the queue in shared memory Producer Process Consumer Process int i; // loop counter int new_item; // the new random # int tail = 0; // the position for the new # for (i=0; i < NUM_REPEAT; i++) { wait(EMPTY); new_item = rand( ); wait(MUTEX); SM[tail] = new_item; signal(MUTEX); tail ++; if (tail  N) tail = 0; signal(FULL); } int top = 0; // the position for the new # wait(FULL); new_item = SM[top]; top ++; if (top  N) top = 0; signal(EMPTY);


Download ppt "FIRST SOLUTION Producer Process Consumer Process"

Similar presentations


Ads by Google