Ho risolto! Avevo sbagliato io a fare lo scanf, non passando il puntatore!
Codice PHP:
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
int main(void) {
fd_set rfds;
struct timeval tv;
int retval;
/* Watch stdin (fd 0) to see when it has input. */
FD_ZERO(&rfds);
FD_SET(fileno(stdin), &rfds);
/* Wait up to five seconds. */
tv.tv_sec = 5;
tv.tv_usec = 0;
retval = select(1, &rfds, NULL, NULL, &tv);
/* Don't rely on the value of tv now! */
int n;
if (retval == -1)
perror("select()");
else if (retval) {
printf("Data is available now.\n");
/* FD_ISSET(0, &rfds) will be true. */
scanf("%d", &n);
}
else {
printf("No data within five seconds.\n");
n=0;
}
printf("n=%d\n", n);
return 0;
}
p.s. ora sorge un altro problema: se ho inserito già dei caratteri ma non ho fatto invio nei 5 secondi, vorrei "svuotare il buffer". Come si può fare?