What are semaphores in programming?

13

In a series of questions I asked to ask questions about parallelism, asynchronism, threads, and the like, I came across a lot of new knowledge and a lot of new terms.

In this answer for example, someone makes an indication of the term traffic lights , when cites the advantages and disadvantages of a process bifurcation .

But what would be traffic lights in programming?

Is it linked to any of the terms I mentioned at the beginning of this post?

Note: I can only find references in English, so I asked the question here.     

asked by anonymous 08.06.2017 / 18:54

4 answers

12

Semaphore is a concept created by Dijkstra to solve conflicts of concurrent access to the same resource by different processes.

An analogy for semaphore would be a nightclub: It has a maximum capacity and that is ensured by the porter / security. Once the capacity is reached, no one can come in and start queuing at the door. So for each individual leaving the nightclub, another has his ticket allowed from the beginning of the queue.

In programming, a semaphore is a protected variable that has an integer value, not negative, and has the following operations:

  • Initialization
  • V(s) = signal(s) : If the semaphore has valor = 0 and has a waiting process, the process is activated. Otherwise, the semaphore value is incremented.

  • P(s) = wait(s) : Decreases the semaphore value. If valor = 0 the process is put on hold.

Using pseudo-code, we have:

inicialização(Semáforo S, Inteiro N){
    S = N;
}

V(Semáforo S){   //semáforo Signal
    Se(S != 0)
        S++;
    Senão
        desbloqueia_processo();
}

P(Semáforo S){    //semáforo Wait
    Se(S == 0)
        bloqueia_processo();
    S--;
}

Fonts :

08.06.2017 / 20:17
6

In simple terms, a semaphore is an indication in the code that some feature (being an object, a function, etc.) is being used.

Think of it this way: I have a function that does writing to an SD card reader. Before I use the function I will query a semaphore (which in this case can be a global variable, or something like that) and check if anyone else is using this function. If the variable indicates that the function is not in use, I can use it. If the variable indicates that the function is in use, I can not.

Generally, it uses traffic lights in situations where the program has multiple threads running, but has some feature that can only be used by one thread at a time.

    
08.06.2017 / 20:04
4

Yes!

Semaphore is a type of framework responsible for controlling access to resources in a multitasking environment. When declared it indicates how many threads can have access to a given resource. Every traffic light contains two basic methods P and V. When we are going to request something, we must call the method P (Parsen / Passar) that checks if it is possible to release the resource. After finishing the method V (Vrygeren / Release) is called to warn the other threads that the resource was released. I'll leave an example of the implementation of both of the following methods:

public synchronized void p () {
    if (this.recursos > 0)
        this.recursos--;
    else {
        this.esperando++;

        try {
           this.wait ();
        } catch (InterruptedException E){}
    }
}

public synchronized void v () {
    if (this.esperando > 0) {
        this.notify ();
        this.esperando--;
    } else
        this.recursos++;
}

If you have any questions, you can read a little more about Traffic Lights in these links:

  • Practicing competition in Java! - Traffic lights
  • THREADS, SEMÁFOROS E DEADLOCKS - THE PHILOSOPHY DINNER
  • 08.06.2017 / 20:21
    2

    Semaphore is a special protected variable (or abstract type of data) that controls access to shared resources (for example, a storage space) in a multitasking environment. Usually a semaphore indicates how many processes (or threads) can have access to a shared resource.

      

    A semaphore is a variable, an active standby synchronization mechanism. This variable can be manipulated through two atomic primitives, that is, they can not be interrupted by processes.

    The main operations on traffic lights are:

      

    Initialization: Receives an integer value indicating the number of processes that can access a particular resource.   Wait or P: Decrements the value of the semaphore. If the traffic light is zero, the process is put to sleep.   Signal or V operation: If the semaphore is set to zero and there is some dormant process, a process will be agreed upon. Otherwise, the semaphore value is incremented.

    Link: link

        
    08.06.2017 / 20:07