Download presentation
Presentation is loading. Please wait.
Published byMerryl Carol Hawkins Modified over 8 years ago
1
1 Intro to Kernel Modules and /proc Sarah Diesburg CS 3430 Operating Systems
2
Kernel Modules Or “drivers”, if you prefer… 2
3
Kernel Module A kernel module is a portion of kernel functionality that can be dynamically loaded into the operating system at run-time Example USB drivers File system drivers Disk drivers Cryptographic libraries 3
4
Why not just compile everything into the kernel? Each machine only needs a certain number of drivers For example, should not have to load every single motherboard driver Load only the modules you need Smaller system footprint Dynamically load modules for new devices Camera, new printer, etc. 4
5
Creating a Kernel Module Hello world example 5
6
Sample Kernel Module: hello.c #include MODULE_LICENSE(“Dual BSD/GPL”); static int hello_init(void) { printk(KERN_ALERT “Hello, world!\n”); return 0; } static void hello_exit(void) { printk(KERN_ALERT “Goodbye, sleepy world.\n”); } module_init(hello_init); module_exit(hello_exit); 6
7
Sample Kernel Module: hello.c #include MODULE_LICENSE(“Dual BSD/GPL”); static int hello_init(void) { printk(KERN_ALERT “Hello, world!\n”); return 0; } static void hello_exit(void) { printk(KERN_ALERT “Goodbye, sleepy world.\n”); } module_init(hello_init); module_exit(hello_exit); 7 Module headers
8
Sample Kernel Module: hello.c #include MODULE_LICENSE(“Dual BSD/GPL”); static int hello_init(void) { printk(KERN_ALERT “Hello, world!\n”); return 0; } static void hello_exit(void) { printk(KERN_ALERT “Goodbye, sleepy world.\n”); } module_init(hello_init); module_exit(hello_exit); 8 License declaration
9
Sample Kernel Module: hello.c #include MODULE_LICENSE(“Dual BSD/GPL”); static int hello_init(void) { printk(KERN_ALERT “Hello, world!\n”); return 0; } static void hello_exit(void) { printk(KERN_ALERT “Goodbye, sleepy world.\n”); } module_init(hello_init); module_exit(hello_exit); 9 Initialization function, runs when module loaded Tells kernel which function to run on load
10
Sample Kernel Module: hello.c #include MODULE_LICENSE(“Dual BSD/GPL”); static int hello_init(void) { printk(KERN_ALERT “Hello, world!\n”); return 0; } static void hello_exit(void) { printk(KERN_ALERT “Goodbye, sleepy world.\n”); } module_init(hello_init); module_exit(hello_exit); 10 Exit function, runs when module exits Tells kernel which function to run on exit
11
Sample Kernel Module: Makefile ifneq ($(KERNELRELEASE),) obj-m := hello.o else KERNELDIR ?= \ /lib/modules/`uname -r`/build/ PWD := `pwd` default: $(MAKE) -C $(KERNELDIR) \ M=$(PWD) modules endif clean: rm -f *.ko *.o Module* *mod* 11
12
/usr/src/hello$> make Creates hello.ko – This is the finished kernel module! Compile the Kernel Module 12
13
Inserting and Removing the Module insmod – insert a module /usr/src/hello$> sudo insmod hello.ko rmmod – remove a module /usr/src/hello$> sudo rmmod hello.ko 13
14
Listing Modules lsmod – lists all running modules /usr/src/hello$>lsmod 14
15
Where is it printing? Look inside /var/log/syslog Hint – to watch syslog in realtime, issue the following command in a second terminal: $> sudo tail –f /var/log/syslog Demo… 15
16
Kernel Module vs User Application All kernel modules are event-driven Register functions Wait for requests and service them Server/client model No standard C library Why not? No floating point support Segmentation fault could freeze/crash your system Kernel ‘oops’! 16
17
Kernel Functions printk() instead of printf() kmalloc() instead of malloc() kfree() instead of free() Where can I find definitions of these kernel functions? 17
18
Kernel manpages Section 9 of manpages Use manpages on prog1 $> sudo apt-get install linux-manual-3.16 $> man printk 18
19
Kernel Headers #include /* module stuff */ #include /* locks */ #include /* linked lists */ #include /* string functions! */ Look inside linux-4.2.2/include/ for more… Google is also your friend 19
20
Project 2: Kernel Modules 20
21
procfs Kernel Module procfs “hello world” example Creates a read/write procfs entry Steps Create entry in module_init function Registers reading/writing function using file_operations structure Delete entry in module_cleanup function 21
22
Testing Procfs $> sudo insmod hello_proc.ko $> sudo tail /var/log/syslog $> cat /proc/helloworld $> echo 2 > /proc/helloworld $> sudo rmmod hello_proc 22
23
Part 2: Remember You will write your own proc module called remember that Allows the user to write a string to /proc/remember (max length 80) Allows the user to read /proc/remember and get back the string that was just added If no string was added, the string EMPTY should be read instead 23
24
Remember Example #> echo “Hello there” > /proc/remember #> cat /proc/backwards Hello there #> 24
25
Part 3: Kitchen Scheduling 25
26
Part 3: Kitchen Scheduling Implement a /proc kernel module that simulates an busy kitchen at a restaurant Your /proc/kitchen file must accept writes of different dishes Each dish takes some time to process Your /proc/kitchen file must return status information when read 26
27
Why Scheduling? Classic producer/consumer analogy Similar to disk schedulers File system produces read/write requests Disk consumes requests, optimized for disk head position, rotational delays, etc. 27
28
Your Kitchen One kitchen 20 order slots Use a static array of ints? Four types of dishes 1 - Caesar Salad 2 - Hamburger 3 - Personal Pizza 4 - Beef Wellington 28
29
Food orders A write of 1-4 to /proc/kitchen will fill an order slot with the dish corresponding to the number You decide how the order slots get filled (e.g. round robin or other way) Kitchen takes 1 second to look in an order slot Regardless if empty or containing an order 29
30
Food Orders The kitchen can only process one order at a time Each order takes a different amount of time to process 2 seconds for a Caesar Salad 3 seconds for a Hamburger 4 seconds for a Personal Pizza 8 seconds for a Beef Wellington 30
31
Food Order Once an order is processed, the kitchen can mark that order slot as empty and should look at other order slots to find more orders to process. 31
32
Additional Kitchen Commands In addition to accepting orders 1-4, a kitchen should respond to writes of 0 or -1 in the following ways 0 : start the kitchen -1: stop the kitchen 32
33
Starting the Kitchen Before a kitchen is started, it cannot be processing orders But it can receive orders on the queue The cooks just aren’t working yet! When a kitchen is stopped, it must finish processing the current order and cease to process any more 33
34
Starting the Kitchen The actual order processing logic will be run in a kthread Introduced in the next recitation lecture 34
35
Status Information Performing a read on /proc/kitchen should return some status information Kitchen status: running or not running Current spot being looked at in the queue Current order being processed If the kitchen is not running, the last two pieces of information do not need to be dislplayed 35
36
Demo 36
37
Scheduling Algorithms A scheduling algorithm considers the state of the consumers and all requests and tries to optimize some metric Throughput: Maximize total requests, minimize processing total time. Priorities: Requests now have deadlines. Maximize number of requests meeting deadlines. Burst throughput: Maximize peak requests that can be handled. Energy: Minimize consumer action 37
38
Kernel Time Constraints #include void ssleep(unsigned int seconds); A call to ssleep will have the program cease to the task scheduler for seconds number of seconds 38
39
Additional Design Considerations How to place orders in order slots? How to check order slots from the kitchen? What scheduling algorithm to use? 39
40
Next Time Locking Must lock when accessing anything global Kitchen queue Kitchen status variables Kthread How to start/stop the looping logic that looks in the order queue and “processes” orders 40
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.