spinlock_t lock=SPIN_LOCK_UNLOCKED; static int file_count; static int signel_open(struct inode *inode,struct file *file ) { printk("get lock\n"); spin_lock(lock); if(file_count) { spin_unlock(lock); return -EBUSY; } file_count++; spin_unlock(lock); return 0; } static int signel_close(struct inode *inode,struct file *file) { file_count--; return 0; } 以下是修改后的,没有加入spin_unlock(); static int signel_open(struct inode *inode,struct file *file ) { printk("get lock\n"); spin_lock(lock); file_count++; return 0; } static int signel_close(struct inode *inode,struct file *file) { file_count--; return 0; } #define DEV_NAME "/dev/signel" int main() { int fd,ret; int a=01; fd=open(DEV_NAME,O_RDWR); if(fd<0) { printf("opened is fail!!\n"); } else { printf("open is scuss!!!\n"); printf("writr a=%d\n",a); write(fd,&a,sizeof(a)); } close(fd); printf("closed the file!!\n"); } |
原帖由 ingdxdy 于 2009-3-16 00:11 发表
自旋锁,本质上讲,是专门为多处理器架构设计的,当然支持抢夺式进程调度的内核在单处理器上也可以使用自旋锁。如果在一个非抢占式单处理器的系统中进入自旋,那么这个自旋将永远无法退出,因为没有其他进程可以 ...
欢迎光临 Chinaunix (http://bbs.chinaunix.net/) | Powered by Discuz! X3.2 |