io_trywrite(3)
π io_trywrite β a lil manual page, section 3, from the Library Functions Manual π
okay bestie sit DOWN because we're about to talk about writing to a file descriptor without ever, ever blocking. no waiting. no pausing. just vibes and bytes. π¦
πΈ NAME πΈ
io_trywrite β write to a descriptor without blocking
β¨ SYNTAX β¨
#include <libowfat/io.h>
int io_trywrite(int64 fd,const char* buf,int64 len);
π DESCRIPTION π
so io_trywrite tries to write len bytes of data from buf[0], buf[1], ..., buf[len-1] over to descriptor fd. π«Ά
β οΈ heads up queen: the effects are undefined iflenis0or smaller. do not do that. we do not do that.
and listen, there are several possible outcomes here, so let's go through them one by one like the group chat debrief:
io_trywritereturns an integer between1andlenβ this number of bytes was immediately written from the beginning ofbuf. and ngl, this number can be, and often is, smaller thanlen. do not assumeio_trywritealways succeeds in writing exactlylenbytes. it's giving "partial write, deal with it". πio_trywritereturns-1, settingerrnotoEAGAINβ zero bytes were written, because the descriptor is not ready. for example, the descriptor is writing to a full pipe that could still be read. not a fail, just a "not right now babe". πio_trywritereturns-3, settingerrnoto something other thanEAGAINβ zero bytes were written, because the write attempt hit a persistent error. think a serious disk failure (EIO), an unreachable network (ENETUNREACH), or an invalid descriptor number (EBADF). that's a real problem, not a timing thing. π
io_trywrite does not pause waiting for a descriptor that is not ready. it will not sit there. it will not wait up for you. if you actually want to pause, use io_waitread or io_wait. β¨
you can make io_trywrite faster and more efficient by making the socket non-blocking with io_nonblock(). obsessed with an optimization. π₯
π a lil history lesson (true story, no cap) π
once upon a time, many UNIX programs neglected to check the success of their writes. they would often encounter EPIPE, and would blithely continue writing, rather than exiting with an appropriate exit code. like. bestie. check your return values. π
so the UNIX kernel developers decided to send a SIGPIPE signal, which terminates the process by default, along with returning EPIPE. this papers over the problem without fixing it: the same programs ignore other errors such as EIO. one hopes that the programs have been fixed by now; kernels nevertheless continue to generate the SIGPIPE signal. π
the first time io_trywrite or io_waitwrite is called, it arranges for SIGPIPE to be ignored. (technically, for SIGPIPE to be caught by an empty signal handler, so this doesn't affect child processes.) so like, heads up:
π¨ Do not use SIGPIPE elsewhere in the program. it's taken. it's spoken for. main character energy only. π
π SEE ALSO π
and that's the whole page, bestie!! π§ this lil manual page is io_trywrite(3) from the Library Functions Manual β section 3, library functions, the one where all the good stuff lives. mwah. go write some bytes. ππΈβ¨
π¦ where this page comes from
- alpine 3.24: libowfat-doc 0.34-r0 Β· GPL-2.0-only
- debian bookworm: libowfat-dev 0.32-4 Β· GPL-2.0-only
- debian trixie: libowfat-dev 0.32-5 Β· GPL-2.0-only
- nixos 26.05: libowfat 0.34
- nixpkgs unstable: libowfat 0.34
- opensuse-leap 16.0: libowfat-devel 0.33-bp160.1.11 Β· GPL-2.0-only
- opensuse-leap 16.0: libowfat-devel 0.33-bp160.1.11 Β· GPL-2.0-only
- opensuse-leap 16.0: libowfat-devel 0.33-bp160.1.11 Β· GPL-2.0-only
- opensuse-leap 16.0: libowfat-devel 0.33-bp160.1.11 Β· GPL-2.0-only
- ubuntu noble: libowfat-dev 0.32-4.1build1 Β· GPL-2.0-only
- ubuntu questing: libowfat-dev 0.32-5
- ubuntu resolute: libowfat-dev 0.34-2 Β· GPL-2.0-only
- ubuntu stonking: libowfat-dev 0.34-2 Β· GPL-2.0-only
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms