Operating System - HP-UX
1833490 Members
2781 Online
110052 Solutions
New Discussion

inc or dec specific values in semaphores

 

inc or dec specific values in semaphores

I am trying to code a printer spool simulation.

Assume printer has a resource of 2048Kb.

I have randomly created consumer threads, consume value between 50Kb and 1024Kb. I need to synchorinize these requests and buffer allocation.

So I need to initiliaze the semaphore value to 2048 at the beginnig. But as long as I know wait and signal functions increment and decrement the semap. value by only one. But for example if the consumer value is 50Kb, I need to decrement it by 50. Do you have any idea how can I achieve this?
2 REPLIES 2

Re: inc or dec specific values in semaphores

a friend of mine suggested that, if the consumer value is 50kb, I should do wait or signal in a loop 0 to 50. But don't sure if this would be ok.
Dennis Handly
Acclaimed Contributor

Re: inc or dec specific values in semaphores

>I need to decrement it by 50.

Best to let it increment by 1. Then take that value and either multiply by 50, or use it as an index in an array where you have your real values.

On PA there is no hardware to do this. So you can invent your own with spinlocks. On IPF, you have a set of fixed increments for fetchadd, so you would have to use compare and exchange to an increment of your arbitrary value.