Using user-buffered I/O for File operations

288 views Asked by At

I am a newbie to system programming please mind me if my doubt is very vague.

I read that inbuilt user-space buffers are used so that we can access block sized data through a system call via the kernel which takes a huge over head but in the user-space we can make minute access to small sized data.I understood how this method is efficient but what i did not understand is,since these user-buffers are pertained to each single process that opens the file.

How will a process recognize the minute changes that are made to a file when both are simultaneously accessing the file.

Does this not create a problem as processes will access the old data but not the data that is changed by some other process which is still in the user-space buffer.

Please mind me if any mistakes.

1

There are 1 answers

2
randomusername On BEST ANSWER

Yes, this happens. Inter-process race conditions are present, and can create weird behavior when IO is buffered. Generally though, input isn't buffered that much (usually only a line at a time) so it isn't that bad. There are ways to get around it though through a function called mmap.

In some cases, you want multiple process to communicate so we use pipes/sockets or some other form of IPC (inter-process communication).

Although, to answer what seems to be your main problem: no, this doesn't really create a big problem. Why are two processes operating on the same file if they aren't meant to be aware of one another while they do it? It just isn't that common, and when it is, something weird happens and the user instinctively runs one program at a time and it's fixed.