Operating System - HP-UX
1823090 Members
3193 Online
109646 Solutions
New Discussion юеВ

what is significance of semaphores to applications

 
SOLVED
Go to solution
Shivkumar
Super Advisor

what is significance of semaphores to applications

What are significance of semaphores to applications running on hpux 11i ?

Thanks,
Shiv
7 REPLIES 7
Rita C Workman
Honored Contributor
Solution

Re: what is significance of semaphores to applications

Sure someone can quote a book, but the way I remember it is that semaphores are just a mechanism that locks things for you, like files or shared memory. Then you set it to what you want, check it for what it is and then clear it and use it again.
So this makes it a handy dandy tool for applications that need to these kinds of controls. And what app doesn't want these kind of controls.

Rgrds,
Rita
Raj D.
Honored Contributor

Re: what is significance of semaphores to applications

Shiv,

Remember file locking? Well, semaphores can be thought of as really generic advisory locking mechanisms. You can use them to control access to files, shared memory, and, well, just about anything you want. The basic functionality of a semaphore is that you can either set it, check it, or wait until it clears then set it ("test-n-set"). No matter how complex the stuff that follows gets, remember those three operations.


A semaphore is a protected variable (or abstract data type) and constitutes the classic method for restricting access to shared resources (e.g. storage) in a multiprogramming environment.

A Semaphore is a data structure that is useful for solving a variety of synchronization problems.

check put:

# man ipcrm
# man ipcs
# man semctl

To remove/cleanup the semaphore identifier semid from the system
# ipcrm -s simid
[ simid can be found from ipcs command , with grepping the process or username using the semaphore for the application .]

hope this will help:

Cheers,
Raj.

" If u think u can , If u think u cannot , - You are always Right . "
James R. Ferguson
Acclaimed Contributor

Re: what is significance of semaphores to applications

Hi Shiv:

The word "semaphore" derives from the Greek word for "flag" or "signal". As such, it is a variable used to control access to some common resource: a file; a piece of shared memory, etc.

A semaphore holds a zero or positive count that denotes the number of a resource that is available. One speaks of "P" and "V" operations to "dePlete" and "reVive" the count. Semaphores implement "locks" to resources.

A "P" operation decrecemnts a resource count. IF the count is zero, the process attempting to do the operation is suspended (waits). A "V" operation increcemnts the semaphore count, allowing another waiting process to resume execution. A binary semaphore (i.e one with states (counts) of only 0 and 1) implements a simple "lock" to a resource.

Semaphores form the basis for any multi-threading environement. WIthout them, it would be a city without traffic lights!

Regards!

...JRF...
Raj D.
Honored Contributor

Re: what is significance of semaphores to applications

Hi Shiv,

Here is a document for semaphore , with good depth and many internals..,


Cheers,
Raj.

" If u think u can , If u think u cannot , - You are always Right . "
A. Clay Stephenson
Acclaimed Contributor

Re: what is significance of semaphores to applications

This explains it rather well and also explains where the "P" and "V" come from.

Mechanisms like semaphores illustrate the reason I think all UNIX admins should have at least some C/C++ experience. It's difficult to use and understand these facilities unless you do and difficult to troubleshoot problems related to them. You can also play with semaphores using Perl using the IPC::Semaphore module.
If it ain't broke, I can fix that.
Indira Aramandla
Honored Contributor

Re: what is significance of semaphores to applications

Hi Shiv,

Semaphores provide an efficient form of interprocess communication. Cooperating processes can use semaphores to synchronize access to resources, most commonly, shared memory. Semaphores can also protect the following resources available to multiple processes from uncontrolled access:
Global variables, such as file variables, pointers, counters, and data structures.

Protecting these variables prevents simultaneous access by more than one process, such as reading information as it is being written by another process. Hardware resources, such as disk and tape drives. Hardware resources require controlled access because simultaneous access can result in corrupted data.

There are named and unnamed semaphores. Named semaphores provide access to a resource between multiple processes. Unnamed semaphores provide multiple accesses to a resource within a single process or between related processes. Some semaphore functions are specifically designed to perform operations on named or unnamed semaphores.

The semaphore lock operation checks to see if the resource is available or is locked by another process. If the semaphore's value is a positive number, the lock is made, the semaphore value is decremented, and the process continues execution. If the semaphore's value is zero or a negative number, the process requesting the lock waits (is blocked) until another process unlocks the resource. Several processes may be blocked waiting for a resource to become available.
The semaphore unlock operation increments the semaphore value to indicate that the resource is not locked.

Refer to the link for more info on ├в Developing and deploying applications on the HP-UX operating system. managing processes, parallel programming, using semaphores├в


Semaphores provide an efficient form of interprocess communication. Cooperating processes can use semaphores to synchronize access to resources, most commonly, shared memory. Semaphores can also protect the following resources available to multiple processes from uncontrolled access:
Global variables, such as file variables, pointers, counters, and data structures.

Protecting these variables prevents simultaneous access by more than one process, such as reading information as it is being written by another process. Hardware resources, such as disk and tape drives. Hardware resources require controlled access because simultaneous access can result in corrupted data.

There are named and unnamed semaphores. Named semaphores provide access to a resource between multiple processes. Unnamed semaphores provide multiple accesses to a resource within a single process or between related processes. Some semaphore functions are specifically designed to perform operations on named or unnamed semaphores.

The semaphore lock operation checks to see if the resource is available or is locked by another process. If the semaphore's value is a positive number, the lock is made, the semaphore value is decremented, and the process continues execution. If the semaphore's value is zero or a negative number, the process requesting the lock waits (is blocked) until another process unlocks the resource. Several processes may be blocked waiting for a resource to become available.
The semaphore unlock operation increments the semaphore value to indicate that the resource is not locked.

Refer to the link for more info on "Developing and deploying applications on the HP-UX operating system. managing processes, parallel programming, using semaphores"


http://h21007.www2.hp.com/partner/technologies/1,2610,,00.html


IA


Never give up, Keep Trying
Mahesh Kumar Malik
Honored Contributor

Re: what is significance of semaphores to applications

Hi Shiv

Following link may be of help

http://www.cim.mcgill.ca/~franco/OpSys-304-427/lecture-notes/node31.html

Regards
Mahesh