当前位置:网站首页>I / O multiplexing and its related details

I / O multiplexing and its related details

2022-04-23 17:56:00 Augustu_

Fd Detailed explanation

Fd: Is a positive integer , It's actually a file descriptor table ( Array ) The index of , The file descriptor table holds the pointer of the opened file .

Every process in PCB(Process Control Block) That is, a file descriptor table is stored in the process control block , The file descriptor is the index of this table , Each entry in the file descriptor table has a pointer to an open file . Now let's make it clear : Open files are used in the kernel file The structure represents , The pointer in the file descriptor table points to file Structure .

file Structure is the structure used to describe file attributes in the kernel .

System call details

The basic concept of system call : Usually , In the operating system A set of subroutines used to realize various system functions , And provide them to the application call .

The program interface is OS Specially set for user programs , It is also obtained by the user program OS The only way to serve . Program interfaces are usually composed of various types of system calls , thus , It can also be said that , System call provides the interface between user program and operating system , The application program realizes its connection with... Through system call OS Communication for , And get its services .

System calls are provided in the operating system , Enables applications to Method through system call , Indirectly call the relevant procedures of the operating system , Obtain corresponding services .

Software interrupt realizes system call :

  • Software interrupt : It is an interrupt triggered by a software instruction .Linux System kernel responds to software interrupt , Switch from user state to kernel state , Execute the corresponding system call .

1.Select

int select(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);

select Allows applications to monitor a set of file descriptors , Wait for one or more file descriptors to become ready , To complete the I/O operation .

  • fd_set Use arrays to implement , The array size uses FD_SETSIZE Definition , So you can only listen to less than FD_SETSIZE Number of descriptors . There are three types of descriptors :readset、writeset、exceptset, Read separately 、 Write 、 Set of descriptors for exception conditions .

  • timeout For timeout parameters , call select It will block until the event with descriptor arrives or the waiting time exceeds timeout.

  • Successful call, return result greater than 0, Error. The returned result is -1, The timeout result is 0.

2.Poll

int poll(struct pollfd *fds, unsigned int nfds, int timeout);
// When timeout by -1 when ,poll The call will always block , Until something happens ;timeout by 0 when , Return immediately 

poll With the function of select similar , It is also waiting for one of a set of descriptors to become ready .

Select And Poll Compare :

  1. select The maximum file descriptor monitored by default is 1024, But it can be modified ,poll There is no restriction on file descriptors .
  2. poll Provides more event types , And the reuse of descriptors is better than select high .

3.Epoll

/* 1.epoll Use a set of functions to complete the task , Not a function  2.epoll Put the events on the file descriptor concerned by the user into the event table in the kernel , So there is no need to be like select and poll The file descriptor set or event set is repeatedly passed in each call  */

// Create a directive epoll File descriptor of kernel event table , This descriptor will be used for other purposes epoll The first parameter of the system call ,size It doesn't work .
int epoll_create(int size);

// This function is used to operate the events on the file descriptor monitored by the kernel event table : register 、 modify 、 Delete 
int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);
  
// Wait for events on a set of file descriptors for a timeout period , If successful, the number of ready file descriptions will be returned 
int epoll_wait(int epfd, struct epoll_event * events, int maxevents, int timeout);
  • epoll_ctl() Used to register a new descriptor with the kernel or change the state of a file descriptor . Registered file descriptors are maintained in a red black tree in the kernel .

  • epoll Than select and poll More flexible , There is no limit on the number of file descriptors .

  • Working mode :

    epoll There are two trigger modes for descriptor events :LT(level trigger) and ET(edge trigger).

    1. LT Pattern

      When epoll_wait() testing fd After an event occurs on and notifies the application of this event , The process will be informed of this , The process may not handle the event immediately , When the application next calls epoll_wait() when ,epoll_wait This event will also be notified to the application again , Until this event is handled . Support at the same time Blocking and Non-Blocking.

    2. ET Pattern

      When epoll_wait() testing fd After an event occurs on and notifies the application of this event , The application must process the event immediately , Because later epoll_wait() The event will no longer be notified to the application .

      Only support No-Blocking, To avoid blocking read due to a file handle / Blocking writes starve the task of processing multiple file descriptors .

    ET The model greatly reduces epoll The number of times the event was triggered repeatedly , therefore ET Mode efficiency ratio LT Mode high .

    1. EPOLLONESHOT

      In order to avoid multiple threads operating one at the same time Socket, You can register EPOLLONESHOT event .

      What we expect is a socket The connection is handled by only one thread at any one time , adopt epoll_ctl Register the file descriptor epolloneshot event , A thread handles socket when , Other threads will not be able to handle , When the thread is finished , Need to pass through epoll_ctl Reset epolloneshot event

Same as different

  1. select and poll The file descriptor is in User mode Add to the file descriptor set , Each call needs to copy the entire collection to the kernel state ; and epoll File descriptors are maintained in Kernel mode , Every time you add a file descriptor, you need to perform a system call .
  2. select Use a linear table to describe the set of file descriptors , The file descriptor has an upper limit ;poll Use a linked list to describe , There is no upper limit ;epoll The set of file descriptors is described with a red black tree , And there is no upper limit .
  3. select and poll You need to traverse the entire set of file descriptors , Determine which file descriptor has an event happening ; and epoll Will maintain a ready list, The ready event is added to the list, Every time you call epol_wait When , Observe only list Whether there is data .

Application scenarios

  1. select Application scenarios

    elect Of timeout The parameter accuracy is microseconds , and poll and epoll For milliseconds , therefore select It is more suitable for scenes with high real-time requirements , For example, the control of nuclear reactors .

    select Better portability , Supported by almost all mainstream platforms .

  2. poll Application scenarios

    poll There is no limit to the maximum number of descriptors , If the platform supports and does not require high real-time performance , You should use poll instead of select.

  3. epoll Application scenarios

    Just run on Linux On the platform , There are a large number of descriptors that need to be polled at the same time , And these connections are preferably long connections , because Epoll The descriptors of all files exist in the kernel , It needs to be called through the system epoll_ctr To change the file descriptor state , Frequent operation will reduce efficiency .

    Need to monitor less than 1000 A descriptor , There is no need to use epoll, Because it can't be reflected in this application scenario epoll The advantages of .

版权声明
本文为[Augustu_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231754306194.html