Are there any platforms where using structure copy on an fd_set (for select() or pselect()) causes problems?

前端 未结 5 1934
生来不讨喜
生来不讨喜 2020-11-28 15:05

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

5条回答
  •  渐次进展
    2020-11-28 15:45

    I've done a little research on MacOS X, Linux, AIX, Solaris and HP-UX, and there are some interesting results. I used the following program:

    #if __STDC_VERSION__ >= 199901L
    #define _XOPEN_SOURCE 600
    #else
    #define _XOPEN_SOURCE 500
    #endif /* __STDC_VERSION__ */
    
    #ifdef SET_FD_SETSIZE
    #define FD_SETSIZE SET_FD_SETSIZE
    #endif
    
    #ifdef USE_SYS_TIME_H
    #include 
    #else
    #include 
    #endif /* USE_SYS_TIME_H */
    
    #include 
    
    int main(void)
    {
        printf("FD_SETSIZE = %d; sizeof(fd_set) = %d\n", (int)FD_SETSIZE, (int)sizeof(fd_set));
        return 0;
    }
    

    It was compiled twice on each platform:

    cc -o select select.c
    cc -o select -DSET_FD_SETSIZE=16384
    

    (And on one platform, HP-UX 11.11, I had to add -DUSE_SYS_TIME_H to get things to compile at all.) I separately did a visual check on FD_COPY - only MacOS X seemed to include it, and that had to be activated by ensuring that _POSIX_C_SOURCE was not defined or by defining _DARWIN_C_SOURCE.

    AIX 5.3

    • Default FD_SETSIZE is 65536
    • The FD_SETSIZE parameter can be resized
    • No FD_COPY

    HP-UX 11.11

    • No header - use instead
    • Default FD_SETSIZE is 2048
    • The FD_SETSIZE parameter can be resized
    • No FD_COPY

    HP-UX 11.23

    • Has
    • Default FD_SETSIZE is 2048
    • The FD_SETSIZE parameter can be resized
    • No FD_COPY

    Linux (kernel 2.6.9, glibc 2.3.4)

    • Default FD_SETSIZE is 1024
    • The FD_SETSIZE parameter cannot be resized
    • No FD_COPY

    MacOS X 10.6.2

    • Default FD_SETSIZE is 1024
    • The FD_SETSIZE parameter can be resized
    • FD_COPY is defined if strict POSIX compliance is not requested or if _DARWIN_C_SOURCE is specified

    Solaris 10 (SPARC)

    • Default FD_SETSIZE is 1024 for 32-bit, 65536 for 64-bit
    • The FD_SETSIZE parameter can be resized
    • No FD_COPY

    Clearly, a trivial modification to the program allows automatic checking of FD_COPY:

    #ifdef FD_COPY
        printf("FD_COPY is a macro\n");
    #endif
    

    What is not necessarily trivial is finding out how to ensure that it is available; you end up doing the manual scan and working out how to trigger it.

    On all these machines, it looks like an fd_set can be copied by a structure copy without running into risk of undefined behaviour.

提交回复
热议问题