3
#include<stdio.h> #include<sys/types.h> #include<sys/ipc.h> #include<sys/shm.h> #include<sys/sem.h> /*Process creating Block */ int create_process(int n) { int j; for (j=1; j<n ; j++) if(fork() == 0) return(j); return(0); } /*Process deletion block*/ void join_process(n,id) { int i; if(id==0) for(i=1;i<n;i++) wait(0); else exit(0); } void error_exit(char *func_name,char *sys_call) { printf("error occured : %s",func_name); perror(sys_call); exit(-1); } /*Initializing the lock*/ void init_lock(lock) int *lock; { union semun{ int val; struct semid_ds *xx; ushort *yy; }control; int j; j = semget(144,1,0666|IPC_ CREAT); if(j<0) error_exit ("lock_init","semget"); *lock =j; control.val = 1; if(0!=semctl(*lock,0,SETVAL,control)) error_exit("lock_init","semctl"); } /*Locking the block*/ void lock(int *lock) { struct sembuf operations; operations.sem_num = 0; operations.sem_op = -1; operations.sem_flg = 0;

shmlib.txt

Embed Size (px)

Citation preview

Page 1: shmlib.txt

7/29/2019 shmlib.txt

http://slidepdf.com/reader/full/shmlibtxt 1/3

#include<stdio.h>#include<sys/types.h>#include<sys/ipc.h>#include<sys/shm.h>#include<sys/sem.h>

/*Process creating Block */int create_process(int n){

int j;for (j=1; j<n ; j++)

if(fork() == 0)return(j);

return(0);}

/*Process deletion block*/void join_process(n,id){

int i;if(id==0)

for(i=1;i<n;i++)wait(0);

else

exit(0);}

void error_exit(char *func_name,char *sys_call){

printf("error occured : %s",func_name);perror(sys_call);

exit(-1);}

/*Initializing the lock*/void init_lock(lock)int *lock;

{ union semun{int val;struct semid_ds *xx;

ushort *yy;}control;

int j;j = semget(144,1,0666|IPC_CREAT);if(j<0)

error_exit ("lock_init","semget");*lock =j;control.val = 1;

if(0!=semctl(*lock,0,SETVAL,control))error_exit("lock_init","semctl");

}/*Locking the block*/void lock(int *lock){

struct sembuf operations;operations.sem_num = 0;operations.sem_op = -1;operations.sem_flg = 0;

Page 2: shmlib.txt

7/29/2019 shmlib.txt

http://slidepdf.com/reader/full/shmlibtxt 2/3

if(0 != semop(*lock ,&operations,1))error_exit("lock","semop");

}/*Releasing the lock*/void unlock(int *lock){

struct sembuf operations;operations.sem_num = 0;operations.sem_op = 1;operations.sem_flg = 0;

if (0 != semop(*lock,&operations,1))error_exit("unlock","semop");

}/*Creating the shared variable*/char *shared(int size, int *aid){

if((*aid = shmget(IPC_PRIVATE,size,0666|IPC_CREAT))<0)error_exit("shared","shmget");

return(shmat(*aid,0,0));}/*Freeing the memory block used by shared variable*/void free_shm(int id)

{ struct shmid_ds *buf;if(shmctl(id,IPC_RMID,buf) != 0)

error_exit("free_shm","shmctl");}/*Freeing the memory block*/void free_memory(int id){

struct shmid_ds *buf;printf("Rename this call free memory\n");

if(shmctl(id,IPC_RMID,buf) != 0)error_exit("cleanup_mamory","shmctl");

}

void free_sem(int *id){if(semctl(*id,1,IPC_RMID,0) != 0)

error_exit("cleanup_sem","semctl");}/*Initializing the barrier*/int *barrier_init(int nproc){

int id,*bar;bar = (int *) shared(sizeof(int) *5, &id);init_lock(&(bar[3]));bar[0]=nproc;bar[1]=0;bar[2]=0;bar[4]=id;return bar;

}

/*Locating the barrier point*/void barrier (int *bar){

int incremented =0;while(1)

{lock(bar+3);if ( (incremented == 0) && (bar[2] >0))

Page 3: shmlib.txt

7/29/2019 shmlib.txt

http://slidepdf.com/reader/full/shmlibtxt 3/3

{unlock(bar+3);continue;

} if (incremented == 0){bar[1]++;

incremented = 1;}if((bar[1] < bar[0])&&(bar[2] == 0 ))

{unlock(bar+3);continue;

}else

{if(bar[2] == 0)

{bar[2] = bar[0] -1 ;bar[1] = 0;

unlock(bar+3);return;

}

else{bar[2]--;unlock(bar+3);return;

}}unlock(bar+3);printf("ERROR - barrier - abort\n\n");exit(-1);

}

}

/*Cleaning the barrier*/void clean_barrier(int *bar){

free_sem(bar+3);free_shm(bar[4]);

}