Linux Kernel Programming - Time,Delays,and Deferred Work
Measuring Time Lapses
The counter and the utility functions to read it live in <linux/jiffies.h>.Needless to say both jiffies and jiffies_64 must be considered read-only.
Processor-Specific Registers
#include <asm/msr.h>
rdtsc(low32,high32);
rdtscl(low32);
rdtscll(var64);
As an example using only the low half of the register,the following lines measure the execution of the instruction itself:
unsigned long ini,end;
rdtscl(ini);rdtscl(end);
printk("time lapse:%li\n",end - ini);
architecture-independent function:
#nclude <linux/timex.h>
cycles_t get_cycles(void);
Delaying Execution
#include <linux/wait.h>
long wait_event_timeout(wait_queue_head_t q,condition,long timeout);
long wait_event_interruptible_timeout(wait_queue_head_t q,condition,long timeout);
wait_queue_head_t wait;
init_waitqueue_head(&wait);
wait_event_interruptible_timeout(wait,0,delay);
Timeout value(delay) represents the number of jiffies to wait
#include <linux/sched.h>
signed long schedule_timeout(signed long timeout);
set_current_state(TASK_INTERRUPTIBLE);
schedule_timeout(delay);
Short Delays
#include <linux/delay.h>
void ndelay(unsigned long nsecs);
void udelay(unsigned long usecs);
void mdelay(unsgined long msecs);
Kernel Timer
A kernel timer is a data structure that instructs the kernel to execute a user-defined function with a user-defined argument at user-defined time.
Timer functions must be atomic
No access to user space is allowed
No sleeping or scheduling may be performed
It's also worth knowing that in an SMP system,the timer function is executed by the same CPU that registered it
The Timer API
#include <linux/time.h>
struct timer_list{
/*.................*/
unsigned long expires;
void (*function)(unsigned long );
unsigned long data;
};
void init_timer(struct timer_list* timer);
struct timer_list TIMER_INITIALIZER(_function,_expires,_data);
void add_timer(struct timer_list *timer);
int del_timer(struct timer_list *timer);
Tasklets
Unlike kernel timers,however,you can't ask to execute the function at a speciic time.
Actually,a tasklet ,just like a kernel timer,is executed in the context of a "soft interrupt",a kernel mechanism that executes asynchronous task with hardware interrupts enabled.
#include <linux/interrupt.h>
struct tasklet_struct{
/*...*/
void (*func)(unsigned long);
unsigned long data;
};
void tasklet_init(struct tasklet_struct *t,void (*func)(unsigned long),unsigned long data);
DECLARE_TASKLET(name,func,data);
DECLATE_TASKLET_DISABLE(name,func,data);
Linux Kernel Programming - Time,Delays,and Deferred Work的更多相关文章
- Linux kernel Programming - Allocating Memory
kmalloc #include <linux/slab.h> void *kmalloc(size_t size,int flags); void kfree(void *addr); ...
- Linux kernel Programming - Advanced Char Driver Operations
ioctl //user space int ioctl(int fd,unsigned long cmd,...); //kernel space int (*ioctl)(struct inode ...
- Linux kernel Programming - Concurrency and Race Conditions
Concurrency and Its Management Race condition can often lead to system crashes, memory leak,corrupte ...
- linux kernel RCU 以及读写锁
信号量有一个很明显的缺点,没有区分临界区的读写属性,读写锁允许多个线程进程并发的访问临界区,但是写访问只限于一个线程,在多处理器系统中允许多个读者访问共享资源,但是写者有排他性,读写锁的特性如下:允许 ...
- Linux Kernel中断子系统来龙去脉浅析【转】
转自:http://blog.csdn.net/u011461299/article/details/9772215 版权声明:本文为博主原创文章,未经博主允许不得转载. 一般来说,在一个device ...
- Linux Kernel C语言编程范式
介绍 不同的编程语言具有不同的抽象原语(如下),有的原语抽象层次低,有的原语抽象层次高.其中函数式.DSL是这几年十分热门的编程语言概念. 过程式抽象原语:变量 对象式抽象原语:对象 函数式抽象原语: ...
- [中英对照]Linux kernel coding style | Linux内核编码风格
Linux kernel coding style | Linux内核编码风格 This is a short document describing the preferred coding sty ...
- 如何进行Linux Kernel 开发
转自:http://www.cppblog.com/flyonok/archive/2011/04/15/144316.html 如何进行Linux Kernel 开发? (Take 3) 译者序:这 ...
- Linux Kernel Maintainers
http://en.wikipedia.org/wiki/Ingo_Molnár http://zh.wikipedia.org/wiki/英格·蒙內 Ingo Molnár Ingo Molnár, ...
随机推荐
- 在整合spring和mongodb中,提示at org.springframework.data.mapping.model.BasicPersistentEntity.findAnnotation(
遇到这种坑,找了好多资料.基本是都是因为springdata的jar包和spring的版本不兼容导致,除了这个错误之外,还有会比较多其他错误,也是版本不兼容导致的. at org.springfram ...
- MySQL ORDER BY主键id加LIMIT限制走错索引
背景及现象 report_product_sales_data表数据量2800万: 经测试,在当前数据量情况下,order by主键id,limit最大到49的时候可以用到索引report_produ ...
- js-QuickStart-base.js
// 1.变量(Variables) var myVariable; myVariable = 'Bob'; // 数据类型 string number boolean array object // ...
- ES 5 中 判断数组的方法
源代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...
- Google Chrome 中安装 PostMan 扩展
简介 PostMan 是调试 HTTP 请求的好工具,也是业界的佼佼者,这对于我们开发 Web Service 提供了很好的调试入口,支持请求认证机制.最关键的是,这个工具提供 Google Chro ...
- python自动化开发-9 进程 线程
进程与线程 程序并不能单独运行,只有将程序装载到内存中,系统为它分配资源才能运行,而这种执行的程序就称之为进程.线程是操作系统能够进行运算调度的最小单位.它被包含在进程之中,是进程中的实际运作单位.一 ...
- Oracle 11g中修改被锁定的用户:scott
在安装完Oracle10g和创建完oracle数据库之后,想用数据库自带的用户scott登录,看看连接是否成功. 在cmd命令中,用“sqlplus scott/ tiger”登录时,老是提示如下信息 ...
- Git的安装配置(win环境)
安装: 首先安装win版本的git msysgit:https://git-for-windows.github.io 注:安装时要勾选生成桌面快捷方式. 默认安装完后依次执行: $ git conf ...
- C语言程序试题
一个无向连通图G点上的哈密尔顿(Hamiltion)回路是指从图G上的某个顶点出发,经过图上所有其他顶点一次且仅一次,最后回到该顶点的路劲.一种求解无向图上哈密尔顿回路算法的基础实现如下: 假设图G存 ...
- Websocket通信过程
1. 客户端与服务器建立连接 2. 客户端通过session向服务器发送消息 3. 服务器接收客户端的消息,调用服务器端的onMessage()方法包装.生成消息内容(新的消息包括客户端ID) 4. ...