What is semaphone?

Answer Posted / dalwinder singh

It is Inter-task /Inter -process communication and resource
sharing technic.

Semaphores are devices used to help with synchronization. If
multiple processes share a common resource, they need a way
to be able to use that resource without disrupting each other.

Multitasking/multithreaded systems must manage sharing data
and hardware resources among multiple tasks. It is usually
"unsafe" for two tasks to access the same specific data or
hardware resource simultaneously. ("Unsafe" means the
results are inconsistent or unpredictable).

If the multithreaded/Multiprocessing feature needs to be
used by the application, then it comes with a set of issues,
such as deadlocks, race conditions, incorrect behavior of
threads. To overcome these issues, the OS provides a set of
tools like Mutex, semaphores, signals and barriers that are
handy in solving multithreaded multiprocessed issues.

Semaphores can be thought of as simple counters that
indicate the status of a resource. This counter is a
protected from user access and shielded by kernel.If counter
is greater that 0, then the resource is available, and if
the counter is 0 or less, then that resource is busy.

Semaphores can be either binary or counting, depending on
the number of shared resources. If a single shared resource
is used, then we would require just one semaphore for
synchronization purposes is referred as a binary semaphore.
In all other cases, where the number of resources shared
across multiple users are greater than one, you would use
multiple semaphores, in which case they are referred as
counting semaphores.

When it is locked, tasks must wait for the semaphore.
Typically a task can set a timeout on its wait for a
semaphore. There are several well-known problems with
semaphore based designs such as priority inversion and
deadlocks.

In priority inversion, a high priority task waits because a
low priority task has a semaphore. A typical solution is to
have the task that owns a semaphore run at (inherit) the
priority of the highest waiting task. But this simplistic
approach fails when there are multiple levels of waiting: A
waits for a binary semaphore locked by B, which waits for a
binary semaphore locked by C. Handling multiple levels of
inheritance without introducing instability in cycles is
complex.

In a deadlock, two or more tasks lock semaphores and then
wait forever (that is, no timeout) for other the other
task's semaphore, creating a cyclic dependency graph. The
simplest deadlock scenario occurs when two tasks lock two
semaphores in lockstep, but in the opposite order. Deadlock
is usually prevented by careful design, or by having floored
semaphores (which pass control of a semaphore to the higher
priority task on defined conditions).

Counting semaphore analogy
Assume the "resources" are tables in a restaurant and the
"processes" are the restaurant's customers. The "semaphore"
is represented by the maître d’hôtel, who has sole
responsibility and authority in granting access to the
tables. The maître d mentally maintains a count of
unoccupied tables (utables) and always knows who is first to
be seated when a table becomes available. He or she is also
very focused and can never be interrupted while performing
his or her duties.

When the restaurant opens for business, it will initially be
empty. In other words, the "value" of the "semaphore" will
be equal to the number of tables (tables) in the
restaurant—that is, utables=tables. When someone arrives,
the maître d will seat him or her, and will mentally reduce
the count of available tables, that is, utables=utables-1.
Now, the "value" of the "semaphore" will be equal to the
number of tables in the restaurant minus one.

If several diners simultaneously arrive, the maître d will
seat them in the order of arrival, assuming there are
sufficient tables for all (utables > 0). Arriving diners
with reservations may be seated ahead of others who do not
have reservations, the former having priority over the
latter. For each table taken, the maître d will again
mentally compute utables=utables-1. If all tables are
occupied, that is, utables=0, new arrivals will have to wait
their turn to be seated.

As each diner leaves, the maître d will mentally compute
utables=utables+1. If another diner is waiting and at least
one unoccupied table is available, the maître d will seat
him or her, and again mentally compute utables=utables-1.
Eventually, all diners will have left and the maître d's
utables=utables+1 mental computation will result in
utables=tables—an empty restaurant.

Is This Answer Correct ?    7 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain the initial process sequence while the system boots up?

551


What is the very first process created by kernel?

597


What is ipc port?

574


Tell me set-user-id is related to (in unix)?

550


Please explain fork() system call?

561






Please describe the initial process sequence while the system boots up?

577


Max relax-able permission value with out giving write permission to others?

574


What is ln(linking)?

584


What is daemon?

534


How to write the program on full-duplex communication on bidirectional(e.g using two pipes)?

586


What is unix ipc?

555


What is i-node numbers?

600


What are two different models of ipc differentiate both?

580


Explain about daemon?

568


What are the various schemes available in ipc?

555