【Linux裝置驅動程式(第三版)】—-完成量completion

來源:互聯網
上載者:User

 【Linux裝置驅動程式(第三版)】----完成量completion

complete.c

#include <linux/init.h>#include <linux/module.h>#include <linux/sched.h>#include <linux/kernel.h>#include <linux/types.h>#include <linux/fs.h>//file_operations, file#include <linux/completion.h>#include <asm/uaccess.h>//copy_to_user & copy_from_userMODULE_LICENSE("Dual BSD/GPL");#define MEMSIZE 100int complete_major = 250;DECLARE_COMPLETION(comp);unsigned char s[MEMSIZE];ssize_t complete_read(struct file *filp, char __user *buf, size_t size, loff_t *pos){unsigned long p = *pos;unsigned int count = size;int ret = 0;if(p >= MEMSIZE)return 0;if(count > MEMSIZE - p)count = MEMSIZE -p;wait_for_completion(&comp);if(copy_to_user(buf, (void*)(s + p), count)){return -EFAULT;}*pos += count;ret = count;return ret;}ssize_t complete_write(struct file *filp, char __user *buf, size_t size, loff_t *pos){unsigned long p = *pos;unsigned int count = size;int ret = 0;if(p >= MEMSIZE)return 0;if(count > MEMSIZE - p)count = MEMSIZE -p;if(copy_from_user(s + p, buf, count)){return -EFAULT;}*pos += count;ret = count;complete(&comp);return ret;}struct file_operations complete_fops = {.owner = THIS_MODULE,.read = complete_read,.write = complete_write,};int complete_init(void){int result;result = register_chrdev(complete_major, "complete", &complete_fops);if(result < 0)return result;return 0;}void complete_exit(void){unregister_chrdev(complete_major, "complete");}module_init(complete_init);module_exit(complete_exit);

Makefie

obj-m:= complete.omodules-objs:= complete.oKDIR:= /usr/src/linux-headers-2.6.31-14-generic/PWD:= $(shell pwd)default:make -C $(KDIR) M=$(PWD) modulesclean:rm -rf *.ko *.mod.c *.mod.o *.o *.markers *.symvers *.order
建立裝置節點
mknod /dev/complete c 250 0

裝載

insmod complete.ko

測試

cat /dev/complete

此時,讀線程會進入休眠。

 

開啟另一個終端輸入:

echo "Test" > /dev/complete

此時,讀線程運行,並輸出結果:“Test”

 

卸載

rmmod complete

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.