Linux System Programming 学习笔记(二) 文件I/O
1.每个Linux进程都有一个最大打开文件数,默认情况下,最大值是1024
2.打开文件
int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, )
int fd = creat(filename, )
3.读文件
size_t readn(int fd, void* buf, size_t len)
{
size_t tmp = len;
ssize_t ret = ;
while (len != && (ret = read(fd, buf, len)) != ) {
if (ret == -) {
if (errno == EINTR) {
continue;
}
fprintf(stderr, "read error\n");
break;
}
len -= ret;
buf += ret;
}
return tmp - len;
}
4.写文件
size_t writen(int fd, void* buf, size_t len)
{
ssize_t ret = ;
size_t tmp = len;
while (len != && (ret = write(fd, buf, len)) != ) {
if (ret == -) {
if (errno == EINTR) {
continue;
}
fprintf(stderr, "write error\n");
break;
}
len -= ret;
buf += ret;
}
return tmp - len;
}
5.文件同步
int ret = fsync(fd);
open调用时 O_SYNC标志表示 文件必须同步
int fd = open(file, O_WRONLY | O_SYNC);
6.文件定位
7.截断文件
int ftruncate(int fd, off_t len);
将给定文件截断为给定长度,这里的给定长度是可以小于文件大小,也可以大于文件大小(会造成空洞)
8.多路I/O
Multiplexed I/O becomes the pivot point for the application,designed similarly to the following activity:
a. Multiplexed I/O : Tell me when any of these file descriptors becomes ready for I/O
b. Nothing ready? Sleep until one or more file descriptors are ready.
c. Woken up ! What is ready?
d. Handle all file descriptors ready for I/O, without bolocking
e. Go back to step a
9.select
int select(int nfds, fd_set* readfds, fd_set* writefds, fd_set* exceptfds, struct timeval* timeout);
FD_CLR(int fd, fd_set* set); // removes a fd from a given set
FD_ISSET(int fd, fd_set* set); // test whether a fd is part of a given set
FD_SET(int fd, fd_set* set); // adds a fd to a given set
FD_ZERO(int fd, fd_set* set); // removes all fds from specified set. shoule be called before every invocation of select()
因为fd_set是静态分配的,系统有一个文件描述符的最大打开数 FD_SETSIZE,在Linux中,该值为 1024
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h> #define TIMEOUT 5 /* select timeout in seconds */
#define BUFLEN 1024 /* read buffer in bytes */ int main(int argc, char* argv[])
{
struct timeval tv;
tv.tv_sec = TIMEOUT;
tv.tv_usec = ; /* wait on stdin for input */
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(STDIN_FILENO, &readfds); int ret = select(STDIN_FILENO + , &readfds, NULL, NULL, &tv);
if (ret == -) {
fprintf(stderr, "select error\n");
return ;
} else if (!ret) {
fprintf(stderr, "%d seconds elapsed.\n", TIMEOUT);
return ;
}
if (FD_ISSET(STDIN_FILENO, &readfds)) {
char buf[BUFLEN + ];
int len = read(STDIN_FILENO, buf, BUFLEN);
if (len == -) {
fprintf(stderr, "read error\n");
return ;
}
if (len != ) {
buf[BUFLEN] = '\0';
fprintf(stdout, "read:%s\n", buf);
}
return ;
} else {
fprintf(stderr, "This should not happen\n");
return ;
} }
10. poll
int poll(struct pollfd* fds, nfds_t nfds, int timeout);
This is a program that uses poll() to check whether a read from stdin and a write to stdout will block
#include <unistd.h>
#include <poll.h> #define TIMEOUT 5 int main(int argc, char* argv[])
{
struct pollfd fds[]; /* watch stdin for input */
fds[].fd = STDIN_FILENO;
fds[].events = POLLIN; /* watch stdout for alibity to write */
fds[].fd = STDOUT_FILENO;
fds[].events = POLLOUT; int ret = poll(fds, , TIMEOUT * );
if (ret == -) {
fprintf(stderr, "poll error\n");
return ;
} if (!ret) {
fprintf(stdout, "%d seconds elapsed.\n", TIMEOUT);
return ;
} if (fds[].revents & POLLIN) {
fprintf(stdout, "stdin is readable\n");
}
if (fds[].revents & POLLOUT) {
fprintf(stdout, "stdout is writable\n");
}
return ;
}
11.内核实现
Linux System Programming 学习笔记(二) 文件I/O的更多相关文章
- Linux System Programming 学习笔记(八) 文件和目录管理
1. 文件和元数据 每个文件都是通过inode引用,每个inode索引节点都具有文件系统中唯一的inode number 一个inode索引节点是存储在Linux文件系统的磁盘介质上的物理对象,也是L ...
- Linux System Programming 学习笔记(四) 高级I/O
1. Scatter/Gather I/O a single system call to read or write data between single data stream and mu ...
- Linux System Programming 学习笔记(一) 介绍
1. Linux系统编程的三大基石:系统调用.C语言库.C编译器 系统调用:内核向用户级程序提供服务的唯一接口.在i386中,用户级程序执行软件中断指令 INT n 之后切换至内核空间 用户程序通过寄 ...
- Linux System Programming 学习笔记(十一) 时间
1. 内核提供三种不同的方式来记录时间 Wall time (or real time):actual time and date in the real world Process time:the ...
- Linux System Programming 学习笔记(九) 内存管理
1. 进程地址空间 Linux中,进程并不是直接操作物理内存地址,而是每个进程关联一个虚拟地址空间 内存页是memory management unit (MMU) 可以管理的最小地址单元 机器的体系 ...
- Linux System Programming 学习笔记(七) 线程
1. Threading is the creation and management of multiple units of execution within a single process 二 ...
- Linux System Programming 学习笔记(六) 进程调度
1. 进程调度 the process scheduler is the component of a kernel that selects which process to run next. 进 ...
- Linux System Programming 学习笔记(五) 进程管理
1. 进程是unix系统中两个最重要的基础抽象之一(另一个是文件) A process is a running program A thread is the unit of activity in ...
- Linux System Programming 学习笔记(三) 标准缓冲I/O
1. partial block operations are inefficient. The operating system has to “fix up” your I/O by ensuri ...
随机推荐
- Eclipse+Tomcat搭建jsp服务器
首先,安装java sdk 环境,这里就不多说了,附上java sdk的下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk ...
- ubuntu 16.04 连接 wifi
我的电脑是win10+ubuntu16.04双系统.在ubuntu下无法连接wifi,一直用usb连接的手机流量,不太方便.现在来用安装无线驱动,顺便翻个墙. https://blog.csdn.ne ...
- [BZOJ] 1907: 树的路径覆盖
一个点必然被路径覆盖,根据是否为路径的端点分类 \(f[x][0]\)表示以\(x\)为根的子树,\(x\)不为端点的最小路径覆盖数 \(f[x][1]\)表示以\(x\)为根的子树,\(x\)为一条 ...
- day13-生成器
def generator(): print(1) yield 'a' rcp = generator() print(rcp.__next__()) 只要含有yield关键字的函数都是生成器函数.y ...
- MySQL迁移升级解决方案
任务背景 由于现有业务架构已不能满足当前业务需求,在保证数据完整的前提下,现需要将原有数据库迁移到另外一台单独的服务器上,在保证原有服务正常的情况下,将原有LAMP环境中mysql数据库版本5.6.3 ...
- Python 基本数据类型 (二) - 字符串1
# ----------- 首字母大写 ---------- test = "alex is a man" v = test.capitalize() print(v): Alex ...
- CSS效果常见问题
详细解答参见上篇博客 问题1.如何用 div 画一个 xxx box-shadow 无限投影 (堆叠成复杂图案) ::before ::after 问题2.如何产生不占空间的边框 1.box-shad ...
- HDU4010 Query on The Trees (LCT动态树)
Query on The Trees Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Othe ...
- Python虚拟机中的一般表达式(一)
在Python虚拟机框架这一章中,我们通过PyEval_EvalFrameEx看到了Python虚拟机的整体框架.而这章开始,我们将了解Python虚拟机是如何完成对Python的一般表达式的执行,这 ...
- python基础学习笔记——网络编程(协议篇)
一 互联网的本质 咱们先不说互联网是如何通信的(发送数据,文件等),先用一个经典的例子,给大家说明什么是互联网通信. 现在追溯到八九十年代,当时电话刚刚兴起,还没有手机的概念,只是有线电话,那么此时你 ...