[Linux Device Driver (Third edition)] -- completion

Source: Internet
Author: User

[Linux Device Driver (Third edition)] ---- 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
Create a device Node
mknod /dev/complete c 250 0

Load

insmod complete.ko

Test

cat /dev/complete

The read thread goes to sleep.

 

Open another terminal and enter:

echo "Test" > /dev/complete

At this point, the read thread runs and the output result is "test"

 

Uninstall

rmmod complete

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.