cancel
Showing results for 
Search instead for 
Did you mean: 

What is semaphore in Unix

Ezhilarasan
Occasional Advisor

What is semaphore in Unix

Hi,

I got details to calculate semaphore kernal
parameters in Unix for Oracle.
But I do not know what is semaphore.
Can you please explain about semaphore
for better understanding.

Thanks
R. Ezhil
7 REPLIES
Stefan Farrelly
Honored Contributor

Re: What is semaphore in Unix

Semaphores are used mainly to keep processes properly synchronized to prevent collisions when accessing shared data structures. Their use in software tends to be complex, so many programmers use alternate means of control where practical.

Heres more detail;

http://www.docs.hp.com/cgi-bin/fsearch/framedisplay?top=/hpux/onlinedocs/TKP-90202/TKP-90202_top.html&con=/hpux/onlinedocs/TKP-90202/00/00/26-con.html&toc=/hpux/onlinedocs/TKP-90202/00/00/26-toc.html&searchterms=semaphore&queryid=20030228-021118
Im from Palmerston North, New Zealand, but somehow ended up in London...
Christian Gebhardt
Honored Contributor

Re: What is semaphore in Unix

Hi

a classic resource-sharing problem:

-- . Dining Philosophers ------

Philosophers do two things:

o Think
o Eat

Thinking is solitary

Eating involves shared resources - a spagetti restaurant with too few forks

#define N = 5; /* five forks */

define philosopher(int i);
repeat forever {
think();
take_fork(i);
take_fork((i + 1) mod N);
eat();
put_fork(i);
put_fork((i + 1) mod N);
}
end

What if everyone picks up the first fork? - deadock

Put down and retry - starvation

Put down and random wait - possible starvation

Need a secure mechanism

Use a mutex

#define N = 5; /* five forks */
semaphor mutex = 1;

define philosopher(int i);
repeat forever {
think();
down(mutex);
take_fork(i);
take_fork((i + 1) mod N);
eat();
put_fork(i);
put_fork((i + 1) mod N);
up(mutex);
}
end

Only allows one philosopher to eat

With 5 forks, 2 could eat!


-- . corect solution ----

Philosophers in 1 of 3 states:

o Thinking
o Hungry
o Eating

Blocking semaphor for each philosopher - may be hungry, but wait to eat.

3 state transitions. Transitions performed in critical region control by mutex
semaphor.

Thinking -> Hungry: set "hungry" state, and try next transition

Hungry -> Eating: block if either neighbour eating, otherwise take forks and
set "eating" state

Eating -> Thinking: release forks, set "thinking" state, try to unblock
neighbours



Chris
U.SivaKumar_2
Honored Contributor

Re: What is semaphore in Unix

Hi,

Ref:

Semaphores are a programming construct designed by E. W. Dijkstra in the late 1960s. Dijkstra's model was the operation of railroads: consider a stretch of railroad in which there is a single track over which only one train at a time is allowed. Guarding this track is a semaphore. A train must wait before entering the single track until the semaphore is in a state that permits travel. When the train enters the track, the semaphore changes state to prevent other trains from entering the track. A train that is leaving this section of track must again change the state of the semaphore to allow another train to enter. In the computer version, a semaphore appears to be a simple integer. A process (or a thread) waits for permission to proceed by waiting for the integer to become 0. The signal if it proceeds signals that this by performing incrementing the integer by 1. When it is finished, the process changes the semaphore's value by subtracting one from it.
Semaphores let processes query or alter status information. They are often used to monitor and control the availability of system resources such as shared memory segments.

regards,

U.SivaKumar


Innovations are made when conventions are broken
Ravi_8
Honored Contributor

Re: What is semaphore in Unix


Hi,

semaphores basically used for processes synchronization
never give up
Vincent Fleming
Honored Contributor

Re: What is semaphore in Unix

You received a lot of elaborate explainations for this, but since English is not your primary language, I will try to explain simply:

A semaphore is used when more than one thread or process need to share a resource (a disk, shared memory, etc).

The kernel and processes work together with the semaphores so that only one process gets to use the resource at a time. The kernel will grant permission to the semaphore (and resource) to only one process at a time.

For every resource, there is a semaphore. So, if you have 10 semaphores, you have 10 shared resources.

I hope this helps you understand.

Vince
No matter where you go, there you are.
Yogeeraj_1
Honored Contributor

Re: What is semaphore in Unix

hello,

Semaphores can be thought of as flags (hence their name, semaphores). They are either on or off. A process can turn on the flag or turn it off. If the flag is already on, processes who try to turn on the flag will sleep until the flag is off. Upon awakening, the process will reattempt to turn the flag on, possibly suceeding or possibly sleeping again.

Such behaviour allows semaphores to be used in implementing a post-wait driver - a system where processes can wait for events (i.e. wait on turning on a semphore) and post events (i.e. turning of a semaphore).

For instance, in Oracle, this is used to maintain concurrency control over the SGA, to control concurrency between all the background processes (pmon, smon, dbwr, lgwr, and oracle shadows), since it is writeable by all processes attached. Semaphores are also used to control two-task communication between the user process and shadow process if the fast (shared memory) driver is used.

There are three OS kernel parameters that work together to limit semaphore allocation (SEMMNI, SEMMSL(hardcoded to 500) & SEMMNS ).

Current semaphore allocation can be displayed with the
OS command 'ipcs -s'.

% ipcs -s

Hope this helps!

Best Regards
Yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
K.Vijayaragavan.
Respected Contributor

Re: What is semaphore in Unix

Hi Ezhil,

Semaphores are used mainly to keep processes properly synchronized to prevent collisions when accessing shared data structures.

Refer the URL,

http://docs.hp.com/cgi-bin/otsearch/getfile?id=/hpux/onlinedocs/939/KCParms/KCparam.SemaParmsOverview.html&searchterms=semaphore&queryid=20030228-120153

-Vijay
"Let us fine tune our knowledge together"