In this video, we'll look at what happens when fclose is called.
A lot of this is conceptualized because we're not actually going to
delve into the details of what the kernel does or how it interacts with the hardware.
The important thing to understand here
is that our program may have buffered up these writes,
it may be holding them in the file structure,
the C library manages and may not
have actually written them down to the Operating System yet.
Therefore, they're not actually on the disk.
However, when we close the file,
these writes have to actually be propagated down.
So, the first thing that's going to happen if the C library has unwritten data,
is it's going to make a system call to write that into the Operating System.
Here, we see the write system call,
which takes a file descriptor,
the low level communication mechanism to
specify which file between a program and the kernel.
In this case, we're going to assume it's 3.
We wouldn't really know unless we looked in
more detail to figure it out and we don't really care at this point.
The write system call also takes a pointer to the data to write,
as well as how many bytes to write.
In this case, it's 12,
because there are three lines each with four bytes.
Each of these have three numerical digits plus the new line character.
So, this data is going to be written into the operating system's memory.
You'll notice I've written it over here as the actual numerical values of
the bytes rather than the textual representation of the digits.
Just as a reminder that everything is
a number and because this is really just dealing with raw data,
we're not really thinking of it as text anymore.
It's just a bunch of bytes going to a disk.
Next, the C library is going to make the close system call,
passing in the file descriptor because
that's how it communicates with the operating system.
Then the operating system is going to
decide that it should write its buffered data to the disk.
So it's going to write all of these bytes onto the disk somewhere.
Then the disk is going to tell the operating system that it actually succeeded.
It's possible that this fails,
maybe some piece of the disk is gone bad,
in which case it wouldn't be able to write the data,
but let's assume that it succeeds.
So the kernel is going to recognize that this file is closed
and also tell the program that it succeeded in closing the file by returning 0,
which is going to be the return value of fclose.
At this point, the file is closed so we can't do anything else with
it and our call to fclose will finish and will continue executing code.