delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2010/07/14/20:45:12

X-Recipient: archive-cygwin AT delorie DOT com
X-SWARE-Spam-Status: No, hits=-0.7 required=5.0 tests=AWL,BAYES_40
X-Spam-Check-By: sourceware.org
Message-ID: <4C3E59E3.4050003@hones.org.uk>
Date: Thu, 15 Jul 2010 01:44:19 +0100
From: Cliff Hones <cliff AT hones DOT org DOT uk>
User-Agent: Thunderbird 2.0.0.24 (Windows/20100228)
MIME-Version: 1.0
To: cygwin AT cygwin DOT com
Subject: Problem with select() on console
X-Spam-Score: -0.0 (/) (dominator.watchfront.net.uk)
X-Spam-Report: dominator.watchfront.net.uk has scanned this email for spam. Results:- T_RP_MATCHES_RCVD=-0.01 (total -0.0, current threshold 4.0)
X-IsSubscribed: yes
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com

When select() is used to test for input availability on the standard
cygwin console in normal (cooked) mode, it indicates input is available
as soon as any key is pressed.  However, a call to read(0,...)
will (correctly) block until a terminating RETURN is entered.

select() should only indicate input is available when a call
to read would *not* block - ie when a read call will immediately
return at least one character or an error such as EOF.

The behaviour of the following test case illustrates this.  When run
in a console window typing a single key causes the program to wait
for the whole line.  When run under mintty or on Linux the
select() calls will continue to return no input until RETURN is
entered.

#include <stdio.h>
#include <stdlib.h>
#include <sys/io.h>
#include <sys/time.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <stdarg.h>

int main(void)
{
  fd_set rs;
  struct timeval tv;
  char buff[100];
  while (1)
  {
    sleep(1);
    printf("Calling select\n");
    FD_ZERO(&rs);
    FD_SET(0, &rs);
    tv.tv_sec = tv.tv_usec = 0;
    int k = select(1, &rs, NULL, NULL, &tv);
    if (k < 0)
      perror("Error calling select");
    else if (FD_ISSET(0, &rs))
    {
      printf("Input available\n");
      int n = read(0, buff, sizeof(buff));
      printf("read returned %d\n", n);
    }
  }
  return 0;
}

-- Cliff

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019