欢迎来到天天文库
浏览记录
ID:33845841
大小:55.58 KB
页数:7页
时间:2019-03-01
《内核同步实验》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、实验^一内核同步实验实验目的常握字符设备驱动程序编写的框架;掌握内核屮同步操作函数的使用;实验原理自旋锁:只有两种状态:锁上,没锁上。(lock,unlock)当一个线程上锁自旋锁时,别的线程想要锁上该白旋锁的时候,会自旋(轮询)。直到第一个线程释放该自旋锁。信号量:跟自旋锁相比:不能获得时,休眠(而不是自旋);有数值(不是两种状态);读写自旋锁:读可以多个线程一起;但写操作时,只能一个线程;读写信号量:休眠互斥体:只有两种状态的信号量(1,0);实验要求(1)修改tiny4412开发板上的LED灯驱动程序(基于实验九);使用应用层
2、打开设备文件时,同一时间,只能有一个进程能够使用该文件。一个进程打开设备文件后,另外一个进程打不开这个设备文件。主要修改驱动的:open,release方法;使用信号量实现;(2)可以让多个进程打开设备文件,但是读写操作不能同时进行,对驱动屮要读写的全局变量进行同步保护;主要修改驱动的:read,write方法;分别使用:自旋锁,读写自旋锁,读写信号量,互斥体实现;Leddrv:#ineludevlinux/module.h>#include#inclucle#includ
3、e#include#ineludevlinux/uaccess.h>#include#inclucle#include#include#include#inclucle#include#include#include4、wsem.h>volatileunsignedlong*VIR_GPM4C0N;volatileunsignedlong*VIR_GPM4DAT;staticatomic_tled_atomic=ATOMIC」NIT(1);DEFINE_SEMAPHORE(Ied_sema);staticintled_open(structinode*in,structfile*fp)if(!atomic_dec_and_test(&led_atomic)){atomic_inc(&led_atomic);return・EBUSY;}*///dow5、n(&1ed_sema);down_interruptible(&led_sema);printk(ukernel:led_openu);*V1R_GPM4CON&=〜(Oxffff);*VIR_GPM4CON6、=(Oxllll);*VIR_GPM4DAT&=~(0xf);return0;staticintled_release(structinode*in,structfile*fp){//atomic_inc(&led_atomic);up(&led_sema);printk(ukernel:led_releasen);7、*VIR_GPM4DAT8、=(Oxf);return0;}DEFINE_RWLOCK(led_rwlock);〃定义读写自旋锁led.rwlockstaticunsignedcharled_state;staticssize_tled_read(structfilechar_user*buf,size_tlen,lofO*offset){read_lock(&led_rwlock);//上锁led_state=led_state«l;copy_to_user(buf,&led_state,1);read_unlock(&led_rw9、lock);//解锁return1;}staticssize_tled_write(structfile*fp,constchar—user*buf,size_tlen,lofLt^offset){write_lock(&led_rwlock);//上锁copy_from_user(&led_state,buf,1);*VIR_GPM4DAT=~(led_state);write_unlock(&led_rwlock);//解锁return1;}staticstructfile_operationsled_ops={.owner=T10、HIS_MODULE,・open=led_open,・release=led_release,・write=led_write,・read=led_read,};staticdev_tdev;structcdev*led_cdev;s
4、wsem.h>volatileunsignedlong*VIR_GPM4C0N;volatileunsignedlong*VIR_GPM4DAT;staticatomic_tled_atomic=ATOMIC」NIT(1);DEFINE_SEMAPHORE(Ied_sema);staticintled_open(structinode*in,structfile*fp)if(!atomic_dec_and_test(&led_atomic)){atomic_inc(&led_atomic);return・EBUSY;}*///dow
5、n(&1ed_sema);down_interruptible(&led_sema);printk(ukernel:led_openu);*V1R_GPM4CON&=〜(Oxffff);*VIR_GPM4CON
6、=(Oxllll);*VIR_GPM4DAT&=~(0xf);return0;staticintled_release(structinode*in,structfile*fp){//atomic_inc(&led_atomic);up(&led_sema);printk(ukernel:led_releasen);
7、*VIR_GPM4DAT
8、=(Oxf);return0;}DEFINE_RWLOCK(led_rwlock);〃定义读写自旋锁led.rwlockstaticunsignedcharled_state;staticssize_tled_read(structfilechar_user*buf,size_tlen,lofO*offset){read_lock(&led_rwlock);//上锁led_state=led_state«l;copy_to_user(buf,&led_state,1);read_unlock(&led_rw
9、lock);//解锁return1;}staticssize_tled_write(structfile*fp,constchar—user*buf,size_tlen,lofLt^offset){write_lock(&led_rwlock);//上锁copy_from_user(&led_state,buf,1);*VIR_GPM4DAT=~(led_state);write_unlock(&led_rwlock);//解锁return1;}staticstructfile_operationsled_ops={.owner=T
10、HIS_MODULE,・open=led_open,・release=led_release,・write=led_write,・read=led_read,};staticdev_tdev;structcdev*led_cdev;s
此文档下载收益归作者所有