Semaphore Definition: Starvation
Semaphore Definition: Starvation
Semaphore Definition: Starvation
Semaphore Definition
A semaphore is a data structure that is shared
by several processes. Semaphores are most
often used to synchronize operations (to avoid
race conditions) when multiple processes
access a common, non-shareable resource.
By using semaphores, we attempt to avoid other
multi-programming problems such as:
Starvation
Occurs when a process is habitually denied access to
a resource it needs.
Deadlock
Occurs when two or more processes each hold a
resource that the other needs while waiting for the
other process to release its resource.
1
SEMAPHORES
Semaphore Definition
To indicate a process has gained access to the
resource, the process decrements the
semaphore.
For events to progress correctly, the test and
decrement operation on the semaphore must be
atomic (i.e., noninterruptible/indivisible).
There are two kinds of Semaphores:
Binary semaphores
Control access to a single resource, taking the value
of 0 (resource is in use) or 1 (resource is available).
Counting semaphores
Control access to multiple resources, thus assuming
a range of nonnegative values.
2
SEMAPHORES
Semaphore Definition
Semaphore is a nonnegative integer that is
stored in the kernel.
Access to the semaphore is provided by a
series of semaphore system calls.
SEMAPHORES
A semaphore set
descriptor
struct sem array[ ]
nsems = 3
struct semid_ds
sem_perm
*sem_base
sem_nsems
sem_otime
sem_ctime
SEMAPHORES
SEMAPHORES
SEMAPHORES
Manual Section
Return
Sets errno
Failure
Success
Yes
1-
SEMAPHORES
10
SEMAPHORES
Semaphore Control
The semctl system call allows the user to
perform a variety of generalized control
operations on the system semaphore structure,
on the semaphores as a set, and on individual
semaphores.
(Table 7.3)
11
SEMAPHORES
Semaphore Control
2
Manual Section
Sets errno
Failure
Success
Yes
1-
SEMAPHORES
Semaphore Control
The semctl system call takes four arguments:
The first argument, semid, is a valid semaphore
identifier that was returned by a previous semget
system call.
The second argument, semnum, is the number of
semaphores in the semaphore set.
The third argument to semctl, cmd, is an integer
command value. the cmd value directs semctl to
take one of several control actions. Each action
requires specific access permissions to the
semaphore control structure.
13
SEMAPHORES
Semaphore Control
The fourth argument to semctl, arg, is a union of
type semun.
SETVAL
Set the value of the individual semaphore referenced
by the semnum argument to the value specified by
the fourth argument to semctl.
14
SEMAPHORES
15
SEMAPHORES
Semaphore Operations
Additional operations on individual semaphores
are accomplished by using the semop system
call.
(Table 7.5.)
16
SEMAPHORES
Semaphore Operations
2
Manual Section
Sets errno
Failure
Success
Yes
1-
SEMAPHORES
Semaphore Operations
The semop system call takes three arguments:
The first argument, semid, is a valid semaphore
identifier that was returned by a previous
successful semget system call.
The second argument, sops, is a reference to the
base address of an array of semaphore
operations that will be performed on the
semaphore set associated with by the semid
value.
The third argument, nsops, is the number of
elements in the array of semaphore operations.
18
SEMAPHORES
Semaphore Operations
If semop fails, it returns a value of 1 and sets
errno to indicate the specific error.
array of semaphore operations
struct sembuf
{
unsignedshort sem_num;//which semaphore (0 ..
nsems-1)
short sem_op;
short sem_flg;
}
19
SEMAPHORES
20
SEMAPHORES
21