public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* Re: Typo in <sys/select.h>?
@ 2022-07-06 14:17 Lavrentiev, Anton (NIH/NLM/NCBI) [C]
  0 siblings, 0 replies; 15+ messages in thread
From: Lavrentiev, Anton (NIH/NLM/NCBI) [C] @ 2022-07-06 14:17 UTC (permalink / raw)
  To: The Cygwin Mailing List

> Remember that 64 is MAXIMUM_WAIT_OBJECTS for WaitForMultipleObjects(),
> the underlying Win32 API used to implement select(), so using more than
> 64 hits some complex code to work around that...

True but the complex code (that involves thread spawning, if that's what you're referring to)
will only be activated if the actual number of objects inquired in those sets really exceed 64,
regardless of how big the sets passed to select() were...

So basically, FD_SETSIZE, as Corinna pointed out, just refers to how much "ballast" the arguments
carry -- with larger default FD_SETSIZE they can become unnecessarily heavy for just a few small
file descriptors.

And since FD_SETSIZE is allowed to be overridden by the user, the complex processing at some
point is unavoidable, since there is no limitation for how large FD_SETSIZE can be, and the sets
are densely populated.

Anton Lavrentiev
Contractor NIH/NLM/NCBI


^ permalink raw reply	[flat|nested] 15+ messages in thread
* Re: Typo in <sys/select.h>?
@ 2022-07-06 15:57 Lavrentiev, Anton (NIH/NLM/NCBI) [C]
  2022-07-06 17:03 ` Ken Brown
  0 siblings, 1 reply; 15+ messages in thread
From: Lavrentiev, Anton (NIH/NLM/NCBI) [C] @ 2022-07-06 15:57 UTC (permalink / raw)
  To: cygwin

> However, discussing this shows how irrelevant the actual default value
> of FD_SETSIZE is.

Correct, yet the comments in the header files (along with used values) should be consistent :-)

> [...] to define FD_SETSIZE according to its requirements anyway.

That'd work for CYGWIN right away;  but won't for glibc (because FD_SETSIZE
is not used in the actual type definition, but __FD_SETSIZE), so there comes
the array trick shown earlier.

Anton Lavrentiev
Contractor NIH/NLM/NCBI

^ permalink raw reply	[flat|nested] 15+ messages in thread
* Re: Typo in <sys/select.h>?
@ 2022-07-06 14:26 Lavrentiev, Anton (NIH/NLM/NCBI) [C]
  2022-07-06 15:07 ` Corinna Vinschen
  0 siblings, 1 reply; 15+ messages in thread
From: Lavrentiev, Anton (NIH/NLM/NCBI) [C] @ 2022-07-06 14:26 UTC (permalink / raw)
  To: cygwin

> DESCRIPTION
>        WARNING: select() can monitor only file descriptors  numbers  that  are
>        less  than  FD_SETSIZE (1024)-an unreasonably low limit for many modern

Whoever wrote this, was wrong (they might have never consulted the actual kernel code, or were just
blindsided by FD_SETSIZE being a predefined constant).  You can take a look at the kernel source code,
if you don't believe me.

This select() trick has been like that from the earliest days of Linux, and the behavior is carefully carried over.

https://github.com/torvalds/linux/blob/master/fs/select.c#L625

It's just an FYI :-)

Anton Lavrentiev
Contractor NIH/NLM/NCBI


