Solution to producer consumer problem using Mutual exclusion in unix

#include<stdio.h>
#include "shmlib.h"

main(){
int *lock1,id,sid1,sid2,*prod_quantity;
lock1 = (int *)shared(sizeof(int),&sid1);
init_lock(lock1);
prod_quantity = (int *)shared(sizeof(int),&sid2);
*prod_quantity = 110;
id = create_process(1);
printf("id %d \n",id);
if(id == 0){
lock(lock1);
printf("record deleted by the consumer\n");
*prod_quantity = *prod_qauntity/2;
printf("the decremented values : # %d\n", *prod_quantity);
unlock(lock1);
}
else{
lock(lock1);
printf("record inserted by the producer\n");
*prod_quantity = *prod_quantity*2;
printf("the incremented value : # %d\n",*prod_quantity);
unlock(lock1);
}
join_process(2,id);
printf("After join : the quantity of product after consumer access : %d\n",*prod_quantity);
free_shm(sid1);
free_shm(sid2);
}

0 comments: