LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-03-2007, 11:38 AM   #1
ketanu
LQ Newbie
 
Registered: Mar 2007
Posts: 3

Rep: Reputation: 0
Issue with semaphore implementation


I am trying to create a semaphore between two of my processes. Process tht created the semaphore got killed. Now i am unable to create the semaphore with the same key when i restart this process. i cannot restart my system . Is there any way tht the kernel destroys this "orphaned" semaphore, which is used by none of the processes.
 
Old 03-03-2007, 12:09 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
I can think of four ways around this.
  1. If you have root access, you can delete the semaphore that way.
  2. If the semaphore still exists from a previous run, just use it. This alternative is scary, because you don't know the current state of the semaphore.
  3. Don't use a constant key. Use IPC_PRIVATE instead, and find a way to communicate the value of the resultant key from one process to the other. The disadvantage to this is that if you kill your processes too many times, you're going to run out of systemwide semaphores. I'm fairly sure there's a set maximum. Dorky, but there you are.
  4. The best best best way is not to use semaphores at all. Use file locking. Create a file (it can contain zero bytes) whose only purpose is synchronization. This way, the situation degrades gracefully when the processes die, and the implementation doesn't have what-ifs or gotchas.
 
Old 03-03-2007, 12:20 PM   #3
ketanu
LQ Newbie
 
Registered: Mar 2007
Posts: 3

Original Poster
Rep: Reputation: 0
thnx wjevans 4 such a prompt reply.

Well i m a windows programmer basically, and i've just started with Linux. In windows these orphaned semaphores are automatically deleted when none of the process holds a handle for it. Is there any similar house keeping done by linux kernel. Can i set any kernel parameters to do the same.
 
Old 03-04-2007, 08:45 AM   #4
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
No, there are no Unix/Linux housekeeping arrangements for orphaned semaphores. This is a "feature", though in your case it doesn't work to your benefit. No kernel parameters, no nothing.

Record locking in Unix/Linux fills the bill. For any range of bytes in a file (even bytes which do not actually exist in the file), you can:
  • set a reader's lock, and return if that cannot be done immediately;
  • set a reader's lock, waiting until it can be done if necessary and not returning until it has been done;
  • do the same things for a writer's lock;
  • release a lock; and
  • test the state of a lock without changing it.

The best decription I've seen of this is in Advanced Programming in the UNIX Environment, by W. Richard Stevens (of happy memory). (That means he's dead now.) But you can get a pretty good description of record locking by doing:

Code:
man fcntl
Hope this helps.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Semaphore vs Signal scanner Linux - General 4 11-18-2006 01:05 PM
Semaphore tzzdvd Programming 1 05-24-2005 09:23 PM
Links for semaphore and multithreading indian Programming 3 09-05-2004 12:40 AM
semaphore beginner_84 Programming 5 08-25-2004 06:54 PM
about semaphore dummyagain Programming 1 11-11-2003 11:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration