Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
12-15-2008, 09:58 PM
|
#1
|
Member
Registered: Jul 2008
Posts: 33
Rep:
|
How to set pseudo terminal non-blocking?
Hi,all
I'm writing a little program using pseudo terminal and I want non-blocking read from pty. So I first using select:
Code:
fd_set rfs;
FD_ZERO(&rfs);
FD_SET(tty,&rfs);
struct timeval tv;
tv.tv_sec=0;
tv.tv_usec=0;
if((select(tty+1,&rfs,NULL,NULL,&tv)>0)){
if(FD_ISSET(tty,&rfs)){
n=read(tty,buf,MAX_BUFFER);
....
But my program will hang, and I use gdb to attach the process found that it block in read().
I did a little googling and found that 'select() may give a
"false indication" of readability ... a read() would complete at the time select() was done, but later (when you actually issue the read())
it might block. '
in http://fixunix.com/unix/84142-portab...nal-input.html
Then I using non-blocking by turning on NON_BLOCKING bit :
Code:
pid=forkpty(&tty,NULL,NULL,&ws);
flag=fcntl(tty,F_GETFL,0);
flag|=O_NONBLOCK;
fcntl(tty,F_SETFL,flag);
.....
fd_set rfs;
FD_ZERO(&rfs);
FD_SET(tty,&rfs);
struct timeval tv;
tv.tv_sec=0;
tv.tv_usec=0;
if((select(tty+1,&rfs,NULL,NULL,&tv)>0)){
if(FD_ISSET(tty,&rfs)){
n=read(tty,buf,MAX_BUFFER);
if(n<=0&&errno==EAGAIN){
..........
But it will block anyway, so how to do a non-blocking read with pty?
Thanks.
Last edited by john.daker; 12-15-2008 at 10:42 PM.
|
|
|
12-17-2008, 09:00 PM
|
#2
|
Senior Member
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541
|
See if this will work for you:
PHP Code:
#include <termios.h>
#include <string.h>
...
struct termios old;
struct termios tmp;
if (tcgetattr(tty, &old))
{
perror("failed to obtain tty attributes");
}
memcpy(&tmp, &old, sizeof(old));
tmp.c_lflag &= ~ICANON;
if (tcsetattr(tty, TCSANOW, (const struct termios*) &tmp))
{
perror("failed to configure tty descriptor");
}
// perform read()
...
// restore tty settings (optional?)
tcsetattr(tty, TCSANOW, (const struct termios*) &old);
...
|
|
|
12-18-2008, 01:31 PM
|
#3
|
Member
Registered: Jul 2007
Location: Porto Alegre, Brazil
Distribution: Slackware
Posts: 44
Rep:
|
I use the following command to set the tty descriptor as non-blocking:
Code:
fcntl(tty, F_SETFL, FNDELAY);
Then you may use select to read only when there is data available:
Code:
char buffer[80];
size_t n;
fd_set myset;
FD_ZERO(&myset);
FD_SET(tty, &myset);
select(FD_SETSIZE, &myset, NULL, NULL, NULL);
if(n=read(tty,buffer, 80)<0) perror("reading");
You can restore normal blocking behavior with
Code:
fcntl(STDIN_FILENO, 0, FNDELAY);
If you are dealing with a console, (keyboard) maybe you want to change it to raw mode so that you can process every key as soon as you type it. Normally there is an input buffer that only sends data to the program when you press ENTER. To set standard input to raw mode use the following:
Code:
struct termios mytty, kbdios;
/* This function is called whenever the program terminates */
void back_to_normal(void)
{
/* Set console back to normal */
fcntl(STDIN_FILENO, 0, FNDELAY);
tcsetattr(STDIN_FILENO, TCSANOW, &kbdios);
}
void kbd_rawmode()
{
/* Set keyboard to raw mode */
tcgetattr(STDIN_FILENO, &kbdios);
atexit(back_to_normal); /* Restore tty mode when exiting */
memcpy(&mytty, &kbdios, sizeof(struct termios));
mytty.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
tcsetattr(STDIN_FILENO, TCSANOW, &mytty);
/* These calls make read to return imedately even when there is
no data in the input stream */
fcntl(STDIN_FILENO, F_SETFL, FNDELAY);
}
See a RS232 terminal the i wrote:
http://www.ee.pucrs.br/~stemmer/linux/ltser.c
Se also a nice tutorial in
http://www.easysw.com/~mike/serial/
|
|
|
12-22-2008, 09:31 PM
|
#4
|
LQ Newbie
Registered: Jul 2008
Distribution: RHEL4
Posts: 8
Rep:
|
RE: Non-Blocking PTY
I did the following for a pseudo-term I did, similar to what has been posted:
int rc=0;
struct termios params;
// Get terminal atributes
rc = tcgetattr(serialDev, ¶ms);
// Modify terminal attributes
cfmakeraw(¶ms);
rc = cfsetispeed(¶ms, B9600);
rc = cfsetospeed(¶ms, B9600);
// CREAD - Enable port to read data
// CLOCAL - Ignore modem control lines
params.c_cflag |= (CLOCAL|CREAD);
// Make Read Blocking
fcntl(serialDev, F_SETFL, 0);
// Set serial attributes
rc = tcsetattr(serialDev, TCSANOW, ¶ms);
// Flush serial device of both non-transmitted
// output data and non-read input data....
tcflush(serialDev, TCIOFLUSH);
|
|
|
All times are GMT -5. The time now is 10:19 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|