1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
int main(void)
{
fd_set rfds,newrfds;
struct timeval tv;
int retval;
/* On regarde l'entree standard (fd 0). */
FD_ZERO(&rfds);
FD_SET(0, &rfds);
/* On attend 5 seconde, a mettre a 0 pour un select non bloquant*/
tv.tv_sec = 5;
tv.tv_usec = 0;
/* On copie rfds car select le modifie */
newrfds = rfds;
/* Le premier argument et le maximum des descripteurs +1 */
retval = select(1, &newrfds, NULL, NULL, &tv);
/* tv a pu etre modifie, remettre a jour si on reutilise select!!! */
/* S'il y a une erreur */
if (retval == -1)
perror("select()");
else
/* Si l'utilisateur a ecrit qq chose, REMARQUE: IL DOIT AVOIR APPUYE SUR ENTREE! */
if (retval)
{
printf("Data is available now.\n");
/* FD_ISSET(0,&newrfds) serait vrai, mais vu que c'est le seul descripteur, on n'a pas besoin de le tester */
//pour vider l'entrée standard
while((int) '\n'!= getchar())
;
}
else
printf("No data within five seconds.\n");
return 0;
} |
Partager