C Programming Tutorial

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

void lock_init(int *lock1)
{
    union semun
    {
            int val;
            struct semid_ds *xx;
            ushort **yy;
    }arg;
    int j;
        j=semget(144,1,0666|IPC_CREAT);
        *lock1=j;
        arg.val=1;
        semctl(*lock1,0,SETVAL,arg);
}

void locksem(int *lock1)
{
      struct sembuf oprations;
          oprations.sem_num=0;
          oprations.sem_op=-1;
          oprations.sem_flg=0;
          semop(*lock1,&oprations,1);

}

void unlock(int *lock1)
{
        struct sembuf oprations;
        oprations.sem_num=0;
        oprations.sem_op=1;
        oprations.sem_flg=0;
        semop(*lock1,&oprations,1);
}

/*void free_sem(int id)
{
    if(semctl(*id,1,IPC_RMID)!=0)
        error_exit(\"cleanup_sem\",\"semctl\");
}
*/

 
 
Didn't find what you were looking for? Find more on PROGRAM OF CREATE HEADER FILE FOR SEMAPHORE