LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 12-15-2008, 09:58 PM   #1
john.daker
Member
 
Registered: Jul 2008
Posts: 33

Rep: Reputation: 15
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.
 
Old 12-17-2008, 09:00 PM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
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, &oldsizeof(old));

tmp.c_lflag &= ~ICANON;

if (
tcsetattr(ttyTCSANOW, (const struct termios*) &tmp))
{
  
perror("failed to configure tty descriptor");
}

// perform read()
...

// restore tty settings (optional?)
tcsetattr(ttyTCSANOW, (const struct termios*) &old);

... 
 
Old 12-18-2008, 01:31 PM   #3
mastemmer
Member
 
Registered: Jul 2007
Location: Porto Alegre, Brazil
Distribution: Slackware
Posts: 44

Rep: Reputation: 17
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/
 
Old 12-22-2008, 09:31 PM   #4
bytehunter
LQ Newbie
 
Registered: Jul 2008
Distribution: RHEL4
Posts: 8

Rep: Reputation: 1
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, &params);

// Modify terminal attributes
cfmakeraw(&params);

rc = cfsetispeed(&params, B9600);

rc = cfsetospeed(&params, 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, &params);

// Flush serial device of both non-transmitted
// output data and non-read input data....
tcflush(serialDev, TCIOFLUSH);
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Pseudo Terminal Problem gabsik Linux - Software 8 07-25-2008 05:00 AM
Pseudo terminal programming Alien_Hominid Programming 5 01-20-2008 04:19 AM
pseudo terminal - scp problem vdx Programming 9 08-22-2007 03:37 AM
Switch to pseudo terminal you didn't log out of? xnomad Linux - General 2 07-19-2005 12:35 AM
Pseudo Terminal Problems ailiez Programming 2 02-09-2004 01:58 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration