Hello,

if we need to protect a given number of resources, do we need 1 semaphore for each?

Let's say:

4 shared memory areas, used by 3 processes;

Will we need to create a semaphore set with 3 semaphores?

Thanks.

You shouldn't. The concept of a semaphore is simple, you set the value to a number (in your case 4) when you use one of the resources you dec(-1) from the value and so on. Then if the value is <=0 then all sources are used so it waits for the value to be incremented again. You have to make sure that the single semaphore is used in all the processes. Basically pass it by reference to the threads.

At least thats how its implemented in linux, not sure about windows.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.