open close
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
1.open
#include
#include
#include
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
返回值:成功返回新分配的文件描述符,出错返回-1并设置errno
pathname 参数是要打开或创建的文件名,和fopen一样,pathname 既可以是相对路径也可以是绝对路径。
flags参数有一系列常数值可供选择,可以同时选择多个常数用按位或运算符连接起来,
所以这些常数的宏定义都以O_开头,表示or 。
以下三个三先一:
O_RDONLY 只读打开
O_WRONLY 只写打开
O_RDWR 可读可写打开
以下可多选(以或的形式)
O_APPEND
表示追加。如果文件已有内容,这次打开文件所写的数据附加到文件的末尾而不覆盖原来的内容。
O_CREAT
若此文件不存在则创建它。使用此选项时需要提供第三个参数mode,表示该文件的访问权限。
O_EXCL
如果同时指定了O_CREAT,并且文件已存在,则出错返回。
O_TRUNC
如果文件已存在,并且以只写或可读可写方式打开,则将其长度截断(Truncate )为0字节。
O_NONBLOCK
对于设备文件,以O_NONBLOCK方式打开可以做非阻塞I/O (Nonblock I/O ),非阻塞I/O
函数与C 标准I/O 库的fopen函数有些细微的区别:
1 以可写的方式fopen一个文件时,如果文件不存在会自动创建,而open一个文件时必须明
确指定O_CREAT才会创建文件,否则文件不存在就出错返回。
2 以w 或w+方式fopen一个文件时,如果文件已存在就截断为0 字节,而open一个文件时必须
明确指定O_TRUNC才会截断文件,否则直接在原来的数据上改写。
第三个参数mode指定文件权限,可以用八进制数表示,比如0644表示-rw-r--r--
实验
1.umask
0022
用touch命令创建一个文件时,创建权限是0666,而touch进程继承了Shell进程的umask掩码,
所以最终的文件权限是0666&~022=0644 。
2.touch aaa.c
ll aaa.c
-rw-r--r-- 1 root root 0 01-30 14:18 aaa.c
同样道理,用gcc 编译生成一个可执行文件时,创建权限是0777,而最终的文件权限
是0777&~022=0755 。
gcc aaa.c
ll a.out
-rwxr-xr-x 1 root root 4943 01-30 14:20 a.out
3.umask 0
再重复上述实验
==========================================================
Given a pathname for a file, open() returns a file
descriptor, a small, non-negative integer for use in
subsequent system calls (read(2), write(2), lseek(2),
fcntl(2), etc.). The file descriptor returned by a suc-
cessful call will be the lowest-numbered file descriptor
not currently open for the process.
The parameter flags must include one of the following
access modes: O_RDONLY, O_WRONLY, or O_RDWR. These
request opening
the file read-only, write-only, or
read/write, respectively.
In addition, zero or more file creation flags and file
status flags can be bitwise-or’d in flags. The file
creation flags are O_CREAT, O_EXCL, O_NOCTTY, and
O_TRUNC. The file status flags are all of the remaining
flags listed below. The distinction between these two
groups of flags is that the file status flags can be
retrieved and (in some cases) modified using fcntl(2).
The full list of file creation flags and file status
flags is as follows:
O_APPEND
The file is opened in append mode. Before each
write(), the file offset is positioned at the end
of the file, as if with lseek(). O_APPEND may
lead to corrupted files on NFS file systems if
more than one process appends data to a file at
once. This is because NFS does not support
appending to a file, so the client kernel has to
simulate it, which can’t be done without a race
condition.
O_CREAT
If the file does not exist it will be created.
The owner (user ID) of the file is set to the
effective user ID of the process. The group own-
ership (group ID) is set either to the effective
group ID of the process or to the group ID of the
parent directory (depending on filesystem type
and mount options, and the mode of the parent
directory, see, e.g., the mount options bsdgroups
and sysvgroups of the ext2 filesystem, as
described in mount(8)).
O_EXCL
When used with O_CREAT, if the file already
exists it is an error and the open() will fail.
In this context, a symbolic link exists, regard-
less of where it points to. O_EXCL is broken on
NFS file systems; programs which rely on it for
performing locking tasks will contain a race con-
dition. The solution for performing atomic file
locking using a lockfile is to create a unique
file on the same file system (e.g., incorporating
hostname and pid), use link(2) to make a link to
the lockfile. If link() returns 0, the lock is
successful. Otherwise, use stat(2) on the unique
file to check if its link count has increased to
2, in which case the lock is also successful.
O_NONBLOCK or O_NDELAY
When possible, the file is opened in non-blocking
mode. Neither the open() nor any subsequent oper-
ations on the file descriptor which is returned
will cause the calling process to wait. For the
handling of FIFOs (named pipes), see also
fifo(7). For a discussion of the effect of
O_NONBLOCK in conjunction with mandatory file
locks and with file leases, see fcntl(2).
O_TRUNC
If the file already exists and is a regular file
and the open mode allows writing (i.e., is O_RDWR
or O_WRONLY) it will be truncated to length 0.
If the file is a FIFO or terminal device file,
the O_TRUNC flag is ignored. Otherwise the effect
of O_TRUNC is unspecified.
The argument mode specifies the permissions to use in
case a new file is created. It is modified by the pro-
cess’s umask in the usual way: the permissions of the
created file are (mode & ~umask). Note that this mode
only applies to future accesses of the newly created
file; the open() call that creates a read-only file may
well return a read/write file descriptor.
RETURN VALUE
open() and creat() return the new file descriptor, or -1
if an error occurred (in which case, errno is set appro-
priately).
ERRORS
EACCES
The requested access to the file is not allowed,
or search permission is denied for one of the
directories in the path prefix of pathname, or
the file did not exist yet and write access to
the parent directory is not allowed. (See also
path_resolution(2).)
EEXIST
pathname already exists and O_CREAT and O_EXCL
were used.
EFAULT
pathname points outside your accessible address space.
creat() is equivalent to open() with flags equal to
O_CREAT|O_WRONLY|O_TRUNC.
creat("aa.c",0666); open("aa.c",O_CREAT|O_WRONLY|O_TRUNC,0666);
2.close
#include
int close(int fd);
返回值:成功返回0 ,出错返回-1并设置errno
参数fd是要关闭的文件描述符。需要说明的是,当一个进程终止时,内核对该进程所有尚未关
闭的文件描述符调用close关闭,所以即使用户程序不调用close,在终止时内核也会自动关闭
它打开的所有文件。但是对于一个长年累月运行的程序(比如网络服务器),打开的文件描述
符一定要记得关闭,否则随着打开的文件越来越多,会占用大量文件描述符和系统资源。
====================================================================================
close()
closes a file descriptor, so that it no longer
refers to any file and may be reused. Any record locks
(see fcntl(2)) held on the file it was associated with,
and owned by the process, are removed (regardless of the
file descriptor that was used to obtain the lock).
3作业
1.打开文件/home/akae.txt用于写操作,以追加方式打开.
int fd;
fd = open("/home/akae.txt", O_WRONLY | O_APPEND);
if (fd == -1)
{
perror("open");
exit(1);
}
2.打开文件/home/akae.txt用于写操作,如果该文件不存在则创建它,创建权限为0666.
fd = open("/home/akae.txt", O_WRONLY | O_CREAT,0666);
if (fd == -1)
{
perror("open");
exit(1);
}
3.打开文件/home/akae.txt用于写操作,如果该文件已存在则截断为0 字节,如果该文件不
存在则创建它,创建权限为0666.
fd = open("/home/akae.txt", O_WRONLY|O_CREAT|O_TRUNC,0666);
4.打开文件/home/akae.txt用于写操作,如果该文件已存在则报错退出,如果
该文件不存在
则创建它,创建权限为0666.
fd = open("/home/akae.txt", O_WRONLY|O_CREAT|O_EXCL|O_TRUNC,0666);