Sunteți pe pagina 1din 27

SEMAPHORES

TOPICS
Semaphore Definition Types of Semaphore Operations on semaphores The basic operations on semaphores Applications Semaphore from railway analogy Summary

SEMAPHORE DEFINITION
A semaphore is a data structure that is shared by several processes. Semaphores are most often used to synchronize operations when multiple processes access a common, non-shareable resource. By using semaphores, we attempt to avoid other multi-programming problems such as:
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.

SEMAPHORE DEFINITION
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). (e.g. an interrupt)

Counting semaphores
Control access to multiple resources, thus assuming a range of nonnegative values.

Semaphore is a nonnegative integer. Access to the semaphore is provided by a series of semaphore system calls.

REVIEW: SEMAPHORES
A Semaphore has a non-negative integer value and supports the following two operations: P()[proberen]: an atomic operation that waits for semaphore to become positive, then decrements it by 1. Think of this as the wait() operation

V()[verhogen]: an atomic operation that increments the semaphore by 1. Think of this as the signal() operation Semaphore operations:

Can change a tasks state. Are fast.

function V(semaphore S): Atomically increment S [S S + 1] function P(semaphore S): repeat: Between repetitions of the loop other processes may operate on the semaphore [if S > 0: Atomically decrement S - note that S cannot become negative S S - 1 break]

IMPLEMENTATION
Counting

semaphores are with two operations, denoted as V (also known as signal()) and P (or wait()) Operation V increments the semaphore S, and operation P decrements it. Square brackets are used to indicate atomic operations, i.e. operations which appear indivisible from the perspective of other processes.

OPERATIONS ON SEMAPHORES

NAME

sem_init, sem_wait, sem_trywait, sem_post, sem_getvalue, sem_destroy - operations on semaphores


#include <semaphore.h> int sem_init(sem_t *sem, int pshared, unsigned int value); int sem_wait(sem_t * sem); int sem_trywait(sem_t *sem); int sem_post(sem_t * sem); int sem_getvalue(sem_t * sem, int * sval); int sem_destroy(sem_t * sem);

SYNTAX

THE BASIC OPERATIONS ON SEMAPHORES ARE

Increment the counter atomically, and Wait until the counter is non-null and Decrement it atomically.

INT SEM_WAIT(SEM_T * SEM); INT SEM_TRYWAIT(SEM_T

*SEM); sem_wait suspends the calling thread until the semaphore pointed to by sem has non-zero count. It then atomically decreases the semaphore count. sem_trywait If the semaphore pointed to by sem has non-zero count, the count is atomically decreased and sem_trywait immediately returns 0.

INT SEM_POST(SEM_T * SEM); INT SEM_GETVALUE(SEM_T * SEM, INT * SVAL);

sem_post atomically increases the count of the semaphore pointed to by sem. This function never blocks and can safely be used in asynchronous signal handlers. sem_getvalue stores in the location pointed to by sval the current count of the semaphore (sem).

INT SEM_DESTROY( SEM_T * SEM);

sem_destroy Destroys a semaphore object, freeing the resources it might hold. No threads should be waiting on the semaphore at the time sem_destroy is called. In the Linux Threads implementation, no resources are associated with semaphore objects, thus sem_destroy actually does nothing except checking that no thread is waiting on the semaphore.

APPLICATIONS
Semaphore products are currently operating in thousands of automation and remote monitoring operations. semaphore flag signaling system. Semaphore line, a system of long-distance communication based on towers with moving arms. Railway semaphore signals for railway traffic control. Semaphore (programming), in computer science, a mechanism for supporting c, c++, java (mutual exclusion) programs.

SEMAPHORE FROM RAILWAY ANALOGY

Here is a semaphore initialized to 2 for resource control

A signal is a mechanical or electrical device erected beside a railway line to pass information relating to the state of the line ahead to train/engine drivers. The driver interprets the signal's indication and acts accordingly. Typically, a signal might inform the driver of the speed at which the train may safely proceed or it may instruct the driver to stop. Signals are used to indicate one or more of the following: that the line ahead is clear or blocked. that the driver has permission to proceed. That points (also called switch or turnout in the US) are set correctly. The speed the train may travel.

Signals can be placed: At the start of a section of track. In advance of other signals. On the approach to a level crossing. 'Running lines' are usually continuously signaled. Each line of a double track railway is normally signaled in one direction only. Mechanical signals may be operated by electric motors. More recently, LEDs have started to be used in place of the incandescent lamps, reflectors and lenses.

It

is important to understand that for signals that use colored aspects. Signals differ both in the manner in which they display aspects and in the manner in which they are mounted with respect to the track. The color and shape of the arm is commonly varied to show the type of signal and therefore type of indication displayed. A common pattern was to use red, square-ended arms for "stop" signals and yellow arms for "distant" signals.

THE OLDEST FORMS OF SIGNAL DISPLAYED


The

oldest forms of signal displayed their different indications by a part of the signal being physically moved. The first railway semaphore was erected by Charles Hutton on the London Railway. at New Cross, south-East London, in 1842-1843.

SIGNALING POWER
Usually, signals and other equipment are powered from a low voltage supply. The reason behind this is that the low voltage allows easy operation from storage batteries . in some parts of the world batteries are the primary power source, as mains power may be unavailable at that location. In urban areas, the trend is now to power signal equipment directly from mains power, with batteries only as backup.

SUMMARY
Binary semBCreate Mutual Exclusion semMCreate

semTake, semGive
show

semDelete

SUMMARY

Binary Semaphores allow tasks to pending until some event occurs.


Create a binary semaphore for the given event. Tasks waiting for the event on a semTake( ). Task or ISR detecting the event calls semGive( ) or semFlush( ).

SUMMARY
Semaphores:

Like

integers

with

restricted

interface. Two operations: P(): Wait if zero; decrement when becomes non-zero V(): Increment task. Can initialize value to any non-negative value Use separate semaphore for each constraint.

THANK YOU!!!

S-ar putea să vă placă și