(十二)linux內核定時器

目錄

        • (一)內核定時器介紹
        • (二)內核定時器相關接口
        • (三)使用步驟
        • (四)實例代碼

(一)內核定時器介紹

內核定時器并不是用來簡單的定時操作,而是在定時時間后,觸發事件的操作,類似定時器中斷,是內核用來控制在未來某個時間點(基于jiffies)調度執行某個函數的一種機制,內核中采用的定時器以jiffies為單位。 單位秒=jiffies/HZ

幾個重要跟時間有關的名詞或變數

HZ:Linux核心每隔固定周期會發出timer interrupt (IRQ 0),HZ是用來定義每一秒有幾次timer interrupts。舉例來說,HZ為1000,代表每秒有1000次timer interrupts。
Tick:Tick是HZ的倒數,意即timer interrupt每發生一次中斷的時間。如HZ為250時,tick為4毫秒(millisecond)。
Jiffies:Jiffies為Linux核心變數(unsigned long),它被用來記錄系統自開機以來,已經過了多少tick。每發生一次timer interrupt,Jiffies變數會被加一。值得注意的是,Jiffies于系統開機時,并非初始化成零,而是被設為-300*HZ ,類似Linux系統中time(日歷時間)

(二)內核定時器相關接口

和定時器先關的數據結構:

struct timer_list {unsigned long expires;	//未來時間點,即超時時間void (*function)(unsigned long);//超時回調函數unsigned long data;	//傳遞給回調函數的數據,也就是定時器數據		
};

初始化定時器相關的數據結構

1. 靜態定以定時器數據結構:
#define DEFINE_TIMER(_name, _function, _expires, _data)		\struct timer_list _name =				\TIMER_INITIALIZER(_function, _expires, _data)2. 動態初始化:-----手動對變量進行初始化
#define init_timer(timer)						\do {								\static struct lock_class_key __key;			\init_timer_key((timer), #timer, &__key);		\} while (0)

可延時定時:

#define init_timer_deferrable(timer)					\do {								\static struct lock_class_key __key;			\init_timer_deferrable_key((timer), #timer, &__key);	\} while (0)

設置定時時間

