BLP chapter3:file(system call)
来源:互联网 发布:windows 7恢复出厂设置 编辑:程序博客网 时间:2024/06/06 10:18
open:打开,或者创建一个文件或设备;
#include <sys/types.h> //定义了系统设置的数据类型.
#include <sys/stat.h> //定义了由stat()函数返回的数据类型
#include <fcntl.h> //定义了文件控制的选项
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
int creat(const char *pathname, mode_t mode);
creat相当于使用了O_CREAT | OWRONLY | O_TRUNC的open;
返回值:open如果成功则返回一个最小的未使用的文件描述符(非负整数)以供后续的系统调用使用,如果失败则返回-1,并设置全局变量errno表明具体的错误类型.
flags:参数必需包含以下访问模式的其中之一:O_RDONLY,O_WRONLY,O_RDWR;
flags可以额外包含以下文件创建模式和文件状态模式的0个或多个:
O_CREAT:如果必要则创建文件,使用mode_t 指定的访问权限;
O_EXCL:必需和O_CREAT一起使用,(否则行为是未定义的),确保open调用的原子性,即保证只有一个程序创建此文件,若文件已存在则open返回错误;pathname不允许是symbolic-link,open将失败.
O_TRUNC:清空文件内容;
O_NOCTTY:如果路径名指向终端设备,不要把这个设备用作本进程的控制终端.
O_APPEND:在文件末尾追加内容;当多个进程同时对NFS使用此标志的时候将导致文件损坏.
O_ASYNC:当输入输出就绪的时候产生一个信号(默认SIGIO,可由fcntl控制),只能用于terminals,pseudo-terninals,sockets,pipes和FIFO.(2.6)
O_CLOEXEC:当执行exec的时候关闭此文件描述符.常用于多线程的环境,避免竞争.(2.6.23)
O_DIRECT:尽量减少本文件的缓存.(2.4.10)
O_DIRECTORY:如果路径不是一个目录则open将失败.此标志是linux特有的,为了防止对FIFO和磁带设备调用opendir进行DOS.(2.1.126)
O_NOFOLLOW:如果pathname是symbolic-link,则open失败.
O_SYNC:同步IO,所有对此文件的write都会阻塞调用进程.
mode:指定文件创建时的权限,此设定将和shell umask的反相与决定最终文件的权限(mode & ~mask)
S_IRWXU:00700 S_IRUSR:00400 S_IWUSR:00200 S_IXUSR:00100
S_IRWXG:00070 S_IRGRP:00040 S_IWGRP:00020 S_IXGRP:00010
S_IRWXO:00007 S_IROTH:00004 S_IWOTH:00002 S_IXOTh:00001
lseek:重定位文件读写位置。
#include <unistd.h> //定义了系统的标准符号常量和类型
off_t lseek(int fd, off_t offset, int whence);
whence有如下几种:
SEEK_SET:偏移量设置为offset
SEEK_CUR:偏移量设置为当前位置加上offset
SEEK_END:偏移量设置为文件长度加上offset
lseek函数允许偏移量超过文件末尾(此时文件大小不会变化),如果以后在这个位置写如数据,中间的空间将用'/0'填补.
返回值:成功则返回从文件开始处到当前位置的偏移值,否则返回-1,并设置errno.
close:终止文件描述符和文件的对应关系
#include <unistd.h>
int close(int fd);
返回值:成功返回0,否值-1,并设置errno.
ioctl:提供了控制设备的行为和配置的接口
#include <unistd.h>
int ioctl(int fd, int cmd, ...);
将执行由cmd指定的功能,对不同的设备可以查询相应的man.
返回值:成功返回非负整数,否则返回-1,并设置errno.
write:
#include <unistd.h>
size_t write(int fd, const void *buf, size_t nbytes);
从buf中向指定的文件描述符写nbytes
返回值:返回值是实际写入的字节数,可以比指定的nbytes少,发生错误则返回-1,并设置全局变量errno指名具体的错误.
read:
#include <unistd.h>
int read(int fd, void *buf, size_t nbytes);
从fd指定的文件中读取nbytes并写入buf指定的缓冲区.
返回值:返回实际写入的字节,可以少于指定的nbytes.返回0则表示到达了文件末尾.错误则返回-1,并设置全局变量errno.
fstat,stat,lstat:
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
int fstat(int fd, struct stat *buf);
int stat(const char *path, struct stat *buf);
int lstat(const char *path, struct stat *buf);
fstat把fd对应文件的状态结构存放在buf指定的地方;
stat和lstat把path指定的文件状态结构存放在buf指定的地方.他们的不同是如果path指定的文件是一个symbolic-link则lstat返回链接本身的状态而stat则返回链接指向文件的状态.
文件状态结构如下
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* inode number */
mode_t st_mode; /* protection */
nlink_t st_nlink; /* number of hard links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
dev_t st_rdev; /* device ID (if special file) */
off_t st_size; /* total size, in bytes */
blksize_t st_blksize; /* blocksize for file system I/O */
blkcnt_t st_blocks; /* number of 512B blocks allocated */
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last status change */
};
以下定义的POSIX宏使用st_mode域检查文件类型:
S_ISREG(m) is it a regular file?
S_ISDIR(m) directory?
S_ISCHR(m) character device?
S_ISBLK(m) block device?
S_ISFIFO(m) FIFO (named pipe)?
S_ISLNK(m) symbolic link? (Not in POSIX.1-1996.)
S_ISSOCK(m) socket? (Not in POSIX.1-1996.)
The following flags are defined for the st_mode field:
S_IFMT 0170000 bit mask for the file type bit fields
S_IFSOCK 0140000 socket
S_IFLNK 0120000 symbolic link
S_IFREG 0100000 regular file
S_IFBLK 0060000 block device
S_IFDIR 0040000 directory
S_IFCHR 0020000 character device
S_IFIFO 0010000 FIFO
S_ISUID 0004000 set UID bit
S_ISGID 0002000 set-group-ID bit (see below)
S_ISVTX 0001000 sticky bit (see below)
返回值:成功返回0,错误返回-1,并设置errno.
dup,dup2:
#include <unistd.h>
int dup(int oldfd);
int dup2(int oldfd, int newfd);
dup复制oldfd为当前最小未使用的文件描述符;dup2复制oldfd到newfd.复制的两个文件描述符共享文件偏移和文件状态标志.但是他们不共享文件描述符标志,复制的文件描述符的close-on-exec(FD_CLOEXEC)标志将会被关闭.
返回值:成功返回新描述符,否则返回-1,并设置errno.
- BLP chapter3:file(system call)
- System Call
- system call
- chapter3
- chapter3
- BLP文件格式
- BLP文件格式
- File System Redirector and Sysnative - how to call 64 bit program from 32 bit application
- 探究C库中的FILE及system call中的文件描述符间的关系
- file system
- file system
- File system
- connect & Interrupted System Call
- Linux System Call Table
- process-system call-interrupt
- Interrupted system call
- System call 的分析
- 增加System Call
- 计算机要学习东西
- java日期处理通用类
- SQL Server2005整合服务: 性能优化技术
- Tcl 表示工具命令语言(Tool Command Language)
- 临时表
- BLP chapter3:file(system call)
- 网页打印之分页打印、隐藏网页元素
- 如何解决MSYS中Couldn’t reserve space for cygwin’s heap问题
- TOMCAT内存溢出
- 金融危机下最抢手的八大IT专业人才
- CodeBlocks下的C语言网络编程
- 关于创业的讨论(转)
- 第一单 计算机系统知识
- Hash算法的经典实现