The difference between PRINTK and printf

Source: Internet
Author: User
Tags syslog

The kernel uses PRINTK () to print! The application layer uses printf () to print!

&&& most commonly used C library functions have been implemented in the Linux kernel. Of all the functions that are not implemented, the most famous is the printf () function. Although the kernel code cannot call the printf () function, it can call the PRINTK () function. The PRINTK () function is responsible for copying the formatted string to the kernel log buffer so that the Syslog program can read the buffer to obtain kernel information. The use of PRINTK () is much like printf (): & PRINTK ("Hello world! A string:%s and an integer:%d\n ", A_string,an_integer); A notable difference between & PRINTK () and printf () is that PRINTK () allows you to set a priority by specifying a flag. The syslog will determine where to display this system information based on this priority flag. Here's an example of using this priority flag: & PRINTK (Kern_err "This was an error!\n");

The &&&&& PRINTK () function is directly used to write function tty_write () to the terminal. The printf () function is written to the standard output device by calling the write () system call function. Therefore, in the user state (such as process 0) is not able to directly use the PRINTK () function, and in the kernel state because he is a privileged level, so no system calls to change the privilege level, so can directly use the PRINTK () function.

printf is used when using standard C library functions, and the kernel cannot use standard C library functions, so even the most common printf cannot be used.

For example, the string parameter of the PRINTK function uses Kern_alert, which actually expands to a string: "<1>", and this part of the information is not output to the terminal. In fact, this part is the log level of the kernel information, and only the information that exceeds the current log level is output to the terminal. The log level of the current kernel can be seen in the/proc/sys/kernel/printk file. This file contains four integers, the first two of which are the current log level of the console and the default log level. We use a higher log level in the parameters of PRINTK to ensure that the information is output. In the <linux/kernel.h> header file, a total of 8 levels (0-7) of output are defined, from high to low, respectively, by the following constants:

Kern_emerg: Highest level, generally only used to print crash information

Kern_alert: Information that needs to be processed immediately

Kern_crit: Critical information that is typically used to display serious hardware and software errors

Kern_err: Used to display hardware errors

Kern_warning: Displays a warning message that does not cause a fatal error

Kern_notice: Show the information that needs attention

Kern_info: Displays general information, such as the list of hardware found by the driver

Kern_debug: Used to display debug information

One difference between PRINTK and printf is that the PRINTK is "line-driven", meaning that only the data that is received by a newline character is actually output to the terminal, otherwise there will be no information output. Another noteworthy problem is that when we debug embedded devices, often from the serial port to obtain display information, if we use printk too often, the transmission speed of the serial port will become a bottleneck, which will cause the system performance degradation or even stop responding.

Via:http://blog.chinaunix.net/uid-25749806-id-461471.html

The difference between PRINTK and printf

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.