examples

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

eventfd.c (458B)


      1 #include <errno.h>
      2 #include <err.h>
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 #include <sys/eventfd.h>
      6 #include <unistd.h>
      7 
      8 void check_err(char const* msg)
      9 {
     10 	if (errno)
     11 		err(EXIT_FAILURE, "%s", msg);
     12 }
     13 
     14 int main()
     15 {
     16 	int fd = eventfd(0, 0);
     17 	check_err("eventfd");
     18 
     19 	if (!fork())
     20 	{
     21 		sleep(1);
     22 		eventfd_write(fd, 1);
     23 		check_err("eventfd_write");
     24 
     25 		exit(0);
     26 	}
     27 
     28 	eventfd_t out;
     29 	eventfd_read(fd, &out);
     30 	check_err("eventfd_read");
     31 
     32 	printf("%lu\n", out);
     33 }