Jump to content
Sign in to follow this  
wally

Semaphore

Recommended Posts

What about using the same semaphore for synchronizing more than two tasks?

 

As a pure example suppose that in the interrupt handler I signal a semaphore when a new byte is received on the RS232 line.

 

What happens if two tasks are waiting for the same semaphore?

 

Will the scheduler let both tasks to run or what happens is that after the semaphore have been decremented from the first task the second task will see it as '0' and will not run?

 

Are the semaphore "queue" replicated for each task or is there a unique semaphore "queue"?

 

Thank's

Share this post


Link to post
Share on other sites
What about using the same semaphore for synchronizing more than two tasks?

 

As a pure example suppose that in the interrupt handler I signal a semaphore when a new byte is received on the RS232 line.

 

What happens if two tasks are waiting for the same semaphore?

 

Will the scheduler let both tasks to run or what happens is that after the semaphore have been decremented from the first task the second task will see it as '0' and will not run?

 

Are the semaphore "queue" replicated for each task or is there a unique semaphore "queue"?

Each semaphore has its own queue of waiting tasks.

A task can only be waiting for one semaphore.

 

If two tasks are waiting for the same semaphore, its the one with the highest priority that gets it. If both tasks have the same priority its the task that was added to the semaphore queue first that will get it.

 

If the priority of a task is changed, it is removed and re-added to the queue, so this can affect which task gets the semaphore if both have the same priority.

 

I hope thats clear.

 

Regards

Dave

Share this post


Link to post
Share on other sites

So if two tasks are waiting for the signaling of the same semaphore both should run when the semaphore is signaled. Which runs first depends on tasks priority following the rules you explained above. Is this right?

 

Regards

Share this post


Link to post
Share on other sites
So if two tasks are waiting for the signaling of the same semaphore both should run when the semaphore is signaled. Which runs first depends on tasks priority following the rules you explained above. Is this right?
No.

Only one task will run.

 

Each signalling will only cause one task to leave a waiting condition.

 

Signall the semaphore twice and both will run.

 

Regards

Dave

Share this post


Link to post
Share on other sites

Ok, now it's perfectly clear.

 

As a suggestion which is the best solution between:

 

1- signaling a semaphore n times, where n is the number of tasks.

2- using n different semaphores.

 

Thanks

Share this post


Link to post
Share on other sites

Wally,

As a suggestion which is the best solution between:

 

1- signaling a semaphore n times, where n is the number of tasks.

2- using n different semaphores.

You could go either way, more semaphores uses more RAM. The longer the semaphore queue the longer it takes to add a new task to it.

 

Regards

Dave

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...