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

查看:642
本文介绍了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()的调用结束。

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().

要知道的一件事(重要的)是这样的系统调用是昂贵的!它不是一个简单的函数调用,因为你需要调用你自己的代码之外的东西,系统必须确保你不要试图做令人讨厌的事情等等。所以它在更高的类似打印功能中是很常见的一些缓冲是内置的;这样写并不总是被调用,但是你的数据被保存在一些隐藏的结构中,并且只在真正需要或必要时写入(缓冲区已满或者你真的想看到你的打印结果)。

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 then on your wallet (this is the print) up to the point it is full or you don't want to kept 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天全站免登陆