apue(2)

 

第二章看不太懂 感觉没那么重要 略过

 

3.1. Introduction

Most file I/O on a UNIX system can be performed using only five functions: open, read, write, lseek, and close. We then examine the effect of various buffer sizes on the read and write functions.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.

 

3.2. File Descriptors

 

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.It is not a feature of the UNIX kernel. Nevertheless, many applications would break if these associations weren't followed.STDIN_FILENO, STDOUT_FILENO, and STDERR_FILENO. These constants are defined in the <unistd.h> header.File descriptors range from 0 through OPEN_MAX.

 

3.3. open Function


#include <fcntl.h>

int open(const char *pathname, int oflag, ... /* mode_t mode */ );


 The third argument is used only when a new file is being create. We'll show how to specify mode in Section 4.5

 

oflag: 

O_RDONLY as 0, O_WRONLY as 1, and O_RDWR as 2. Only one

O_APPEND O_CREAT O_EXCL(with O_CREAT) O_TRUNC(truncate its length to 0) O_NOCTTY(?) O_NONBLOCK(?)

O_RSYNC(single read) O_SYNC O_DSYNC(similiar write synchronized)

 

 

File status flag

Description

O_RDONLY

open for reading only

O_WRONLY

open for writing only

O_RDWR

open for reading and writing

O_APPEND

append on each write

O_NONBLOCK

nonblocking mode

O_SYNC

wait for writes to complete (data and attributes)

O_DSYNC

wait for writes to complete (data only)

O_RSYNC

synchronize reads and writes

O_FSYNC

wait for writes to complete (FreeBSD and Mac OS X only)

O_ASYNC

asynchronous I/O (FreeBSD and Mac OS X only)

 

Filename and Pathname Truncation ( when file name is longer than NAME_MAX)

3.4. creat Function

#include <fcntl.h> int creat(const char *pathname, mode_t mode);

This function is equivalent to

open (pathname, O_WRONLY | O_CREAT | O_TRUNC, mode);

 

We'll show how to specify mode in Section 4.5

 

3.5. close Function

An open file is closed by calling the close function.

#include <unistd.h>

int close(int filedes);
When a process terminates, all of its open files are closed automatically by the kernel. Many programs take advantage of this fact and don't explicitly close open files.

3.6. lseek Function

 

#include <unistd.h> off_t lseek(int filedes, off_t offset, int whence);
whence:
SEEK_SET SEEK_CUR SEEK_END
If the file descriptor refers to a pipe, FIFO, or socket, lseek sets errno to ESPIPE and returns 1.
The file's offset can be greater than the file's current size, in which case the next write to the file will extend the file. This is referred to as creating a hole in a file and is allowed. Any bytes in a file that have not been written are read back as 0.
File hole would consume less blocks but is the same in file size.

3.7. read Function

#include <unistd.h> ssize_t read(int filedes, void *buf, size_t nbytes);
Before a successful return, the offset is incremented by the number of bytes actually read.

3.8. write Function

#include <unistd.h>

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

3.10. File Sharing

The kernel uses three data structures to represent an open file, process table ,file table and v-node structure( Linux has no v-node. Instead, a generic i-node structure is used. Although the implementations differ, the v-node is conceptually the same as a generic i-node. Both point to an i-node structure specific to the file system.).
When two process share the same file,only the i-node table is the same.However, it is possible for more than one file descriptor entry to point to the same file table entry.(dup fork)
The lseek function modifies only the current file offset in the file table entry. No I/O takes place.

3.11. Atomic Operations

Appending to a File

The UNIX System provides an atomic way to do this operation if we set the O_APPEND flag when a file is opened. As we described in the previous section, this causes the kernel to position the file to its current end of file before each write. We no longer have to call lseek before each write.

注意,使用O_APPEND标志后,任何写操作只能在尾部,但lseek可以随意移动读取。

Creating a File
We also said that the check for the existence of the file and the creation of the file was performed as an atomic operation( O_CREAT and O_EXCL options for the open function).

3.12. dup and dup2 Functions

