Linux Kernel時序的三種機制
最近在寫Driver時,常常遇到需要「等待一段時間」再處理的動作,以往我都傻傻的用msleep()或mdelay(),殊不知這種busy waiting會hold住cpu資源,在這段期間內都無法讓給其他process執行,時間短(10ms以下等級)或許還可以,太長就不行了,所以需要Kernel本身就有提供的「時序」機制來做處理,於是我漸漸學會了如何使用Timer、Tasklet和Workqueue的用法,在O'Reilly的Linux Device Drivers第七章有詳細的講解,我將書上的精華茲簡單整理如下:
##CONTINUE##
一、Timer
#include <linux/timer.h>
struct timer_list
{
/* ... */
unsigned long expires;//期望運行的 jiffies 值
void (*function)(unsigned long);
unsigned long data;//傳給function的參數
};//使用前必須初始化
void init_timer(struct timer_list *timer);
//初始化一個timer_list
struct timer_list TIMER_INITIALIZER(_function, _expires, _data);
//初始化一個靜態的timer_list
void add_timer(struct timer_list * timer);
int mod_timer(struct timer_list *timer, unsigned long expires);
int del_timer(struct timer_list * timer);
int del_timer_sync(struct timer_list *timer);
//同 del_timer 一樣,但保證函數return時,定時器函數不在任何 CPU 上運行
//避免在SMP 系統上競爭
int timer_pending(const struct timer_list * timer);
//返回真或假來指示是否定時器已被運行
二、Tasklet
#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);
//直接申明就可用了
DECLARE_TASKLET_DISABLED(name, func, data);
void tasklet_disable(struct
相关文档:
一:前言
最近在研究android的sensor driver,主要是E-compass,其中用到了Linux input子系统.在网上也看了很多这方面的资料,感觉还是这篇分析的比较细致透彻,因此转载一下以便自己学习,同时和大家分享!
(这篇博客主要是以键盘驱动为例的,不过讲解的是Linux Input Subsystem,可以仔细的研究一下!)
键盘驱动将检 ......
原文地址:http://www.wangzhongyuan.com/archives/488.html
以下是一个Linux/Unix下由两个管道提供双向数据流的C程序,这是操作系统课程中经常使用的基本程序
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <string.h>
int m ......
用户模式Linux (User Mode Linux), 缩写为UML。顾名思义,UML就是在一个可以在用户空间运行的linux内核。
UML能够为kernel虚拟一些实际的物理设备,启动UML只需要要指定一个root文件系统的image文件。UML实际上也是一种虚拟化技术。
UML的好处
第一,做一个虚拟服务器,UML可以以安全的方式运行在用户空间,它可 ......