Design and Implementation of Linux kernel Reading Notes (10)-timer and time management (2)

Source: Internet
Author: User

6. Actual time

The current actual time (Wall time) is defined in the file kernel/Timer. C:

 
StructTimespec xtime;

The timespec data structure is defined in the file <Linux/time. h> in the following format:

 
StructTimespec {time_t TV _sec;/*Seconds*/LongTV _nsec;/*Nanoseconds*/};

Xtime. TV _sec is stored in seconds as the time elapsed since January 1, July 1, 1970 (UTC. Xtime. TV _nsec records the number of nanoseconds that have elapsed since the last second.

To read and write the xtime variable, you must use the xtime_lock lock, which is a seqlock.

 

7. Timer

The timer enables the task to be executed at a specified time point. The timer is represented by the time_list structure and is defined in the file <Linux/Timer. h>.

 Struct  Timer_list {  Struct List_head entry; /*  Timer Linked List entry  */  Unsigned  Long Expires; /*  Time Value in jiffies  */  Spinlock_t  Lock ; /*  Protection timer lock  */      Void (* Function) (unsigned Long ); /*  Timer processing function  */  Unsigned  Long Data; /*  Long Integer parameter passed to the processing function  */      Struct Tvec_t_base_s *Base ; /*  Internal timer value. Do not use  */  }; 

Use a Timer:

 Struct  Timer_list my_timer; init_timer ( & My_timer ); /*  Initialize Timer  */  My_timer.expires = Jiffies + delay; /*  The number of times the timer times out.  */ My_timer.data = 0 ; /*  Input 0 value to the timer processing function  */  My_timer.function = My_function; /*  Functions called when the timer times out  */  Add_timer ( & My_timer ); /*  Activate Timer  */  

To change the timeout value, call the mod_timer function:

Mod_timer (& my_timer, jiffies + new_delay );

The mod_timer function is activated no matter whether or not my_timer is activated. Once it is returned from mod_timer, my_timer is activated and a new scheduled value is set. If my_timer is not activated during the call, this function returns 0; otherwise, 1 is returned.

If you need to stop the timer, you can use the del_timer and del_timer_sync functions.

 
Del_timer (&My_timer); del_timer_sync (& My_timer );

The activated or inactive timer can be used. If the timer is not activated, the function returns 0; otherwise, 1 is returned. Note: you do not need to call this function for timed-out timers because they are automatically deleted. The difference between the two is that del_timer_sync will wait for the timer processing on other processors.ProgramAll are exited before execution. Therefore, del_timer_sync cannot be used in the interrupt context. In terms of security, del_timer_sync is preferred.

 

8. delayed execution

1) busy waiting

UnsignedLongDelay = jiffies +5*Hz;While(Time_before (jiffies, delay) cond_resched ();

The cond_resched function schedules a new program to run, but it takes effect only after the need_resched flag is set. In other words, the effective condition of this method is that more important tasks need to be run in the system. Note that the method must call the scheduler, so it can only be used in the process context.

2) short latency

 
VoidUdelay (unsignedLongUsecs );VoidMdelay (unsignedLongMsecs );

The previous function uses a busy loop to delay the task to a specified number of microseconds before running, and the latter delays the specified number of milliseconds.

3) schedule_timeout

This method will cause the task to be delayed to sleep to the specified delay time and then run again, but there is no guarantee that the sleep time is exactly the same as the specified delay time-only try to make the sleep time close to the specified delay time. When the specified time expires, the kernel wakes up the delayed task and places it back in the running queue.

 
/*Set the task to sleep interrupted.*/Set_current_state (task_interruptible );/*NAP for a while, wake up after "S" seconds*/Schedule_timeout (S* Hz );

The only parameter is the relative delay time, In jiffies. Note that before calling the schedule_timeout function, you must set the task to one of the task_interruptible or task_uninterruptible States. Otherwise, the task will not sleep.

4) set the timeout time and wait for the queue to sleep

You can put yourself in the waiting queue and call the scheduler to execute new tasks. Once an event occurs, the kernel calls the wake_up function to wake up the task in the sleep queue and re-run it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.