Skip to content

Program for Solution to Producer Consumer problem using Mutual Exclusion

Aim: Program for Solution to Producer Consumer problem using Mutual Exclusion

Source Code:

#include<stdio.h>
#include “shmlib.h”
#include <stdlib.h>

int main()
{

printf(“Mutual Exlcusion for producer consumer problem n”);
int i,*lock1, id, sid1, sid2, *prod_quantity;
lock1 = (int*)shared(sizeof(int),&sid1);

lock_init(lock1);
prod_quantity = (int*)shared(sizeof(int),&sid2);
*prod_quantity = 100;

id= create_process(3);
printf(” id %d n”, id);

if(id==2)
{
   
    lock(lock1);
    printf(“Record deleted by consumern”);
    *prod_quantity = *prod_quantity/2;
    printf(“The decrement value: # %dn”, *prod_quantity);
    unlock(lock1);
}
else if(id==1)
{    
    lock(lock1);
    printf(“Record inserted by the Producern”);
    *prod_quantity = *prod_quantity * 2;
    printf(“The increment quantity of prod %dn”, *prod_quantity);
    unlock(lock1);
}
join_process(3,id);
printf(“After join: %dn”, *prod_quantity);
   
    free_shm(sid1);
    free_shm(sid2);
    free_sem(lock1);

return 0;

}

Output:


Comment bellow for your Query and Feedback

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!