Difference between Counting and Binary Semaphores

前端 未结 3 1143
花落未央
花落未央 2020-12-02 18:51

What is the difference between Counting and binary semaphore.

What I have seen somewhere is that both can control N number of processes which have requested for a r

3条回答
  •  Happy的楠姐
    2020-12-02 19:22

    Actually, both types are used to synchronize access to a shared resource, whether the entity which is trying to access is a process or even a thread.

    The difference is as follows:

    Binary semaphores are binary, they can have two values only; one to represent that a process/thread is in the critical section(code that access the shared resource) and others should wait, the other indicating the critical section is free.

    On the other hand, counting semaphores take more than two values, they can have any value you want. The max value X they take allows X process/threads to access the shared resource simultaneously.

    For further information, take a look at this link.
    http://www.chibios.org/dokuwiki/doku.php?id=chibios:articles:semaphores_mutexes

    EDIT
    The max value that a counting semaphore can take is the the number of processes you want to allow into the critical section at the same time.
    Again, you might have a case where you want exclusion over a certain resource, yet you know this resource can be accessed by a max number of processes (say X), so you set a counting semaphore with the value X.

    This would allow the X processes to access that resource at the same time; yet the process X+1 would have to wait till one of the processes in the critical section gets out.

提交回复
热议问题