Nonblocking I/O

What really are descriptors?

Creation and Release of Descriptors

Close-on-exec

Chapter 7. I/O System Overview, from the book Design and Implementation of the FreeBSD Operating System. Page 315

File Entry

Fork/Dup and File Entries

#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main(char *argv[]) {
int fd = open("abc.txt", O_WRONLY | O_CREAT | O_TRUNC, 0666);
fork();
write(fd, "xyz", 3);
printf("%ld\n", lseek(fd, 0, SEEK_CUR));
close(fd);
return 0;
}
3
6

Offset-per-descriptor

Anatomy of a File Entry

From the book the Linux Programming Interface — page 95

Non-Blocking descriptors

Readiness of Descriptors

Level Triggered

Edge Triggered

Multiplexing I/O on descriptors

Multiplexing I/O with Non-Blocking I/O

What happens to the descriptors?

What happens in the process?

What happens in the kernel?

What are the cons?

When it might make sense to use this approach?

Multiplexing I/O via Signal Driven I/O

What happens to the descriptors?

What happens in the proceess?

What happens in the kernel?

What are the cons of this approach?

When it might make sense to use this approach?

Multiplexing I/O via Polling I/O

What happens to the descriptors?

What happens in the process?

Select

int
select(
int nfds,
fd_set *restrict readfds,
fd_set *restrict writefds,
fd_set *restrict errorfds,
struct timeval *restrict timeout
);
int
select(
int nfds,
fd_set *readfds,
fd_set *writefds,
fd_set *exceptfds,
struct timeval *timeout
);

Poll

int poll(
struct pollfd fds[],
nfds_t nfds,
int timeout
);
int poll(
struct pollfd *fds,
nfds_t nfds,
int timeout
);

What happens in the kernel?

What are the cons of this approach?

When it might make sense to use this approach?

Kernel event polling on BSD

What happens to the descriptors?

What happens in the process?

What happens in the kernel?

Async I/O in POSIX

Conclusion

--

--

@copyconstruct on Twitter. views expressed on this blog are solely mine, not those of present or past employers.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Cindy Sridharan

@copyconstruct on Twitter. views expressed on this blog are solely mine, not those of present or past employers.