non-blocking input

non-blocking input

Post by andrear.. » Tue, 24 Oct 2000 04:00:00



I'm a bit of a newbie in this, so this could be a stupid
question.  I'm writing a program (in C) that needs to
check to see if the user typed a character to the screen,
without waiting for that character if it isn't there.
So, while I'm doing a bunch of stuff in a while(1) type
of loop, the user could type 'q' to the screen, and at
a certain spot in the loop, the program would check to
see if the user typed 'q'.  If the user didn't type anything,
I would want it to continue on as if nothing happened.
Is this possible?  I was told to mess around with the termios
settings, but I haven't figured it out yet.

Thanks!

Sent via Deja.com http://www.deja.com/
Before you buy.

 
 
 

non-blocking input

Post by Derek M. Fly » Tue, 24 Oct 2000 04:00:00



> I'm a bit of a newbie in this, so this could be a stupid
> question.  I'm writing a program (in C) that needs to
> check to see if the user typed a character to the screen,
> without waiting for that character if it isn't there.
> So, while I'm doing a bunch of stuff in a while(1) type
> of loop, the user could type 'q' to the screen, and at
> a certain spot in the loop, the program would check to
> see if the user typed 'q'.  If the user didn't type anything,
> I would want it to continue on as if nothing happened.
> Is this possible?  I was told to mess around with the termios
> settings, but I haven't figured it out yet.

Use select or poll on the input file descriptor.

 
 
 

non-blocking input

Post by Miguel Arregu » Tue, 24 Oct 2000 04:00:00


#include <poll.h>

int main ( void ){
   int pollStatus = -1;
   struct pollfd inputStream [1];
   char c;

   inputStream [0].events = POLLRDNORM;
   inputStream [0].revents = 0;
   inputStream [0].fd = 0;

   while ( 1 ){
       pollStatus = poll (inputStream, 1, 0);
       if (pollStatus == -1) exit ( -1 );
       if (pollStatus){
           read (0, &c, 1);
           write (1, &c, 1);
           inputStream [0].revents = 0;
       }
   }  
   exit ( 0 );

Quote:}

This avoids using a blocking READ statement. The user will
still have to hit ENTER to enter input of any size.

#> I'm a bit of a newbie in this, so this could be a stupid
#> question.  I'm writing a program (in C) that needs to
#> check to see if the user typed a character to the screen,
#> without waiting for that character if it isn't there.
#> So, while I'm doing a bunch of stuff in a while(1) type
#> of loop, the user could type 'q' to the screen, and at
#> a certain spot in the loop, the program would check to
#> see if the user typed 'q'.  If the user didn't type anything,
#> I would want it to continue on as if nothing happened.
#> Is this possible?  I was told to mess around with the termios
#> settings, but I haven't figured it out yet.
#
#Use select or poll on the input file descriptor.
#
#

 
 
 

non-blocking input

Post by Andrew Giert » Tue, 24 Oct 2000 04:00:00


 Miguel>    inputStream [0].events = POLLRDNORM;
 Miguel>    inputStream [0].revents = 0;
 Miguel>    inputStream [0].fd = 0;

 Miguel>    while ( 1 ){
 Miguel>        pollStatus = poll (inputStream, 1, 0);
 [...]
 Miguel> This avoids using a blocking READ statement. The user will
 Miguel> still have to hit ENTER to enter input of any size.

The user will still have to hit ENTER in order for poll() to return
that input is ready, unless you also take the terminal out of
canonical input mode. see FAQ.

--
Andrew.

comp.unix.programmer FAQ: see <URL: http://www.erlenstar.demon.co.uk/unix/>
                           or <URL: http://www.whitefang.com/unix/>

 
 
 

non-blocking input

Post by Tony R. Benne » Wed, 25 Oct 2000 04:00:00



>I'm a bit of a newbie in this, so this could be a stupid
>question.  I'm writing a program (in C) that needs to
>check to see if the user typed a character to the screen,
>without waiting for that character if it isn't there.
>So, while I'm doing a bunch of stuff in a while(1) type
>of loop, the user could type 'q' to the screen, and at
>a certain spot in the loop, the program would check to
>see if the user typed 'q'.  If the user didn't type anything,
>I would want it to continue on as if nothing happened.
>Is this possible?  I was told to mess around with the termios
>settings, but I haven't figured it out yet.

>Thanks!

>Sent via Deja.com http://www.deja.com/
>Before you buy.

From the FAQ:

3.2 How can I read single characters from the terminal?
 =======================================================

     How can I read single characters from the terminal? My program is
 always waiting for the user to press `<RETURN>'.

 Terminals are usually in canonical mode, where input is read in lines
after it is edited. You may set this into non-canonical mode, where you
set how many characters should be read before
 input is given to your program. You also may set the timer in
non-canonical mode terminals to 0, this timer flushs your buffer at set
intervals. By doing this, you can use `getc()' to grab the
 key pressed immediately by the user. We use `tcgetattr()' and
`tcsetattr()' both of which are defined by POSIX to manipulate the
`termios' structure.

     #include <stdlib.h>
     #include <stdio.h>

     #include <termios.h>
     #include <string.h>

     static struct termios stored_settings;

     void set_keypress(void)
     {
         struct termios new_settings;

         tcgetattr(0,&stored_settings);

         new_settings = stored_settings;

         /* Disable canonical mode, and set buffer size to 1 byte */
   new_settings.c_lflag &= (~ICANON);
         new_settings.c_cc[VTIME] = 0;
         new_settings.c_cc[VMIN] = 1;

         tcsetattr(0,TCSANOW,&new_settings);
         return;
     }

     void reset_keypress(void)
     {
         tcsetattr(0,TCSANOW,&stored_settings);
         return;
     }

 3.3 How can I check and see if a key was pressed?
 =================================================

     How can I check and see if a key was pressed? On DOS I use the
`kbhit()' function, but there doesn't seem to be an equivalent?

 If you set the terminal to single-character mode (see previous answer),
then (on most systems) you can use `select()' or `poll()' to test for
readability.

--


 
 
 

1. Non-blocking input after a select call

    Hello,
I am writing a little program that starts a five second timer
via a select call and want it to where during that time a one
keypress input can be received which would cause the program
to exit(). If by the end of the five seconds a key isn't pressed
a system() call is made then the program terminates normally,
but as it stands now if I make an input call after the select()
call the timer will continue to run and won't terminate after
the timeout unless I remove the input call. I'm trying to figure
out how I can make it to where the timer is non-blocked along
with the input being non-blocking. If somebody has an idea what
I'm talking about,a few clues in how I need to go about acheiving
this would be appreciated.

Curtiss Cicco

2. DE450 and the 21041 chipset

3. Non-Blocking Input Function

4. Network not accessible after booting

5. non-blocking input

6. arp message?

7. Problems with non-blocking input read

8. auth. via user-defined page

9. Non-blocking Input

10. Non- Blocking Input

11. non blocking input from terminal devices with C++ ifstream

12. Non- Blocking Input

13. Non-blocking I/O from C/Unix standard input