linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Marco Roeland <marco.roeland@xs4all.nl>
To: Linux Kernel Development <linux-kernel@vger.kernel.org>
Cc: Eli Barzilay <eli@barzilay.org>
Subject: Re: Repost: Bug with select?
Date: Fri, 25 Jul 2003 15:41:55 +0200	[thread overview]
Message-ID: <20030725134155.GA19211@localhost> (raw)
In-Reply-To: <16159.28266.868297.372200@mojave.cs.cornell.edu>

On Thursday July 24th 2003 at 01:28 uur Eli Barzilay wrote:

> When I run the following program, and block the terminal's output
> (C-s), the `select' doesn't seem to have any effect, resulting in a
> 100% cpu usage (this is on a RH8, with 2.4.18).  I wouldn't be
> surprised if I'm doing something stupid, but it does seem to work fine
> on Solaris.
> 
> Is there anything wrong with this, or is this some bug?
> 
> ======================================================================
> #include <unistd.h>
> #include <fcntl.h>
> int main() {
>   int flags, fd, len; fd_set writefds;
>   fd = 1;
>   flags = fcntl(fd, F_GETFL, 0);
>   fcntl(fd, F_SETFL, flags | O_NONBLOCK);

You use non-blocking mode here.

>   while (1) {
>     FD_ZERO(&writefds);
>     FD_SET(fd, &writefds);
>     len = select(fd + 1, NULL, &writefds, NULL, NULL);

A select with no timeout, so it will immediately return.

>     if (!FD_ISSET(fd,&writefds)) exit(0);

This might be what Solaris does differently, by _not_ including '1' in
the returned descriptors? Linux will say (rightly) that a following call
will not block, which is something very different than 'will not fail'!

>     len = write(fd, "hi\n", 3);

You don't check the exit status here, but when you press Ctrl-C (stdout
blocked) it will indicate an error here (exit status -1) with errno set
to EAGAIN, meaning you should try again, which is the appropriate result
for a non-blocking descriptor or socket here. Anyway, the call "succeeds" and
we loop back into the while(1), indeed as you say creating a busy loop.
No surprises there I'd say.

>   }
>   fcntl(fd, F_SETFL, flags);
> }

You might start by checking for EAGAIN as result of the write, and then
reacting according to your needs (waiting a while or exiting the
program or whatever).

  reply	other threads:[~2003-07-25 13:26 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-06-18  8:49 Problem " Eli Barzilay
2003-07-24  5:28 ` Repost: Bug " Eli Barzilay
2003-07-25 13:41   ` Marco Roeland [this message]
2003-07-26  0:20     ` Ben Greear
2003-07-26  9:05       ` Marco Roeland
2003-07-26  0:35   ` Philippe Troin
2003-07-26 14:29     ` Eli Barzilay
2003-07-26 14:25   ` Eli Barzilay
2003-07-26 15:37     ` Marco Roeland
2003-07-27 19:29 Manfred Spraul

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20030725134155.GA19211@localhost \
    --to=marco.roeland@xs4all.nl \
    --cc=eli@barzilay.org \
    --cc=linux-kernel@vger.kernel.org \
    --subject='Re: Repost: Bug with select?' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).