#include <unistd.h> int dup(int filedes); int dup2(int filedes, int filedes2); 
In dup2, if filedes2 is already open, it is first closed. If filedes equals filedes2, then dup2 returns filedes2 without closing it.
The new file descriptor that is returned as the value of the functions shares the same file table entry as the filedes argument.
As we describe in the next section, the close-on-exec file descriptor flag for the new descriptor is always cleared by the dup functions.

 

 dup(filedes); is equivalent to fcntl(filedes, F_DUPFD, 0);
Similarly, the call

dup2(filedes, filedes2); is equivalent to( notconsidering atomic) close(filedes2); fcntl(filedes, F_DUPFD, filedes2);

 

3.13. sync, fsync, and fdatasync Functions

 

Traditional implementations of the UNIX System have a buffer cache or page cache in the kernel through which most disk I/O passes. When we write data to a file, the data is normally copied by the kernel into one of its buffers and queued for writing to disk at some later time. This is called delayed write.

 

#include <unistd.h> int fsync(int filedes); int fdatasync(int filedes);
void sync(void);

The sync function simply queues all the modified block buffers for writing and returns; it does not wait for the disk writes to take place.

The function sync is normally called periodically (usually every 30 seconds) from a system daemon, often called update. This guarantees regular flushing of the kernel's block buffers. The command sync(1) also calls the sync function.

The function fsync refers only to a single file, specified by the file descriptor filedes, and waits for the disk writes to complete before returning. The intended use of fsync is for an application, such as a database, that needs to be sure that the modified blocks have been written to the disk

 

 

 

With fsync, the file's attributes are also updated synchronously.The aim of fdatasync() is to reduce disk activity for applications that do not require all metadata to be synchronized with the disk.

3.14. fcntl Function

The fcntl function can change the properties of a file that is already open.

#include <fcntl.h>

int fcntl(int filedes, int cmd, ... /* int arg */ );


 

The fcntl function is used for five different purposes.

Duplicate an existing descriptor ( cmd = F_DUPFD)
Get/set file descriptor flags ( cmd = F_GETFD or F_SETFD)
Get/set file status flags ( cmd = F_GETFL or F_SETFL)
Get/set asynchronous I/O ownership ( cmd = F_GETOWN or F_SETOWN)
Get/set record locks ( cmd = F_GETLK, F_SETLK, or F_SETLKW)

 

F_DUPFD

The new descriptor shares the same file table entry as filedes. (Refer to Figure 3.8.) But the new descriptor has its own set offile descriptor flags, and its FD_CLOEXEC file descriptor flag is cleared.

 

F_GETFD

Return the file descriptor flags for filedes as the value of the function. Currently, only one file descriptor flag is defined: the FD_CLOEXEC flag.

 

F_SETFD

Set the file descriptor flags for filedes.(FD_CLOXEC or 1) 

 

F_GETFL

Return the file status flags for filedes as the value of the function.

Therefore, we must first use the O_ACCMODE mask to obtain the access-mode bits and then compare the result against any of the three values.

 

 

F_SETFL

Set the file status flags to the value of the third argument (taken as an integer). The only flags that can be changed are O_APPEND, O_NONBLOCK, O_SYNC, O_DSYNC, O_RSYNC, O_FSYNC, and O_ASYNC.

 

 

 

F_GETOWN

Get the process ID or process group ID currently receiving the SIGIO and SIGURG signals. We describe these asynchronous I/O signals in Section 14.6.2.

 

F_SETOWN

Set the process ID or process group ID to receive the SIGIO and SIGURG signals. A positive arg specifies a process ID. A negative arg implies a process group ID equal to the absolute value of arg.

 

    cmd 2>> file 把 stderr 重定向到 file 文件中(追加);

    5<>temp.foo opens the file temp.foo for reading and writing on file descriptor 5.

     

    val |= flags; /* turn on flags */
    val &= ~flags; /* turn flags off */
    不同文件系统对于O_SYNC的处理不同,linux ext2基本山不处理O_SYNC, Mac OS 处理。

    3.15. ioctl Function

    #include <unistd.h> /* System V */ #include <sys/ioctl.h> /* BSD and Linux */ #include <stropts.h> /* XSI STREAMS */ int ioctl(int filedes, int request, ...); 
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值