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

Difference between Printk() and Printf() in Linux

我是 Linux OS 的新用户,目前正在学习如何使用它。我正在研究 OS 调试 和相关主题,作为基本的调试工具,建议打印出有关系统状态的某些信息。

这个任务可以通过提到的函数printf()printk()来完成,并且发现"k"代表内核,是某种"primitive" 形式的打印函数。另外 printf() 是由 C 库而不是内核库给出的(不完全确定其含义)。

我想知道除了明显的优势之外,使用它们中的任何一个是否还有其他优势(即在引导的早期阶段使用 printk(),因为 printf() 尚未可用).两者同时使用是否可以获得相同的信息?

printk() 是一个内核级函数,它能够打印出不同的 loglevels,如 <linux/kernel.h>.

中所定义

printf() 将始终打印到文件描述符 - STD_OUT

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. The kernel displays all messages with a loglevel below a specified value on the console.

More Information Here

printf()C Standard Library

中的函数

如果您还没有访问 C 库的权限,您想要使用 printk() 是正确的。 printk() 为您提供 printf() 其他方式无法提供的功能。

可以找到两者很好的比较here

编辑笔记 - replaced dead link 静态存档版本