networking · beginner · ~15 min
Add a flag bit with bitwise OR.
Add the O_NONBLOCK bit to an fd's flags — the bit you OR in before fcntl(fd, F_SETFL, ...).
Implement int add_nonblock(int flags, int o_nonblock).
flags: the current file-status flags.o_nonblock: the O_NONBLOCK bit value.Return flags with the o_nonblock bit OR-ed in.
add_nonblock(2, 2048) -> 2050
add_nonblock(2050, 2048) -> 2050 (already set: idempotent)
flags: current file-status flags; o_nonblock: the O_NONBLOCK bit.
flags with the o_nonblock bit OR-ed in.
Use bitwise OR; setting an already-set bit is a no-op.
int add_nonblock(int flags, int o_nonblock) {
/* TODO */
return flags;
}
Assuming read/write transmits all requested bytes at once; ignoring socket file descriptor cleanup; unhandled endianness conversions.
Short reads/writes; peer closing socket (recv returns 0); signal interruption (EINTR); non-blocking EAGAIN.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.