Thursday, 4 December 2014

program to view semaphore operation in Unix system


#include"myHeader.h"
main(int argc,char **argv)
{
int id;
struct sembuf v;
id=semget(1,3,IPC_CREAT|0600);
printf("id=%d in process %d\n",id,getpid());
printf("about to pend on semaphore\n");
v.sem_num=0;
v.sem_op=0;
v.sem_flg=0;
semop(id,&v,1);
printf("after semop on critical region\n");
printf("process exiting...\n");
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex36.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
id=0 in process 5741
about to pend on semaphore
after semop on critical region
process exiting...