INTERPROCESS COMMUNICATION USING SEMAPHORES
#include “ sys/sem.h “
#include “ sys/types.h “
#include “ fcntl.h “
int main()
{
int id,a;
struct sembuf s;
id=semget(IPC_PRIVATE,1,10644IPC_CREAT);
a=fork();
if(a==0)
{
sleep(1);
printf("\nChild before semaphore operation\n");
s.sem_num=0;
s.sem_op=0;
semop(id,&s,1);
printf("\nChild ok\n");
}
else
{
printf("\nParent before semctl\n");
semctl(id,0,SETVAL,1);
printf("\nParent sleeping\n");
sleep(3);
printf("\nParent before 2 semctl\n");
semctl(id,0,SETVAL,0);
printf("\nParent ok\n");
exit(0);
}
}
OUTPUT
[user1@localhost user1]$ cc semaphore.c
[user1@localhost user1]$ ./a.out
Parent before semctl
Parent sleeping
Child before semaphore operation
Child ok
Parent before 2 semctl
Parent ok
[user1@localhost user1]$
Thursday, February 4, 2010
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment