Difference between revisions of "Esercizio 1, prova pratica 29.05.2013"
Jump to navigation
Jump to search
Line 10: | Line 10: | ||
#include <stdlib.h> | #include <stdlib.h> | ||
#include <stdio.h> | #include <stdio.h> | ||
+ | #include <stdint.h> | ||
int main (int argc, char *argv[]) { | int main (int argc, char *argv[]) { | ||
− | int efd,i | + | int efd, i; |
− | + | long long unsigned int val; | |
− | + | ssize_t s; | |
− | |||
− | |||
− | |||
/* Check for eventfd error */ | /* Check for eventfd error */ | ||
Line 29: | Line 27: | ||
switch (fork()) { | switch (fork()) { | ||
− | |||
case 0: | 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); | exit(EXIT_SUCCESS); | ||
Line 49: | Line 48: | ||
exit(EXIT_FAILURE); | exit(EXIT_FAILURE); | ||
− | |||
default: | default: | ||
− | + | /* Wait for number */ | |
− | exit(EXIT_SUCCESS); | + | 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); | ||
} | } | ||
} | } | ||
− | |||
</source> | </source> |
Revision as of 20:45, 23 March 2015
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.
Soluzione di Pierg
#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);
}
}