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
相关文档:
1. HCI层协议概述:
HCI提供一套统一的方法来访问Bluetooth底层。如图所示:
从图上可以看出,Host Controller Interface(HCI) 就是用来沟通Host和Module。Host通常就是PC, Module则是以各种物理连接形式(USB,serial,pc-card等)连接到PC上的bluetooth Dongle。
在Host这一端:application,SDP,L2cap等协议 ......
例一:发送Signaling Packet:
Signaling Command是2个Bluetooth实体之间的L2CAP层命令传输。所以得Signaling Command使用CID 0x0001.
多个Command可以在一个C-frame(control frame)中发送。
如果要直接发送Signaling Command.需要建立SOCK_RAW类型的L2CAP连接Socket。这样才有机会自己填充Command Code,Identi ......
假设php安装在/usr/local/php5/bin/php,初始在命令行执行PHP的时候要输入一长串的路径,eg:
$>/usr/local/php5/bin/php phpinfo.php
非常的麻烦,现在只需将php拷贝至usr/sbin目录即可直接使用php命令,eg:
$>cp /usr/local/php5/bin/php /usr/sbin/
$>php phpinfo.php
......
函数原型:
#include <pthread.h>
int pthread_create(pthread_t *restrict tidp,const pthread_attr_t *restrict attr, void *(*start_rtn)( ......