kmalloc.c

来自「fsmlabs的real time linux的内核」· C语言 代码 · 共 77 行

C
77
字号
/* vim: set ts=4: *//* * Copywrite 2002 Der Herr Hofrat * License GPL V2 * Author der.herr@hofr.at *//* * example of using kmalloc - basically you only need to call kmalloc but it * many cases it makes sense to not give up imediatly if the request is not * honored - so here we do it in a loop until we get something or time out. */#include <rtl.h>#include <time.h>#include <pthread.h>#include <linux/slab.h> /* kmalloc */int size=128*1024 ; /* 128k - maximum posible */void *mem_ptr; hrtime_t *rt_times;pthread_t thread;/* rt-thread that does nothing */void * start_routine(void *arg){	struct sched_param p;	p . sched_priority = 1;	rt_times = (hrtime_t *)mem_ptr; 	pthread_setschedparam (pthread_self(), SCHED_FIFO, &p);	pthread_make_periodic_np (pthread_self(), gethrtime(), 500000);	while((void *)rt_times < (mem_ptr+size)){		pthread_wait_np ();		/* record the time we were woken up */		*rt_times=gethrtime();		rt_times++;	}	return (void *)0;}int init_module(void) {	int attempts=0;	/* don't give up imediatly if kmalloc fails */	while ((mem_ptr = kmalloc(size, GFP_KERNEL)) == NULL && attempts < 5) {		current->state   = TASK_INTERRUPTIBLE;		schedule_timeout(HZ/10);		attempts++;	}	memset(mem_ptr, 0, size);	return pthread_create(&thread,NULL,start_routine, (void *)0);}void cleanup_module(void) {	void * retval;	int i;	int len=(size/sizeof(hrtime_t));	pthread_delete_np (thread);	pthread_join(thread,&retval);	printk("thread terminated with %d\n",(int)retval);	/* run through the recorded data on rmmod */	rt_times = (hrtime_t *)mem_ptr; 	for(i=0;i<len;i++){		printk("I was there at %Ld\n",*rt_times);		rt_times++;	}	kfree(mem_ptr);}

⌨️ 快捷键说明

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