130 likes | 259 Views
모듈 초기화. module_init(hello_init); module_exit(hello_exit);. MODULE_LICENSE(“Dual BSD/GPL”) MODULE_AUTHOR(“UNIX CLASS”) MODULE_DESCRIPTION(“TEST MODULE”) module_param short, ushort, int, uint, long, ulong, charp, bool, intarray(int *). #include <linux/init.h> #include <linux/module.h>
E N D
모듈 초기화 module_init(hello_init); module_exit(hello_exit); MODULE_LICENSE(“Dual BSD/GPL”) MODULE_AUTHOR(“UNIX CLASS”) MODULE_DESCRIPTION(“TEST MODULE”) module_param short, ushort, int, uint, long, ulong, charp, bool, intarray(int *)
#include <linux/init.h> #include <linux/module.h> #include <linux/kernel.h> #include <linux/moduleparam.h> static int onevalue = 1; static char *twostring = NULL; module_param(onevalue, int, 0); module_param(twostring, charp, 0); static int hello_init(void) { printk("Hello, world [onevalue=%d:twostring=%s]\n", onevalue, twostring ); return 0; } static void hello_exit(void) { printk("Goodbye, world\n"); } module_init(hello_init); module_exit(hello_exit); MODULE_AUTHOR(“”); MODULE_DESCRIPTION(“Module Parameter Test Module”); MODULE_LICENSE(“Dual BSD/GPL”);
이식성과 데이터형 • 서로 다른 프로세서 상에서의 이식성을 위해 • 가급적 리눅스 커널이 제공하는 데이터형을 • 사용하는 것이 좋다. • #include <asm/types.h>에 정의. • 주로 사용되는 데이터형
kmalloc(), kfree()-1 메모리주소 kmalloc(할당 받을 크기, 옵션); kfree(메모리주소); * 할당 가능한 최대 크기는 32 x PAGE_SIZE (일반적으로 128KB) #include <linux/slab.h> char *buf; buf = kmalloc(1024, GFP_KERNEL); if (buf != NULL) { … kfree(buf); }
kmalloc(), kfree()-2 메모리 할당 시 사용되는 옵션 (할당된 메모리에 특성을 주거나 할당 시점에 처리방식을 지정)
vmalloc(), vfree() 메모리주소 vmalloc(할당 받을 크기); vfree(메모리주소); #include <linux/vmalloc.h> char *buf; buf = vmalloc(1024); if (buf != NULL) { … vfree(buf); }
vmalloc(), vfree() 특징 • 가상 주소 공간에서 할당받기 때문에 해당주소의 영역이 • 하드디스크에 있을 수도 있어 실패할 수 있다. • 커다란 연속된 공간을 할당하기 위해 가상메모리 관리 루틴이 • 수행되기 때문에 kmalloc() 보다 속도가 매우 느리다. • 할당 시 프로세스가 잠들지 못하게 할 수 없기 때문에 • 인터럽트 서비스 함수 안에서는 사용할 수 없다.
__get_free_pages(), free_pages() 메모리주소 __get_free_pages(옵션, 차수); free_pages(메모리주소); #include <linux/mm.h> //2.4 #include <linux/gfp.h> //2.6 #include <asm/page.h> //get_order char *buf; buf = __get_free_pages(GFP_KERNEL, order); /* order=0이면 1개의 page, order=3이면 3개의 page /* MAX_ORDER은 11, 그러나 5이하의 값만 사용하는 것이 안전 (32*PAGE_SIZE) */ if (buf != NULL) { … free_pages(buf, order); }