Method for simulating epoll interface by adopting select

By encapsulating and implementing the epoll interface on a system that supports the select interface, the problem of supporting the epoll interface without modifying the operating system kernel and driver is solved, and smooth porting of the epoll interface and maintaining the response time is realized.

CN119597372APending Publication Date: 2025-03-11JIANGSU AUTOMATION RESEARCH INSTITUTE
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411520774.7
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-10-29
Publication Date
2025-03-11

AI Technical Summary

Technical Problem

The prior art is difficult to implement the support of the epoll interface without modifying the operating system kernel and driver, which leads to the difficulty of porting software using the epoll interface on non-Linux operating systems.

Method used

By encapsulating and implementing the epoll interface on a system that supports the select interface, using the select interface to simulate the function of epoll_wait, and implementing the encapsulation of epoll_create and epoll_ctl.

Benefits of technology

It supports the epoll interface without modifying the operating system kernel and drivers, ensuring smooth porting of software using the epoll interface on non-Linux operating systems, and there is no significant loss in response time.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119597372A_ABST
    Figure CN119597372A_ABST
Patent Text Reader

Abstract

The invention discloses a method for simulating an epoll interface by adopting select. The method comprises the following steps: firstly, declaring a data structure struct EpolFdInfo, and declaring a structure variable Efi of which the type is struct EpolFdInfo, and the structure variable Efi is used for recording to-be-monitored file handle information of epollfd created by calling epollcreate; the method comprises the following steps of: firstly, writing an initialization function, initializing Efi, and finally, according to an epoll standard, operating the Efi, calling select, packaging interfaces such as epoll create, epoll ctl, epoll wait and the like, so as to realize corresponding functions of the epoll. According to the method, the kernel of the operating system does not need to be modified, and the smooth adaptation of the software adopting the epoll mechanism can be realized based on the select function and the interface of the operating system.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The invention belongs to the technical field of multi-channel IO multiplexing, and in particular relates to a method for simulating an epoll interface by using select. Background Art

[0002] epoll is an efficient IO multiplexing mechanism, which has replaced select as the majority of software under Linux. However, most embedded operating systems have not yet implemented the epoll mechanism. Implementing the epoll mechanism requires modifying the operating system kernel, and related drivers also need to be modified synchronously, which is a huge workload. This brings great inconvenience to the porting of some software using the epoll mechanism. Summary of the invention

[0003] The purpose of the present invention is to provide a method for implementing the epoll interface based on the original select interface encapsulation without modifying the operating system kernel and driver, so as to solve the problem of smooth migration of software using the epoll interface. Tests and verifications show that compared with select, its response time has no obvious loss, and the smooth migration of the original application based on the epoll interface can be guaranteed.

[0004] In order to achieve the purpose of the present invention, the present invention discloses a method of simulating an epoll interface by using select, comprising the following steps:

[0005] Step 1, declare a data structure struct Epoll_Fd_Info for recording the file handle information data structure to be monitored by epollfd created by calling epoll_create, and declare a structure array Efi of type struct Epoll_Fd_Info;

[0006] Step 2: According to the epoll interface standard, encapsulate and implement the function epoll_create by setting the Efi related members;

[0007] Step 3: According to the epoll interface standard, encapsulate and implement the function epoll_ctl by setting the Efi related members;

[0008] Step 4: According to the epoll interface standard and Efi related members, encapsulate and implement the function epoll_wait by calling select.

[0009] Furthermore, the number of file handles that epoll can handle is FDSET_MAX, the number of file handles that select can handle under the corresponding operating system.

[0010] Furthermore, the struct Epoll_Fd_Info contains the following members: the int type member efd, which is used to save the epoll handle returned by epoll_create; the struct epoll_event type array member events[FDSET_MAX], which is used to save the event variables added, deleted, and modified by epoll_ctl; the int type array member fds[FDSET_MAX], which is used to save the int type variable fd added, deleted, and modified by epoll_ctl, and fds[] corresponds to events[] one by one; the int type member maxfd, which is used to save the maximum value of the elements in fds[]; the binary semaphore type member semBId, which is used to protect the critical section when accessing fd[] and events[].

[0011] Furthermore, in step 2, the interface form of the encapsulated function epoll_create is exactly the same as that of Linux, and the internal implementation includes the following steps:

[0012] Step 2-1, initialize the member array efd of Efi, and assign efd to 0xA55A or other agreed values;

[0013] Step 2-2, initialize the member array events[FDSET_MAX] of Efi, and assign all members of events to 0;

[0014] Step 2-3, initialize the member array fds[FDSET_MAX] of Efi, and assign each element of the array fds to -1;

[0015] Step 2-4, initialize the member binary semaphore maxfd of Efi, the initial value of maxfd is -1;

[0016] Step 2-5, initialize the member binary semaphore semBId of Efi, the initial value of semBId is 1;

[0017] Step 2-6: Return the value of the member efd of Efi.

[0018] Furthermore, in step 3, the interface form of the encapsulated function epoll_ctl is exactly the same as that of Linux, and the internal implementation includes the following steps:

[0019] Step 3-1, determine whether the first parameter epfd is equal to Efi.efd, if not, jump to step 3-7, if equal, jump to step 3-2;

[0020] Step 3-2: Determine whether the third parameter fd is in the interval [0, FDSET_MAX-1]. If it is not in the interval, jump to step 3-7; if it is in the interval, jump to step 3-3.

[0021] Step 3-3, wait for the semaphore Efi.semBId to be 1, and jump to step 3-4 after successful acquisition;

[0022] Step 3-4: According to the value of the second parameter op, if op is to add or modify a handle to be monitored, the third parameter fd is used as the subscript, and the fourth parameter event is copied to Efi.events[fd], and Efi.fds[fd] is assigned a value of 1, and it is determined whether the third parameter fd is greater than maxfd. If it is greater, maxfd is assigned a value equal to fd; if op is to delete a handle to be monitored, the third parameter fd is used as the subscript, Efi.events[fd] is cleared, and Efi.fds[fd] is assigned a value of -1; jump to step 3-5;

[0023] Step 3-5, release the semaphore Efi.semBId; jump to step 3-6;

[0024] Step 3-6, return success, end;

[0025] Step 3-7, return error, end.

[0026] Furthermore, in step 4, the interface form of the encapsulated function epoll_wait is exactly the same as that of Linux, and the internal implementation includes the following steps:

[0027] Step 4-1, determine whether the first parameter epfd is equal to Efi.efd, if not equal, jump to step 4-10, if equal, jump to step 4-2;

[0028] Step 4-2: Clear the three file handle sets fdsetRead, fdsetWrite, and fdsetExcept required for the select call; assign the first parameter iwidth of the select call to 0;

[0029] Step 4-3, wait for the semaphore Efi.semBId to be 1, and jump to step 4-4 after successful acquisition;

[0030] Step 4-4, traverse Efi.efd[], if Efi.efd[i] (i∈[0,maxfd]), if Efi.efd[i] is equal to 1, then according to the situation of Efi.events[i], add i to fdsetRead, fdsetWrite, fdsetExcept, and assign iwidth equal to i;

[0031] Step 4-5, release the semaphore Efi.semBId; jump to step 4-6;

[0032] Step 4-6, if the fourth parameter timeout is 0, the fifth parameter ts of select is assigned a null pointer; otherwise, timeout is converted to an equivalent struct timeval type and assigned to ts; jump to step 4-7;

[0033] Step 4-7, call select with iwdth, fdsetRead, fdsetWrite, fdsetExcept, ts as parameters; after returning, jump to step 4-8;

[0034] Step 4-8, after select returns, traverse the values ​​of fdsetRead, fdsetWrite, and fdsetExcept, and update the value of the second parameter events of epoll_wait; jump to step 4-9;

[0035] Step 4-9, return success, end;

[0036] Step 4-10, return error, end.

[0037] Compared with the prior art, the significant improvements of the present invention are: 1) it does not need to modify the operating system kernel or the driver to implement support for the epoll interface on a system that supports the select interface; 2) it can effectively reduce the porting of some software using the epoll mechanism to non-Linux operating systems.

[0038] In order to more clearly illustrate the functional characteristics and structural parameters of the present invention, further description is given below in conjunction with the accompanying drawings and specific implementation methods. BRIEF DESCRIPTION OF THE DRAWINGS

[0039] The drawings described herein are used to provide a further understanding of the present invention and constitute a part of this application. The exemplary embodiments of the present invention and their descriptions are used to explain the present invention and do not constitute an improper limitation of the present invention. In the drawings:

[0040] Figure 1 A schematic diagram of the member types and functions of the structure Epoll_Fd_Info of the present invention;

[0041] Figure 2 This is a schematic diagram of the epoll_create encapsulation process of the present invention;

[0042] Figure 3 This is a schematic diagram of the epoll_ctl encapsulation process of the present invention;

[0043] Figure 4 This is a schematic diagram of the epoll_wait encapsulation process of the present invention;

[0044] Figure 5It is a schematic diagram of the overall process of the present invention. DETAILED DESCRIPTION

[0045] The technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, rather than all the embodiments; based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of the present invention.

[0046] like Figure 5 As shown, a method of using select to simulate the epoll interface includes the following steps:

[0047] Step 1, declare a data structure struct Epoll_Fd_Info for recording the file handle information data structure to be monitored by epollfd created by calling epoll_create, and declare a structure array Efi of type struct Epoll_Fd_Info;

[0048] Step 2: According to the epoll interface standard, encapsulate and implement the function epoll_create by setting the Efi related members;

[0049] Step 3: According to the epoll interface standard, encapsulate and implement the function epoll_ctl by setting the Efi related members;

[0050] Step 4: According to the epoll interface standard and Efi related members, encapsulate and implement the function epoll_wait by calling select.

[0051] like Figure 1 As shown in the figure, the member types and functions of the structure Epoll_Fd_Info are as follows:

[0052] 1 integer member efd, used to save the epoll handle returned by epoll_create;

[0053] 2struct epoll_event structure number array member events[FDSET_MAX] is used to save the event variables added, deleted and modified by epoll_ctl;

[0054] 3 The integer array member fds[FDSET_MAX] is used to save the int variable fd added, deleted, and modified by epoll_ctl. fds[] corresponds to events[] one by one.

[0055] 4 The integer member maxfd is used to record the maximum value of the elements in fds[];

[0056] 5 The binary semaphore member semBId is used to protect the critical section when accessing fd[] and events[].

[0057] like Figure 2-4 As shown, Figure 2 , 3 4 respectively give the encapsulated epoll three interface function flow charts, including the following steps:

[0058] Step S1, the number of file handles that epoll can process is the number of file handles that select can process under the corresponding operating system FDSET_MAX, declare a data structure struct Epoll_Fd_Info for recording the file handle information data structure to be monitored by epollfd created by calling epoll_create, and declare a structure array Efi of type struct Epoll_Fd_Info; struct Epoll_Fd_Info includes the following members: int type member efd, used to save the epoll handle returned by epoll_create; struct epoll_event type array member events[FDSET_MAX], used to save the event variable added, deleted, and modified by epoll_ctl; int type array member fds[FDSET_MAX], used to save the int type variable fd added, deleted, and modified by epoll_ctl, fds[] corresponds to events[] one by one; int type member maxfd, used to save the maximum value of the elements in fds[]; binary semaphore type member semBId, used for critical section protection when accessing fd[] and events[].

[0059] Step S2. Encapsulate and implement the function epoll_create. The interface form is exactly the same as that of Linux. The internal implementation includes the following steps:

[0060] Step S2-1, initialize the member array efd of Efi, and assign efd a value of 0xA55A or other agreed values;

[0061] Step S2-2, initialize the member array events[FDSET_MAX] of Efi, and assign all members of events to 0;

[0062] Step S2-3, initialize the member array fds[FDSET_MAX] of Efi, and assign a value of -1 to each element of the array fds;

[0063] Step S2-4, initialize the member binary semaphore maxfd of Efi, the initial value of maxfd is -1;

[0064] Step S2-5, initialize the member binary semaphore semBId of Efi, the initial value of semBId is 1;

[0065] Step S2-6, return the value of the member efd of Efi;

