Linux using printk () method

  

If you have learned about the Linux kernel, then you should have a certain understanding of the printk function, then for those who have not touched the printk function, printk () how to Use it? The following small series explains the use of printk() through an example, so that everyone can have a deeper understanding.

Example:

New hello.c

#include "linux /kernel.h"

#include "linux /module.h

int init_module(void)

<

printk(KERN_INFO “Welcome.. . . .n”);

return 0;

}

void cleanup_module(void)

{

printk(KERN_INFO “Bye.....\ ”);

}

Then write the Makefile in the same directory:

obj-m := hello.o

all:

make -C /lib /modules/$(shell uname -r)/build M=$(shell pwd)

clean:

-rm *.o *.ko Mod* mod* *.mod.c

Enter the terminal and switch to the corresponding directory

Execute make

After compiling, you will get hello.ko//hello.ko is the compiled module

Execute sudo insmod hello.ko //Load module

Execute lsmod to view the currently loaded module, if hello.ko already Carrier, then executing the lsmod after the output of the function can be seen already loaded good hello.ko

can be performed dmesg view the printk (), then you can see the printed available for purchase .. This information is actually stored in the /var/log/syslog file. You can also see the output information Welcom. .

After the test is completed, execute the sudo rmmod hello uninstall module.

The above is the method of using printk() in Linux. This article is just an example to explain the usage of the printk function. You can find out more about printk() usage online.

Copyright © Windows knowledge All Rights Reserved