^ permalink raw reply	[flat|nested] 15+ messages in thread
* Re: Typo in <sys/select.h>?
@ 2022-07-06 13:19 Lavrentiev, Anton (NIH/NLM/NCBI) [C]
  2022-07-06 14:17 ` Corinna Vinschen
  0 siblings, 1 reply; 15+ messages in thread
From: Lavrentiev, Anton (NIH/NLM/NCBI) [C] @ 2022-07-06 13:19 UTC (permalink / raw)
  To: cygwin

> On Linux, select(2) is really only capable to
> handle file descriptors numbers up to descriptor number 1023,

That is not true.  While FD_SETSIZE is defined as a fixed constant,
Linux kernel does not actually "know" (or care) about it.

So you can have an array of fd_sets, like this, in your code:

fd_set r_fds[NFDS];

and then if "fd" is a file descriptor in question, you'd do

FD_SET(fd % FD_SETSIZE, &r_fds[fd / FD_SETSIZE]);

and then

n = select(maxfd + 1, r_fds, ...);

The Linux kernel is guided by the maxfd parameter and assumes the sets are as
large as required to cover that number of file descriptors (obviously checking
that those sets are still within the process reach).

NFDS above is chosen such a way that "NFDS * FD_SETSIZE" covers all your required
file descriptors, if there are more than just FD_SETSIZE.

Anton Lavrentiev
Contractor NIH/NLM/NCBI

^ permalink raw reply	[flat|nested] 15+ messages in thread
* RE: Typo in <sys/select.h>?
@ 2022-07-05 15:11 Lavrentiev, Anton (NIH/NLM/NCBI) [C]
  0 siblings, 0 replies; 15+ messages in thread
From: Lavrentiev, Anton (NIH/NLM/NCBI) [C] @ 2022-07-05 15:11 UTC (permalink / raw)
  To: cygwin

> I'm no expert, but it seems the FD_SETSIZE should have been 128.
> Long is 8 bytes, 64 bits. One bit if one open file, 64 * 64 = 4096.

FD_SETSIZE is the file descriptor bitset capacity in _BITS_.

64 (as currently in there) means 1 int (on 64 bit platforms, or 2 ints on 32 bit
platforms, respectively), and is capable of representing 64 file descriptors by
default.

Anton Lavrentiev
Contractor NIH/NLM/NCBI

^ permalink raw reply	[flat|nested] 15+ messages in thread
* Typo in <sys/select.h>?
@ 2022-07-05 14:13 Lavrentiev, Anton (NIH/NLM/NCBI) [C]
  2022-07-05 14:55 ` Andrey Repin
  2022-07-05 21:51 ` Ken Brown
  0 siblings, 2 replies; 15+ messages in thread
From: Lavrentiev, Anton (NIH/NLM/NCBI) [C] @ 2022-07-05 14:13 UTC (permalink / raw)
  To: 'cygwin@cygwin.com'

Hi,

There's some inconsistency between <sys/select.h> and <sys/param.h>:

sys/select.h has this:
-----------------------
/*
 * Select uses bit masks of file descriptors in longs.
 * These macros manipulate such bit fields (the filesystem macros use chars).
 * FD_SETSIZE may be defined by the user, but the default here
 * should be >= NOFILE (param.h).
 */
#ifndef FD_SETSIZE
#define FD_SETSIZE      64
#endif
----------------------

Now, this is the relevant part of sys/param.h looks like this:
----------------------
/* Max number of open files.  The Posix version is OPEN_MAX.  */
/* Number of fds is virtually unlimited in cygwin, but we must provide
   some reasonable value for Posix conformance */
#define NOFILE          8192
----------------------

So it's either "<= NOFILE" that was actually meant to be there in the comment (or,
an equivalent "should NOT be > NOFILE"), or FD_SETSIZE should have been defined as 8192,
if the comment is actually correct.  Or maybe I'm missing something here :-)

I understand that if I redefined FD_SETSIZE in my code before including <sys/select.h>,
it'd work with whatever large (or small) fd_set I need, but that's not what I'm after.

Anton Lavrentiev
Contractor NIH/NLM/NCBI


^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2022-07-06 17:03 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-06 14:17 Typo in <sys/select.h>? Lavrentiev, Anton (NIH/NLM/NCBI) [C]
  -- strict thread matches above, loose matches on Subject: below --
2022-07-06 15:57 Lavrentiev, Anton (NIH/NLM/NCBI) [C]
2022-07-06 17:03 ` Ken Brown
2022-07-06 14:26 Lavrentiev, Anton (NIH/NLM/NCBI) [C]
2022-07-06 15:07 ` Corinna Vinschen
2022-07-06 13:19 Lavrentiev, Anton (NIH/NLM/NCBI) [C]
2022-07-06 14:17 ` Corinna Vinschen
2022-07-05 15:11 Lavrentiev, Anton (NIH/NLM/NCBI) [C]
2022-07-05 14:13 Lavrentiev, Anton (NIH/NLM/NCBI) [C]
2022-07-05 14:55 ` Andrey Repin
2022-07-05 21:51 ` Ken Brown
2022-07-06  7:42   ` Corinna Vinschen
2022-07-06 14:01     ` Jon Turney
2022-07-06 14:15       ` Corinna Vinschen
2022-07-06 14:22         ` Corinna Vinschen

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 read-only IMAP folder(s) and NNTP newsgroup(s).