Linux file I/O
来源:互联网 发布:手机合成音乐软件 编辑:程序博客网 时间:2024/04/29 19:55
#include <unistd.h>
int dup(int filedes) ; => fcntl (filedes, F_DUPFD, 0);
int dup2(int filedes, int filedes2) ; => close(filedes2) ; fcntl(filedes, F_DUPFD, filedes2);
(atomic operation) (2 operations)
Synopsis
#include <unistd.h>
int dup(int fildes);
int dup2(int fildes, int fildes2);
Description
The dup() and dup2() functions provide an alternative interface to the service provided by fcntl() using the F_DUPFD command. The call:
fid = dup(fildes);
shall be equivalent to:
fid = fcntl(fildes, F_DUPFD, 0);
The call:
fid = dup2(fildes, fildes2);
shall be equivalent to:
close(fildes2);fid = fcntl(fildes, F_DUPFD, fildes2);
except for the following:
- *
- If fildes2 is less than 0 or greater than or equal to {OPEN_MAX}, dup2() shall return -1 with errno set to [EBADF].
- *
- If fildes is a valid file descriptor and is equal to fildes2, dup2() shall return fildes2 without closing it.
- *
- If fildes is not a valid file descriptor, dup2() shall return -1 and shall not close fildes2.
- *
- The value returned shall be equal to the value of fildes2 upon successful completion, or -1 upon failure.
fcntl()
Name
fcntl - manipulate file descriptor
Synopsis
#include <unistd.h>#include <fcntl.h>int fcntl(int fd, int cmd);int fcntl(int fd, int cmd, long arg);int fcntl(int fd, int cmd, struct flock *lock);
Description
fcntl() performs one of the operations described below on the open file descriptor fd. The operation is determined by cmd.
Duplicating a file descriptor
- F_DUPFD
- Find the lowest numbered available file descriptor greater than or equal to arg and make it be a copy of fd. This is different from dup2(2) which uses exactly the descriptor specified.
On success, the new descriptor is returned.
See dup(2) for further details.
File descriptor flags
- The following commands manipulate the flags associated with a file descriptor. Currently, only one such flag is defined: FD_CLOEXEC, the close-on-exec flag. If the FD_CLOEXEC bit is 0, the file descriptor will remain open across an execve(2), otherwise it will be closed.
- F_GETFD
- Read the file descriptor flags.
- F_SETFD
- Set the file descriptor flags to the value specified by arg.
File status flags
- Each open file description has certain associated status flags, initialized by open(2) and possibly modified by fcntl(2). Duplicated file descriptors (made with dup(), fcntl(F_DUPFD), fork(), etc.) refer to the same open file description, and thus share the same file status flags.
The file status flags and their semantics are described in open(2).
- F_GETFL
- Read the file status flags.
- F_SETFL
- Set the file status flags to the value specified by arg. File access mode (O_RDONLY, O_WRONLY, O_RDWR) and file creation flags (i.e., O_CREAT, O_EXCL, O_NOCTTY, O_TRUNC) in arg are ignored. On Linux this command can only change the O_APPEND, O_ASYNC, O_DIRECT, O_NOATIME, and O_NONBLOCK flags.
Advisory locking
- F_GETLK, F_SETLK and F_SETLKW are used to acquire, release, and test for the existence of record locks (also known as file-segment or file-region locks). The third argument lock is a pointer to a structure that has at least the following fields (in unspecified order).
struct flock { ... short l_type; /* Type of lock: F_RDLCK, F_WRLCK, F_UNLCK */ short l_whence; /* How to interpret l_start: SEEK_SET, SEEK_CUR, SEEK_END */ off_t l_start; /* Starting offset for lock */ off_t l_len; /* Number of bytes to lock */ pid_t l_pid; /* PID of process blocking our lock (F_GETLK only) */ ...};
- linux file I/O
- Linux file I/O
- linux 学习笔记之 FILE I/O
- innodb_flush_method 与 Linux File I/O
- MySQL innodb_flush_method 与 File I/O (Linux)
- C++ File I/O
- FILE I/O
- Multithreaded File I/O
- FILE I/O
- Python File I/O
- I/O 1.0 File
- File I/O
- C++ File I/O
- I/O----File类
- Python File I/O
- Perl - File I/O
- 3-File I\O
- Python File I/O
- C++ 虚函数表解析
- 利用序列化将java对象深拷贝
- c# 泛型集合Dictionary
- 新型3G接入点:Femtocell的功能与应用
- 执行commit时oracle做哪些工作
- Linux file I/O
- 在Linux下面去压缩文件或者目录
- u-boot剪裁成功
- C#泛型详解
- 在Ubuntu下使用记录
- 图像识别_2010暑期实训有感【二】
- Oracle RowID
- doctype
- 网站变灰色代码方法大集合(站点哀悼代码之用)