Experiment # 9: Familiarize Semaphore Operation
AIM: To write a program in C to illustrate basic Semaphore operation.
THEORY:
A semaphore is a protected variable or abstract data type that constitutes a
classic method of controlling access by multiple processes to a common resource
in a parallel programming environment. A semaphore generally takes one of two
forms: binary and counting. A binary semaphore (flag) is a simple "true/false"
(locked/unlocked) variable that controls access to a single resource. A counting
semaphore is a counter for a set of resources. Either type may be employed to
prevent a race condition. On the other hand, a semaphore is of no value in
preventing resource deadlock, such as illustrated by the dining philosophers
problem.
A semaphore is an integer variable with two atomic operations wait and signal.
Other names for wait are down, P and lock. Other names for signal are up, V,
unlock and post.Main use of semaphores is to synchronize the access to shared
memory segments
int semget(key_t, int nsems, int semflag);
A semaphore is created or an existing semaphore is accessed with semget
system call.The value returned by semget is the semaphore identifier, semid or
1 if an error occurred.nsems argument specifies the number of semaphores in
the set.If we are not creating
a new semaphore set, but only accessing an
existing set then we can specify semflag argument as zero instead of ipc_creat.
int semop(int semid,struct sembuf **opsptr, unsigned int nops);
once the semaphore set is opened with semget, operations are performed on
one or more of the semaphore values in the set by using semop system call
To lock the semaphore we call semop to do two operations atomically
1)wait for semaphore value to become zero
2)increment the value to one
static struct sembuf op-lock[2]=
{
0, 0, 0, /*wait for sem#0 to become 0*/
0, 1, 0 /*then increment sem#0 by 1*/
};
To unlock the resource , we call semop to decrement the semaphore value
Static struct sembuf op-unlock[1]=
{
0, -1, IPC-NOWAIT /*decrement sem#0 by 1
(sets it to 0)*/
};
Fig: Basic Semaphore Operations
REQUIREMENTS:
HARDWARE
SOFTWARE
: PIII Processor , 128 MB RAM, 10GB
: OS: LINUX
ALGORITHM:
STEP 1: Create sembuf structures for locking and unlocking
STEP 2: Create a semaphore using semget() system call
STEP 3: Now first perform the lock operation and then unlock it
Familiarize Semaphore Operation
// File 1: smop.h
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#define SEMKEY 1234L
#define PERMS 0666
static struct sembuf lock0[2]=
{
0,0,0,
0,1,0
};
static struct sembuf unlock0[1]=
{
0,-1,IPC_NOWAIT
};
static struct sembuf lock1[2]=
{
1,0,0,
1,1,0
};
static struct sembuf unlock1[2]=
{
1,-1,IPC_NOWAIT
};
static struct sembuf lock2[2]=
{
2,0,0,
2,1,0
};
static struct sembuf unlock2[1]=
{
2,-1,IPC_NOWAIT
};
int semlock0(int semid)
{
semop(semid,&lock0[0],2);
}
int semunlock0(int semid)
{
semop(semid,&unlock0[0],1);
}
int semlock1(int semid)
{
semop(semid,&lock1[0],2);
}
int semunlock1(int semid)
{
semop(semid,&unlock1[0],1);
}
int semlock2(int semid)
{
semop(semid,&lock2[0],2);
}
int semunlock2(int semid)
{
semop(semid,&unlock2[0],1);
}Familiarize
Semaphore Operation
PROGRAM:
#include "smop.h"
#include<stdio.h>
main()
{
int semid;
char res;
semid=semget(SEMKEY,1,IPC_CREAT | PERMS);
semunlock0(semid);
printf("\nlocked");
printf("\ndo u want to unlock?(Y/N)");
res=getchar();
semunlock0(semid);
printf("\nunlocked\n");
}
RESULT: A Program using C to illustrate semaphore operations is executed
successfully.
Experiment #9(a): IMPLEMENTATION OF DINING
PHILOSOPHER PROBLEM USING SEMAPHORES.
AIM:
To write a c program to Implement Dining Philosopher Problem Using
Semaphores
THEORY:
The dining-philosophers problem is considered a classic synchronization
problem, because it is an example for a large class of concurrency-control
problems. It is a simple representation of the need to allocate several
resources among several processes in a deadlock and starvation free manner.
One simple solution is to represent each chopstick by a semaphore. A
philosopher
Tries to grab the chopstick by executing a wait operation on that semaphore;
she releases her chopstick by executing the signal operation on the
appropriate semaphores. Thus the shared data are
var chopstick: array[0..4] of semaphore
where all the elements of the chopstick are initialized to 1.The structure of
philosopher i is shown below.
repeat
Wait(chopstick[i]);
Wait(chopstick[i+1 mod 5]);
eat
signal(chopstick[i]);
signal(chopstick[i+1 mod 5]]);
think
until false;
figure illustrating dining philosophers problem
REQUIREMENTS:
HARDWARE
: PIII Processor , 128 MB RAM, 10GB
SOFTWARE
: OS: LINUX
PROGRAM:
#include <stdio.h>
#include "smop.h"
main()
{
int pno, semid,tmp;
semid=semget(SEMKEY,3,PERMS|IPC_CREAT);
printf("\n\t\tEnter Philosopher Number(0-2): ");
scanf("%d",&pno);
switch(pno)
{
case 0: printf("\n\n\tPhilosopher %d thinking...",pno);
printf("\n\tEnter 0 to start eating ...");
scanf("%d",&tmp);
semlock0(semid);
semlock1(semid);
printf("\n\n\tPhilosopher %d eating...",pno);
printf("\n\tPress 1 to stop eating ...");
scanf("%d",&tmp);
semunlock0(semid);
semunlock1(semid);
printf("\n\n\tPhilosopher %d thinking...",pno);
break;
case 1: printf("\n\n\tPhilosopher %d thinking...",pno);
printf("\n\tEnter 0 to start eating ...");
scanf("%d",&tmp);
semlock1(semid);
semlock2(semid);
printf("\n\n\tPhilosopher %d eating...",pno);
printf("\n\tPress 1 to stop eating ...");
scanf("%d",&tmp);
semunlock1(semid);
semunlock2(semid);
printf("\n\n\tPhilosopher %d thinking...",pno);
break;
case 2: printf("\n\n\tPhilosopher %d thinking...",pno);
printf("\n\tEnter 0 to start eating ...");
scanf("%d",&tmp);
semlock2(semid);
semlock0(semid);
printf("\n\n\tPhilosopher %d eating...",pno);
printf("\n\tPress 1 to stop eating ...");
scanf("%d",&tmp);
semunlock2(semid);
semunlock0(semid);
printf("\n\n\tPhilosopher %d thinking...",pno);
break;
}
}
RESULT: A program to Implement Dining Philosopher Problem Using Semaphores
in C is executed successfully.
Experiment #9(b): IMPLEMENTATION OF READER WRITER PROBLEM USING SEMAPHORES.
AIM: To write a c program To Implement Reader-Writer Problem Using Semaphores.
THEORY:
A data object such as file or a record is to be shared among several
concurrent processes.Some of the processes may want only to read the
content of the shared object, and are called as readers whereas others may
want to update that is, to read and write and are called as writers
The first readers-writers problem, requires that no reader will be kept
waiting unless a writer has already obtained permission to use the shared
object.The second readers-writers problem requires that, once a writer is
ready that writer performs its write as soon as possible
Figure illustrating readers writers problem
The structure of a writer process
wait(wrt);
writing is performed
signal(wrt);
The structure of a reader process
wait(mutex);
readcount=readcount+1;
if readcount=1 then wait(wrt);
signal(mutex);
reading is performed
wait(mutex);
readcount:=readcount-1;
if(readcount=0 then signal(wrt);
signal(mutex);
REQUIREMENTS:
HARDWARE
SOFTWARE
: PIII Processor , 128 MB RAM, 10GB
: OS: LINUX
PROGRAM:
// File 1: smop.h
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#define SEMKEY 223010L
#define PERMS 0666
static struct sembuf lock0[2]=
{
0,0,0,
0,1,0
};
static struct sembuf unlock0[1]=
{
0,-1,IPC_NOWAIT
};
static struct sembuf lock1[2]=
{
1,0,0,
1,1,0
};
static struct sembuf unlock1[1]=
{
1,-1,IPC_NOWAIT
};
static struct sembuf lock2[2]=
{
2,0,0,
2,1,0
};
static struct sembuf unlock2[1]=
{
2,-1,IPC_NOWAIT
};
int semlock0(int semid)
{
semop(semid,&lock0[0],2);
}
int semunlock0(int semid)
{
semop(semid,&unlock0[0],1);
}
int semlock1(int semid)
{
semop(semid,&lock1[0],2);
}
int semunlock1(int semid)
{
semop(semid,&unlock1[0],1);
}
int semlock2(int semid)
{
semop(semid,&lock2[0],2);
}
int semunlock2(int semid)
{
semop(semid,&unlock2[0],1);
}
/ /File 2: Reader.c
#include <stdio.h>
#include "smop.h"
#define nreaders 2
main()
{
int semid=semget(SEMKEY,2,PERMS|IPC_CREAT);
int rno,tmp;
printf("\n\n\t\tEnter Reader Number : ");
scanf("%d",&rno);
printf("\n\n\t\tPress 0 to start reading: ");
scanf("%d",&tmp);
switch(rno)
{
case 0: semlock0(semid);
break;
case 1: semlock1(semid);
break;
}
printf("\n\n\t\t%d Reader reading.....",rno);
printf("\n\t\tEnter 1 to stop reading");
scanf("%d",&tmp);
switch(rno)
{
case 0: semunlock0(semid);
break;
case 1: semunlock1(semid);
break;
}
}
/ /File 3: Writer.c
#include <stdio.h>
#include "smop.h"
main()
{
int semid=semget(SEMKEY,2,PERMS|IPC_CREAT);
int tmp;
printf("\n\n\t\tEnter 0 to start writing : ");
scanf("%d",&tmp);
semlock0(semid);
semlock1(semid);
printf("\n\n\t\t Writer writing.....");
printf("\n\t\tEnter 1 to stop writing: ");
scanf("%d",&tmp);
semunlock0(semid);
semunlock1(semid);
}
RESULT: A program in c to implement readers writers problem using
semaphores is implemented successfully.