Week 6 Intro To Kernel Modules, Project 2: Sarah Diesburg Florida State University
Week 6 Intro To Kernel Modules, Project 2: Sarah Diesburg Florida State University
Sarah Diesburg
1
Kernel Logistics
2
Kernel Logistics
4
Kernel Modules
5
Kernel Module
6
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.
7
Creating a Kernel Module
8
Sample Kernel Module: hello.c
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE(Dual BSD/GPL);
module_init(hello_init);
module_exit(hello_exit);
9
Sample Kernel Module: hello.c
Module headers
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE(Dual BSD/GPL);
module_init(hello_init);
module_exit(hello_exit);
10
Sample Kernel Module: hello.c
License
#include <linux/init.h>
#include <linux/module.h>
declaration
MODULE_LICENSE(Dual BSD/GPL);
module_init(hello_init);
module_exit(hello_exit);
11
Sample Kernel Module: hello.c
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE(Dual BSD/GPL);
Initialization
function, runs
static int hello_init(void) when module
{ loaded
printk(KERN_ALERT Hello, world!\n);
return 0;
}
12
Sample Kernel Module: hello.c
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE(Dual BSD/GPL);
else
KERNELDIR ?= \
/lib/modules/`uname -r`/build/
PWD := `pwd`
default:
$(MAKE) -C $(KERNELDIR) \
M=$(PWD) modules
endif
clean:
rm -f *.ko *.o Module* *mod*
14
Compile the Kernel Module
/usr/src/hello$> make
15
Inserting and Removing the Module
16
Listing Modules
/usr/src/hello$>lsmod
17
Where is it printing?
Demo
18
Kernel Module vs User Application
19
Kernel Functions
20
Kernel manpages
Section 9 of manpages
Must install manually for our development
kernel
$> wget http://ftp.us.debian.org/debian/pool/main/l/linux-
2.6/linux-manual-2.6.32_2.6.32-22_all.deb
21
Kernel Headers
23
Project 2: /Proc Kernel
Module and Elevator
24
procfs Kernel Module
25
Procfs: Headers and Global Data
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
MODULE_LICENSE(GPL);
26
Procfs: Creation
int hello_proc_init(void)
{
proc_entry =
create_proc_entry(ENTRY_NAME,
PERMS,PARENT);
/* check proc_entry != NULL */
proc_entry->read_proc = procfile_read;
proc_entry->mode = S_IFREG | S_IRUGO;
proc_entry->uid = 0;
proc_entry->gid = 0;
proc_entry->size = 11;
27
Procfs: Reading
int procfile_read(char *buf, char **buf_location,
off_t offset, int buffer_length, int *eof, void
*data)
{
int ret;
printk(/proc/%s read called.\n, ENTRY_NAME);
return ret;
}
28
Procfs: Deletion
void hello_proc_exit(void)
{
remove_proc_entry(ENTRY_NAME, NULL);
printk(Removing /proc/%s.\n, ENTRY_NAME);
}
29
Procfs: Registration
module_init(hello_proc_init);
module_exit(hello_proc_exit);
30
Testing Procfs
31
Part 2: Kernel Time
32
Part 3: Elevator Scheduling
33
Part 3: Elevator Scheduling
34
Why Elevator Scheduling?
35
Your Elevator
One elevator
Five floors
Four types of people
Adults
Children
Delivery people
Maintenance people
The elevator cannot exceed its maximum
weight load
36
Your Elevator
38
Each passenger has a destination floor
in mind
I want
to go to
floor 3
39
The elevator must be started to service
passengers
Start!
40
The elevator must be started to service
passengers
Elevator
starts on the
first floor
41
Passengers enter in FIFO order
Make sure
passengers dont
exceed weight
limit!
42
Passengers enter in FIFO order
More passengers
can be queuing
up!
43
Elevator can move to any floor
Going to floor 3!
44
Elevator can move to any floor
45
Elevator can move to any floor
46
Elevator can move to any floor
47
Elevator can move to any floor
48
Elevator can move to any floor
49
Passengers disappear when they exit
50
Elevator stop in progress
Must finish
delivering
passengers
before stopping
Stop in
Progress
51
Elevator stop in progress
Must finish
delivering
passengers
before stopping
Stop in
Progress
52
Elevator stop in progress
Must finish
delivering
passengers
before stopping
Stop in
Progress
53
Elevator stop
Full stop
54
Controlling the Elevator
int start_elevator(void)
int stop_elevator(void)
55
Elevator Scheduling Algorithms
consumer.c
Runs in infinite loop
Issues K passenger requests once per second
producer.c
Takes an argument telling the elevator to start or
stop
57
Kernel Time Constraints
#include <linux/delay.h>
void ssleep(unsigned int seconds);
58
Additional Design Considerations
59
Next Time
60
What you should do?
61