?
Solved

semaphore breifing

Posted on 2003-03-31
4
Medium Priority
?
294 Views
Last Modified: 2010-04-15
this isn't necessarily a c, question, c++ or psuedocode will do as well.  in fact, i prefer if you keep it simple in pseudocode so i can figure it out mostly for myself.

i want to synchronize 4 processes as follows:

proc1 forks to create proc2,3, and 4.  (i have already ensured that the other procs dont keep creating others) basically as follows.
all processes share 2 variables, pretend for now they are chars.  there are three possible combinations of these 2 chars:

a and b // call proc 2
b and c // call proc 3
a and c // call proc 4

i want proc1 to randomly produce the chars.
then proc2,3, and 4 are supposed to determine if thier chars were chosen, and the process whose combo was chosen should execute what it is supposed to do.

main()
{

initialize semaphores; // how many do i need, and what should thier initial values be, and when do i signal/wait?
initialize shared_mem; // i've already done this.

proc2=fork();
if (proc2!=0)
{
    proc3=fork();
    if (proc3!=0)
    {
        proc4=fork();
        if (proc4!=0)
           ( // here is the code for proc1
             if any other proc (2,3, or 4) is running, wait.
             else sleep for 2 seconds, then change the shared memory variables.  
           }
        else
           {
            // we are in proc 4
            if the shared memory variables are the ones needed to execute, printf("proc4 executing") and sleep for 1 second.
            else wait until the sharedmem vars are the ones needed;
           }
    }
    else
    {
        //we are in proc3
        if the shared memory variables are the ones needed to execute, printf("proc3 executing") and sleep for 1 second.
        else wait until the shared mem vars are the ones needed;
    }
}
else
{
//we are in proc2
if the shared memory variables are the ones needed to execute, printf("proc2 executing") and sleep for 1 second.
else wait until the shared mem vars are the ones needed;
}
}

also, each if/else block is really a loop that should be continued until a third shared variable, count, is equal to some integer. (say 5 if it helps)


also, please don't refer me to try to do it a better way, or your favorite way.  I want to do it this way (using semaphores in the method described).  i'm trying to teach myself to use unix semaphores, and i have an idea on what my answer is.  i am doing this to help me learn the asked method, not to try any new methods yet.  I will ask for better methods when I am ready for them. For now, I want to learn the basics, because I don't think knowledge is worth anything unless I know how to derive it, which helps me understand it.

thanks for your help.
             
0
Comment
Question by:substand
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 6

Expert Comment

by:GaryFx
ID: 8247736
If you don't care if proc's 2, 3, or 4 wake up, check their variables, decide they're not ready, and go back to sleep, then you can do it with one semaphore that guarantees that only one process has access to the variables at a time.

If you really want to make sure that the right process wakes up and the others continue to sleep without waking until the variables are set for them to proceed, then you have to have a semaphore for each of 2, 3, and 4, and furthermore, proc1 needs to have enough knowledge of the variables to know which subprocess to wake up.  At this point, the variables might be redundant, and you could just use the semaphores to indicate which subprocess is the one to go.

It isn't clear from your pseudocode whether your intent is to always use a timed sleep and poll the semaphore, or to sometimes wait directly for the semaphore.  The former is less efficient but sidesteps some deadlock issues.

Gary
0
 
LVL 10

Author Comment

by:substand
ID: 8248366
i always want a timed sleep when the process is in its "doing something" state.  ie, part of the thing it does is to sleep.

of course, when the process isn't doing anything, i want it to be in say the OS wait queue, rather than keep doing nothing in the OS ready queue, taking up cpu time.  It should, ideally, wake up when its 2 values are the ones in the shared mem.

0
 
LVL 10

Author Comment

by:substand
ID: 8259080
0
 
LVL 6

Accepted Solution

by:
Mindphaser earned 0 total points
ID: 8261174
Points refunded and moved to PAQ

** Mindphaser - Community Support Moderator **
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question