⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rt_sig_mmap1.c

📁 fsmlabs的real time linux的内核
💻 C
字号:
/* * Written by Der Herr Hofrat, der.herr@hofr.at * (C) 2002 FSMLabs * License: GPL Version 2 */#include <rtl.h>#include <time.h>#include <pthread.h>#include <rtl_signal.h>#include <linux/sched.h>#include <linux/module.h>#include <linux/version.h>#include <linux/init.h>#include <linux/kernel.h>#include <linux/fs.h>#include <linux/errno.h>#include <linux/mm.h>#include <linux/slab.h>#include <linux/mman.h>#include <linux/slab.h>#include <linux/wrapper.h>#include <asm/io.h>#include <asm/uaccess.h>  #include "device_common.h"static pthread_t rt_thread;static char *kmalloc_area;static void * rt_thread_code(void *arg){	struct sched_param p;	p . sched_priority = 1;	pthread_setschedparam (pthread_self(), SCHED_FIFO, &p);	while (1) {		rtl_printf("RT-Thread woke up buffer=%s\n",kmalloc_area);		pthread_suspend_np (pthread_self());	}	return 0;}static int driver_open(struct inode *inode, 	struct file *file ){	MOD_INC_USE_COUNT;	return 0;}static int driver_close(struct inode *inode, 	struct file *file){	/* some driver closed the device again so check what we got */	pthread_kill(rt_thread,RTL_SIGNAL_WAKEUP); 	MOD_DEC_USE_COUNT;	return 0;}static intdriver_mmap(struct file *file,	struct vm_area_struct *vma){	vma->vm_flags |= VM_SHARED|VM_RESERVED;  	if(remap_page_range(vma->vm_start,		virt_to_phys(kmalloc_area),		LEN,		PAGE_SHARED))		{			printk("mmap failed\n");			return -ENXIO;		}	return 0;}	static struct file_operations simple_fops={	mmap:	driver_mmap,	open:	driver_open, 	release:	driver_close,};static int __init simple_init(void){	struct page *page;	int ret;	char msg[]="Hello Mmap'ed World";	kmalloc_area=kmalloc(LEN,GFP_USER);	if(!kmalloc_area){		printk("kmalloc failed - exiting\n");		return -1;	}	page = virt_to_page(kmalloc_area); 	mem_map_reserve(page);	memcpy(kmalloc_area,msg,sizeof(msg));	if(register_chrdev(SIMPLE_MAJOR,DEV_NAME, &simple_fops) == 0) {		printk("driver for major %d registered\n",SIMPLE_MAJOR);		ret = pthread_create (&rt_thread, 			NULL, 			rt_thread_code, 			0);        	return 0;	}	printk("unable to get major %d\n",SIMPLE_MAJOR);	return -EIO;}static void __exit simple_exit(void){	/* delete the rt-thread */	pthread_delete_np (rt_thread);	unregister_chrdev(SIMPLE_MAJOR,DEV_NAME);	kfree(kmalloc_area);	printk("rt_sig_thread exit\n");}module_init(simple_init);module_exit(simple_exit);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -