Write non block linux

Is the write() function in C blocking or non-blocking?

I looked on the Linux man pages for the answer but can’t seem to find it. I know that read() is blocking but I’m still not sure about write() . Can anyone point me to any documentation for clarification?

Yes, absolutely. If the write buffer is full, the write can block. You can certainly deadlock yourself with reads and writes. (The file descriptor would need to be made non-blocking explicitly to not suffer from this.)

Blocking would mean that write doesnt return till the write is finished. Define ‘finished’. It will return once it has done enough to allow the write operation to be completed at some point (ie it has the data and you can release the buffer), but is the data on disk, perhaps, is it in the remote machines TCP stack,doubtful

1 Answer 1

Read POSIX on read() and write() . See also functions such as open() and pipe() .

It depends on the attributes of the file descriptor you’re reading from or writing to (think O_NONBLOCK , for example), and on the underlying file type (disk file vs pipe vs FIFO vs socket vs character or block special), and so on.

Succinctly, both read() and write() can be blocking or non-blocking, depending on circumstances.

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Читайте также:  Gaming on linux guide

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.14.43533

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

non-blocking write in c

I have read some materials about socket programming online. By default, write() is blocking. In some materials, write() only blocks when socket buffer is full. Some other materials say write() is blocked until all the data in the user buffer has been moved to the system buffer, which means write() will also block if there is not enough space for the data to place. I am wondering which statement is correct if write() is set to blocking.

The Linux man pages suggest that this will write the amount of data that can be written to the buffer and will return after the data is written even if only part of the data could fit into the socket buffer, but I believe that it depends on the actual implementation of the particular system / socket.

The two behaviors of a blocking write that you describe sound identical to me. Can you elaborate on what the difference would be? Is there any experiment you could run that would demonstrate the difference?

2 Answers 2

In some materials, write() only blocks when socket buffer is full. Some other materials say write() is blocked until all the data in the user buffer has been moved to the system buffer, which means write() will also block if there is not enough space for the data to place.

It is not clear to me that these are actually saying anything different.

Читайте также:  Mozilla firefox linux через терминал

First, what one refers to as the «system buffer» is what the other refers to as the «socket buffer», but they are the same thing.

Secondly, when the first statement says «write() only blocks when socket buffer is full» this should be interpreted as «write() only blocks when the write operation would overflow the (system) buffer». This is the same as «write() is blocked until all the data in the user buffer has been moved to the system buffer»; it is just that one says that the condition will cause write() to block and the other says that the block will cease once the condition becomes false. This is not contradictory.

Источник

Оцените статью
Adblock
detector