Linux file I/O

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值