Use of the select function in iinux

Source: Internet
Author: User

Http://bbs.ednchina.com/BLOG_ARTICLE_151601.HTM

Blocking I/O programming has two features:


1. If one finds that I \ O has input and the other has input during reading, no response will be generated at this time, that is, data input is available only when your program statement is selected.


2. When the program goes to select, if there is no data input, the program will wait until there is a data location, that is, there is no loop or sleep in the program.


 


Select is still important in socket programming, but it is not very popular for beginners of socket programming, they are just used to writing blocking programs such as connect, accept, Recv, or recvfrom (the so-called blocking method block, as the name suggests, means that the process or thread must wait for an event to occur when executing these functions, if the event does not occur, the process or thread will be blocked and the function cannot return immediately ). However, using select can achieve non-blocking (the so-called non-block method means that the process or thread does not have to wait for the event to execute this function. Once executed, it will definitely return, different return values are used to reflect the execution of the function. If the event occurs in the same way as the blocking method, if the event does not occur, a code is returned to inform the event that the event has not occurred, while the process or thread continues to execute, so it is more efficient). It can monitor changes in file descriptors that we need to monitor-read/write or exceptions. The following is a detailed introduction!


Select Function Format (I am talking about Berkeley socket programming in UNIX, which is different from that in windows. I will explain it later ):


Int select (INT maxfdp, fd_set * readfds, fd_set * writefds, fd_set * errorfds, struct timeval * timeout );


Two struct types are described first:


First, struct fd_set can be understood as a collection in which the file descriptor (file descriptor) is stored, that is, the file handle. This can be what we call a common object, of course, any device, pipeline, and FIFO in UNIX are all in the file format, so there is no doubt that a socket is a file, and a socket handle is a file descriptor. The fd_set set can be operated manually by some macros, such as clearing the set fd_zero (fd_set *) and adding a given file descriptor to the set fd_set (INT, fd_set *), deletes a given file descriptor from the collection fd_clr (int
, Fd_set *), check whether the specified file descriptor in the set can read and write fd_isset (INT, fd_set *). For example.


Second, struct timeval is a common structure used to represent the time value. It has two members: one is the number of seconds and the other is the number of milliseconds.


Specific interpretation of select parameters:


Int maxfdp is an integer that indicates the range of all file descriptors in the collection. That is, the maximum value of all file descriptors is 1. It cannot be wrong! In Windows, the value of this parameter does not matter. You can set it incorrectly.


Fd_set * readfds is a pointer to the fd_set structure. This collection should contain file descriptors. We want to monitor the read changes of these file descriptors, that is, we are concerned about whether data can be read from these files. If a file in this collection is readable, select returns a value greater than 0, indicating that the file is readable, if there is no readable file, the timeout parameter is used to determine whether to time out. If the time exceeds the timeout time, select returns 0. If an error occurs, a negative value is returned. You can pass in a null value, indicating that you do not care about any file read changes.


Fd_set * writefds is a pointer to the fd_set structure. This collection should contain file descriptors. We want to monitor the write changes of these file descriptors, that is, we are concerned about whether data can be written to these files. If there is a file in this collection that can be written, select will return a value greater than 0, indicating that a file is writable, if no writable file exists, the timeout parameter is used to determine whether to time out. If the time exceeds the timeout time, select returns 0. If an error occurs, a negative value is returned. You can pass in a null value, indicating that you do not care about any file write changes.


Fd_set * errorfds indicates the intention of the two parameters above, which is used to monitor file error exceptions.


Struct timeval * timeout is the select timeout time. This parameter is crucial. It can make the SELECT statement in three States. First, if null is input as an input parameter, that is, the time structure is not input, that is, the SELECT statement is placed in the blocking state. It must wait until a file descriptor in the collection of monitored file descriptors changes. Second, if the time value is set to 0 s and 0 ms, it becomes a pure non-blocking function. no matter whether the file descriptor has changed or not, it immediately returns to continue execution. If the file does not change, 0 is returned, and a positive value is returned. Third, the value of timeout is greater than 0, this is the waiting timeout time, that is, the SELECT statement is blocked within the timeout time, and an event will be returned within the time-out time. Otherwise, no matter how long it will be returned, the return value is the same as the above.


Return Value:


Negative value: Select error positive value: some files can be read or written, or errors 0: wait for timeout, there is no readable or wrong file


