ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
void TTYCook(int) ;
int main() {
int c ;
TTYCook( 0 ) ;
while (1) {
c = getchar() ;
if (c != -1) break ;
printf( "X\n" ) ;
}
printf( "done\n" ) ;
TTYCook( 1 ) ;
return 0 ;
}
void TTYCook(int yes) {
struct termios ttystate ;
int f ;
tcgetattr( STDIN_FILENO,&ttystate ) ;
f = fcntl( STDIN_FILENO,F_GETFL,0 ) ;
if (yes) {
ttystate.c_lflag |= ICANON ;
ttystate.c_lflag |= ECHO ;
f &= ~O_NONBLOCK ;
} else {
ttystate.c_lflag &= ~ICANON ;
ttystate.c_lflag &= ~ECHO ;
f |= O_NONBLOCK ;
}
fcntl( STDIN_FILENO,F_SETFL,f ) ;
tcsetattr( STDIN_FILENO,TCSANOW,&ttystate ) ;
}
It confuses me that pressing a key either is ignored, or has heavy lag.
I understand it's a busy loop, but input is checked on every single iteration.
My understanding is that every keypress is stored in memory until you take it out with getchar, so a character should be waiting in that buffer after I hit a key.
this program works fine when I replaced the
Code:
printf( "X\n" )
with a call
to a heavy function that takes .3 seconds to compute. All that happened in that case
was that (assuming I pressed a key immediately) the program took either .3 or .6 seconds to stop.
To me that means that there must be something more to the terminal in/out stuff I don't know that would explain this behaviour, I believe that understanding/solving this problem would lead to valuable knowledge for myself.
please let me know if I wasn't clear enough about what I'm asking for help with, thx
O_NONBLOCK means don't block. If you never block, then nothing else can happen on the CPU while your program is running. I would guess your CPU is at 100% while in the loop.
smallpond: I appreciate the feedback, but would you mind explaining more clearly what you're trying to tell me ?
NevemTeve: I tried the code, with the exception of that read function 'read(handle=0)' not sure why you're setting a variable inside of a function I only found one function that was close that is syntax
Code:
ssize_t read(int,void*,size_t)
and when I tried it out the input was blocking so It didn't get me to the solution I was looking for.
ntubski: I would really love to find out what the differences are for our circumstances. I ran
Code:
gcc test.c
Code:
./a.out
with gcc 4.9.1-1
on
xterm 309-1
ummm this is my output of 'stty'
I would ask for cpuinfo stuff, but I'm really hoping that your circumstances are ones that I can duplicate
I'm not sure what other settings would count, but just to verify, you compiled that code, and when it ran it printed X's in an infinite loop, and when you hit a key it stopped immediately ?
NevemTeve: i will try it with your read function again once i get the chance God willing. As for the purpose, it was just a test program and this post was to find out why it didnt work the way i expected it to.
ntubski: that is very funny i actually kept thinking "what if hes using a different terminal like urxvt ?"
your post is very helpful, at least i have a chance to find out what might be the cause in difference of behaviour.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.