基本概念
相关系统调用
Open 以及文件的打开标志
原子操作
文件共享
dup 和 dup2
fcntl 和 ioctl
容易混淆的概念
===============================================================================
#include <sys/types.h>
#include <sys/stat.h>
#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);
-------------------------------------------------------------------------------
#include <unistd.h>
int close(int fd);
===============================================================================
===============================================================================
#include <sys/types.h>
#include <unistd.h>
off_t lseek(int fildes, off_t offset, int whence);
===============================================================================
清单 5-1: 建立有洞的文件 (hole.c)
===============================================================================
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
char buf1 [] = "abcdefghij";
char buf2 [] = "ABCDEFGHIJ";
void err_sys (const char* info)
{
perror (info);
exit (1);
}
int main (void)
{
int fd;
if ( (fd = creat ("file.hole", 0644)) < 0)
err_sys ("create error");
if (write (fd, buf1, 10) != 10)
err_sys ("buf1 write error");
if (lseek (fd, 40960, SEEK_SET) == -1)
err_sys ("lseek error");
if (write (fd, buf2, 10) != 10)
err_sys ("buf2 write error");
exit (0);
}
===============================================================================
===============================================================================
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);
ssize_t write(int fd, const void *buf, size_t count);
===============================================================================
===============================================================================
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname, int flags, ... /*, mode_t mode*/);
===============================================================================
===============================================================================
O_CREAT
O_EXCL
O_NOCTTY
O_TRUNC
O_APPEND
O_NONBLOCK/O_NDELAY
O_SYNC
===============================================================================
===============================================================================
S_IRWXU 00700
S_IRUSR (S_IREAD) 00400
S_IWUSR (S_IWRITE) 00200
S_IXUSR (S_IEXEC) 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
===============================================================================

图 5-6 进程的文件信息
===============================================================================
#include <unistd.h>
int dup(int oldfd);
int dup2(int oldfd, int newfd);
===============================================================================
===============================================================================
#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);
------------------------------------------------------------------------------
#include <sys/ioctl.h>
int ioctl(int d, int request, ...)
===============================================================================
===============================================================================
ioctl 的适用类型 常量名称 还要包含的头文件
磁盘卷标 DIOxxx <disklabel.h>
文件 I/O FIOxxx <ioctl.h>
磁带 I/O MTIOxxx <mtio.h>
套接字 I/O SIOxxx <ioctl.h>
终端 I/O TIOxxx <termios.h>
===============================================================================
![]()