Child Semaphore Parent Semaphore CS1254 Operating Systems lap Linux based

Semaphore is used to protect variable. It was invented by Edsger Dijkstra. It does not prevent resource deadlock. Semaphore is used to perform synchronization. When multiple process occur it share the common resource in the same way it does no disrupt with each other.
Source code C Programming Algorithm Linux based
#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
main()
{
int semid,pid;
struct sembuf sop;

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);
}
}

OUPUT CS1254 Operating Systems lap Linux based
[redhat35@localhost Rhel5]$ cc semaphore.c
[redhat35@localhost Rhel5]$ ./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

Post a Comment

0 Comments