examples

Toy examples in single C files.
git clone git://henryandlizzy.uk/examples
Log | Files | Refs

commit e745bc260c0b094e692e4dc198797e87fa9d33f6
parent b7acadc829f6d835137fd72de76689db9c9a91e7
Author: Henry Wilson <henry@henryandlizzy.uk>
Date:   Mon,  6 Feb 2023 21:57:57 +0000

eventfd: Add eventfd example - A better version of the pipe trick.

Diffstat:
M.gitignore | 1+
Asrc/eventfd.c | 33+++++++++++++++++++++++++++++++++
2 files changed, 34 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -22,6 +22,7 @@ /elastic-tabstops /enviro /epoll +/eventfd /flat-set /gl-asteroids /glob diff --git a/src/eventfd.c b/src/eventfd.c @@ -0,0 +1,33 @@ +#include <errno.h> +#include <err.h> +#include <stdio.h> +#include <stdlib.h> +#include <sys/eventfd.h> +#include <unistd.h> + +void check_err(char const* msg) +{ + if (errno) + err(EXIT_FAILURE, "%s", msg); +} + +int main() +{ + int fd = eventfd(0, 0); + check_err("eventfd"); + + if (!fork()) + { + sleep(1); + eventfd_write(fd, 1); + check_err("eventfd_write"); + + exit(0); + } + + eventfd_t out; + eventfd_read(fd, &out); + check_err("eventfd_read"); + + printf("%lu\n", out); +}