5

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.

1 Answer 1

9

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).

Sign up to request clarification or add additional context in comments.

1 Comment

I'm glad you mentioned that it "blocks the process until the content of the buffer is copied to kernel space"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.