gpt4 book ai didi

c - 读写函数中的系统调用

转载 作者:太空宇宙 更新时间:2023-11-04 07:51:53 25 4
gpt4 key购买 nike

我看了课本(Unix环境高级程序设计)

The functions described in this chapter are often referred to as unbuffered I/O, in contrast to the standard I/O routines, which we describe in Chapter 5. The term unbuffered means that each read or write invokes a system call in the kernel. These unbuffered I/O functions are not part of ISO C, but are part of POSIX.1 and the Single UNIX Specification.

我对术语无缓冲感到困惑,意思是每次读取或写入都会在内核中调用系统调用。

读写函数是

ssize_t read(int fd, void *buf, size_t nbytes);
ssize_t write(int fd, const void *buf, size_t nbytes);

我没发现他们的参数有什么特别之处,哪个参数是系统调用?

最佳答案

系统调用是昂贵的,因此标准 IO 库尽可能推迟它们,例如通过内部缓冲输出。当它不能再缓冲时,它最终会调用系统调用。

此缓冲区通常与 FILE 指针相关联,因此经验法则是采用 FILE * 的函数进行缓冲,而采用原始 的函数>int 文件描述符进行系统调用。这只是一个约定,仅此而已。

Posix 命名法试图在标准 IO 例程前加上 f 前缀,例如 fwrite,而原始系统调用是 write(相同fopenopen 等)。参数没有特别之处,在名字里。

关于c - 读写函数中的系统调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53273561/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com