Friday 15 June 2012

c - Get User Input without Blocking Endless loop -



c - Get User Input without Blocking Endless loop -

i have written simple c programme consists of endless loop counts upwards. during loop, user asked input- , here comes tricky part: loop should not blocked while waiting on user, display input entered it:

int main(void){ int i; char dec; for(;;i++){ printf("%d\n", i); sleep(5); if(i==4 || i==8){ printf("please come in y or n\n"); dec = fgetc(stdin); printf("%c\n", dec); } } homecoming 0; }

i found similiar question python here python. need force user interaction new thread pthread or there easier option?

thanks!

edit

int main(void){ int i=0; char dec; fd_set input_set; for(;;i++){ printf("%d\n", i); sleep(2); if(i==4 || i==8){ fd_zero(&input_set ); /* empty fd set */ fd_set(0, &input_set); /* hear input descriptor */ dec = select(1, &input_set, null, null, 0); } } homecoming 0; }

what want possible scheme dependent libraries. instance on unix typically utilize ncurses user if have pressed button.

the reason scheme dependent asynchronous io not available file scheme streams. in particular user i/o blocks , block unavoidable.

if committed having multi-threaded programme still uses read/write scheme calls need have 2 threads, 1 i/o , 1 else. on else thread query shared memory area , see if i/o thread has written right type of info shared memory area.

c loops pthreads block user-input

No comments:

Post a Comment