Enhanced timer design method under embedded operating system
By adding time series information to the kernel-mode timer object in the embedded operating system and extending the timer interface, the problem of timers not being able to support multiple cycles is solved, achieving the effects of simplified management and improved performance.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- JIANGSU AUTOMATION RESEARCH INSTITUTE
- Filing Date
- 2026-01-27
- Publication Date
- 2026-06-09
AI Technical Summary
In existing embedded operating systems, timers cannot effectively support the periodic setting of time series {T1, T2, T3, ..., Tn}, resulting in high management overhead, complex programming, and inflexibility.
In the timer object data structure of the operating system kernel, members such as time series pointer, length, trigger mode, current index and execution cycle are added. The user-mode timer setting interface is extended, and the time series of the timer is processed in the kernel mode to establish strict timing logic and avoid managing multiple timers.
It reduces timer management overhead, improves system performance and user programming flexibility, and simplifies the programming process.
Smart Images

Figure CN122172931A_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of computer operating systems, and specifically relates to an enhanced timer design method under an embedded operating system. Background Technology
[0002] Clock timer management is primarily used to support the association of a specified function with a specific time interval, so that the specified function is immediately invoked when the interval ends. Timers can be categorized into single-shot timers and periodic timers based on their trigger type.
[0003] The POSIX standard interface provides timer management interfaces, including timer creation (timer_create), timer deletion (timer_delete), and timer setting (timer_settime). However, the timer setting interface only supports fixed-period periodic interfaces and does not support periodic settings of time sequences {T1, T2, T3, ..., Tn} (n>=2), where T1, T2, T3, ..., Tn are in an increasing relationship; for example, Chinese patent CN103034480A, entitled "A Method for Implementing Timers in Embedded Systems." However, in a control system, it is often necessary to execute different sequences of tasks at different periods within a certain time period. For example, within a 10ms period, task A is executed at 1ms, task B at 5ms, and task C at 8ms. The ABC sequence is executed cyclically at 10ms periods, with a time sequence of {1, 5, 8} and a period of 10. User programming requires careful arrangement and management of these timing sequences. Summary of the Invention
[0004] The purpose of this invention is to propose an enhanced timer design method under an embedded operating system. It establishes strict timing logic internally, supports the periodic setting of time series {T1,T2,T3,..Tn}, avoids creating multiple timers for each time series for user management, reduces management overhead, simplifies programming, improves system performance, and increases the flexibility of user programming.
[0005] The technical solution to achieve the purpose of this invention is as follows:
[0006] A method for enhancing the management of operating system clock timers includes the following steps:
[0007] Step 1: Add a pointer to the time series (timerlist), the time series length (len), the timer trigger mode (mode), the current index (index) used to mark the time series, and the execution period (period) to the k_timer data structure of the timer object in the operating system kernel mode;
[0008] Step 2: The user space uses the POSIX standard interface timer_create to create a timer. If creation is successful, the timer ID is returned.
[0009] Step 3: Define the function interface for setting the timer in user mode. The input parameters of this function interface include, in order, the timer ID, the timer flag, the timer trigger mode, a pointer to the time series, the length of the time series, and the execution period of the timer time series.
[0010] Step 4: After calling the function interface for setting the timer in Step 3, the corresponding kernel-mode function is entered through the system call mechanism.
[0011] Step 5: Design the user-mode interface for obtaining the timer. The input parameters of this function interface include, in order, the timer ID, a pointer storing the remaining time of the timer until its next expiration time, and a pointer storing the index value of the timer in the time series.
[0012] Step 6: When the user space uses the POSIX standard interface timer_delete to delete the timer, the kernel space timer memory resources, including the memory corresponding to the pointer address of the time sequence allocated in step 4, are released in the system call function corresponding to timer_delete.
[0013] Furthermore, the timer triggering mode includes two modes: single trigger and periodic trigger.
[0014] Furthermore, the timing flag includes both relative and absolute time flags.
[0015] Furthermore, step 4 specifically includes:
[0016] Step 4-1: This kernel-mode function finds the corresponding timer object through the timer ID, allocates an address for the time series pointer of the kernel-mode timer object according to the length value of the time series passed from user mode, and copies the time series value passed from user mode; sets the timer trigger mode of the kernel-mode clock object to the timer trigger mode value passed from user mode; and sets the time series execution period of the kernel-mode clock object to the execution period value passed from user mode.
[0017] Step 4-2: Define the callback function kern_timer_fn for kernel-mode timer expiration, and call hrtimer_init to initialize the kernel-mode high-precision timer object;
[0018] Step 4-3: Set the initial expiration time T of the timer, and the base value of the timer is the current time value. If the timer flag in step 3 is a relative time, the expiration time T is set to the current time + the first value T0 in the time series; otherwise, T0 is used as the absolute time, that is, T is set to T0; the index of the current time series is 0; the count of the already run cycles is initialized to 0, and the kernel-mode timer is started.
[0019] Step 4-4: When the timer expires, execute the kernel-mode callback function kern_timer_fn described in step 4-2. This function handles timer events, sends a signal to the user-mode process / thread, and passes the current index value to the user-mode, allowing the user program to handle different tasks based on different index values; updates the index of the current time series, i.e., index = (index + 1) % len (len is the length of the time series); sequentially retrieves the time value corresponding to the index from the time series timerlist, i.e., timerlist[index], calculates the next expiration time of the timer; sets a new timeout, and sets the timer to active state.
[0020] When the index equals the length of the time series, update the number of cycles already run (count) to count+1; if the timer trigger mode is single-trigger mode, the kernel-mode timer will no longer be activated.
[0021] Furthermore, the method for calculating the next expiration time of the timer is as follows: if the timer flag is a relative time flag, the next expiration time is timerlist[index] + count * period + base; if the timer flag is an absolute time flag, the next expiration time of the timer is timerlist[index] + count * period; where period is the execution period of the time series, base is the base value of the timer, and count represents the number of periods that have been run.
[0022] Furthermore, the operating system is a system that conforms to the POSIX standard interface.
[0023] Compared with the prior art, the present invention has the following beneficial technical effects: The present invention provides a periodic timer that supports time series. By adding data members related to time series to the data structure k_timer of the kernel-mode timer object, the function interface for setting the timer in user mode is extended, and the time series of the timer is processed in the corresponding kernel-mode system call. Strict timing logic is established internally, avoiding the creation of multiple timers for each time series for management, reducing management overhead, improving system performance, and increasing the flexibility of user programming. Attached Figure Description
[0024] Figure 1 This is a flowchart of the method of the present invention. Detailed Implementation
[0025] To enable those skilled in the art to better understand the solutions described in this invention, the technical solutions in the embodiments of this invention will be supplemented below with reference to the accompanying drawings, thereby providing a clearer and more complete description of the solutions. Obviously, the described embodiments are only a part of the embodiments of this invention, and not all of them. Implemented in Linux kernel 4.19, the Linux system supports high-precision clocks, and the theoretical precision of the timer can reach the nanosecond level.
[0026] This embodiment provides an enhanced operating system clock timer management method that establishes strict timing logic internally, simplifying application development. A timer's periodic mode is defined as a time sequence {T1, T2, T3, ..., Tn}. Within a periodic mode, the timer automatically reloads according to delays of T1, T2, T3...Tn time units. After a periodic mode completes execution, if it is a periodically triggered mode, the timer will automatically repeat the time sequence until the timer is stopped or deleted. Figure 1 The method specifically includes:
[0027] Step 1: In the operating system kernel's timer object data structure k_timer, add a pointer to the time series (timerlist), the time series length (len), the timer trigger mode (mode), the current index in the time series (index), the timer time series execution period (period), and the timer base value (base). The timer trigger mode includes two modes: single trigger and periodic trigger.
[0028] The data structure k_timer takes the following form:
[0029] struct k_itimer {
[0030] struct list_headlist; ......
[0032] int mode; / / Timer trigger mode
[0033] ktime_t *timerlist; / / Time series
[0034] int len; / / Length of the time series
[0035] int index; / / Marks the current position in the time series
[0036] ktime_t base; / / The base value for the timer;
[0037] ktime_t period; / / The execution period of the timer's time series.
[0038] int count; / / Number of cycles to run;
[0039] };
[0040] Step 2: The user space uses the POSIX standard interface timer_create to create a timer; timer_create enters the kernel space through the system call interface, where a new k_itimer object and a new ID are allocated for the timer, and the new ID is returned to the user space.
[0041] Step 3: Define the function interface for setting the timer in user mode. The function interface should look like this:
[0042] int timer_settime_ex(timer_t timerid, int flags, int mode, const structtimerspec *timerlist, int len, timerspec period);
[0043] In the above parameters, timerid is the timer ID, mode is the timer trigger mode, flags are the timer flags, including both relative and absolute time flags; timerlist is the time series; len is the length of the data elements in timerlist, and period is the execution period of the timer time series.
[0044] Step 4: timer_settime_ex enters kernel mode based on the newly added system call. The processing steps are as follows:
[0045] Step 4-1: In kernel mode, locate the corresponding timer object `k_itimer` using the timer ID. Allocate an address for the `timerlist` member of `k_itimer` with a length of `len` (i.e., `k_itimer->timerlist`). Use the `copy_from_user` interface to sequentially retrieve the values (of type `timerspec`) from the user-mode `timerlist` members. Then, use `timespec64_to_ktime` to convert the `timerspec` type value to a `ktime_t` type value and copy it to `k_itimer->timerlist`. Set the timer trigger mode of the kernel-mode clock object (`k_itimer->mode`) to the timer trigger mode value passed from user mode, and set the time series execution period (`period`) of the kernel-mode clock object to the time series execution period value passed from user mode.
[0046] Step 4-2: Define the callback function kern_timer_fn for kernel timer expiration, and call hrtimer_init to initialize the kernel timer object;
[0047] Step 4-3: Set the initial expiration time T of the timer, and the base value of the timer is the current time value. If the timer flag in step 3 is a relative time, the expiration time T is set to the current time + the first value T0 in the time series, i.e., k_itimer->timerlist[0]; otherwise, T0 is used as the absolute time, i.e., T is set to T0; the index of the current time series is 0; the count of the timer's cycles is 0; start the kernel-mode timer.
[0048] Step 4-4: When the timer expires, execute the kernel-mode callback function kern_timer_fn described in Step 4-2. This function handles the timer event, sends a signal to the user-mode process / thread, and passes the current index value; updates the index of the current time series, i.e., index = (index + 1) % len (where len is the length of the k_itimer->timerlist time series). It sequentially retrieves the time value corresponding to the index from the time series k_itimer->timerlist, i.e., k_itimer->timerlist[index]; if the timer flag is a relative time flag, the next timer expiration time is k_itimer->timerlist[index] + count * k_itimer->period + k_itimer->base; if the timer flag is an absolute time flag, the next timer expiration time is k_itimer->timerlist[index] + count * k_itimer->period; sets a new timer duration and sets the timer to active state; where period is the execution period of the timer time series, base is the timer base value, and count is the number of times the timer has run.
[0049] When the index equals the length len of the time series, the number of cycles already run is updated to count + 1; if the timer trigger mode is single trigger mode, the kernel-mode timer is no longer activated.
[0050] Step 5: Design the user-mode interface for retrieving the timer. The input parameters of this function interface include, in order: the timer ID, a pointer storing the remaining time until the timer's next expiration time, and a pointer storing the timer's current index value in the time series. User programming can execute different tasks based on the time series index value.
[0051] The interface format is as follows:
[0052] int timer_gettime_ex(timer_t timerid, struct timerspec *curr_value,int * index);
[0053] `timer_gettime_ex` enters kernel mode by adding a system call. In kernel mode, it finds the corresponding timer object `k_itimer` by timer ID, obtains the current time based on the timer's clock, calculates the difference between the current time and `k_itimer->timerlist[index]`, and then converts it to a `structtimerspec` structure using `ktime_to_timespec64`, and copies it to user mode using `copy_to_user`.
[0054] Step 6: When the user space uses the POSIX standard interface timer_delete to delete the timer, in the system call function corresponding to timer_delete, the corresponding timer object k_itimer is found by the timer ID, and the memory corresponding to the time series is released, i.e., k_itimer->timerlist, and the kernel-mode timer memory resource k_itimer is released.
[0055] This invention extends the user-mode function interface for setting timers by adding time-series data members to the k_timer data structure of the kernel-mode timer object, and processes the time series of the timer in the corresponding kernel-mode system calls. This method provides a periodic timer that supports time series, avoiding the need to create and manage multiple timers for each time series, reducing management overhead, simplifying programming, and improving system performance.
[0056] Although embodiments of the invention have been shown and described, it will be understood by those skilled in the art that various changes, modifications, substitutions and alterations can be made to these embodiments without departing from the principles and spirit of the invention, the scope of which is defined by the appended claims and their equivalents.
Claims
1. An enhanced method for managing operating system clock timers, characterized in that, include: Step 1: Add a pointer to the time series (timerlist), the time series length (len), the timer trigger mode (mode), the current index (index) used to mark the time series, and the execution period (period) to the k_timer data structure of the timer object in the operating system kernel mode. Step 2: The user space uses the POSIX standard interface timer_create to create a timer. If creation is successful, the timer ID is returned. Step 3: Extend the user-mode function interface for setting the timer. The input parameters of this function include the timer ID, timer flag, timer trigger mode, pointer to the time series, length of the time series, and execution period of the time series. Step 4: After calling the function interface for setting the timer in Step 3, the corresponding kernel-mode function is entered through the system call mechanism to process the timer's time sequence; Step 5: Extend the user-mode function interface to obtain the timer. The input parameters of this function interface include the timer ID, a pointer to the remaining time of the timer until the next expiration time, and a pointer to the current index value of the timer in the time series. Step 6: When the user space uses the POSIX standard interface timer_delete to delete the timer, the kernel-mode timer memory resources are released in the system call function corresponding to the standard interface timer_delete.
2. The enhanced operating system clock timer management method according to claim 1, characterized in that, The timer triggering mode includes two modes: single trigger and periodic trigger.
3. The enhanced operating system clock timer management method according to claim 1, characterized in that, The timing flags include both relative and absolute time flags.
4. The enhanced operating system clock timer management method according to claim 3, characterized in that, Step 4, which involves entering the corresponding kernel-mode function via a system call mechanism, specifically includes: Step 4-1: The kernel-mode function finds the corresponding timer object through the timer ID, allocates an address for the time sequence pointer of the kernel-mode timer object according to the length value of the time sequence passed in from the user mode, and copies the passed-in time sequence value from the user mode; sets the timer trigger mode of the kernel-mode clock object to the timer trigger mode value passed in from the user mode; and sets the time sequence execution period of the kernel-mode clock object to the time sequence execution period value passed in from the user mode. Step 4-2: Define the callback function kern_timer_fn for kernel timer expiration. This callback function is used to handle timer events, send signals to user-mode processes / threads, and initialize kernel-mode timer objects. Step 4-3: Set the initial expiration time T of the timer, and the base value of the timer is the current time value. If the timer flag in step 3 is a relative time, the expiration time T is set to base + the first value T0 in the time series; otherwise, T0 is used as the absolute time, that is, T is set to T0; initialize the index of the current time series to 0, initialize the count of the already run cycles to 0; start the kernel-mode timer. Step 4-4: When the timer expires, execute the kernel-mode callback function kern_timer_fn described in step 4-2, send a signal to notify the user-mode process / thread, and pass the current index value; update the index of the current time series; sequentially retrieve the time value corresponding to the index from the time series timerlist, i.e., timerlist[index], and calculate the next expiration time of the timer; set a new timeout and set the timer to active state; when the index equals the length of the time series, update the number of cycles already run count = count + 1. If the timer trigger mode is single trigger mode, the kernel-mode timer will no longer be activated.
5. The enhanced operating system clock timer management method according to claim 4, characterized in that, In step 4-4, update the index of the current time series: index = (index + 1) % len, where len is the length of the time series.
6. The enhanced operating system clock timer management method according to claim 4, characterized in that, The method for calculating the next expiration time of the timer in step 4-4 is as follows: If the timer flag is a relative time flag, the next expiration time is timerlist[index] + count * period + base; if the timer flag is an absolute time flag, the next expiration time of the timer is timerlist[index] + count * period; where period is the execution period of the time series, base is the base value of the timer, and count represents the number of cycles that have been run so far.
7. The enhanced operating system clock timer management method according to claim 1, characterized in that, The operating system is a system that conforms to the POSIX standard interface.