Scrivere un programma testeventfd che faccia uso della system call eventfd.
In particolare il programma deve eseguire una fork, quando l'utente digita un numero letto dal processo padre, il processo
figlio deve stampare un numero uguale di x.
#include <sys/eventfd.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
int main (int argc, char *argv[]) {
int efd, i;
long long unsigned int val;
ssize_t s;
/* Check for eventfd error */
efd = eventfd(0, 0);
if (efd == -1) {
perror("Eventfd Error");
}
/* Use fork to move eventfd form parent to child */
switch (fork()) {
case 0:
/* Read parent event */
s = read(efd, &val, sizeof(long long unsigned int));
if (s != sizeof(long long unsigned int)) {
perror("Read Error");
exit(EXIT_FAILURE);
}
/* For is used to print the 'x' of the argument passed by terminal */
for (i = (int)val; i > 0; i--) {
printf ("x\n");
}
exit(EXIT_SUCCESS);
/* Check for error */
case -1:
perror("Fork Error");
exit(EXIT_FAILURE);
default:
/* Wait for number */
printf ("Inserisci un intero diverso da 0: \n");
scanf ("%llu", &val);
/* Write parent event */
s = write(efd, &val, sizeof(long long unsigned int));
if (s != sizeof(long long unsigned int)) {
perror("Write Error");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
}