导航

FILE I/O -3.1-3.2-

Posted on 2008-09-10 12:01  ·小新·  阅读(361)  评论(0编辑  收藏  举报

3.1 Introduction

                  介绍

           We'll start our discussion of the UNIX System by describing the functions available for file I/Oopen a file, read a file, write a file, and so on.Most file I/O on a UNIX system can be performed using only five functions: open, read, write, lseek, and close.

           我们开始讨论关于UNIX系统可用的文件I/O函数,读文件,写文件等等。大多数UNIX系统用到的文件I/O只有5个函数:open,read,write,lseek,和close.

           We then examine the effect of various buffer sizes on the read and write functions.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.

           然后,我们说明一下不同缓存尺寸对 read 和 write 函数的影响. 本章所说明的函数往往被称为无缓存I/O(与将在第五章说明的标准I/O对照).

           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.

           术语无缓存意味着每次read 和 write 都要调用内核的一次系统调用.这些无缓存I/O函数不是标准C的一部分,但是它属于POSIX.1的一部分,并且符合UNIX规范.

           Whenever we describe the sharing of resources among multiple processes, the concept of an atomic operation becomes important. We examine this concept with regard to file I/O and the arguments to the open function.

           当我们叙述多进程间资源共享,原子操作的概念变得很重要.我们将通过文件I/O和传递给open函数的参数来说明这个概念.

          This leads to a discussion of how files are shared among multiple processes and the kernel data structures involved. After describing these features, we describe the dup, fcntl, sync, fsync, and ioctl functions.

            进一步讨论多进程间如何共享文件以及涉及到的内核数据结构.在这些功能描述之后,我们描述一下dup,fcntl,sync,fsync,和ioctl函数.

 

3.2 File Descriptors

                文件描述符

 

           To the kernel, all open files are referred to by file descriptors. A file descriptor is a non-negative integer. When we open an existing file or create a new file, the kernel returns a file descriptor to the process.

            对于内核,所有已打开的文件都是由文件描述符来引用.文件描述符是一个非负整数.当我们打开一个现存文件或创建一个新文件,内核返回一个文件描述符给进程.

           When we want to read or write a file, we identify the file with the file descriptor that was returned by open or creat as an argument to either read or write.By convention, UNIX System shells associate file descriptor 0 with the standard input of a process, file descriptor 1 with the standard output, and file descriptor 2 with the standard error. This convention is used by the shells and many applications; it is not a feature of the UNIX kernel.Nevertheless, many applications would break if these associations weren't followed.

           当我们想读或写一个文件,我们用open 或者 creat返回一个文件的文件描述符确定一个文件,作为read或者write的参数.按照惯例,UNIX SHELL将文件描述符 0 作为标准输入, 文件描述符 1作为标准输出, 2做标准出错.这个惯例被SHELL和很多应用程序使用,但它不是UNIX内核的功能.如果这些管理不允许使用,很多应用程序将无法工作.

            The magic numbers 0, 1, and 2 should be replaced in POSIX-compliant applications with the symbolic constants STDIN_FILENO, STDOUT_FILENO, and STDERR_FILENO. These constants are defined in the <unistd.h> header.File descriptors range from 0 through OPEN_MAX. (Recall Figure 2.10.) Early historical implementations of the UNIX System had an upper limit of 19, allowing a maximum of 20 open files per process, but many systems increased this limit to 63.With FreeBSD 5.2.1, Mac OS X 10.3, and Solaris 9, the limit is essentially infinite, bounded by the amount of memory on the system, the size of an integer, and any hard and soft limits configured by the system administrator. Linux 2.4.22 places a hard limit of 1,048,576 on the number of file descriptors per process.

            数0,1,2(或者叫幻数?或者叫虚数?) 在POSIX 应用程序标准中被符号常数STDIN_FILENO,STDOUT_FILENO,和STDERR_FILENO所替代.这些常数定义 在<unistd.h>头文件中.文件描述符的范围从0到OPEN_MAX.早期的UNIX系统上限为19,也就是每个进程最多允许打开20 个文件,但是现在很多系统将他提高到63个.在FreeBSD 5.2.1,Mac OS X10.3和Solaris 9中这个限制基本上是无限的,而是由系统内存总额,整型大小,和系统管理员对软件和硬件的限制配置所定.Linux 2.4.22对一个进程能打开的文件数进行了硬件限制,最大为1048576个.