Wednesday 18 September 2013


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 consumer\n");
    *prod_quantity = *prod_quantity/2;
    printf("The decrement value: # %d\n", *prod_quantity);
    unlock(lock1);
}
else if(id==1)
{    
    lock(lock1);
    printf("Record inserted by the Producer\n");
    *prod_quantity = *prod_quantity * 2;
    printf("The increment quantity of prod %d\n", *prod_quantity);
    unlock(lock1);
}
join_process(3,id);
printf("After join: %d\n", *prod_quantity);
   
    free_shm(sid1);
    free_shm(sid2);
    free_sem(lock1);

return 0;

}

Output:

Comment bellow for your Query and Feedback

Related Posts :



0 comments:

Confused? Feel free to ask

Post a Comment