Select () function analysis in Linux and query serial data using select in Linux

Source: Internet
Author: User

Address: http://tieba.baidu.com/p/94875093

 

Select () function analysis in Linux

 

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 files, named pipelines, or device handles. The programmer completes the connection establishment. When select () is called, the content of fd_set is modified by the kernel according to the IO status, in this way, the socket or file of the select () process is notified to be readable. The specific explanation is as follows:

# Include
# Include
# Include

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. It is generally set to the maximum file number in each file to be monitored 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. (For details, see/usr/sys/select. H. It can be accurate to one second per million !)

When the image files in readfds or writefds are readable, writable, or time-out, this select () operation ends and returns. With the macro provided by a set of systems, programmers can determine which file is readable or writable at the end of select. 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: time-out completion, no time-out blocking, and round-robin. Because
Timeval can be accurate to one second per million, so windows settimer () is nothing. 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.

 

Query serial port data using select in Linux

Using the read serial port in Linux may cause congestion or data reading errors. However, the SELECT statement can be used to query the comport first, and read can be used to avoid it. When the comport is delayed, the program can exit, so that the program will not die because the comport is blocked. My code is as follows:

Bool readdevice (INT hcomm, unsigned long Ulen, char * pdata)

{

Int nread = 0;

Char inbuf [Ulen];

Char buff [Ulen];

Memset (inbuff, '\ 0', Ulen );

Memset (buff, '\ 0', Ulen );

Fd_set readset;
Struct timeval TV;
Int maxfd = 0;

Int C = 0;
Int Z;
Do
{
Fd_zero (& readset );
If (hcomm> = 0)
Fd_set (hcomm, & readset );
Maxfd = hcomm + 1;
TV. TV _sec = 0;
TV. TV _usec = 500000;
Do
{
Z = select (maxfd, & readset, 0, 0, & TV );
} While (Z =-1 & errno = eintr );
If (Z =-1)
Printf ("select (2) \ n ");
If (Z = 0)
{
Hcomm =-1;
}

If (hcomm> = 0 & fd_isset (hcomm, & readset ))
{
Z = read (hcomm, buff, Ulen-C );
C + = z;
If (Z =-1)
{
Hcomm =-1;
}
If (z> 0)
{

Buff [Z + 1] = '\ 0 ';
Strcat (inbuff, buff );
Memset (buff, 0x00, Ulen );
}
Else
{
Hcomm =-1;
}
}
} While (hcomm> = 0 );

Memcpy (pdata, inbuff, C );

Return true;

}

 

 

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.