Semaphore: process returning more keys than got for processing

  1. I “new” a semephore with 2 keys
  2. Two processes P1, P2. P1 needs two keys also P2 needs two keys for its process to start
  3. P1 gets 2 keys but when I returned 5 keys from P1, [b]it did not give my any compile/runtime error.

Question:
1)How a process can return 5 keys when it has got only two keys?
2)Why it did not give me any compile/runtime error while returning the keys?
3)For P2 are there now 5 keys available or 2 keys available which we “new” while creating the semaphore?
Please carify.

A semaphore does not manage the number of keys; it only manages handing out of keys that it has.
Think of a bank with a set of coins. When you construct the bank, the new() constructor deposits an initial set of coins. Then people stand in line waiting to get() a certain number of coins, while other people come to put() coins in the bank. There are many different ways to use a semaphore, but all of them require the user to manage the number of keys that are get() or put().

A mailbox, on the other hand is constructed with a maximum size.

In reply to dave_59:

Thnx, now understood the concept clearly.