1. |
Preface:
A. |
Before proceeding to the following work, please read my article "compile a kernel module that can be loaded under the Linux 2.6 kernel" to configure a Linux environment that can be loaded with the kernel module |
B. |
Download the source code for the Linux Device Driver (Third edition). You can download it here. If you cannot download it, leave a message or send me an email. |
C. |
The working environment in this article is Fedora Core 5. Use "uname-R" to check whether the kernel version is 2.6.15-1.2054 _ fc5. |
D. |
This article focuses on the sixth chapter in "Linux Device Driver (version 3)": Advanced Character Device Driver operations. An example is provided on page 151st to illustrate simple sleep, this document describes how to implement and run this example. |
E. |
Some of the following operations require the root permission, so try to use the root user to execute the command. |
|
2. |
Decompress the source code package and enter the MISC-modules folder. The files include:
[Wooin @ 15:05 MISC-modules] $ pwd /Home/wooin/ldd.3rd. Examples/MISC-Modules [Wooin @ 15:06 MISC-modules] $ LL Total 60 k -RW-r -- 1 wooin Bao 2.1 K Complete. c -RW-r -- 1 wooin Bao 2.0 k 2005-02-01 04:31 faulty. c -RW-r -- 1 wooin Bao 463 hello. c -RW-r -- 1 wooin Bao 1.2 K 2005-02-01 04:31 hellop. c -RW-r -- 1 wooin Bao 6.1 K jiq. c -RW-r -- 1 wooin Bao 7.4 k jit. c -RW-r -- 1 wooin Bao 2.3 K 2005-02-01 04:31 kdataalign. c -RW-r -- 1 wooin Bao 1.5 K 2005-02-01 04:31 kdatasize. c -RW-r -- 1 wooin Bao 987 makefile -RW-r -- 1 wooin Bao 2.0 k 2005-02-01 04:31 seq. c -RW-r -- 1 wooin Bao 6.1 K 2005-02-01 04:31 silly. c -RW-r -- 1 wooin Bao 2.2 k 2005-02-01 04:31 sleepy. c |
Sleepy. c is the file to be discussed in this article. open the file and change "kern_debug1" in the printk () function "Kern_alert", save and exit to view debugging information in the/var/log/messages file. |
3. |
Compile: You can run the make command in the MISC-modules directory to compile the file. If you have any compilation problems, please read this article. Compile a kernel module that can be loaded under Linux 2.6 kernel After compilation is successful, many files are generated, including a sleepy. ko is the kernel module we want. Part of its source code is in chapter 6 of "Linux Device Driver (version 3)": Advanced Character device driver operation, page 151st. |
4. |
Add the device to the kernel: Run the following command:
Add it to the kernel. At this time, the kernel can recognize the sleepy device, but we cannot access it because there is no sleepy device in the user space, that is to say, there is no sleepy device in/dev/. We need to manually add it. First View In the/proc/devices file, locate the sleepy device and find its primary device number, for example:
[Root @ Fox MISC-modules] # Cat/proc/devices Character devices: 1 mem 4/dev/VC/0 4 tty 4 TTYs : : 180 USB 189 usb_device 226 DRM 253 sleepy # check that the master device Number of sleepy is 253 254 PCMCIABlock devices: 1 ramdisk 1 ramdisk 3 ide0 7 Loop 8 SD : : |
Create a sleepy device in the/dev/directory:
// 253 indicates the master device number. You can modify this value as needed. 0 indicates the next device number. # Mknodded/dev/sleepy C 253 0
// View the newly added Device # Ls-L/dev/sleepy CrW-r -- 1 Root 253, 0 08-03/dev/sleepy |
|
5. |
Try the device: According to the introduction in the book, sleepy is used to demonstrate simple sleep: when the device is read, the process enters the sleep state until other processes write the device, the read process will be awakened, let's read the sleepy device:
# Dd If =/dev/sleepy of = ~ /Temp |
The above command reads data from the/dev/sleepy device and writes it ~ In the/Temp File, when you press the Enter key, the command line will be suspended. If you want to die, you can view the/var/log/messages file similar to the following content: "Aug 3 15:58:20 Fox kernel: Process 13534 (dd) going to sleep" This is the debugging information in the sleepy_read () function in sleepy. C. It tells you that the process has sleep. In this case, open another terminal and run the following command to write the sleepy device:
This command will return immediately and output the debugging information in the/var/log/messages file: "Aug 3 16:05:22 Fox kernel: Process 13583 (LS) awakening the readers ..." This is the debugging information in the sleepy_write () function in sleepy. C. It tells you that the current process has started to wake up the read process. Let's look back at the pending read operations and find that "dd If =/dev/sleepy of = ~ The/temp command has been returned and output debugging information in the/var/log/messages file: Aug 3 16:05:22 Fox kernel: awoken 13534 (dd) This is the debugging information in the sleepy_read () function in sleepy. C. It tells you that the process has been awakened. |
6. |
Related code: Follow up the order of printed information by printk to get a general idea of the entire "Sleep" and "wake up" process.
34 ssize_t sleepy_read (struct file *filp, char __user *buf, size_t count, loff_t *pos) 35 { 36 printk(KERN_ALERT "process %i (%s) going to sleep/n", 37 current->pid, current->comm); 38 wait_event_interruptible(wq, flag != 0); 39 flag = 0; 40 printk(KERN_ALERT "awoken %i (%s)/n", current->pid, current->comm); 41 return 0; /* EOF */ 42 } 43 44 ssize_t sleepy_write (struct file *filp, const char __user *buf, size_t count, 45 loff_t *pos) 46 { 47 printk(KERN_ALERT "process %i (%s) awakening the readers.../n", 48 current->pid, current->comm); 49 flag = 1; 50 wake_up_interruptible(&wq); 51 return count; /* succeed, to avoid retrial */ 52 |
|
7. |
|
8. |
|
9. |
|
|
|
|
|