# semaphore for two groups of processes

Can someone help me giving a pseudo code to this problem : There are two groups of processes , A and B. Maximum 3 Processes from group A or group B(only from one group) can use the given resource . How can this be implemented using semaphores ?

I'm thinking of something like this :

Semaphore work = 1
semaphore nProcesses = 3
Process A:

P(work);
P(nProcesses);

access(resource);

V(nProcesses);
….
V(work);

Process B:

P(work);
P(nProcesses);

access(resource);

V(nProcesses);
….
V(work);


..but I don't know when to free the work semaphore .

• Use one semaphore to let only one group use it, and another semaphore to let only three processes use it? – Draconis Jul 19 '18 at 21:19
• Which part are you having trouble with? – Draconis Jul 19 '18 at 21:20
• Can you give a pseudo code for Process A and Process B ? I would initialize the first semaphore with 1 and the second with 3 ,but when should I free the first semaphore ? how can I know that there are no more from Process A so Process B can be able to start ? – unnamed Jul 19 '18 at 21:25
• @Draconis I updated my question . – unnamed Jul 19 '18 at 21:34