spinlock.h

 /*	
Name    : Divyen K Patel Roll No:24 MCA-4
Program : 1
Header files for locking the critical region
*/

void spin_lock_init(int *lok)
{
int init_sem_value=1;
*lok=semget(IPC_PRIVATE,1,(0600 | IPC_CREAT));

if(*lok==-1)
{
perror("Error in Semget ...");
exit(1);
}

if(semctl(*lok,0,SETVAL,init_sem_value) < 0)
{
perror("Error in Semctl ...");
exit(1);
}

}

void spin_lock(int *lok)
{
struct sembuf sembuffer,*sops;
sops= &sembuffer;
sops->sem_num=0;
sops->sem_op=-1;
sops->sem_flg=0;

if(semop(*lok,sops,1) < 0)
{
perror("semop lock Error ...");
exit(1);
}

}
void spin_unlock(int *lok)
{
struct sembuf sembuffer,*sops;
sops=&sembuffer;
sops->sem_num=0;
sops->sem_op=1;
sops->sem_flg=0;

if(semop(*lok,sops,1) < 0)
{
perror("semop Unlock Error ...");
exit(1);
}
}

void cleanup_semaphore(int *lok)
{
if(semctl(*lok,1,IPC_RMID,0)  < 0)
{
perror("cleaning");
}
}

Project Homepage: