UNIX C Program for Semaphore | CS1254-Operating Systems Lab

AIM:
To write a LINUX/UNIX C Program for the Implementation of Semaphore in CS1254 - Operating Systems Laboratory.

SOURCE CODE:
#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
main()
{
int semid,pid;
struct sembuf sop;
semid=semget(0*20,1,IPC_CREAT|0666);
semctl(semid,0,SETVAL,1);
pid=fork();
if(pid!=0)
{
printf("\n child before semaphore\n");
sop.sem_num=0;
sop.sem_op=-1;
sop.sem_flg=0;
semop(semid,&sop,1);
printf("\n child in critical section\n");
sleep(2);
printf("\n child in critical section \n");
sop.sem_num=0;
sop.sem_op=1;
sop.sem_flg=0;
semop(semid,&sop,1);
}
else
{
printf("\n parent before semaphore \n");
sop.sem_num=0;
sop.sem_op=-1;
sop.sem_flg=0;
semop(semid,&sop,1);
printf("\n parent in critical section");
sleep(2);
printf("\n parent coming out of critical section");
sop.sem_num=0;
sop.sem_op=1;
sop.sem_flg=0;
semop(semid,&sop,1);
}
}

OUTPUT:
[examuser35@localhost Jebastin]$ cc semaphore.c
[examuser35@localhost Jebastin]$ ./a.out
parent before semaphore
child before semaphore
parent in critical section
parent coming out of critical section
child in critical section
child in critical section
Previous
Next Post »

If you still didn't find what you're looking for, you can search this website below: