CN115658156A - Timed task implementation method and device based on linux system - Google Patents

Timed task implementation method and device based on linux system Download PDF

Info

Publication number
CN115658156A
CN115658156A CN202211350553.0A CN202211350553A CN115658156A CN 115658156 A CN115658156 A CN 115658156A CN 202211350553 A CN202211350553 A CN 202211350553A CN 115658156 A CN115658156 A CN 115658156A
Authority
CN
China
Prior art keywords
timing
timer
task
user process
overtime
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202211350553.0A
Other languages
Chinese (zh)
Inventor
季栋辉
于波
叶臻
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
China Automotive Innovation Co Ltd
Original Assignee
China Automotive Innovation Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by China Automotive Innovation Co Ltd filed Critical China Automotive Innovation Co Ltd
Priority to CN202211350553.0A priority Critical patent/CN115658156A/en
Publication of CN115658156A publication Critical patent/CN115658156A/en
Pending legal-status Critical Current

Links

Images

Landscapes

  • Debugging And Monitoring (AREA)

Abstract

The invention relates to a method and a device for realizing a timing task based on a linux system, wherein the method comprises the following steps of: a timing service process receives a plurality of timing requests sent by a first user process; the timing request comprises the index number of each timing task, the timing duration of each timing task and the identification of the first user process; respectively allocating a target timer for each timing task from the timer pool and initializing the target timer; circularly traversing each timer in the timer pool according to a preset time interval; when detecting that the target timer is set with the timeout marker, sending a timeout notification message to the first user process in an interprocess communication mode; the overtime notification message is used for indicating the first user process to execute the overtime callback function corresponding to the timing task. According to the scheme, the first user process executes the timeout callback function, the timeout callback function does not need to be executed in the signal processing function, and the implementation of a plurality of timing tasks can be supported in one timing service process.

Description

