Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

program waits before the select system call

1 view
Skip to first unread message

Ajay

unread,
Dec 17, 2003, 8:29:44 AM12/17/03
to
Hi,

I have a sample program in which I want to read one character at a
time from the stdin. I am using to select() system call to detect in
there is a input on stdin and am just want to read the input till the
user presses the enter key.

The problem is that when I run , my program, it just read the first
character of whatever i type on stdin and then it just hangs before
select(). Now if I press enter key once more, I am able to see rest
all of the character.

For eg: if I enter abcd on stdin . my program reads only "a" and then
just hangs before select. Now if I press the enter key once more, it
reads all the rest of the characters.
It is a short program. If anyone can help me out whats going wrong it
will be
very helpful. I ran this program on AIX433 and solaris using gcc.

To compile my test program test.c
g++ -fPIC test.c

Here is my sample program test.c

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

void TST_GetInput( char* aosBuffer);
int tstGetInput ( fd_set *fdSet );
const char gcReturn = '\r';

int main()
{

char input_buffer[512];
TST_GetInput(input_buffer);
}

void TST_GetInput( char* aosBuffer)
{
int doneInput = 0;
int inputChar;
fd_set fdSet;
FD_ZERO( &fdSet );
while( ! doneInput )
{
inputChar = tstGetInput( &fdSet );
if( isprint(inputChar))
{
printf("\n the charcter is %c\n ", inputChar);
}
else
{
switch( inputChar )
case gcReturn:
doneInput = 1;
}
}
}

int tstGetInput ( fd_set *fdSet )
{
int inputFd = fileno( stdin ),
maxFd,
selectRc,
lockResult,
cancelState;
maxFd = inputFd+1 ;
FD_SET( inputFd, fdSet );
printf("\n Select about to be blocked\n");
selectRc = select( maxFd, fdSet, 0,0,0);
if( ( selectRc > 0 ) &&
FD_ISSET( inputFd, fdSet ) )
{
selectRc = getc(stdin);
}
printf("\n in tstGetInput char is %c\n",selectRc);
return selectRc;
}

0 new messages