[0066] Step S3, encapsulate and implement the function epoll_ctl, the interface form is exactly the same as that of Linux, and the internal implementation includes the following steps:

[0067] Step S3-1, determine whether the first parameter epfd is equal to Efi.efd, if not, jump to step S3-7, if equal, jump to step S3-2;

[0068] Step S3-2, determine whether the third parameter fd is in the interval [0, FDSET_MAX-1]. If it is not in the interval, jump to step S3-7. If it is in the interval, jump to step S3-3.

[0069] Step S3-3, wait for the semaphore Efi.semBId to be 1, and jump to step S3-4 after successful acquisition;

[0070] Step S3-4, according to the value of the second parameter op, if op is to add or modify a handle to be monitored, the third parameter fd is used as the subscript, the fourth parameter event is copied to Efi.events[fd], Efi.fds[fd] is assigned a value of 1, and it is determined whether the third parameter fd is greater than maxfd. If it is greater, maxfd is assigned a value equal to fd; if op is to delete a handle to be monitored, the third parameter fd is used as the subscript, Efi.events[fd] is cleared, and Efi.fds[fd] is assigned a value of -1; jump to step S3-5;

[0071] Step S3-5, release the semaphore Efi.semBId; jump to step S3-6;

[0072] Step S3-6, return success, end;

[0073] Step S3-7, return error and end.

[0074] Step S4, encapsulate and implement the function epoll_wait, the interface form is exactly the same as that of Linux, and the internal implementation includes the following steps:

[0075] Step S4-1, determine whether the first parameter epfd is equal to Efi.efd, if not, jump to step S4-10, if equal, jump to step S4-2;

[0076] Step S4-2, clear the three file handle sets fdsetRead, fdsetWrite, fdsetExcept required for the select call; assign the first parameter iwidth of the select call to 0;

[0077] Step S4-3, wait for the semaphore Efi.semBId to be 1, and jump to step S4-4 after successful acquisition;

[0078] Step S4-4, traverse Efi.efd[], if Efi.efd[i] (i∈[0,maxfd]), if Efi.efd[i] is equal to 1, then according to the situation of Efi.events[i], add i to fdsetRead, fdsetWrite, fdsetExcept, and assign iwidth equal to i;

[0079] Step S4-5, release the semaphore Efi.semBId; jump to step S4-6;

[0080] Step S4-6, if the fourth parameter timeout is 0, the fifth parameter ts of select is assigned a null pointer; otherwise, timeout is converted to an equivalent struct timeval type and assigned to ts; jump to step S4-7;

[0081] Step S4-7, call select with iwdth, fdsetRead, fdsetWrite, fdsetExcept, ts as parameters; after returning, jump to step S4-8;

[0082] Step S4-8, after select returns, traverse the values ​​of fdsetRead, fdsetWrite, and fdsetExcept, update the value of the second parameter events of epoll_wait, and jump to step S9;

[0083] Step S4-9, return success, end;

[0084] Step S4-10, return error and end.

[0085] It should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device.

[0086] Although embodiments of the present invention have been shown and described, it will be appreciated by those skilled in the art that various changes, modifications, substitutions and variations may be made to the embodiments without departing from the principles and spirit of the present invention, and that the scope of the present invention is defined by the appended claims and their equivalents.

Claims

1. A method of simulating an epoll interface using select, characterized in that: The following steps are involved: Step 1, declare a data structure struct Epoll_Fd_Info for recording the file handle information data structure to be monitored by epollfd created by calling epoll_create, and declare a structure array Efi of type struct Epoll_Fd_Info; Step 2: According to the epoll interface standard, encapsulate and implement the function epoll_create by setting the Efi related members; Step 3: According to the epoll interface standard, encapsulate and implement the function epoll_ctl by setting the Efi related members; Step 4: According to the epoll interface standard and Efi related members, encapsulate and implement the function epoll_wait by calling select.

2. According to claim 1, a method for simulating an epoll interface using select, characterized in that: The number of file handles that epoll can handle is FDSET_MAX, the number of file handles that select can handle under the corresponding operating system.

