write() 和 printf() 的区别 [英] Difference between write() and printf()

查看:51
本文介绍了write() 和 printf() 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我在研究操作系统..我只是想知道:

Recently I am studying operating system..I just wanna know:

系统调用(如 write())和标准库函数(如 printf())有什么区别?

What’s the difference between a system call (like write()) and a standard library function (like printf())?

推荐答案

系统调用是对不属于应用程序但位于内核内部的函数的调用.内核是一个软件层,它为您提供一些基本功能来为您抽象硬件.粗略地说,内核是将硬件变成软件的东西.

A system call is a call to a function that is not part of the application but is inside the kernel. The kernel is a software layer that provides you some basic functionalities to abstract the hardware to you. Roughly, the kernel is something that turns your hardware into software.

您最终总是使用 write() 在外围设备上写入任何内容,无论您在哪种设备上写入.write() 旨在写入字节序列,仅此而已.但是由于 write() 被认为太基础了(你可能想写一个以十为基础的整数,或者一个科学计数法的浮点数等),不同类型的编程为你提供了不同的库环境让您轻松.

You always ultimately use write() to write anything on a peripheral whatever is the kind of device you write on. write() is designed to only write a sequence of bytes, that's all and nothing more. But as write() is considered too basic (you may want to write an integer in ten basis, or a float number in scientific notation, etc), different libraries are provided to you by different kind of programming environments to ease you.

例如,C 编程语言为您提供了 printf(),可让您以多种不同的格式编写数据.因此,您可以将 printf() 理解为将数据转换为格式化字节序列并调用 write() 将这些字节写入输出的函数.但是 C++ 给了你 cout;Java System.out.println 等.这些函数中的每一个都以调用 write() 结束(至少在 POSIX 系统上).

For example, the C programming langage gives you printf() that lets you write data in many different formats. So, you can understand printf() as a function that convert your data into a formatted sequence of bytes and that calls write() to write those bytes onto the output. But C++ gives you cout; Java System.out.println, etc. Each of these functions ends to a call to write() (at least on POSIX systems).

需要知道的一件事(重要)是这样的系统调用代价高昂!这不是一个简单的函数调用,因为你需要调用你自己的代码之外的东西,系统必须确保你没有尝试做讨厌的事情等.所以在更高的类似打印的函数中很常见缓冲是内置的;这样 write 并不总是被调用,但您的数据会保存在某个隐藏结构中,并且仅在真正需要或必要时才写入(缓冲区已满或您确实想查看打印结果).

One thing to know (important) is that such a system call is costly! It is not a simple function call because you need to call something that is outside of your own code and the system must ensure that you are not trying to do nasty things, etc. So it is very common in higher print-like function that some buffering is built-in; such that write is not always called, but your data are kept into some hidden structure and written only when it is really needed or necessary (buffer is full or you really want to see the result of your print).

这正是您管理资金时会发生的情况.如果很多人给你每人5块钱,你不会每人都去银行存钱!你把它们放在你的钱包里(这是印刷品)直到它装满或者你不想再保留它们了.然后你去银行存了一大笔存款(这是).而且您知道将 5 美元存入您的钱包比去银行存入存款要快得多.银行是内核/操作系统.

This is exactly what happens when you manage your money. If many people gives you 5 bucks each, you won't go deposit each to the bank! You keep them on your wallet (this is the print) up to the point it is full or you don't want to keep them anymore. Then you go to the bank and make a big deposit (this is the write). And you know that putting 5 bucks to your wallet is much much faster than going to the bank and make the deposit. The bank is the kernel/OS.

这篇关于write() 和 printf() 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