Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 650627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:04:26+00:00 2026-05-13T22:04:26+00:00

The select() and pselect() system calls modify their arguments (the ‘ fd_set * ‘

  • 0

The select() and pselect() system calls modify their arguments (the ‘fd_set *‘ arguments), so the input value tells the system which file descriptors to check and the return values tell the programmer which file descriptors are currently usable.

If you are going to call them repeatedly for the same set of file descriptors, you need to ensure that you have a fresh copy of the descriptors for each call. The obvious way to do that is to use a structure copy:

fd_set ref_set_rd;
fd_set ref_set_wr;
fd_set ref_set_er;
...
...code to set the reference fd_set_xx values...
...
while (!done)
{
    fd_set act_set_rd = ref_set_rd;
    fd_set act_set_wr = ref_set_wr;
    fd_set act_set_er = ref_set_er;
    int bits_set = select(max_fd, &act_set_rd, &act_set_wr,
                          &act_set_er, &timeout);
    if (bits_set > 0)
    {
        ...process the output values of act_set_xx...
    }
 }

(Edited to remove incorrect struct fd_set references – as pointed out by ‘R..’.)

My question:

  • Are there any platforms where it is not safe to do a structure copy of the fd_set values as shown?

I’m concerned lest there be hidden memory allocation or anything unexpected like that. (There are macros/functions FD_SET(), FD_CLR(), FD_ZERO() and FD_ISSET() to mask the internals from the application.)

I can see that MacOS X (Darwin) is safe; other BSD-based systems are likely to be safe, therefore. You can help by documenting other systems that you know are safe in your answers.

(I do have minor concerns about how well the fd_set would work with more than 8192 open file descriptors – the default maximum number of open files is only 256, but the maximum number is ‘unlimited’. Also, since the structures are 1 KB, the copying code is not dreadfully efficient, but then running through a list of file descriptors to recreate the input mask on each cycle is not necessarily efficient either. Maybe you can’t do select() when you have that many file descriptors open, though that is when you are most likely to need the functionality.)


There’s a related SO question – asking about ‘poll() vs select()’ which addresses a different set of issues from this question.


Note that on MacOS X – and presumably BSD more generally – there is an FD_COPY() macro or function, with the effective prototype:

  • extern void FD_COPY(const restrict fd_set *from, restrict fd_set *to);.

It might be worth emulating on platforms where it is not already available.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T22:04:26+00:00Added an answer on May 13, 2026 at 10:04 pm

    Since struct fd_set is just a regular C structure, that should always be fine. I personally don’t like doing structure copying via the = operator, since I’ve worked on plenty of platforms that didn’t have access to the normal set of compiler intrinsics. Using memcpy() explicitly rather than having the compiler insert a function call is a better way to go, in my book.

    From the C spec, section 6.5.16.1 Simple assignment (edited here for brevity):

    One of the following shall hold:

    …

    • the left operand has a qualified or unqualified version of a structure or union type compatible with the type of the right;

    …

    In simple assignment (=), the value of the right operand is converted to the type of the assignment expression and replaces the value stored in the object designated by the left operand.

    If the value being stored in an object is read from another object that overlaps in any way the storage of the first object, then the overlap shall be exact and the two objects shall have qualified or unqualified versions of a compatible type; otherwise, the behavior is undefined.

    So there you go, as long as struct fd_set is a actually a regular C struct, you’re guaranteed success. It does depend, however, on your compiler emitting some kind of code to do it, or relying on whatever memcpy() intrinsic it uses for structure assignment. If your platform can’t link against the compiler’s intrinsic libraries for some reason, it may not work.

    You will have to play some tricks if you have more open file descriptors than will fit into struct fd_set. The linux man page says:

    An fd_set is a fixed size buffer. Executing FD_CLR() or FD_SET() with a value of fd that is negative or is equal to or larger than FD_SETSIZE will result in undefined behavior. Moreover, POSIX requires fd to be a valid file descriptor.

    As mentioned below, it might not be worth the effort to prove that your code is safe on all systems. FD_COPY() is provided for just such a use, and is, presumably, always guaranteed:

    FD_COPY(&fdset_orig, &fdset_copy) replaces an already allocated &fdset_copy file descriptor set with a copy of &fdset_orig.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 516k
  • Answers 516k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If whatever is always positive then you can use this:… May 16, 2026 at 6:48 pm
  • Editorial Team
    Editorial Team added an answer You may be looking for this instance method: - (NSArray… May 16, 2026 at 6:48 pm
  • Editorial Team
    Editorial Team added an answer OK I found a simple solution myself (with a little… May 16, 2026 at 6:48 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

select myvalue from mytable where id=1; it return a value of 10 digits( Ex:
How can I try to read data from socket with timeout? I know, select,
I have the following structure: <form> <div class=content> ... </div> <div class=action> <p>Select <a
I have a query that looks like SELECT P.Column1, P.Column2, P.Column3, ... ( SELECT
The following query: WITH CteProductLookup(ProductId, oid) AS ( SELECT p.ProductID, p.oid FROM [dbo].[ME_CatalogProducts] p
SELECT count(*) c FROM full_view WHERE verified > ( DATE (NOW()) - INTERVAL 30
SELECT ROUND(123.4567, 2)` gives me `123.4600` But I need 123.46 . Data type of
select * from ss.mailer_data where id = 249122 and address_3 like'%%' will not hit
SELECT telephone_number FROM table WHERE telephone_number REGEXP '^1[() -]*999[() -]*999[() -]*9999$'; how do i
SELECT left_tbl.* FROM left_tbl LEFT JOIN right_tbl ON left_tbl.id = right_tbl.id WHERE right_tbl.id IS

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.