Академический Документы
Профессиональный Документы
Культура Документы
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
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
REQUIREMENTS:
HARDWARE
SOFTWARE
ALGORITHM:
// 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);
}
}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");
}
AIM:
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.
where all the elements of the chopstick are initialized to 1.The structure of
philosopher i is shown below.
repeat
Wait(chopstick[i]);
eat
signal(chopstick[i]);
signal(chopstick[i+1 mod 5]]);
think
until false;
REQUIREMENTS:
HARDWARE
SOFTWARE
: OS: LINUX
PROGRAM:
#include <stdio.h>
#include "smop.h"
main()
{
int pno, semid,tmp;
semid=semget(SEMKEY,3,PERMS|IPC_CREAT);
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;
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
writing is performed
signal(wrt);
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
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
};
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);
}