简体   繁体   English

Linux 中 Printk() 和 Printf() 的区别

[英]Difference between Printk() and Printf() in Linux

I am a new user to Linux OS and am currently learning my way in it.我是 Linux 操作系统的新用户,目前正在学习我的方法。 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.这个任务可以通过提到的函数printf()printk() ,并且发现“k”代表内核,是某种“原始”形式的打印函数。 Also that printf() is given by the C library rather than the kernel one (not completely sure of the meaning of this).此外, printf()是由 C 库而不是内核库给出的(不完全确定其含义)。

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).我想知道除了明显的优势之外,使用它们中的任何一个是否还有其他优势printk()鉴于printf()尚不可用,因此在启动的早期阶段使用printk() )。 Can one obtain the same information when using the two of them?使用两者时是否可以获得相同的信息?

printk() is a kernel level function, which has the ability to print out to different loglevels as defined in <linux/kernel.h> . printk()是一个内核级函数,它能够打印到<linux/kernel.h>定义的不同日志级别

printf() will always print to a file descriptor - STD_OUT printf()将始终打印到文件描述符 - STD_OUT

The major difference between printk() and printf() is the capability of the former to specify a loglevel. printk()printf()之间的主要区别在于前者指定日志级别的能力。 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 printf()C 标准库中的一个函数

You are correct that you would want to use printk() if you did not yet have access to the C libraries yet.如果您还没有访问 C 库的权限,那么您想使用printk()是正确的。 printk() gives you printf() functionality where you would not have it otherwise. printk()为您提供printf()功能,否则您将无法使用它。

A good comparison of the two can be found here可以在这里找到两者的很好比较

Edit note - replaced dead link with static archive version编辑注释 - 用静态存档版本替换死链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM