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) */    ...};

 

原创粉丝点击