Timed task implementation method and device based on linux system
Technical Field
The invention relates to the technical field of computers, in particular to a timed task implementation method and device based on a linux system.
Background
The timing function is the most basic function in the meter software system, and most processes need to process some periodic tasks or perform some overtime detection, which cannot be separated from the timing function.
The use method of the timer interface provided by the Linux operating system comprises the following steps: and generating a signal after the timer is overtime, and executing a timer overtime callback function in the signal processing function. If the first method is adopted, when a plurality of timers are started and the same signal is generated, a conflict occurs, and the signal processing function cannot know which timer is generated by the current signal. If different timers are allowed to generate different signals, there is a problem of insufficient signals because Linux only provides two custom signals for users to use.
Therefore, the timer interface provided by the Linux system is difficult to support the implementation of a plurality of timing tasks in one process.
Disclosure of Invention
The present invention is directed to solving at least one of the problems of the prior art.
To this end, a first aspect of the present invention provides a method for implementing a timed task based on a linux system, which is applied to a timed service process, and the method includes:
receiving a plurality of timing requests sent by a first user process; the timing request comprises an index number of each timing task, timing duration of each timing task and an identifier of the first user process;
respectively allocating a target timer for each timing task from a timer pool and initializing the target timer;
circularly traversing each timer in the timer pool according to a preset time interval;
when detecting that the target timer is set with a timeout marker, sending a timeout notification message to the first user process in an inter-process communication mode; the overtime notification message comprises an index number of a timing task which reaches the timing duration and an identifier of the first user process; the timeout notification message is used for instructing the first user process to execute a timeout callback function corresponding to the timing task.
In one possible embodiment, the method further comprises:
receiving a timing closing request sent by the first user process;
determining a corresponding timer according to the index number of the timing task and the identification of the first user process included in the timing closing request;
and closing the corresponding timer.
In a possible implementation, the timing request further includes a timing pattern, the timing pattern includes a first pattern for representing a single timer and a second pattern for representing a cycle timer, and the cycle of traversing each timer in the timer pool according to a preset time interval includes:
circularly traversing each timer in the timer pool according to a preset time interval, and decreasing the count value of the working timer during each traversal;
if the timing mode is the first mode, setting an overtime mark for the timer when the count value of the timer is reduced to 0;
and if the timing mode is the second mode, reloading the count value of the timer and setting an overtime mark for the timer when the count value of the timer is reduced to 0.
The second aspect of the present invention provides a timing task implementation method based on a linux system, which is applied to a first user process, and the method includes:
sending a timing request to a timing service process, wherein the timing request comprises an index number of each timing task, a timing duration of each timing task and an identifier of the first user process;
selecting an idle list position from a timing task list initialized in advance to store the index number, the list position use state mark and the overtime callback function of each timing task;
receiving an overtime notification message returned by the timing service process, wherein the overtime notification message comprises the index number of the timing task reaching the timing duration and the identifier of the first user process;
and determining an overtime callback function according to the index number of the timing task, and executing the overtime callback function.
In a possible implementation, after executing the timeout callback function, the method further includes:
and marking the position in the timing task list corresponding to the index number as idle.
In a possible implementation, before sending the timing request to the timing service process, the method further includes:
determining type parameters of each timing task; the type parameters comprise a first parameter and a second parameter, the first parameter is used for representing timing tasks with short execution time and high frequency, and the second parameter is used for representing timing tasks with long execution time and low frequency;
the executing the timeout callback function includes:
if the type parameter is the first parameter, executing the overtime callback function in a message receiving thread;
and if the type parameter is the second parameter, creating a new thread to execute the timeout callback function.
In a possible embodiment, the timing request further includes a timing pattern, and the timing pattern includes a first pattern for indicating a cycle timer and a second pattern for indicating a one-time timer.
The third aspect of the present invention provides a timing task implementing apparatus based on linux system, which is applied to timing service process, and the apparatus includes:
a timing request receiving module, configured to receive multiple timing requests sent by a first user process; the timing request comprises index numbers of all timing tasks, timing duration of all timing tasks and identification of the first user process;
the distribution module is used for distributing a target timer for each timing task from a timer pool and initializing the target timer;
the traversing module is used for circularly traversing each timer in the timer pool according to a preset time interval;
a message sending module, configured to send a timeout notification message to the first user process in an inter-process communication manner when detecting that the timeout flag is set in the target timer; the overtime notification message comprises an index number of a timing task which reaches the timing duration and an identifier of the first user process; and the overtime notification message is used for indicating the first user process to execute an overtime callback function corresponding to the timing task.
In a possible embodiment, the apparatus further comprises:
a closing timing request receiving module, configured to receive a closing timing request sent by the first user process;
a timer determining module, configured to determine a corresponding timer according to the index number of the timing task and the identifier of the first user process included in the timing closing request;
and the closing module is used for closing the corresponding timer.
In a possible implementation, the timing request further includes a timing pattern, the timing pattern includes a first pattern for indicating a one-time timer and a second pattern for indicating a cycle timer, and the traversing module is specifically configured to:
circularly traversing each timer in the timer pool according to a preset time interval, and decreasing the count value of the working timer during each traversal;
if the timing mode is the first mode, setting an overtime mark for the timer when the count value of the timer is reduced to 0;
and if the timing mode is the second mode, reloading the count value of the timer and setting an overtime mark for the timer when the count value of the timer is reduced to 0.
The fourth aspect of the present invention provides a timing task implementing device based on a linux system, which is applied to a first user process, and the device includes:
a timing request sending module, configured to send a timing request to a timing service process, where the timing request includes an index number of each timing task, a timing duration of each timing task, and an identifier of the first user process;
the storage module is used for selecting an idle list position from a timing task list initialized in advance to store the index number, the list position use state mark and the overtime callback function of each timing task;
a message receiving module, configured to receive an timeout notification message returned by the timing service process, where the timeout notification message includes an index number of a timing task that has reached the timing duration and an identifier of the first user process;
and the execution module is used for determining an overtime callback function according to the index number of the timing task and executing the overtime callback function.
In one possible embodiment, the apparatus further comprises:
and the marking module is used for marking the position in the timing task list corresponding to the index number as idle.
In a possible embodiment, the apparatus further comprises:
the type parameter determining module is used for determining the type parameter of each timing task; the type parameters comprise a first parameter and a second parameter, the first parameter is used for representing timing tasks with short execution time and high frequency, and the second parameter is used for representing timing tasks with long execution time and low frequency;
the execution module is specifically configured to:
if the type parameter is the first parameter, executing the overtime callback function in a message receiving thread;
and if the type parameter is the second parameter, creating a new thread to execute the timeout callback function.
A fifth aspect of the present invention provides an electronic device, which includes a processor and a memory, where the memory stores at least one instruction, at least one program, a set of codes, or a set of instructions, and the at least one instruction, the at least one program, the set of codes, or the set of instructions is loaded and executed by the processor to implement the linux system-based timed task implementation method according to the first aspect or the second aspect.
A sixth aspect of the present invention provides a computer-readable storage medium, in which at least one instruction, at least one program, a code set, or a set of instructions is stored, and the at least one instruction, the at least one program, the code set, or the set of instructions is loaded and executed by a processor to implement the linux system-based timed task implementation method according to the first aspect or the second aspect.
The embodiment of the invention has the following beneficial effects:
in the embodiment of the invention, a timing service process receives a plurality of timing requests sent by a first user process; the timing request comprises an index number of each timing task, timing duration of each timing task and an identifier of the first user process; respectively allocating a target timer for each timing task from a timer pool and initializing the target timer; circularly traversing each timer in the timer pool according to a preset time interval; when detecting that the target timer is set with a timeout marker, sending a timeout notification message to the first user process in an inter-process communication mode; the overtime notification message comprises an index number of a timing task which reaches the timing duration and an identifier of the first user process; and the overtime notification message is used for indicating the first user process to execute an overtime callback function corresponding to the timing task. According to the scheme, a linux native timer interface is used as a basis, a timing service process receives a plurality of timing requests sent by a first user process, when the timing time reaches, a plurality of overtime notification messages are respectively sent to each first user process, and an overtime callback function is executed by the first user process, so that the realization of a plurality of timing tasks can be supported in one timing service process.
Additional aspects and advantages of the invention will be set forth in part in the description which follows and, in part, will be obvious from the description, or may be learned by practice of the invention.
Drawings
In order to more clearly illustrate the technical solution of the present invention, the drawings used in the description of the embodiment or the prior art will be briefly described below. It is obvious that the drawings in the following description are only some embodiments of the invention, and that for a person skilled in the art it is also possible to derive other drawings from them without inventive effort.
Fig. 1 is a flowchart of steps of a first method for implementing a timed task based on a linux system according to an embodiment of the present invention;
FIG. 2 is a schematic diagram of a timing service process cycling through a timer pool;
fig. 3 is a sequence diagram of a start timer according to an embodiment of the present invention;
fig. 4 is a flowchart illustrating steps of a second method for implementing a timed task based on a linux system according to an embodiment of the present invention;
fig. 5 is a block diagram of a first timing task implementation apparatus based on a linux system according to an embodiment of the present invention;
fig. 6 is a block diagram of a second timing task implementation apparatus based on a linux system according to an embodiment of the present invention.
Detailed Description
The technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are only a part of the embodiments of the present invention, and not all of the embodiments. All other embodiments, which can be obtained by a person skilled in the art without any inventive step based on the embodiments of the present invention, are within the scope of the present invention.
The present specification provides method steps as described in the examples or flowcharts, but more or fewer steps may be included based on routine or non-invasive labor. In practice, the system or server product may be implemented in a sequential or parallel manner (e.g., parallel processor or multi-threaded environment) according to the embodiments or methods shown in the figures.
Fig. 1 is a flowchart illustrating steps of a first method for implementing a timed task based on a linux system according to an embodiment of the present invention. The method comprises the following steps:
step 101, receiving a plurality of timing requests sent by a first user process; the timing request comprises the index number of each timing task, the timing duration of each timing task and the identification of the first user process.
The first user process is a process with timing requirements, and sends a timing request to the timing service process by calling a timer interface, wherein the timing request may include a plurality of timing tasks. The message format of the timing request message may be as follows:
Figure BDA0003918690620000071
wherein the switch command has two valid values, 0 represents the off timer and 1 represents the start timer. The process ID represents an identification of a different first user process to distinguish the first user process. The timing duration represents the duration of time that the task needs to be timed. The index number of the timing task indicates the index number of the timing task in the timing task list. The timing pattern has two valid values, 0 for a round-robin timer and 1 for a one-time timer.
And 102, respectively allocating a target timer for each timing task from a timer pool and initializing the target timer.
A plurality of timers are arranged in the timer pool. When the timing task needs to be executed, a timer is taken from the timing task for use. If the timer in the timer pool is idle, taking out the idle timer to execute the task, wherein the timer for executing the task is a target timer; the timer pool, if busy, will not respond to the timing request. When the system is designed by using the method, the size of the timer pool is configured according to actual requirements, and the quantity of the timers is ensured to be sufficient.
Initializing the target timer mainly includes setting basic parameters of the timer, including setting a counting mode, a counting period, a use state flag, and the like.
And 103, circularly traversing each timer in the timer pool according to a preset time interval.
The timing service process executes the action of circularly traversing each timer in the timer pool every time the timing service process receives the signal sent by the reference timer.
FIG. 2 is a schematic diagram of a timing service process cycling through a pool of timers.
As shown in fig. 2, after the loop starts, it is first determined whether All timers have been processed (i.e., whether All timer count values have been decremented by 1) (All timers disposed: it is determined whether the Current timer is idle (Current timer idle. It is determined at this time whether the current timer count is 0 (Counter = 0. Then, it is determined whether the Current timer is a loop timer (Current timer is loop. If not, the process is executed for the next timer until all timers are processed.
In a possible embodiment, the timing request further includes a timing pattern, the timing pattern includes a first pattern for representing a single timer and a second pattern for representing a cycle timer, and the cycle is performed through each timer in the timer pool according to a preset time interval, and the method includes the following steps:
step 1031, circularly traversing each timer in the timer pool according to a preset time interval, and decreasing the count value of the timer in operation in each traversal;
step 1032, if the timing mode is the first mode, setting a timeout flag to the timer when the count value of the timer is reduced to 0;
step 1033, if the timing mode is the second mode, reloading the count value of the timer and setting a timeout flag for the timer when the count value of the timer is decreased to 0.
In steps 1031 to 1033, the preset time interval is set to be 10ms, for example, and all timers in the timer pool are cycled through every preset time interval, and the count value of the timer in operation is decreased by 1.
If the timing mode is the first mode, the timer is a one-time timer, and when the count value of the timer is reduced to 0, a timeout flag is set for the timer. If the timing mode is the second mode, the cycle timer is represented, and when the count value of the timer is reduced to 0, the timeout flag is set for the timer and the current timer is reloaded.
Step 104, when detecting that the target timer is set with a timeout flag, sending a timeout notification message to the first user process in an inter-process communication mode; the overtime notification message comprises an index number of a timing task which reaches the timing duration and an identifier of the first user process; and the overtime notification message is used for indicating the first user process to execute an overtime callback function corresponding to the timing task.
And when detecting that the target timer is set with the timeout marker, generating a timeout notification message, and sending the timeout notification message to the first user process corresponding to the target timer in an interprocess communication mode.
The message format of the timeout notification message is as follows:
process ID Timed task index number
4 bytes 2 bytes
The process ID represents the identification of the first user process, and the index number of the timing task represents the index number of the timing task which reaches the timing duration in the timer list.
In one possible embodiment, the method further comprises:
receiving a timing closing request sent by the first user process;
determining a corresponding timer according to the index number of the timing task and the identification of the first user process included in the timing closing request;
and closing the corresponding timer.
In the embodiment of the present invention, if the switch command of the call request is a command parameter corresponding to the "close timer", the timing request is a close timing request. The timing service process can be positioned to the corresponding timing task through the index number and the identification of the first user process, so that the timer corresponding to the timing task is found. The timer is then turned off.
Fig. 3 is a sequence diagram of a start timer according to an embodiment of the present invention.
As shown in fig. 3, the regular thread (timer user's normal thread) of the first user process sends a timing request (send "time application" message) to the timing service process by calling the timing service interface, and the message receiving thread (timer user's timer message receiving thread) of the timing service process allocates a timer to the timing request and initializes the timer after receiving the timing request. When the timing time arrives, the reference timer processing thread (timer server's base timer handle thread) of the timing service process will set the timeout flag (set "timeout" flag) of this timer. When a timer server's timeout notification thread of the timer process finds that the timeout flag of the timer is set, a timer timeout notification message is sent to the first user process, and after receiving the notification, the message receiving thread of the first user process executes a callback function (execute callback function). And when the callback is executed, determining whether to execute the timeout callback function in the current thread or create a new thread to execute the timeout callback function according to parameters transmitted when the first user process calls the timer interface.
In summary, in the embodiment of the present invention, the timing service process receives a plurality of timing requests sent by the first user process; the timing request comprises index numbers of all timing tasks, timing duration of all timing tasks and identification of the first user process; respectively allocating a target timer for each timing task from a timer pool and initializing the target timer; circularly traversing each timer in the timer pool according to a preset time interval; when detecting that the target timer is set with a timeout marker, sending a timeout notification message to the first user process in an inter-process communication mode; the overtime notification message comprises an index number of a timing task which reaches the timing duration and an identifier of the first user process; the timeout notification message is used for instructing the first user process to execute a timeout callback function corresponding to the timing task. According to the scheme, a linux native timer interface is used as a basis, a timing service process receives a plurality of timing requests sent by a first user process, when the timing time reaches, a plurality of overtime notification messages are respectively sent to each first user process, and an overtime callback function is executed by the first user process, so that the realization of a plurality of timing tasks can be supported in one timing service process.
Fig. 4 is a flowchart of steps of a second method for implementing a timed task based on a linux system according to an embodiment of the present invention.
The method is applied to a first user process, and as shown in fig. 4, the method comprises the following steps:
step 201, sending a timing request to a timing service process, where the timing request includes an index number of each timing task, a timing duration of each timing task, and an identifier of the first user process.
The first user process is a process with timing requirements, the first user process sends timing requests to the timing service process by calling a timer interface, and one timing request comprises information of one timing task.
Specifically, the index number of the timing task, the timing duration of each timing task, and the identifier of the first user process are included.
Step 202, selecting an idle list position from a timing task list initialized in advance to store the index number, the list position use state mark and the overtime callback function of each timing task.
Specifically, the list format of the timed task list may be as follows:
Timed task0 Timed task1
Callback:functionA Callback:Null
Used:true Used:false
the Timed task indicates a timing task index number, and the timing task index number is automatically generated when the list is created. Callback represents a timeout Callback function, and Used represents a usage status flag for the list location.
If the list position is occupied, the Used value is true, and the Callback value is a specific timeout Callback function.
If the list position is free, the Used value is false and the Callback value is NULL.
Step 203, receiving an overtime notification message returned by the timing service process, where the overtime notification message includes the index number of the timing task having reached the timing duration and the identifier of the first user process.
And when the timing service process detects that the target timer is set with the timeout marker, generating a timeout notification message and sending the timeout notification message to the first user process. The timeout notification message includes an index number of the timing task, so that the first user process determines the corresponding timing task through the index number.
And 204, determining an overtime callback function according to the index number of the timing task, and executing the overtime callback function.
And the first user process inquires the timing task corresponding to the index number and the timeout callback function corresponding to the timing task from the timing task list and executes the timeout callback function.
In a possible implementation manner, after the executing the timeout callback function, the method further includes:
and 205, marking the position in the timing task list corresponding to the index number as idle.
Specifically, the first user marks the position in the timing task list corresponding to the index number of the timing task as idle only after executing the timeout callback function of the timing task, so that when a new timing task exists, the idle position can be used for storing new timing task information.
In a possible implementation, before sending the timing request to the timing service process, the method further includes:
step 301, determining type parameters of each timing task; the type parameters comprise a first parameter and a second parameter, the first parameter is used for representing timing tasks with short execution time and high frequency, and the second parameter is used for representing timing tasks with long execution time and low frequency;
the executing the timeout callback function includes:
step 302, if the type parameter is the first parameter, executing the timeout callback function in a message receiving thread;
step 303, if the type parameter is the second parameter, creating a new thread to execute the timeout callback function.
In the prior art, a timer interface provided by the Linux operating system is used in a method that a new thread is created after a timer expires, and a timer timeout callback function is executed in the new thread. With this method, when there is a high frequency periodic timing task in the process, the operation of creating a new thread will be frequently executed, which will seriously affect the performance of the system.
In the embodiment of the invention, when the first user process executes the timeout callback function, whether the timeout callback function is executed in the current thread or a new thread is created to execute the timeout callback function is determined according to the type of the parameter transmitted when the timer interface is called.
Specifically, if the parameter type is a first parameter, the timeout callback function is executed in the message receiving thread; and if the parameter type is the second parameter, creating a new thread to execute the timeout callback function. Because the first parameter represents the timing task with short execution time and high frequency, the timing task is executed in the message receiving thread, the requirement of high frequency of the timing task can be met, and thread resources cannot be occupied for a long time. And the second parameter represents a timing task with long execution time and low frequency, and the timing task is executed in a new thread, so that the requirement of the timing task with long execution time is met, and the resources of a normal message receiving thread are not occupied.
Therefore, the first user process determines whether to create a new thread or execute the timing task in the message receiving thread according to the parameter types, and not all timers create the new thread, so that when a high-frequency periodic timing task exists, the operation of creating the thread cannot be frequently executed, and the performance of the system is prevented from being influenced.
In a possible embodiment, the timing request further includes a timing pattern, and the timing pattern includes a first pattern for indicating a cycle timer and a second pattern for indicating a one-time timer.
Specifically, the message format of the timing request further includes a timing mode field to facilitate the timing service process to distinguish the cycle timer from the one-time timer.
In summary, in the embodiment of the present invention, a first user process sends a timing request to a timing service process, where the timing request includes an index number of each timing task, a timing duration of each timing task, and an identifier of the first user process; selecting an idle list position from a timing task list initialized in advance to store the index number, the list position use state mark and the overtime callback function of each timing task; receiving an overtime notification message returned by the timing service process, wherein the overtime notification message comprises the index number of the timing task reaching the timing duration and the identifier of the first user process; and determining an overtime callback function according to the index number of the timing task, and executing the overtime callback function. According to the scheme, the first user process receives the overtime notification message returned by the timing service process and executes the corresponding overtime callback function in the timing task list, so that the realization of a plurality of timing tasks can be supported in one timing service process.
Fig. 5 is a block diagram of a first timing task implementation apparatus based on a linux system according to an embodiment of the present invention.
The apparatus is applied to a timing service process, and as shown in fig. 5, the apparatus 400 includes:
a timing request receiving module 401, configured to receive multiple timing requests sent by a first user process; the timing request comprises an index number of each timing task, timing duration of each timing task and an identifier of the first user process;
an allocating module 402, configured to allocate a target timer from a timer pool for each timing task and initialize the target timer;
a traversing module 403, configured to cycle through each timer in the timer pool according to a preset time interval;
a message sending module 404, configured to send a timeout notification message to the first user process in an inter-process communication manner when detecting that the timeout flag is set in the target timer; the overtime notification message comprises an index number of a timing task which reaches the timing duration and an identifier of the first user process; the timeout notification message is used for instructing the first user process to execute a timeout callback function corresponding to the timing task.
It is clear to those skilled in the art that, for convenience and brevity of description, the specific working processes of the above-described systems, apparatuses and units may refer to the corresponding processes in the foregoing method embodiments, and are not described herein again.
Fig. 6 is a block diagram of a second timing task implementation apparatus based on a linux system according to an embodiment of the present invention.
The apparatus is applied to a first user process, and as shown in fig. 6, the apparatus 500 includes:
a timing request sending module 501, configured to send a timing request to a timing service process, where the timing request includes an index number of each timing task, a timing duration of each timing task, and an identifier of the first user process;
a storing module 502, configured to select an idle list position from a pre-initialized timing task list to store the index number, the list position use state flag, and the timeout callback function of each timing task;
a message receiving module 503, configured to receive an timeout notification message returned by the timing service process, where the timeout notification message includes an index number of a timing task that has reached the timing duration and an identifier of the first user process;
and the execution module 504 is configured to determine an timeout callback function according to the index number of the timing task, and execute the timeout callback function.
It is clear to those skilled in the art that, for convenience and brevity of description, the specific working processes of the above-described systems, apparatuses and units may refer to the corresponding processes in the foregoing method embodiments, and are not described herein again.
In yet another embodiment provided by the present invention, an apparatus is further provided, which includes a processor and a memory, where the memory stores at least one instruction, at least one program, a set of codes, or a set of instructions, and the at least one instruction, the at least one program, the set of codes, or the set of instructions is loaded and executed by the processor to implement the linux system-based timed task implementation method in the embodiment of the present invention.
In yet another embodiment provided by the present invention, a computer-readable storage medium is further provided, in which at least one instruction, at least one program, a code set, or a set of instructions is stored, and the at least one instruction, the at least one program, the code set, or the set of instructions is loaded and executed by a processor to implement the linux system-based timed task implementation method described in the embodiment of the present invention.
In the above embodiments, the implementation may be wholly or partially realized by software, hardware, firmware, or any combination thereof. When implemented in software, may be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When loaded and executed on a computer, cause the processes or functions described in accordance with the embodiments of the invention to occur, in whole or in part. The computer may be a general purpose computer, a special purpose computer, a network of computers, or other programmable device. The computer instructions may be stored in a computer readable storage medium or transmitted from one computer readable storage medium to another, for example, from one website site, computer, server, or data center to another website site, computer, server, or data center via wired (e.g., coaxial cable, fiber optic, digital Subscriber Line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.). The computer-readable storage medium can be any available medium that can be accessed by a computer or a data storage device, such as a server, a data center, etc., that includes one or more available media. The usable medium may be a magnetic medium (e.g., floppy Disk, hard Disk, magnetic tape), an optical medium (e.g., DVD), or a semiconductor medium (e.g., solid State Disk (SSD)), among others.
It should be noted that, in this document, relational terms such as first and second, and the like are used solely to distinguish one entity or action from another entity or action without necessarily requiring or implying any actual such relationship or order between such entities or actions. Also, the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or apparatus. Without further limitation, an element defined by the phrases "comprising a," "8230," "8230," or "comprising" does not exclude the presence of additional like elements in a process, method, article, or apparatus that comprises the element.
All the embodiments in the present specification are described in a related manner, and the same and similar parts among the embodiments may be referred to each other, and each embodiment focuses on differences from other embodiments. In particular, as for the system embodiment, since it is substantially similar to the method embodiment, the description is relatively simple, and reference may be made to the partial description of the method embodiment for relevant points.
The above description is only for the preferred embodiment of the present invention, and is not intended to limit the scope of the present invention. Any modification, equivalent replacement, or improvement made within the spirit and principle of the present invention shall fall within the protection scope of the present invention.

Claims (11)

1. A timed task implementation method based on a linux system is applied to a timed service process, and comprises the following steps:
receiving a plurality of timing requests sent by a first user process; the timing request comprises an index number of each timing task, timing duration of each timing task and an identifier of the first user process;
respectively allocating a target timer for each timing task from a timer pool and initializing the target timer;
circularly traversing each timer in the timer pool according to a preset time interval;
when detecting that the target timer is set with a timeout marker, sending a timeout notification message to the first user process in an inter-process communication mode; the overtime notification message comprises an index number of a timing task which reaches the timing duration and an identifier of the first user process; and the overtime notification message is used for indicating the first user process to execute an overtime callback function corresponding to the timing task.
2. The method of claim 1, further comprising:
receiving a timing closing request sent by the first user process;
determining a corresponding timer according to the index number of the timing task and the identification of the first user process included in the timing closing request;
and closing the corresponding timer.
3. The method of claim 1, wherein the timing request further includes a timing pattern, the timing pattern includes a first pattern for representing a single timer and a second pattern for representing a round-robin timer, and the round-robin traversing each timer in the timer pool at preset time intervals includes:
circularly traversing each timer in the timer pool according to a preset time interval, and decreasing the count value of the timer in work during each traversal;
if the timing mode is the first mode, setting an overtime mark for the timer when the count value of the timer is reduced to 0;
and if the timing mode is the second mode, reloading the count value of the timer and setting an overtime mark for the timer when the count value of the timer is reduced to 0.
4. A timed task implementation method based on a linux system is applied to a first user process, and comprises the following steps:
sending a timing request to a timing service process, wherein the timing request comprises an index number of each timing task, a timing duration of each timing task and an identifier of the first user process;
selecting an idle list position from a timing task list initialized in advance to store the index number, the list position use state mark and the overtime callback function of each timing task;
receiving an overtime notification message returned by the timing service process, wherein the overtime notification message comprises the index number of the timing task reaching the timing duration and the identifier of the first user process;
and determining an overtime callback function according to the index number of the timing task, and executing the overtime callback function.
5. The method of claim 4, further comprising, after executing the timeout callback function:
and marking the position in the timing task list corresponding to the index number as idle.
6. The method of claim 4, prior to sending the timing request to the timing service process, further comprising:
determining type parameters of each timing task; the type parameters comprise a first parameter and a second parameter, the first parameter is used for representing timing tasks with short execution time and high frequency, and the second parameter is used for representing timing tasks with long execution time and low frequency;
the executing the timeout callback function includes:
if the type parameter is the first parameter, executing the overtime callback function in a message receiving thread;
and if the type parameter is the second parameter, creating a new thread to execute the timeout callback function.
7. The method of claim 4, wherein the timing request further comprises a timing pattern, and wherein the timing pattern comprises a first pattern for indicating a recurring timer and a second pattern for indicating a one-time timer.
8. A timed task implementation device based on a linux system is applied to a timed service process, and comprises the following steps:
a timing request receiving module, configured to receive multiple timing requests sent by a first user process; the timing request comprises index numbers of all timing tasks, timing duration of all timing tasks and identification of the first user process;
the distribution module is used for respectively distributing a target timer for each timing task from a timer pool and initializing the target timer;
the traversing module is used for circularly traversing each timer in the timer pool according to a preset time interval;
a message sending module, configured to send a timeout notification message to the first user process in an inter-process communication manner when detecting that the timeout flag is set in the target timer; the overtime notification message comprises an index number of a timing task which reaches the timing duration and an identifier of the first user process; and the overtime notification message is used for indicating the first user process to execute an overtime callback function corresponding to the timing task.
9. A timed task implementation device based on a linux system is applied to a first user process, and the device comprises:
a timing request sending module, configured to send a timing request to a timing service process, where the timing request includes an index number of each timing task, a timing duration of each timing task, and an identifier of the first user process;
the storage module is used for selecting an idle list position from a timing task list initialized in advance to store the index number, the list position use state mark and the overtime callback function of each timing task;
a message receiving module, configured to receive an timeout notification message returned by the timing service process, where the timeout notification message includes an index number of a timing task that has reached the timing duration and an identifier of the first user process;
and the execution module is used for determining an overtime callback function according to the index number of the timing task and executing the overtime callback function.
10. An electronic device, comprising a processor and a memory, wherein at least one instruction, at least one program, a set of codes, or a set of instructions is stored in the memory, and the at least one instruction, the at least one program, the set of codes, or the set of instructions is loaded and executed by the processor to implement the linux system based timed task implementation method according to any one of claims 1-7.
11. A computer-readable storage medium, wherein at least one instruction, at least one program, a set of codes, or a set of instructions is stored in the storage medium, and the at least one instruction, the at least one program, the set of codes, or the set of instructions is loaded and executed by a processor to implement the linux system based timed task implementation method according to any one of claims 1-7.
CN202211350553.0A 2022-10-31 2022-10-31 Timed task implementation method and device based on linux system Pending CN115658156A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202211350553.0A CN115658156A (en) 2022-10-31 2022-10-31 Timed task implementation method and device based on linux system

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202211350553.0A CN115658156A (en) 2022-10-31 2022-10-31 Timed task implementation method and device based on linux system

Publications (1)

Publication Number Publication Date
CN115658156A true CN115658156A (en) 2023-01-31

Family

ID=84994535

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202211350553.0A Pending CN115658156A (en) 2022-10-31 2022-10-31 Timed task implementation method and device based on linux system

Country Status (1)

Country Link
CN (1) CN115658156A (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN117200301A (en) * 2023-11-08 2023-12-08 深圳海辰储能科技有限公司 Port calling method and device based on energy storage system

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN117200301A (en) * 2023-11-08 2023-12-08 深圳海辰储能科技有限公司 Port calling method and device based on energy storage system
CN117200301B (en) * 2023-11-08 2024-01-26 深圳海辰储能科技有限公司 Port calling method and device based on energy storage system

Similar Documents

Publication Publication Date Title
US11558244B2 (en) Improving performance of multi-processor computer systems
CN107301178B (en) Data query processing method, device and system
CN109152061B (en) Channel allocation method, device, server and storage medium
CN109800204B (en) Data distribution method and related product
CN111290917A (en) YARN-based resource monitoring method and device and terminal equipment
CN110990142A (en) Concurrent task processing method and device, computer equipment and storage medium
CN110109741B (en) Method and device for managing circular tasks, electronic equipment and storage medium
CN115658156A (en) Timed task implementation method and device based on linux system
CN113742109B (en) Third party service docking method, device, equipment and storage medium
CN110691120A (en) Timing task state detection method, server and detection system
CN106020984B (en) Method and device for creating process in electronic equipment
CN1055557C (en) Method of controlling message overload of unit program in multi processor controlling system
CN110262878B (en) Timed task processing method, device, equipment and computer readable storage medium
CN109284193B (en) Distributed data processing method based on multithreading and server
CN113242149B (en) Long connection configuration method, apparatus, device, storage medium, and program product
CN112860387A (en) Distributed task scheduling method and device, computer equipment and storage medium
CN113329345A (en) Method and device for improving short message sending success rate
CN111831408A (en) Asynchronous task processing method and device, electronic equipment and medium
CN109558254B (en) Asynchronous callback method, system, device and computer readable storage medium
CN112463348B (en) Timed task processing method and device
CN114691321A (en) Task scheduling method, device, equipment and storage medium
CN114048033A (en) Load balancing method and device for batch running task and computer equipment
CN112559148A (en) Execution method, execution device and execution system of ordered tasks
CN113572701A (en) Service interface flow control method, device, equipment and storage medium
CN114356517B (en) Method, system, electronic equipment and storage medium for managing serial bus resources

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination