大佬教程收集整理的这篇文章主要介绍了linux – 如果需要调用copy_to_user,如何使用自旋锁?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
static ssize_t char_dev_read(struct file *file,char *buf,size_t lbuf,loff_t *ppos) { int maxbytes; /* number of bytes from ppos to MAX_LENGTH */ int bytes_to_do; /* number of bytes to read */ int nbytes; /* number of bytes actually read */ maxbytes = MAX_LENGTH - *ppos; if( maxbytes > lbuf ) bytes_to_do = lbuf; else bytes_to_do = maxbytes; if( bytes_to_do == 0 ) { printk("Reached end of device\n"); return -ENOSPc; /* Causes read() to return EOF */ } /* Tesing for accidental release */ // accidental_release(); printk(KERN_DEBUG "READER: trying for critical region lock \n"); spin_lock(&myspin);/*begin of critical region */ printk(KERN_DEBUG "READER : acquired lock: execuTing critical code\n"); nbytes = bytes_to_do - copy_to_user( buf,/* to */ char_device_buf + *ppos,/* from */ bytes_to_do ); /* how many bytes */ spin_unlock(&myspin); /* end of critical region */ *ppos += nbytes; return nbytes; }
>处理mmap()编辑文件
>该过程调用驱动程序为该mmap()ed区域提供地址
>您的代码运行,锁定,然后copy_to_user导致该地址发生页面错误 – 内存不存在,以便进程进入休眠状态,直到数据来自磁盘.
>内核计划处理B,它以相同的方式调用驱动程序.
>死锁 – 进程A正在等待IO返回锁内但不会被调度,因为B正在等待同一个锁被解锁的cpu.
除非100%保证copy_ {to,from} _user不会导致段错误,否则不能使用自旋锁,但必须使用睡眠锁,例如’mutex_lock’.睡眠锁定会对调度程序产生控制,而旋转锁定则不会.
以上是大佬教程为你收集整理的linux – 如果需要调用copy_to_user,如何使用自旋锁?全部内容,希望文章能够帮你解决linux – 如果需要调用copy_to_user,如何使用自旋锁?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。