3. The method of using select to simulate epoll interface according to claim 1, characterized in that: The struct Epoll_Fd_Info includes the following members: an int member efd, used to save the epoll handle returned by epoll_create; an array member events[FDSET_MAX] of struct epoll_event type, used to save the event variable added, deleted or modified by epoll_ctl; an int array member fds[FDSET_MAX], used to save the int variable fd added, deleted or modified by epoll_ctl, and fds[] corresponds to events[] one by one; an int member maxfd, used to save the maximum value of the elements in fds[]; and a binary semaphore type member semBId, used for critical section protection when accessing fd[] and events[].

4. The method of using select to simulate epoll interface according to claim 1, characterized in that: In step 2, the encapsulated function epoll_create interface is exactly the same as that of Linux, and the internal implementation includes the following steps: Step 2-1, initialize the member array efd of Efi, and assign efd to 0xA55A or other agreed values; Step 2-2, initialize the member array events[FDSET_MAX] of Efi, and assign all members of events to 0; Step 2-3, initialize the member array fds[FDSET_MAX] of Efi, and assign each element of the array fds to -1; Step 2-4, initialize the member binary semaphore maxfd of Efi, the initial value of maxfd is -1; Step 2-5, initialize the member binary semaphore semBId of Efi, the initial value of semBId is 1; Step 2-6: Return the value of the member efd of Efi.

5. The method of using select to simulate epoll interface according to claim 1, characterized in that: In step 3, the interface form of the encapsulated function epoll_ctl is exactly the same as that of Linux, and the internal implementation includes the following steps: Step 3-1, determine whether the first parameter epfd is equal to Efi.efd, if not, jump to step 3-7, if equal, jump to step 3-2; Step 3-2: Determine whether the third parameter fd is in the interval [0, FDSET_MAX-1]. If it is not in the interval, jump to step 3-7; if it is in the interval, jump to step 3-3. Step 3-3, wait for the semaphore Efi.semBId to be 1, and jump to step 3-4 after successful acquisition; Step 3-4: According to the value of the second parameter op, if op is to add or modify a handle to be monitored, the third parameter fd is used as the subscript, and the fourth parameter event is copied to Efi.events[fd], and Efi.fds[fd] is assigned a value of 1, and it is determined whether the third parameter fd is greater than maxfd. If it is greater, maxfd is assigned a value equal to fd; if op is to delete a handle to be monitored, the third parameter fd is used as the subscript, Efi.events[fd] is cleared, and Efi.fds[fd] is assigned a value of -1; jump to step 3-5; Step 3-5, release the semaphore Efi.semBId; jump to step 3-6; Step 3-6, return success, end; Step 3-7, return error, end.

6. The method of using select to simulate epoll interface according to claim 1, characterized in that: In step 4, the interface form of the encapsulated function epoll_wait is exactly the same as that of Linux, and the internal implementation includes the following steps: Step 4-1, determine whether the first parameter epfd is equal to Efi.efd, if not equal, jump to step 4-10, if equal, jump to step 4-2; Step 4-2: Clear the three file handle sets fdsetRead, fdsetWrite, and fdsetExcept required for the select call; assign the first parameter iwidth of the select call to 0; Step 4-3, wait for the semaphore Efi.semBId to be 1, and jump to step 4-4 after successful acquisition; Step 4-4, traverse Efi.efd[], if Efi.efd[i] (i∈[0,maxfd]), if Efi.efd[i] is equal to 1, then according to the situation of Efi.events[i], add i to fdsetRead, fdsetWrite, In fdsetExcept, iwidth is assigned the value equal to i; Step 4-5, release the semaphore Efi.semBId; jump to step 4-6; Step 4-6, if the fourth parameter timeout is 0, the fifth parameter ts of select is assigned a null pointer; otherwise, timeout is converted to an equivalent struct timeval type and assigned to ts; jump to step 4-7; Step 4-7, call select with iwdth, fdsetRead, fdsetWrite, fdsetExcept, ts as parameters; after returning, jump to step 4-8; Step 4-8, after select returns, traverse the values ​​of fdsetRead, fdsetWrite, and fdsetExcept, and update the value of the second parameter events of epoll_wait; jump to step 4-9; Step 4-9, return success, end; Step 4-10, return error, end.