Select Source Learning Record

Source: Internet
Author: User

First of all, record the results of learning, and gradually improve
Four correlation functions
The structure of Fd_set is described in the previous article and explains why the maximum is 1024.
-fd_set is a long array structure of 1024/32. That is, the structure holds a long array.

int FD_ZERO(int*fdset);   int FD_CLR(int*fdset);   int FD_SET(int*fd_set);   int FD_ISSET(int*fdset);

Fd_zero, put the whole fd_set 0;
FD_CLR know that a certain person will automatically calculate the bit in that byte,
The same in the back

When a set of file descriptors is declared, all locations must be zeroed with Fd_zero. The position of the descriptor to which we are interested is then manipulated as follows:
Fd_set RSet;
int FD;
Fd_zero (&rset);
Fd_set (FD, &rset);
Fd_set (stdin, &rset);
After the select is returned, use the Fd_isset test to set the position:
if (Fd_isset (FD, &rset)
{ ... }
http://blog.csdn.net/lingfengtengfei/article/details/12392449

Use method, parameter meaning
Explain the parameters of select in detail:
(1) INTMAXFDP is an integer value that refers to the range of all file descriptors in the collection, that is, the maximum value of all file descriptors plus 1, and cannot be wrong.
Description: The explanation of this principle can be seen in a detailed explanation of the above fd_set, Fd_set is to store these file descriptors in the form of bitmaps. MAXFDP defines the number of bits that are valid in the bitmap.
(2) Fd_set*readfds is a pointer to the FD_SET structure, this set should include the file descriptor, we are to monitor the read changes of these file descriptors, that is, we are concerned about whether the data can be read from these files, if there is a file in this collection is readable, Select returns a value greater than 0 indicating that the file is readable, and if there is no readable file, the timeout parameter is used to determine whether to time out, and if timeout is exceeded, select returns 0 if an error returns a negative value. You can pass in a null value to indicate that you do not care about any file read changes.
(3) Fd_set*writefds is a pointer to the FD_SET structure, this set should include the file descriptor, we are to monitor the write changes of these file descriptors, that is, we are concerned about whether the data can be written to these files, if there is a file in this collection can be written, Select returns a value greater than 0, indicating that a file is writable, and if there is no writable file, the timeout parameter is used to determine whether to time out, and if timeout is exceeded, select returns 0 if an error returns a negative value. You can pass in a null value to indicate that you do not care about any file write changes.
(4) Fd_set*errorfds with the intent of the above two parameters, used to monitor file error exception file.
(5) structtimeval* timeout is the time-out of SELECT, this parameter is critical, it can make select in three states, first, if NULL is passed as a parameter, that is, the time structure is not passed in, that is, the select is put in a blocking state, Be sure to wait until one of the file descriptors in the monitor file descriptor collection changes; second, if the time value is set to 0 seconds 0 milliseconds, it becomes a purely non-blocking function, regardless of whether the file descriptor is changed, immediately return to continue execution, the file has no change return 0, there is a change to return a positive value; Timeout value is greater than 0, this is the waiting time-out period, that is, select in timeout time block, the timeout period has the arrival of the event to return, otherwise after the timeout, anyway must return, the return value with the above.
Description
function returns:
(1) The Kernel (I/O) modifies the set of file descriptors based on the state and returns a number greater than 0 when the corresponding file descriptor set of the monitor satisfies the criteria, such as when the data arrives in the set of read file descriptors.
(2) The Select function returns a value of 0 when there is no file descriptor that satisfies the condition, and the set Timeval monitoring time times out.
(3) An error occurs when Select returns a negative value.
http://blog.csdn.net/lingfengtengfei/article/details/12392449

View Source and analyze

Sys_select (intN, fd_set __user *inp, Fd_set __user *outp, fd_set __user *Exp,structTimeval __user *TVP) {fd_set_bits FDS;//This is the user data that will be passed into the kernel, that is, the structure of the poll, which contains 6 members, including Inp,outp,exp, followed by three to indicate the result to be outgoing    Char*bits;LongTimeoutintRET, size, max_fdset; Timeout = max_schedule_timeout;if(TVP) {//If the Timeval struct is passed in non-nulltime_t sec, usec;if(ret = Verify_area (Verify_read, TVP,sizeof(*TVP))) || (ret = __get_user (sec, &tvp->tv_sec)) | | (ret = __get_user (USEC, &tvp->tv_usec)))//Copy time parameter to kernel space            GotoOut_nofds;//Data invalid?? ret =-einval;if(Sec <0|| USEC <0)//Parameter check            GotoOut_nofds;if((unsigned Long) sec < max_select_seconds) {timeout = round_up (USEC,1000000/HZ); Timeout + = sec * (unsigned Long) HZ; }} ret =-einval;//Note whether timeval is non-null will come here    if(N <0)GotoOut_nofds;/ * Max_fdset can increase, so grab it once to avoid race * /Max_fdset = current->files->max_fdset;if(n > Max_fdset) n = max_fdset;/* * We need 6 bitmaps (In/out/ex for both incoming and outgoing), * since we used fdset we need to allocate mem    Ory in units of * long-words. */ret =-enomem;    size = Fds_bytes (n); BITS = select_bits_alloc (size);if(!bits)GotoOut_nofds; Fds.in = (unsigned Long*) bits; Fds.out = (unsigned Long*) (bits + size); Fds.ex = (unsigned Long*) (bits +2* size); Fds.res_in = (unsigned Long*) (bits +3* size); Fds.res_out = (unsigned Long*) (bits +4* size); FDS.RES_EX = (unsigned Long*) (bits +5* size);if(ret = Get_fd_set (n, INP, fds.in)) | | (ret = Get_fd_set (n, OUTP, fds.out)) | |//Copy to FDS, the following results will be used to return        GotoOut    Zero_fd_set (n, fds.res_in);    Zero_fd_set (n, fds.res_out);    Zero_fd_set (n, FDS.RES_EX); ret = Do_select (n, &fds, &timeout);//Enter the core function of real polling here    if(TVP &&!) (Current->personality & Sticky_timeouts)) {time_t sec =0, USEC =0;if(timeout)            {sec = timeout/hz;            USEC = timeout% HZ; USEC *= (1000000/HZ);        } put_user (sec, &tvp->tv_sec); Put_user (USEC, &tvp->tv_usec);//How much time is left, calculate and copy to user space}if(Ret <0)GotoOutif(!ret) {ret =-erestartnohand;if(Signal_pending (current))//Wait here to be readable and writable? If the time struct is null then wait indefinitely.             GotoOut RET =0;//timeout put back}if(Set_fd_set (n, INP, fds.res_in) | |//The computed results are copied to inp,outp,exp, so if no data is available, then all is 0, which is to reset the Fd-set before each call to select. Or they're all 0 .Set_fd_set (n, OUTP, fds.res_out) | | Set_fd_set (N,Exp, fds.res_ex)) ret =-efault;out:select_bits_free (bits, size); Out_nofds:returnRET;}
intDo_select (intN, Fd_set_bits *fds,Long*timeout) {structPoll_wqueues table; Poll _table *wait;intretval, I;Long__timeout = *timeout; Spin_lock (&current->files->file_lock);//Guaranteed operation, lockoutretval = MAX_SELECT_FD (n, FDS);//This explains why N is set to the maximum fd+1, he can calculate the need to poll a few bytes, if not, then not every time you have to poll 1024 descriptors, efficiency is too lowSpin_unlock (&current->files->file_lock);if(RetVal <0)returnretval    n = retval;    Poll_initwait (&table); wait = &table.pt;if(!__timeout) wait = NULL; retval =0; for(;;) {unsignedLong*RINP, *ROUTP, *rexp, *INP, *OUTP, *exp; Set_current_state (task_interruptible);//select can be interrupted.INP = fds->inch; OUTP = fds-> out;        Exp = fds->ex; RINP = fds->res_in; ROUTP = fds->res_out; Rexp = fds->res_ex; for(i =0; I < n; ++RINP, ++ROUTP, ++rexp) {unsignedLong inch, out, ex, all_bits, bit =1, Mask, J; UnsignedLongRes_in =0, Res_out =0, RES_EX =0;structFile_operations *f_op = NULL;structFile *file = NULL;inch= *inp++; out= *outp++;            ex = *exp++; All_bits =inch| out| Ex//test if there is data available            if(All_bits = =0) {i + = __nfdbits;Continue; } for(j =0; J < __nfdbits; ++j, ++i, bit <<=1) {if(I >= N) Break;if(! (Bit & all_bits))Continue; File = Fget (i);if(file)                    {f_op = file->f_op; mask = Default_pollmask;if(F_op && f_op->poll) mask = (*f_op->poll) (file, retval?) null:wait);//Driver poll function, written by driver all knowFput (file);if(Mask & Pollin_set) && (inch& bit) {res_in |= bit; retval++;//Increment the available Descriptor count value}if(Mask & Pollout_set) && ( out& bit) {res_out |= bit;                    retval++; }if(Mask & Pollex_set) && (ex & Bit))                        {res_ex |= bit;                    retval++;            }} cond_resched (); }if(res_in) *rinp = res_in;if(res_out) *ROUTP = res_out;if(res_ex) *rexp = RES_EX; } wait = NULL;if(retval | |!__timeout | | signal_pending (current)) Break;if(table.error) {retval = Table.error; Break;    } __timeout = Schedule_timeout (__timeout);    } __set_current_state (task_running); Poll_freewait (&table);/ * * Up-to-date the caller timeout. */*timeout = __timeout;returnretval;}

In the source code above clearly explains why Select to use
If there is no, please point out, thank you

Select Source Learning Record

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.