#define setup_timer(timer, fn, data)					\
do {								\static struct lock_class_key __key;			\setup_timer_key((timer), #timer, &__key, (fn), (data));\
} while (0)

向內核添加定時器:

/*** add_timer - start a timer* @timer: the timer to be added** The kernel will do a ->function(->data) callback from the* timer interrupt at the ->expires point in the future. The* current time is 'jiffies'.** The timer's ->expires, ->function (and if the handler uses it, ->data)* fields must be set prior calling this function.** Timers with an ->expires field in the past will be executed in the next* timer tick.*/
void add_timer(struct timer_list *timer)
{BUG_ON(timer_pending(timer));mod_timer(timer, timer->expires);
}

從內核刪除定時器:

/*** del_timer - deactive a timer.* @timer: the timer to be deactivated** del_timer() deactivates a timer - this works on both active and inactive* timers.** The function returns whether it has deactivated a pending timer or not.* (ie. del_timer() of an inactive timer returns 0, del_timer() of an* active timer returns 1.)*/
int del_timer(struct timer_list *timer)

修改定時時間:

/*** mod_timer - modify a timer's timeout* @timer: the timer to be modified* @expires: new timeout in jiffies** mod_timer() is a more efficient way to update the expire field of an* active timer (if the timer is inactive it will be activated)** mod_timer(timer, expires) is equivalent to:**     del_timer(timer); timer->expires = expires; add_timer(timer);** Note that if there are multiple unserialized concurrent users of the* same timer, then mod_timer() is the only safe way to modify the timeout,* since add_timer() cannot modify an already running timer.** The function returns whether it has modified a pending timer or not.* (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an* active timer returns 1.)*/
int mod_timer(struct timer_list *timer, unsigned long expires)

jiffies和時間的轉換:

extern unsigned int jiffies_to_msecs(const unsigned long j);
extern unsigned int jiffies_to_usecs(const unsigned long j);
extern unsigned long msecs_to_jiffies(const unsigned int m);
extern unsigned long usecs_to_jiffies(const unsigned int u);

(三)使用步驟

1、向內核添加定時器

   setup_timer();設置定時器add_timer();.

2、解綁定時器

   int del_timer(struct timer_list *timer)

(四)實例代碼

#include <linux/timer.h>
#include <linux/kernel.h>
#include <linux/module.h>struct timer_list timer;
void function(unsigned long data)
{static int count=0;printk("this is timer test:%d\n",count++);mod_timer(&timer,jiffies+1*HZ);
}
static int __init timer_module_init(void)
{timer.expires = jiffies+5*HZ;setup_timer(&timer,function,0);add_timer(&timer);return 0;
}
static void __exit timer_module_cleanup(void)
{del_timer(&timer);
}module_init(timer_module_init);
module_exit(timer_module_cleanup);
MODULE_LICENSE("GPL");

測試


本文章僅供學習交流用禁止用作商業用途,文中內容來水枂編輯,如需轉載請告知,謝謝合作

微信公眾號:zhjj0729

微博:文藝to青年

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/531407.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/531407.shtml
英文地址,請注明出處:http://en.pswp.cn/news/531407.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

java Proxy(代理機制)

我們知道Spring主要有兩大思想&#xff0c;一個是IoC&#xff0c;另一個就是AOP&#xff0c;對于IoC&#xff0c;依賴注入就不用多說了&#xff0c;而對于Spring的核心AOP來說&#xff0c;我們不但要知道怎么通過AOP來滿足的我們的功能&#xff0c;我們更需要學習的是其底層是怎…

(十三)linux中斷底半部分處理機制

這篇文章介紹一下linux中斷的底半部分的tasklet和workquene兩種處理機制&#xff0c;其中tasklet中不能有延時函數&#xff0c;workquene的處理函數可以加入延時操作 目錄&#xff08;一&#xff09;tasklet小任務處理機制&#xff08;1&#xff09;tasklet相關函數接口&#x…

Codeforces Round #326 (Div. 2) B. Pasha and Phone C. Duff and Weight Lifting

B. Pasha and PhonePasha has recently bought a new phone jPager and started adding his friends phone numbers there. Each phone number consists of exactly n digits. Also Pasha has a number k and two sequences of length n?/?k (n is divisible by k) a1,?a2,?…

vmware中裝的ubuntu上不了網

本文章針對橋接方式進行講解&#xff0c;如果需要另外兩種連接方式請參考文末給出的鏈接 &#xff08;一&#xff09;問題 主機和虛擬機可以相互ping通&#xff0c;但是卻不能ping網址 &#xff08;二&#xff09;解決辦法 vmware為我們提供了三種網絡工作模式&#xff0c;…

document.getElementById()與 $()區別

document.getElementById()返回的是DOM對象&#xff0c;而$()返回的是jQuery對象 什么是jQuery對象&#xff1f; ---就是通過jQuery包裝DOM對象后產生的對象。jQuery對象是jQuery獨有的&#xff0c;其可以使用jQuery里的方法。 比如&#xff1a; $("#test").html() 意…

關于gedit的編碼問題

今天由于gedit的編碼格式導致LCD顯示屏的問題&#xff0c;開始沒有想到后來才發現&#xff0c;在這記錄一下 #include <stdio.h> #include <unistd.h> #include <stdio.h> #include <fcntl.h> #include <linux/fb.h> #include <sys/mman.h>…

c語言表白程序代碼

雙十一要到了&#xff0c;好激動啊&#xff01;&#xff01;&#xff01; 是時候準備出手了&#xff01; 花了一天的時間寫的表白代碼。 表示自己弱弱的..... 看了網上好多都是js寫的&#xff0c;感覺碉堡了&#xff01;js用的不熟&#xff0c;前端不好&#xff0c;java&#x…

tiny4412移植tslib庫

1、將tslib-1.4.tar.gz拷貝到虛擬機某個路徑進行解壓 2、進入解壓路徑tslib 3、執行#./autogen.sh 如果提示&#xff1a;./autogen.sh: 4: ./autogen.sh: autoreconf: not found 原因&#xff1a;沒有安裝automake工具, 解決辦法:需要安裝此工具&#xff1a; apt-get instal…

移植QT到tiny4412開發板

目錄&#xff08;一&#xff09; 環境準備&#xff08;二&#xff09; Qt源代碼下載&#xff08;三&#xff09; 移植tslib庫&#xff08;四&#xff09;操作流程1.解壓qt源碼包2.配置編譯環境3.生成Makefile4.編譯安裝5.安裝一些庫用來支持 qt6. 添加以下內容到開發板目錄下的…

c++面試常用知識(sizeof計算類的大小,虛擬繼承,重載,隱藏,覆蓋)

一. sizeof計算結構體 注&#xff1a;本機機器字長為64位 1.最普通的類和普通的繼承 #include<iostream> using namespace std;class Parent{ public:void fun(){cout<<"Parent fun"<<endl;} }; class Child : public Parent{ public:void fun(){…

嵌入式面試題(一)

目錄1 關鍵字volatile有什么含義&#xff1f;并給出三個不同的例子2. c和c中的struct有什么不同&#xff1f;3.進程和線程區別4.ARM流水線5.使用斷言6 .嵌入式系統的定義7 局部變量能否和全局變量重名&#xff1f;8 如何引用一個已經定義過的全局變量&#xff1f;9、全局變量可…

能ping通ip但無法ping通域名和localhost //ping: bad address 'www.baidu.com'

錯誤描述&#xff1a; ~ # ping localhost ping: bad address localhost原因&#xff0c;在/etc目錄下缺少hosts文件&#xff0c;將linux中的/etc hosts文件拷入即可 ~ # ping localhost PING localhost (127.0.0.1): 56 data bytes 64 bytes from 127.0.0.1: seq0 ttl64 tim…

eclipse導入web項目之后項目中出現小紅叉解決辦法

項目中有小紅叉我遇到的最常見的情況&#xff1a; 1、項目代碼本身有問題。&#xff08;這個就不說了&#xff0c;解決錯誤就OK&#xff09; 2、項目中的jar包丟失。&#xff08;有時候eclipse打開時會出現jar包丟失的情況&#xff0c;關閉eclipse重新打開或者重新引入jar包就O…

arm開發板通過網線連接筆記本電腦上外網

需要工具&#xff1a;arm開發板&#xff0c;網線&#xff0c;一臺雙網卡的win7筆記本電腦&#xff08;筆記本電腦一般都是雙網卡&#xff09; 一、筆記本電腦需要先連上外網&#xff0c;可以連上家里的WIFI&#xff0c;或者手機開熱點&#xff08;本人未測試過連接手機的熱點&…

windows下實現Git在局域網使用

1.首先在主機A上創建一個文件夾用于存放你要公開的版本庫。然后進入這個文件夾&#xff0c;右鍵->Git create repository here&#xff0c;彈出的窗口中勾選Make it Bare&#xff01;之后將這個文件夾完全共享&#xff08;共享都會吧&#xff1f;注意權限要讓使用這個文件夾…

解決linux下QtCreator無法輸入中文的情況

安裝了QtCreator(Qt5.3.1自帶版本)后無法輸入中文&#xff0c;確切的說是無法打開輸入法。以前使用iBus輸入法的時候沒有這個問題&#xff0c;現在使用sougou輸入法才有的這個問題。 可以查看此文 http://www.cnblogs.com/oloroso/p/5114041.html 原因 有問題就得找原因&…

lintcode 滑動窗口的最大值(雙端隊列)

題目鏈接&#xff1a;http://www.lintcode.com/zh-cn/problem/sliding-window-maximum/# 滑動窗口的最大值 給出一個可能包含重復的整數數組&#xff0c;和一個大小為 k 的滑動窗口, 從左到右在數組中滑動這個窗口&#xff0c;找到數組中每個窗口內的最大值。 樣例 給出數組 [1…

你的main函數規范嗎?

在學習c語言的時候&#xff0c;有一個函數一直被我們使用&#xff0c;那就是main函數&#xff0c;但是你知道標準里面是怎么規定它的寫法嗎&#xff1f; 平時看見的main函數有下面這幾種&#xff1a; 1.int main(void){ }2.int main(){ }3.int main(int argc, char *argv[])…

lintcode 最長上升連續子序列 II(二維最長上升連續序列)

題目鏈接&#xff1a;http://www.lintcode.com/zh-cn/problem/longest-increasing-continuous-subsequence-ii/ 最長上升連續子序列 II 給定一個整數矩陣&#xff08;其中&#xff0c;有 n 行&#xff0c; m 列&#xff09;&#xff0c;請找出矩陣中的最長上升連續子序列。&a…

適用于Linux的Windows子系統WSL

以前使用的都是在虛擬機里安裝linux&#xff0c;最近才發現在win10提供了WSL(Windows Subsystem for Linux) &#xff0c;簡單來說就是可以在win10里面直接使用Linux。 &#xff08;一&#xff09;首先打開Microsoft Store , 搜索 Linux &#xff08;二&#xff09;選擇自己需…