|
Home > Archive > Unix Programming > September 2004 > select () on pseudo-terminal
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
select () on pseudo-terminal
|
|
| cherico 2004-09-02, 6:50 pm |
| I've got a simple example of pseudo-terminal.
The child process printed some message and then
sent it to the parent process throught pseudo-terminal.
But the select() on parent side didn't return, seeming
to be blocked.
Anyone knows what the problem is.
#include <stdio.h>
#include <unistd.h>
#include <pty.h>
#include <sys/select.h>
int main ()
{
int md ; // master descriptor
int pid ;
if ( ( pid = forkpty ( & md, NULL, NULL, NULL ) ) == 0 )
{
// child process
printf ( "this is child\n" );
}
else
{
// parent process
fd_set readset ;
FD_ZERO ( & readset ) ;
FD_SET ( md, & readset ) ;
char c [ 128 ] ; c [ 127 ] = 0 ;
int n, r ;
while (1)
{
r = select ( 2, & readset, NULL, NULL, NULL ) ;
// BLOCK HERE
n = read ( md , ( void * ) c, 128 ) ;
printf ( "%d %s\n", n, c ) ;
}
}
}
| |
| Jens.Toerring@physik.fu-berlin.de 2004-09-02, 6:50 pm |
| cherico <cherico@bonbon.net> wrote:
> I've got a simple example of pseudo-terminal.
> The child process printed some message and then
> sent it to the parent process throught pseudo-terminal.
> But the select() on parent side didn't return, seeming
> to be blocked.
> Anyone knows what the problem is.
> r = select ( 2, & readset, NULL, NULL, NULL ) ;
I never used forkpty() but if 'md' is the file descriptor you want
to read from you need here
r = select ( md + 1, & readset, NULL, NULL, NULL ) ;
Regards, Jens
--
\ Jens Thoms Toerring ___ Jens.Toerring@physik.fu-berlin.de
\__________________________ http://www.toerring.de
| |
| Michael Kerrisk 2004-09-02, 6:51 pm |
| On 30 Aug 2004 18:00:07 GMT, Jens.Toerring@physik.fu-berlin.de wrote:
>cherico <cherico@bonbon.net> wrote:
>
>
>
>
>I never used forkpty() but if 'md' is the file descriptor you want
>to read from you need here
>
> r = select ( md + 1, & readset, NULL, NULL, NULL ) ;
>
> Regards, Jens
Yes. Also, the FD_ZERO and FD_SET must be moved inside the loop.
select() *changes* the descriptor sets that it is passed.
Cheers,
Michael
| |
| David Schwartz 2004-09-02, 6:51 pm |
|
"cherico" <cherico@bonbon.net> wrote in message
news:afc115ec.0408300919.a7e5fd5@posting.google.com...
> r = select ( 2, & readset, NULL, NULL, NULL ) ;
Where did that '2' come from? It should be 'md+1'.
DS
|
|
|
|
|