What is a race condition?

Contents

running stick

Have you ever run a race? If so, You know that sometimes a photographic finish is needed to select the winner! But, What if two people reached the finish line at the same time? Welcome to the race conditions.

Which are Computer threads?

To explain a race condition, first we have to understand a bit how computers work internally. When using an operating system, will perform various actions like opening a command terminal window, open a browser, etc. Each of these actions will result in a reaction from the operating system to start a new computer procedure / computer scientist.

A thread is a computer procedure / computer scientist who will run / will execute the multiple steps (programming steps, originally written in source code format and regularly compiled by a compiler) needed to execute the task you requested from the operating system or software running on it. .

En Linux, such a thread is uniquely identified by a PID (Procedure identifier). To learn more about PIDs in Linux, puede leer nuestros posts Bash Automation and Scripting Basics (Part 3) y How Linux Signals Work: SIGINT, SIGTERM y SIGKILL.

In Windows, a thread is also uniquely identified via a procedure ID (Ref. PID column in Windows Task Manager), even though the implementation of process management is different between Linux and Windows; different code behind, different PID interaction tools, etc. and limited compatibility. At the same time, the Windows procedure ID PID not to be confused with product ID PID (same term, different meaning) O AT (Provider ID). The last two refer to device identification and are not related to process management.

When a thread begins, can itself start other threads. The original thread is often called the main or parent thread. As an example, when you click on the icon of your favorite web browser, will immediately start a thread (the main thread), and that thread will very quickly start multiple child threads or threads and, because, will become the main thread.

You can also think of threads as runners in a race. As an example, think of a busy database server serving many different connected clients. Each of those client threads (note the use of the word thread) will have (in several cases) in and by itself at least one thread on the database host server and / or within the database software itself (In other words, two threads, one registered in the operating system and one within the database software).

The database server is trying to serve all those threads at the same time, hence the term concurrent processes O concurrent threads and if there are bugs in the database software (or in the operating system, etc.), sooner or later you may find yourself in a race condition.

What is a Race condition?

A simple way to relate this to runners running in a race is to imagine a finish photo in which two runners actually cross the finish line at the same point in time.. Is doable, even though quite unlikely, that this happens in human races. For computers that process hundreds of operations per millisecond, becomes much more viable.

As another example, imagine a relay race in which runners pass a baton (the flashy colored stick) from one person to another. Imagine now that one of the members of the race makes a mistake, and now there are two runners who think they should get the red flag.

An important event in a relay race is the passing of the baton, since this may mean that the previous holder of the token may stop running, and now it is up to the new owner of the witness to give his best. Now there are two runners grabbing the baton. It will be an interesting situation to watch on television (if you like that kind of thing), but it is clear that there will be some consequences.

In essence, a race condition is an error, error or glitch in the computer system code that produces unpredictable results: an unexpected sequence of events. It is regularly due to two threads conflicting in some way, since more than two threads may be involved in the actual conflict and, often, more than two threads running in faulty software.

In our example of the human race, we had two people accessing an object at roughly the same time, and the corruption (a computer definition to indicate that some data was corrupted, where such data could reside in memory or on disk or on CPU, etc.) it happened the moment two people (or two threads in analogy with the computer) they tried to grab the witness and the conflict occurred. In computing terms, two threads tried to write a memory space that regularly should only be written by one thread (a runner).

Race conditions can occur in various areas, like internal electronics, computer software and life in general. As an example, a call collision is a telecommunications definition to describe the situation where a communication channel is seized at both ends simultaneously. Inside computer software, one of the most prominent areas of race conditions, there is a wide variety of possible racing conditions.

As another example of a race condition within computer software, imagine two compute threads working with a given memory space. A user has just submitted a form and the backend software is writing this form to memory. At the same time, another user is reading the fields of this form from the same memory space. Depending on what happens, the reader user may receive a partially incorrect form with partially updated information.

Prevention of race conditions: Thread safety

There has been a lot of discussion about career conditions in the IT industry. Depending on the coding language you use, there may be many or few provisions to handle race conditions. A frequently used definition is thread safety or a safe from threats application or programming language [construct]. These terms are used to indicate whether a piece of code or software as a whole is safe from threats, In other words, written in such a way as to avoid and even avoid race conditions.

If the software is considered safe from threats, is considered free from the opportunity of race conditions. In various cases, ‘considered‘Thread-safe is the best that developers can offer, and even more so when many threads and interactions are possible. The complexity of many threads working with many resources can easily turn into a myriad of code handling and an even greater myriad of possible race conditions.

Various programming constructs can be used to avoid race conditions. As an example, traffic lights and mutex. The complexity of using such constructs will depend on the programming language used and its native support to drive thread handling.. As an example, and C ++ one can look at the std :: mutex class to implement a mutex (In other words mutually exclusive) close with key. In bash, despite this, one does not find such a construction natively.

Taking a step further, you can also consider which constructions, functions or even executables and libraries in particular are already thread safe, and later use said constructions, functions, executables and libraries as a basis for building a new build, function, executable, library. or complete software package.

Putting even basic thread-safety-handling constructs into practice can be a complex matter. As an example, consider the difficulty of implementing a semaphore in bash.

Ending

In this post, we explore computing threads and race conditions. We analyze analogies with race races and relay races in human life to explore some basic race conditions that can occur within computers.. To end, we explore thread safety, the different implementations of race condition handling in computer coding languages ​​and how we can prevent race conditions.

If you liked this post, take a look at the post How logic gates work: OR, AND, XOR, NOR, NAND, XNOR y NOT.

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.