资源描述:
《实验十三 内核同步实验》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、实验十三内核同步实验实验目的掌握字符设备驱动程序编写的框架;掌握内核中同步操作函数的使用;实验原理自旋锁:只有两种状态:锁上,没锁上。(lock,unlock)当一个线程上锁自旋锁时,别的线程想要锁上该自旋锁的时候,会自旋(轮询)。直到第一个线程释放该自旋锁。信号量:跟自旋锁相比:不能获得时,休眠(而不是自旋);有数值(不是两种状态);读写自旋锁:读可以多个线程一起;但写操作时,只能一个线程;读写信号量:休眠互斥体:只有两种状态的信号量(1,0);实验要求(1)修改fs2410开发板上的LED灯驱动程序(基于下面的代码);使用应用层打开设备文件时,
2、同一时间,只能有一个进程能够使用该文件。一个进程打开设备文件后,另外一个进程打不开这个设备文件。主要修改驱动的:open,release方法;使用信号量实现;(2)驱动中对全局变量led_states的同步保护;主要修改驱动的:read,write方法;分别使用:自旋锁,读写自旋锁,读写信号量,互斥体实现;Led驱动程序如下:#include#include#include#include#include
3、#include#include#include#include#include#include#include#include#include#includestaticstructclass*led_class;stat
4、icunsignedlonggpfbase;#defineGPFCON(*(volatileunsignedlong*)(gpfbase+0x0))#defineGPFDAT(*(volatileunsignedlong*)(gpfbase+0x4))#defineGPFUP(*(volatileunsignedlong*)(gpfbase+0x8))staticintfs2410_led_open(structinode*inode,structfile*file){printk("firstdrveropencall");GPFCON=0x
5、5500;GPFUP=0xf0;GPFDAT=0x0;return0;}staticintfs2410_led_release(structinode*inode,structfile*file){printk("firstdirverreleasecall!");GPFDAT=0xf0;return0;}//全局变量的读写使用读写自旋锁staticcharled_states=0x0;staticssize_tfs2410_led_write(structfile*file,constchar__user*userbuf,size_tbyte
6、s,loff_t*off){charval;copy_from_user(&val,userbuf,1);printk("%d",val);GPFDAT=~(val<<4);led_states=val;returnsizeof(int);}staticssize_tfs2410_led_read(structfile*file,char__user*userbuf,size_tbytes,loff_t*off){copy_to_user(userbuf,&led_states,sizeof(char));printk("firstdirverre
7、adcall!");returnsizeof(char);}staticstructfile_operationsfops={.owner=THIS_MODULE,.open=fs2410_led_open,.release=fs2410_led_release,.write=fs2410_led_write,.read=fs2410_led_read,};staticintmajor=0;staticstructcdev*fs2410_led_cdev;#defineFIRST_DRV_NAME"fs2410_led"staticint__i
8、nitfs2410_led_init(void){/*申请设备号*/dev_tdev;alloc_chrdev_regio