With select, you can write a decent network program! A simple example is to accept data from the network and write it into a file.


Example:


Main ()


{


Int sock;


File * FP;


Struct fd_set FDS;


Struct timeval timeout = {3, 0}; // select wait 3 seconds, 3 seconds round-robin, set to 0 if it is not blocked


Char buffer [256] = {0}; // receiving buffer of 256 bytes


/* Assume that a UDP connection has been established, and the specific process is not written or simple. Of course, the same applies to TCP. The host IP address and port are already specified, and the file to be written has already been opened.


Sock = socket (...);


BIND (...);


Fp = fopen (...);*/


While (1)


{


Fd_zero (& FDs); // clear the set for each loop. Otherwise, the descriptor changes cannot be detected.


Fd_set (sock, & FDs); // Add Descriptor


Fd_set (FP, & FDs); // same as above


Maxfdp = sock> FP? Sock + 1: FP + 1; // Add 1 to the maximum value of the descriptor


Switch (select (maxfdp, & FDS, & FDS, null, & timeout) // select


{


Case-1: exit (-1); break; // select error, exit the program


Case 0: break; // Round Robin again


Default:


If (fd_isset (sock, & FDs) // test whether sock is readable, that is, whether there is data on the network.


{


Recvfrom (sock, buffer, 256,...); // accept Network Data


If (fd_isset (FP, & FDs) // test whether the file can be written


Fwrite (FP, buffer...); // write a file


Buffer clearing;


} // End if break;


} // End Switch


} // End while


} // End main


The Select () mechanism provides an fd_set data structure, which is actually a long array,

Each array element can be associated with an open file handle (whether it is a socket handle or other

File, named pipe, or device handle) to establish a contact. The programmer completes the process of establishing a contact,

When select () is called, the content of fd_set is modified by the kernel according to the IO status.

Which socket or file is readable for the select () process:


# Include <sys/types. h>

# Include <sys/times. h>

# Include <sys/select. h>


Int select (NFDs, readfds, writefds, limit TFDs, timeout)

Int NFDs;

Fd_set * readfds, * writefds, * required TFDs;

Struct timeval * timeout;


NDfS: the number of file handles monitored by the SELECT statement, depending on the number of files opened in the process.

The maximum file number plus one.

Readfds: a collection of readable file handles monitored by select.

Writefds: a set of writable file handles monitored by select.

Except TFDs: a collection of abnormal file handles monitored by select.

Timeout: the timeout end time of this select () operation. (See/usr/sys/select. H,

Accurate to one second per million !)


When the image file in readfds or writefds is readable, writable, or times out, this select ()

Returns the result. Programmers can use the macros provided by a set of systems to determine when select () ends.

Which file is readable or writable. Readfds is particularly useful for socket programming.

Several related macros are explained as follows:


Fd_zero (fd_set * fdset): clears the contact between fdset and all file handles.

Fd_set (int fd, fd_set * fdset): Establishes the connection between the file handle FD and fdset.

Fd_clr (int fd, fd_set * fdset): clears the contact between the file handle FD and fdset.

Fd_isset (int fd, fdset * fdset): Check whether the file handle FD associated with fdset is

It can be read and written.> 0 indicates that it can be read and written.

(For definition of fd_set and related macros, see/usr/include/sys/types. h)


In this way, your socket only needs to be read when there is something to read, roughly as follows:


...

Int sockfd;

Fd_set FDR;

Struct timeval timeout = ..;

...

For (;;){

Fd_zero (& FDR );

Fd_set (sockfd, & FDR );

Switch (select (sockfd + 1, & FDR, null, & timeout )){

Case-1:

Error handled by u;

Case 0:

Timeout hanled by u;

Default:

If (fd_isset (sockfd )){

Now u read or Recv something;

/* If sockfd is father and

Server socket, u can now

Accept ()*/

}

}

}


Therefore, an fd_isset (sockfd) is equivalent to a FD readable notification.

For the struct timeval function, use man select. Different timeval settings

Enable Select () to display three features: timeout termination, no timeout blocking, and round robin. Because

Timeval can be accurate to one second per million, so windows settimer () is not

What. You can use select () to make a super clock.


What is the implementation of fd_accept? Still above, because the client will send

The Connection Request message. At this time, select () will certainly end. fd_isset (sockfd) is of course large.

At zero, because there is a message readable! As for this application, it mainly lies in the parent of the service provider.

Socket, if you do not like the active accept (), you can change it to the above mechanism to accept ().


As for the implementation and processing of fd_close, it takes a lot of CPU processing time and is not finished yet.


--

This article discusses how to use select () to detect socket shutdown by the other Party:


The local socket is still readable, because when the socket of the other party is closed, a closed connection will be sent.

The notification message will be immediately detected by select. TCP connection (three-way handshake) and off

For more information about the closed (secondary handshake) mechanism, see related books on TCP/IP.


For some reason, Unix does not seem to have provided a notification to the process about socket or pipe being disabled by the other party.

Signal, or the CPU is limited. In short, when the other party closes, one executes Recv () or read (),

Return-1 immediately. The value of the global variable errno is 115, and the corresponding sys_errlist [errno]

Connect refused (see/usr/include/sys/errno. h ). Therefore

In the for (;)... select () block, when something is readable, check Recv () or

When the return value of read () is-1, the local socket is shut down; otherwise, select () will

I always think that there is something to read, and the result is that the CPU has been heartbroken to cut the pin. If you don't believe it, try: No Check

Query Recv () to return the result, and write the received result (not actually received) to the standard output...

Similar problems also occur in programming of famous pipelines. For details, refer to release as: release a useful

The source code of the Socket Client.


As for the processing that the other party suddenly closes when writing the socket, it can simply capture the signal sigpipe and perform

The corresponding process of shutting down the local socket and so on. Sigpipe is interpreted as writing a non-reader pipeline.

We will not repeat it here. Please refer to man signal.


The above is the experience accumulated by the CPU in the TCP/IP data transmission experiment. If there is any error or omission, please kill it.


Alas, yesterday, a bunch of grandsons in the hacker area had almost no short circuits. Ren CPU (Pentium Core) Z80


In addition, the application of select in asynchronous (non-blocking) connect was just started with socket programming.

I have always used blocking-type connect. The problem with non-blocking connect is that proxy scan was used at the time.

However

Through online communication with netizens and related FAQs, I finally learned how to solve this problem.

Using select can solve this problem well. The general process is as follows:


1. Set the opened socket to non-blocking. You can use fcntl (socket, f_setfl, o_ndelay)

(Some systems can also use fnedlay ).


2. Send the Connect call. At this time,-1 is returned, but errno is set to einprogress, meaning that connect is still

The process is not completed yet.


3. Set the opened socket to the monitored writable (note that it is not readable) file set and use select for monitoring,

If it can be written, use

Getsockopt (socket, sol_socket, so_error, & error, sizeof (INT ));

To get the error value. If it is zero, connect is successful.


In many UNIX versions of proxyscan programs, you can see similar processes, in addition to the essence of Solaris

Area-> there is a general connect module with timeout parameters in programming skills.

Select and blocking and non-blocking

Http://hi.baidu.com/freshwater2009/item/f0ce1dcf2739fc2ca0b50a38

During the past two days, I have been discussing with my colleagues about the occasion when the socket is not blocked. I think that the socket used in the SELECT statement should be set to blocked. I searched the internet today to find it, it turns out that we have made a mistake. A socket is blocked or not blocked, and select is there. It can be used for these two sockets to check the polling of any socket, the timeout time is valid, but the difference is:

When the SELECT statement is complete and the socket is considered readable,

1. The blocked socket will block the read until all required bytes are read;

2. A non-blocking socket will allow the read to read data in FD and then return the data. However, if you originally requested to read 10 data records, 8 data records are read only, if you do not use select again to determine whether it is readable, but read directly, it is likely to return eagain OR = ewouldblock (BSD style ),
This error is returned by operations that cannot be completed immediately on a non-blocking Socket. For example, the Recv () function is called when no queued data is readable on the socket. This error is not a serious one. Try again later. To call the connect () function on a non-blocking sock_stream socket, it is normal to report ewouldblock because it takes some time to establish a connection.

Ewouldblock means that if you do not set the socket to the non-blocking (blocking) mode, this read operation will be blocked, that is, the data is not ready (but the system knows the data is coming, so select tells you that the socket is readable ). Careful persons who perform I/O operations in non-blocking mode will check whether errno is eagain, ewouldblock, or eintr. If yes, they should re-read it and generally use a loop. If you do not need to use non-blocking, do not set it to this, which is why the default mode of the system is blocking.

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.