Demonstrate the concept of Mutual Exclusion in Parallel Processing
For this program you must have some knowledge of mutual exclusion and pointers
#include<stdio.h>
#include "shmlib.h"
main(){
int *lock1,id, sid1, sid2, *i, j;
//this allocates memory of int size and this is a shared memory which is accessed from sd1
lock1 = (int*)shared(sizeof(int),&sid1);
//initializing lock
init_lock(lock1);
//this allocates memory of int size and this is a shared memory which is accessed from sd2
i=(int*)shared(sizeof(int),&sid2);
*i=110;
j=110;
printf("before fork : %d %d\n",*i,j);
id=create_process(2);
lock(lock1);
*i=id;
j=id*2;
printf("After Fork : %d %d\n", i*,j);
//lock released
unlock(lock1);
join_process(3,id);
printf("After Join : %d %d\n",*i,j);
//memory released
free_shm(sid1);
free_shm(sid2);
}
#include<stdio.h>
#include "shmlib.h"
main(){
int *lock1,id, sid1, sid2, *i, j;
//this allocates memory of int size and this is a shared memory which is accessed from sd1
lock1 = (int*)shared(sizeof(int),&sid1);
//initializing lock
init_lock(lock1);
//this allocates memory of int size and this is a shared memory which is accessed from sd2
i=(int*)shared(sizeof(int),&sid2);
*i=110;
j=110;
printf("before fork : %d %d\n",*i,j);
id=create_process(2);
lock(lock1);
*i=id;
j=id*2;
printf("After Fork : %d %d\n", i*,j);
//lock released
unlock(lock1);
join_process(3,id);
printf("After Join : %d %d\n",*i,j);
//memory released
free_shm(sid1);
free_shm(sid2);
}
0 comments: