Lab
University
California State University, Los AngelesCourse
CS4961 | Software Design LaboratoryPages
4
Academic year
2023
Julie Elizabeth John
Views
0
EXPERIMENT No.1 Producer consumer Problem using Samaphores Aim write a C-program - to Simulate Producer consumer problem Using Semaphores. Algorithm 1. Start 2. Define the maximum buffer Size 3. Enter the no. of Producers and consumers 4. The producer produces the job and Put it in the buffer. 5. The consumer takes the job from the buffer. 6. if the buffer is full the producer goes to sleep. 7. if the buffer is empty the consumer goes to sleep. 8. stop Program Code mi # include h stdio.hy # include < 8tdlib.h7 int mutex = ', full=o, empty =3, x 0; int main () E L int n; void producer (); void consumer c) int wait (int) ; int Signal (int) ; printf ("In 1. producer In 2. Consumer In 3. Exit"); while (1) { Printf ( "In Enter the choice"); Scanf ("%d, 4n);
Switch (n) { case 1: if ((mutex == 1) & & (empty ! = o)) produces(); else Printf ( "Buffer is full !!");; break ; case 2 : if (mutex = = 1) & & (full! = 0)) consumer (); else Printl (" Buffer is empty ! 1 "); break; Case 3: exit (0); break; } } return O; } 11 int wait (int s) { return(- - 5); & int signal (int s) { return (+ +s) ; } void Producer () { matex = wait (mutex) ; full = Signal (full); empty = wait (empty); xtt; printf (" In producer produces the item %
muter mutex = signal (mutex); void Consumer () & mutex = wait (mutex) ; full = wait (full) empty = signal (empty); printf("In Consumer consumes item %d 11 a); mutex = signal (mutex); } Result The program is executed and the output is obtained. 1_ SHIAMIHT H
Output 1. Producer 2. consumer 3. Exit producer produces the item enter the choice 1 enter the choice 2 consumer consumes the item enter the choice 2 buffer is empty enter the choice 1 producer produces the item I enter the choice / producer produces the item 2 enter the choice 1 producer produces the item 3 enter the choice I Buffer is full
Producer Consumer Problem Using Semaphores
Please or to post comments