The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
semaphores using up and down ddx08 High Level Programming 7 04-03-2007 11:38 PM
semaphores kekanap Shell Programming and Scripting 1 03-27-2007 06:56 AM
semaphores qntmteleporter High Level Programming 2 01-28-2006 08:10 PM
Semaphores with key of 0 doeboy UNIX for Advanced & Expert Users 0 10-28-2004 04:41 PM
Semaphores joseph_shibu High Level Programming 1 11-01-2001 01:01 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-06-2007
Watto86 Watto86 is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 3
How many semaphores?

Hello, first of all I apologize if this thread is not in the correct section of this forum, but this one just seemed the most appropriate.
The question I have does not concern Unix specifically, it applies to virtually any OS, however it is in Unix where I learned about this problem.

So, the question is: What is the least number of semaphores needed to guarantee that n processes will run in a specified order ("chain of processes"), like this:

P1 -> P2 -> P3 -> ...... -> Pn ?

It means that P2 will not be allowed into critical section before P1 has exited it, P3 will not go before P2, and so on.

I would really appreciate if you could comment on this, since I am trying to get the correct answer for quite some time and nobody I asked could provide the answer.
Thank you.
  #2 (permalink)  
Old 11-06-2007
porter porter is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 2,965
Would it not be "n-1" where each semaphore is effectively creating the synchronisation for one arrow?
  #3 (permalink)  
Old 11-06-2007
Watto86 Watto86 is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 3
Quote:
Originally Posted by porter View Post
Would it not be "n-1" where each semaphore is effectively creating the synchronisation for one arrow?
No, I'm afraid not. A process can wait on more than one semaphore, so we don't need that many of them. I am told it is even less than log(n) with base 2 (!), although I cannot prove it.
  #4 (permalink)  
Old 11-07-2007
kahuna's Avatar
kahuna kahuna is offline
Registered User
  
 

Join Date: Apr 2007
Posts: 149
Is the requirement that only semaphores be used? It seems to me that all it would take is single semaphore that guards access to a memory location that indicates the last process completed. Each process would wait on the semaphore and once it got access, would check to see if its predecessor had run. If so it would execute and update the last process completed. If not, it would release the semaphore and try again.
  #5 (permalink)  
Old 11-07-2007
Watto86 Watto86 is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 3
Quote:
Originally Posted by kahuna View Post
Is the requirement that only semaphores be used? It seems to me that all it would take is single semaphore that guards access to a memory location that indicates the last process completed. Each process would wait on the semaphore and once it got access, would check to see if its predecessor had run. If so it would execute and update the last process completed. If not, it would release the semaphore and try again.
Yes, it is. This is a problem given by my Operating Systems teacher a couple of weeks ago. It was a lesson on which he described the use of semaphores and an example that preceded this problem contained a pseudocode like this:

Code:
Process1()
{
    down(s1);down(s2);       // waits on 2 semaphores
    DoWork();                // performs job
    up(s2);up(s3);           // signals some semaphores
}

so I suppose we are to determine how can those semaphores be arranged in a way such that the least number of them is used.
  #6 (permalink)  
Old 11-07-2007
porter porter is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 2,965
Post

1. Are the semaphores binary? Or can it start off at a value of n and will let you until it gets down to zero?

2. Can the process do


Code:
up(s1);
down(s2);
up(s3)
down(s4)

doWork();

up(s4);
down(s3);
up(s2);
down(s5);

?

Last edited by porter; 11-07-2007 at 10:25 PM..
  #7 (permalink)  
Old 11-08-2007
Neo's Avatar
Neo Neo is online now Forum Staff  
Administrator
  
 

Join Date: Sep 2000
Location: Asia Pacific
Posts: 6,794
Quote:
Originally Posted by Watto86 View Post
Yes, it is. This is a problem given by my Operating Systems teacher a couple of weeks ago
FYI, posting homework problems are against the rules of this forum.

Here at UNIX.COM we are interested in solving real-world problems, not doing homework for students.

Thank you.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:52 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0