Why does the operating system leave it to the programmer to deal with racing conditions?

Asked

Viewed 130 times

2

That doubt came to me after that question: What is a racing condition?

From my point of view then correct me if I’m wrong, for the kernel everything is read or written, hence also the reason for the question.

From what I understand, race condition can only occur in situations where the shared resource can be accessed simultaneously by different processes or not.

Taking as an example of reading the file, the question is, why the operating system does not perform the "lock" of the file at the time of access to it.

  • 3

    But the operating system blocks access. This is what can give problem. If two processes try to operate on the same file simultaneously, one of them will not be able to properly access the resource that is already allocated to the other; if untreated this or will generate a fatal error that will kill the process or may not generate the expected result.

  • So the operating system already performs the lock, I confess I formulated the question without a previous search, just based on what I think, thanks for the comment.

1 answer

5


The kernel usually does not take care of IO this is part of other subsystems of the operating system.

The race condition can occur in the same process, it occurs whenever there is competition, not necessarily parallelism.

The operating system does some blocking as much as it can do, but racing condition goes beyond that. If the race condition is you check if a file exists and then you try to access it and it is no longer there, what lock do you expect the OS to do? There’s nothing to do, just because you asked if the file is there the OS should block it? Why? Until when? And if you didn’t want it? If you want to make sure the file is there you must block it, or do it in a different way that does not have the racing condition (the lock is only one of these forms). The OS cannot guess what you want. When it is obvious that the lock is necessary it does (each at your discretion). It may also not do.

In the end it is always the programmer’s responsibility to deal with it in the best way. That’s why it’s important to understand the race condition and other things to program.

Most of bugs more complicated that we see out there are because of lack of understanding of the whole process of software development, people think just put one line after the other in the code and everything looks great, but the decisions that you don’t see in the code matter a lot more.

Browser other questions tagged

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