What are Traffic Lights in Programming?

Asked

Viewed 2,530 times

13

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

In that reply for example, someone makes an indication of the term traffic lights, when it cites the advantages and disadvantages of a process fork.

But what would be traffic lights in the programming?

Is it connected to any of the terms I quoted at the beginning of this publication?

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

4 answers

12


Semaphore, is a concept created by Dijkstra to resolve competing access conflicts to the same recourse by separate procedures.

An analogy for traffic lights would be a nightclub: It contains a maximum capacity and is ensured by the doorman/security. Once the capacity is reached, no one can enter and begin to form queues at the door. So for every individual who leaves the nightclub, another has his or her ticket allowed from the beginning of the queue.

In programming, a semaphore is a protected variable that has an integer and non-negative value, and that has operations:

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

  • P(s) = wait(s): Decreases the value of the semaphore. 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--;
}

Sources:

  • 2

    Although not a baladeiro, +1 by the nightclub ;)

6

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

Think of it this way: I have a function that makes writing on an SD card reader. Before using the function I refer to a semaphore (which in this case may be a global variable, or something similar) 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 cannot.

Usually semaphores are used in situations where the program has multiple threads running, but has some feature that can only be used by one thread at a time.

4

Yes!

Semaphore is a type of structure responsible for controlling resource access in a multitasking environment. When declared it indicates how many threads can have access to a given resource. Every semaphore counts two basic methods P and V. When we order something we should call the method P(Parsen/Pass) which checks if it is possible to release the resource. After finishing the V(Vrygeren/Libera) method, it is called to warn the other threads that the resource has been released. I will leave an example of the implementation of both methods below:

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 still have any questions you can read a little more about the Traffic Lights in these links:

  1. Practicing Java Competition! - Traffic Lights
  2. THREADS, TRAFFIC LIGHTS AND DEADLOCKS - PHILOSOPHERS' DINNER

2

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

A semaphore is a variable, an active wait-free synchronization mechanism. This variable can be manipulated through two atomic primitives, i.e., which cannot be interrupted by processes.

The main traffic lights are:

Initialization: Receives an integer value indicating the amount of processes that can access a given resource.
Operation Wait or P: Decreases the semaphore value. If the semaphore is set to zero, the process is put to sleep.
Operation Signal or V: If the semaphore is at zero and there is some sleeping process, a process will be agreed. Otherwise the semaphore value is incremented.

Link: https://www.revista-programar.info/artigos/threads-semaforos-e-deadlocks-o-jantar-dos-filosofos/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.