Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Linux - Kernel This forum is for all discussion relating to the Linux kernel.


  Search this Thread
Old 07-26-2007, 07:08 AM   #1
Rama Anandakrishnan
LQ Newbie
Registered: Jul 2007
Posts: 2

Rep: Reputation: 0
semaphore with multiple tasks

I have an application where multiple tasks say p1, p2, p3 with the same priority are using a semaphore. p1 has acquired the semaphore and p2 and p3 are waiting for the semaphore to be released. Is it possible to ensure that p2 will get the semaphore next as it requested for this earlier than p3.
Should we write own driver, incase we need such a feature.
Old 07-26-2007, 07:30 AM   #2
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
In java, it can be done.
When fairness is set true, the semaphore guarantees that threads invoking any of the acquire methods are selected to obtain permits in the order in which their invocation of those methods was processed (first-in-first-out; FIFO).
In C, you need to implement this yourself. Design a queue which is protected by a semaphore. Better yet, design a queue which is protected by advisory record locking on some lock file. This is better than System V semaphores, because System V semaphores don't automatically go away when the last user does.

Hope this helps.
Old 07-27-2007, 04:16 AM   #3
Rama Anandakrishnan
LQ Newbie
Registered: Jul 2007
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks for the reply. This is of great help.
Old 08-01-2007, 06:48 PM   #4
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 8,455
Blog Entries: 4

Rep: Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918
Generally speaking, when you need to guarantee (say) FIFO-ordering, you should plan to implement that bit of it yourself.

For example, one way to solve this problem would be to devise a data-structure which contains a FIFO-queue and which happens to be protected by a semaphore. (Our clients will view this thing as an opaque "object," and I'm describing a possible implementation.)

We'll grab the semaphore to guarantee serial access to the object, and we'll see if the FIFO-queue is empty and the object is unowned. If so, we become the owner and we can just move right along... releasing the semaphore as we go. Otherwise, we need to arrange to wait. We add ourselves to the end of the FIFO-queue and go to sleep waiting for some user-signal after releasing the semaphore again.

As each client leaves the protected area, they grab the semaphore, and see if the queue is non-empty. If so, they pop the first guy off the queue, establish him as the new 'owner,' and send him a signal. Then, they release the semaphore and go about their business.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
semaphore beginner_84 Programming 5 08-25-2004 06:54 PM
about semaphore dummyagain Programming 1 11-11-2003 11:09 AM
Semaphore setting for Redhat 7.0 bgupta Linux - General 1 01-17-2003 11:04 AM > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 05:10 PM.

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