1 #include <err.h> 2 #include <inttypes.h> 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <sys/eventfd.h> 6 #include <sys/types.h> 7 #include <unistd.h> 8 9 int 10 main(int argc, char *argv[]) 11 { 12 int efd; 13 uint64_t u; 14 ssize_t s; 15 16 if (argc < 2) { 17 fprintf(stderr, "Usage: %s <num>...\n", argv[0]); 18 exit(EXIT_FAILURE); 19 } 20 21 efd = eventfd(0, 0); 22 if (efd == -1) 23 err(EXIT_FAILURE, "eventfd"); 24 25 switch (fork()) { 26 case 0: 27 for (size_t j = 1; j < argc; j++) { 28 printf("Child writing %s to efd\n", argv[j]); 29 u = strtoull(argv[j], NULL, 0); 30 /* strtoull() allows various bases */ 31 s = write(efd, &u, sizeof(uint64_t)); 32 if (s != sizeof(uint64_t)) 33 err(EXIT_FAILURE, "write"); 34 } 35 printf("Child completed write loop\n"); 36 37 exit(EXIT_SUCCESS); 38 39 default: 40 sleep(2); 41 42 printf("Parent about to read\n"); 43 s = read(efd, &u, sizeof(uint64_t)); 44 if (s != sizeof(uint64_t)) 45 err(EXIT_FAILURE, "read"); 46 printf("Parent read %"PRIu64" (%#"PRIx64") from efd\n", u, u); 47 exit(EXIT_SUCCESS); 48 49 case -1: 50 err(EXIT_FAILURE, "fork"); 51 } 52 }