Aim: Write a Program to Explain Mutual Exclusion
MSc IT Parallel Processing Practical No. 4
Index of all Practicals ~ Click Here
Code:
Output:
MSc IT Parallel Processing Practical No. 4
Index of all Practicals ~ Click Here
Code:
#include<stdio.h>
#include"shmlib.h"
int main()
{
int id,id1,id2,id3;
int *inventory;
int *lock1,*lock2,i;
inventory=(int *)shared(sizeof(int),&id1);
lock1=(int *)shared(sizeof(int),&id2);
lock2=(int *)shared(sizeof(int),&id3);
lock_init(lock1);
for(i=0;i<4;i++)
{
lock(lock1);
*inventory=*inventory+2;
printf("The value in the inventory is: after producing is %d\n\n",*inventory);
unlock(lock1);
lock(lock2);
*inventory=*inventory-1;
printf("The value in the inventory after consuming is: %d\n\n",*inventory);
unlock(lock2);
}
return 0;
}
#include"shmlib.h"
int main()
{
int id,id1,id2,id3;
int *inventory;
int *lock1,*lock2,i;
inventory=(int *)shared(sizeof(int),&id1);
lock1=(int *)shared(sizeof(int),&id2);
lock2=(int *)shared(sizeof(int),&id3);
lock_init(lock1);
for(i=0;i<4;i++)
{
lock(lock1);
*inventory=*inventory+2;
printf("The value in the inventory is: after producing is %d\n\n",*inventory);
unlock(lock1);
lock(lock2);
*inventory=*inventory-1;
printf("The value in the inventory after consuming is: %d\n\n",*inventory);
unlock(lock2);
}
return 0;
}
Output:
0 comments:
Confused? Feel free to ask
Post a Comment