I am a new user to Linux OS and am currently learning my way in it. I was studying about OS Debugging and related topics and as a basic debugging tool it is recomended to print out certain information regarding the state of the System.
This task can be accomplished by the mentioned functions printf()
and also printk()
, and have found that the "k" stands for kernel, and is some sort of "primitive" form of print function. Also that printf()
is given by the C library rather than the kernel one (not completely sure of the meaning of this).
I was wondering if there is other advantage for using either of them other than the obvious one (which is to use printk()
in earlier stages of the booting given that printf()
is not yet avialable). Can one obtain the same information when using the two of them?
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.
printf() is one of the APIs or interfaces exposed to user space to call functions from C library. printf() actually uses write() system call. The write() system call is actually responsible for sending data to the output.
All printk() messages are printed to the kernel log buffer, which is a ring buffer exported to userspace through /dev/kmsg.
printk () is a kernel level function, which has the ability to print out to different loglevels as defined in <linux/kernel.h>. The major difference between printk () and printf () is the capability of the former to specify a loglevel. The kernel uses the loglevel to decide whether to print the message to the console.
One of the most well-known Linux kernel functions is printk (). It’s the default tool for printing messages and the most basic method of tracing and debugging. The printf () method prints the parameters to the stdout stream, which are written in double inverted quotations.
One of the most well-known Linux kernel functions is printk (). It’s the default tool for printing messages and the most basic method of tracing and debugging.
Echo command adds " " characters to the end of the string. That is, when we print something with the echo command, it automatically pass to the new line. But printf does not do this. echo "string" equal to "string "and print "string" equal to "string". so the output of "wc -m" command is different.
printk()
is a kernel level function, which has the ability to print out to different loglevels as defined in <linux/kernel.h>
.
printf()
will always print to a file descriptor - STD_OUT
The major difference between
printk()
andprintf()
is the capability of the former to specify a loglevel. The kernel uses the loglevel to decide whether to print the message to the console. The kernel displays all messages with a loglevel below a specified value on the console.
More Information Here
printf()
is a function in the C Standard Library
You are correct that you would want to use printk()
if you did not yet have access to the C libraries yet. printk()
gives you printf()
functionality where you would not have it otherwise.
A good comparison of the two can be found here
Edit note - replaced dead link with static archive version
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With