Hello and welcome.
In this new chapter, let's discuss the advanced file I/O operations, the different topics covered in this
chapter are...
We will study about atomicity, race condition.
We will describe about Fcntl system call.
What is blocking and non blocking I/O system call.
What is file descriptor table and open file.
Table.
Then finally we will see what is the inode of a file.
Before we discuss the concept of race around and atomicity.
Let's have a look into what is about multiprocessing or multithreading.
Linux operating system provides a multiprocessing or a multithreading environment.
This means there are several processes running parallelly in Linux system.
Each process has its own priority and based on the priority, scheduler gives every process a definite
amount of time on the CPU so that the process can execute.
Hence, overall the user gets a virtual feeling that, as if all the processes are executing at the
same time and in parallel.
Now in a multi-processing environment there arises some issues such as race around condition.
A restaurant is a situation where the result of different processes running together on a shared resource
can behave in an unexpected way.
Or in other words, we can say that when several processes are running in a multiprocessing environment
and if more than one process are utilizing the same resource, then there can be a chance of output
being unpredictable.
This is called a race condition.
So each process has to raise to access the resource and process on the resource and perform to provide
an output.
So a situation will arise when the processor racing to finish the execution.
We will discuss with some examples in order to understand the race condition.
Let's begin with the simple example.
Here on left hand side we have program 1 and on.
Right hand side we have programmed 2.
Both programs are identical in the sense each program will open a common file called log.txt in
read write mode.
And let's say that we are going to write a thousand bytes of data on the same file using a write function.
The same thing we are doing in program 1 and program 2.
Now scheduler schedules.
Each program 1 and program 2 according to the scheduling algorithm.
Let say that scheduler has scheduled a program 1 and program 1 has completed the first line.
And it has opened the file in read write mode.
Now, before writing to the file, let's say that scheduler time was expired and the scheduler has given
program 2 the CPU time.
And now the second program has opened the same file in read write mode, and it is about to write 1000
bytes of data onto the file.
Assume that.
Step one.
Step two, step three is finished in program 2.
Now the CPU time is given back to the program 1.
And program 1 starts at the point from where it had left earlier.
So it starts the execution from step two, and now it starts to write buffer of 1000 bytes.
But previously the program 2 has already written a thousand bytes of data onto the log file.
But now program 1 rewrites or overrides a new set of 1000 bytes onto the log.txt file.
So as a result, there is a race between program one and program two to access the log.txt which
is a common resource.
And the output of this would be unpredictable, and it depends upon the scheduler scheduling the process.
So in the same way, let's take one more example of race condition.
Let us say that the goal of the program is to create a file, basically to check a file for its existence,
and if the file is not present, then create a file and print whether it has created the file successfully
or not.
So now we have two identical programs running parallel.
Now let's assume that program one opens a file in read write mode and checks whether a file is present
or not.
And now if open system call fails, then it has to create a file.
And after creating a file, it prints file creation success.
Now let's say that the scheduler has given program one the scheduling time, and it has performed open
system call and checked if the log.txt file exists or not.
Before processing to the next system call.
Let's say that the scheduler has given the scheduling time to program two and the.
And the dotted line says that the program is waiting for the CPU, whereas the hard line says that it
is in executing mode and the CPU is executing the program.
Now, the program 2 which is waiting has got the CPU time.
And it has executed and it has checked for the file that needs to be created.
And now the file fails and now the open fails.
In this case, because the file does not exist and it goes forward and opens a file and it creates the
file because of O_CREAT flag.
And then after creation of the file, it returns success to the.
Success to program 2, saying that the program 2 has created the new file.
Now, after printing success, the scheduler has given scheduling time back to program 1.
Now the execution starts from this point where it is about to create.
A file.
So the open system call here fails because the file is already created, even though open system call
fails.
Since we have not given a O_EXECL, the open system call here returns a positive value
and prints successful message in program 1.
so the user who is running both the program.
Parallely is in a feeling that both the programs has created the required file.
Program two also says that it has created the new file, and program one also says that it has created
the new file.
But the fact is, program 2 has created the new file and program 1 has not created the new file.
So this is a problem of race condition where both are claiming to have created the new file.
Thank you.
In the next video session, we will study about atomicity.
Thank you.