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.
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.
– Woss
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.
– Diego Ferreira