Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write system call and blocking the process

In UNIX: read system call blocks the process until it is done.

How does write system call behaves? does it block the process when it is writing on the disk?

With write system call I mean write(fd, bf, nbyte) procedure call.

like image 346
Feridun Akpinar Avatar asked May 26 '14 21:05

Feridun Akpinar


1 Answers

No, it only blocks the process until the content of the buffer is copied to kernel space. This is usually very short time, but there are some cases where it may wait for some disk operations:

  • If there are no free pages, some must be freed. If there are clean pages, their content can be discarded (as it is just copy from disk), but if there are not, some pages must be laundered, which involves write. Since pages are laundered automatically after few seconds, this almost never happens if you have enough memory.
  • If the write is to the middle of the file, the surrounding content may need to be read, because page cache has page granularity (aligned 4 KiB blocks on most platforms). This happens rarely because it is rare to update file without reading it and if you read it first, the content is cached already.

If you want to wait until the data actually hit the plates, you need to follow up with fsync(2).

like image 84
Jan Hudec Avatar answered Sep 17 '22 22:09

Jan Hudec