Reputation: 149
When we open a file using fopen() in C(Ubuntu platform and gcc compiler) and write to it,does the content is directly written to the hard disk address where the file resides or is it first brought into primary memory? What is the actual process with which a file could be written or read from its location in hard disk through a C program in Linux.
Upvotes: 1
Views: 213
Reputation: 46027
The C library does not make the actual write to disk. It is the job of operating system. C library will make a system call to kernel to write it to the disk. It may even implement a buffer to minimize the number of system calls. And kernel also implement buffer to optimize real writing to disk. In general when you are working with C you don't think this much low level. However, you need to ensure that you have closed the file correctly. The actual disk management is the job of OS.
The Design of the UNIX Operating System by Maurice J. Bach contains nice explanation of Unix kernel. You may have a look as a beginning.
Upvotes: 2
Reputation: 881113
Under UNIX-like systems, generally, there are two levels of caching when writing information to a file on disk.
The first is in the C run time libraries, where it's likely to be buffered (unless you turn off buffering in some manner). You can use a C call like fflush
to flush these buffers.
The second is at the operating system level, where buffers are held, before being written to the physical disk. A call to fsync
can force these buffers to be flushed to disk.
Upvotes: 1