scanf() 在操作系统中如何工作?

发布于 2024-07-30 09:07:13 字数 99 浏览 4 评论 0原文

我一直想知道 scanf()/printf() 在硬件和操作系统级别实际上是如何工作的。 数据在哪里流动以及操作系统在这些时间里到底在做什么? 操作系统进行哪些调用? 等等...

I've been wondering how scanf()/printf() actually works in the hardware and OS levels. Where does the data flow and what exactly is the OS doing around these times? What calls does the OS make? And so on...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我们的影子 2024-08-06 09:07:13

scanf() 和 printf() 是 libc(C 标准库)中的函数,它们分别调用 read() 和 write() 操作系统系统调用,分别与文件描述符 stdin 和 stdout 通信(fscanf 和 fprintf 允许您指定您要读取/写入的文件流)。

对 read() 和 write() (以及所有系统调用)的调用会导致从用户级应用程序“上下文切换”到内核模式,这意味着它可以执行特权操作,例如直接与硬件对话。 根据您启动应用程序的方式,“stdin”和“stdout”文件描述符可能绑定到控制台设备(例如 tty0)或某种虚拟控制台设备(例如 xterm 公开的设备)。 read() 和 write() 安全地将数据复制到称为“uio”的内核缓冲区或从该缓冲区复制数据。

scanf 和 printf 的格式字符串转换部分不会在内核模式下发生,而是在普通用户模式下(在“libc”内)发生,系统调用的一般经验法则是尽可能不频繁地切换到内核模式,既为了避免上下文切换的性能开销,并且为了安全(您需要非常小心内核模式中发生的任何事情!内核模式中的代码越少意味着操作系统中的错误/安全漏洞越少)。

顺便说一句..所有这些都是从unix角度编写的,我不知道MS Windows是如何工作的。

scanf() and printf() are functions in libc (the C standard library), and they call the read() and write() operating system syscalls respectively, talking to the file descriptors stdin and stdout respectively (fscanf and fprintf allow you to specify the file stream you want to read/write from).

Calls to read() and write() (and all syscalls) result in a 'context switch' out of your user-level application into kernel mode, which means it can perform privileged operations, such as talking directly to hardware. Depending on how you started the application, the 'stdin' and 'stdout' file descriptors are probably bound to a console device (such as tty0), or some sort of virtual console device (like that exposed by an xterm). read() and write() safely copy the data to/from a kernel buffer called a 'uio'.

The format-string conversion part of scanf and printf does not occur in kernel mode, but just in ordinary user mode (inside 'libc'), the general rule of thumb with syscalls is you switch to kernel mode as infrequently as possible, both to avoid the performance overhead of context switching, and for security (you need to be very careful about anything that happens in kernel mode! less code in kernel mode means less bugs/security holes in the operating system).

btw.. all of this was written from a unix perspective, I don't know how MS Windows works.

以可爱出名 2024-08-06 09:07:13

在我的操作系统上,我使用的 scanf 和 printf 基于函数 getch() ant putch()。

On my OS I am working with scanf and printf are based on functions getch() ant putch().

寄居者 2024-08-06 09:07:13

我认为操作系统只提供两个流,一个用于输入,另一个用于输出,这些流抽象了输出数据的呈现方式或输入数据的来源。

那么什么是 scanf & printf 所做的只是从任一流中添加字节(或消耗字节)。

I think the OS just provides two streams, one for input and the other for output, the streams abstract away how the output data gets presented or where the input data comes from.

so what scanf & printf are doing are just adding bytes (or consuming bytes) from either streams.

简美 2024-08-06 09:07:13

scanf 、 printf 等内部所有这些类型的函数都不能直接用 c/c++ 语言编写。 内部都是用汇编语言编写的,通过使用关键字“asm”,任何使用关键字“asm”编写的东西都会直接引入到目标文件中,与编译无关(不是即使在编译后也发生了变化),并且在汇编语言中,我们有预定义的代码可以实现所有这些功能……所以简而言之,SCANF PRINTF 等都是在内部用汇编语言编写的。 您可以使用关键字“ASM”设计自己的输入函数。

scanf , printf etc internally all these types of functions can't be directly written in c/c++ language. internally they all are written in assembly language by the use of keword "asm", any thing written with keyword "asm" are directly introduced to object file irrespective of compilation (not changed even after compilation), and in assembly language we have got predefined codes which can implement all these functions ...... so in short SCANF PRINTF etc ALL ARE WRITTEN IN ASSEMBLY LANGUAGE INTERNALLY. YOU CAN DESIGN YOUR OWN INPUT FUNCTION USING KEYWORD "ASM".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文