Function in C programming
printk is a C function from the Linux kernel interface that prints messages to the kernel log. It accepts a string parameter called the format string, which specifies a method for rendering an arbitrary number of varied data type parameter(s) into a string. The string is then printed to the kernel log.
It provides a codice_1 abstraction and its parsing of the format string and arguments behave similarly to codice_2. It acts as a debugging tool for kernel programmers who need this function for logging messages from the kernel.
The codice_3 function prototype is:
int printk(const char *fmt, ...);
C standard library and its codice_2 function are unavailable in kernel mode, hence the need for codice_3.
Differences from codice_2.
The function codice_3 is based on codice_2, but cannot always be used in the same way that codice_2 is used.
Log levels.
codice_3 allows a caller to specify the type and importance of the message being sent. This specifier is called the log level.
The log level specifies the type of message being sent to the kernel message log. The log level is specified by prepending (using C's string literal concatenation) a string describing the log level to the start of the message to be produced. For example, a message could be produced at the codice_11 using the following:
printk(KERN_INFO "Message: %s\n", arg);
The string specifying the log level consists of the ASCII start of the header character followed by a digit describing the log level or the character 'c' to indicate the message is a continuation of the previous message. The following log levels, along with their interpretations, are given below.
When a log level is not specified, the default log level is codice_12, unless a different default has been set in the kernel itself, such as with the codice_13 boot argument.
Log levels are defined in codice_14. Which log levels are printed is configured using the sysctl file codice_15.
Pointer formats.
The codice_16 format specifier (used for printing pointers in codice_2) is extended to add additional formatting modes, for example, requesting to print a codice_18 using codice_19 would print an IPv4/v6 address and port in a human-friendly format (e.g. "1.2.3.4:12345" or "[1:2:3:4:5:6:7:8]:12345").
No floating point support.
While codice_2 supports output of floating point numbers, codice_3 does not, since the Linux kernel does not use floating point numbers within the kernel.
Description.
The function tries to lock the semaphore controlling access to the system console. If it succeeds, the output is logged and the console drivers are called. If it is not possible to acquire the semaphore the output is placed into the log buffer, and the current holder of the console semaphore will notice the new output when they release the console semaphore and will send the buffered output to the console before releasing the semaphore.
One effect of this deferred printing is that code which calls codice_3 and then changes the log levels to be printed may break. This is because the log level to be printed is inspected when the actual printing occurs.
The function codice_3 can be called from anywhere in the kernel except during the very early stages of the kernel boot process; when the system console is not initialised. The alternative function codice_24 is implemented on some architectures and is used identically to codice_3 during the early stages of the boot process.
References.
 ;templatestyles src="Reflist/styles.css" / ;