What is semaphone?

Answers were Sorted based on User's Feedback



What is semaphone?..

Answer / e.sridhar reddy

a semaphor is nothing but a term used in unix for a
variable which acts as a counter.for instance there may be
times when two processes try to access the same file
simultaneously.in this event we must control the access of
the when the other process is accessing
this is done by assigning value to a semaphore.
the value of the semaphore is initialized by the 1st
process when the file is in access by it.when the 2nd
process try to access the file it checks the value of the
semaphore and if it finds the value as initialized it does
not access the file.

Is This Answer Correct ?    16 Yes 3 No

What is semaphone?..

Answer / gogune suresh kumar

Semaphore is one method used for interprocess communication.

Is This Answer Correct ?    11 Yes 3 No

What is semaphone?..

Answer / 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

What is semaphone?..

Answer / govind

semaphore is positive binary number , this value is stored in
in kernel .

it is process synchronisation technique .it is also advisory
locking technique.

it is ipc but no information in exchanged between the process

see book stevens "unix networking prog"

Is This Answer Correct ?    2 Yes 0 No

What is semaphone?..

Answer / vinoth

We all know it is an IPC and it is mainly required to have Synchronization of shared memory access among different threads or process.

hmmm .... even mutex does the same thing... They too synchronize the access of the shared memory... then what is the difference..??

semaphore allows n number of users to access the same resources. we go for semaphore only when the resource that can handle some n process/threads at the same time.


Mutex is a semphore with a value 1.


Only one user can access the shared resources at a given time for mutex.

Is This Answer Correct ?    1 Yes 3 No

Post New Answer

More Unix IPC Interview Questions

Which ipc is faster?

0 Answers  


What is IPC? What are the various schemes available?

5 Answers  


what is the condition required for dead lock in unix sustem ?

2 Answers  


What is an advantage of executing a process in background?

2 Answers  


What is ln(linking)?

0 Answers  






What are various IDs associated with a process?

4 Answers  


What are the various schemes available in ipc?

0 Answers  


Explain the advantage of executing a process in background?

0 Answers  


What is a Daemon?

3 Answers  


What is ipc port?

0 Answers  


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

0 Answers  


What is fork()?

0 Answers  


Categories