unsigned long 的用法
来源:互联网 发布:淘宝营销推广方式 编辑:程序博客网 时间:2024/05/20 12:25
unsigned long 的用法
在很多函数调用里面都有 unsigned long 参数,事实上他不只是传递一个数(当然大多数情况下就传递一个数),更重要的是通过强转他还能代表一个地址,即当一个指针使用,下面就举例说明。
(指针与unsigned long什么关系?没有关系,C中值的类型并非值本身的属性,而是取决于它的使用方式。)
一,内核定时器的使用。
内核定时器一般分为声明、注册、删除等操作。
#include <linux/timer.h>
Struct timer_list
{
Unsigned long expires;
Void (*function)(unsigned long)
Unsigned long data;
}
Void init_timer(struct timer_list *timer);
Void add_timer(struct timer_list *timer);
Void del_timer(struct timer_list *timer);
expire 表示期望达到的值,到达这个值后就调用function 函数,并传递data作为参数,这里的data 是一个unsigned long 数据,如果我们要传递多个数据,就可以把几个数据捆绑成一个结构,然后传递结构的指针就可以了。
1)在我的pwm 驱动里面就用了内核定时器,其使用过程为:
struct pwm_dev *my_pwm_dev;
init_timer(&my_pwm_dev->my_timer);
my_pwm_dev->my_timer.expires = jiffies + HZ/5; // 定时器超时时的节拍数
my_pwm_dev->my_timer.data = (unsigned long)my_pwm_dev;
my_pwm_dev->my_timer.function = my_function; // 定时器超时时调用的函数
add_timer(&my_pwm_dev->my_timer);// 这里开始计时了
在这里 timet_list 的成员data 本来是一个unsigned long的数,这个是传递的一个指针,即设备的指针。
int my_function( unsigned long arg )// 这里的unsigned long arg即为 data。
{
struct pwm_dev *my_pwm_dev = (struct pwm_dev *)arg;
…..
}
在这个函数里面我们可以看到,在my_function函数里面,我们又把一个长整型的数强制转换成一个设备指针,这样就实现了利用 unsigned long 作指针传递数据的过程。
3使用过后还得删除定时器
del_timer(&my_pwm_dev->my_timer);
2) "./kernel/kernel/workqueue.c"
static int __init init_workqueues(void)
{
struct global_cwq*gcwq = get_gcwq(cpu);
spin_lock_init(&gcwq->lock);
INIT_LIST_HEAD(&gcwq->worklist);
gcwq->cpu = cpu;
gcwq->flags |= GCWQ_DISASSOCIATED;
INIT_LIST_HEAD(&gcwq->idle_list);
for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)
INIT_HLIST_HEAD(&gcwq->busy_hash[i]);
init_timer_deferrable(&gcwq->idle_timer);
gcwq->idle_timer.function = idle_worker_timeout;
gcwq->idle_timer.data = (unsigned long)gcwq;
setup_timer(&gcwq->mayday_timer, gcwq_mayday_timeout,
(unsigned long)gcwq);
.....
}
二.Ioctl()函数中 unsigned long 的使用。
Ioctl函数原型为:
int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
在驱动函数中表现为:
ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
这里也有一个unsigned long arg;同样,我们也可以用这个参数传递一个数或者结构体的指针,只用方法和上面的 timer_list 一样,传递时,使用地址,在ioctl 函数里,将arg 强制转换成原来的数或者结构体的指针。
- unsigned long 的用法
- unsigned long 的用法
- unsigned long long的问题
- unsigned long的问题
- unsigned long int 的陷阱
- (*(volatile unsigned long *)) 的理解
- (*(volatile unsigned long *)) 的理解(
- (*(volatile unsigned long *))的理解
- unsigned long的一点应用
- (*(volatile unsigned long *))的分析
- int,long int, unsigned int,unsigned long int,long long的表示范围问题(转)
- unsigned long
- unsigned long
- unsigned long
- unsigned long
- unsigned long unsigned int unsigned
- Java入门学习-理解float,double,unsigned,long,short,final,extends,implements的用法。
- unsigned int 与 unsigned long 在一般情况下是一样的
- 基于tomcat6.0的SSL实现
- C# winform 多线程中创建等待窗体
- WCF服务调试
- 长知识:谈网络炒作的五种方式和病毒式传播的效用
- 修改XML节点名称
- unsigned long 的用法
- »Spring 之AOP AspectJ切入点语法详解(最全了,不需要再去其他地找了)
- 在winform里面如何使用razor模板引擎?
- 关于yaf自动加载的一个疑惑
- Balsamiq Mockups完全手册
- cocos2d 坐标系使用
- 海量数据面试题举例
- jQuery学习08---相对定位,工资点击求和
- 我要开始写博客了。