Linux System Programming 学习笔记(二) 文件I/O
int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644) int fd = creat(filename, 0644)
size_t readn(int fd, void* buf, size_t len) { size_t tmp = len; ssize_t ret = 0; while (len != 0 && (ret = read(fd, buf, len)) != 0) { if (ret == -1) { if (errno == EINTR) { continue; } fprintf(stderr, "read error\n"); break; } len -= ret; buf += ret; } return tmp - len; }
size_t write_all(int fd, void* buf, size_t len) { ssize_t ret = 0; size_t tmp = len; while (len != 0 && (ret = write(fd, buf, len)) != 0) { if (ret == -1) { if (errno == EINTR) { continue; } fprintf(stderr, "write error\n"); break; } len -= ret; buf += ret; } return tmp - len; }
int ret = fsync(fd);
open调用时 O_SYNC标志表示 文件必须同步
int fd = open(file, O_WRONLY | O_SYNC);
int ftruncate(int fd, off_t len);
将给定文件截断为给定长度,这里的给定长度是可以小于文件大小,也可以大于文件大小(会造成空洞)
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 = 0; /* wait on stdin for input */ fd_set readfds; FD_ZERO(&readfds); FD_SET(STDIN_FILENO, &readfds); int ret = select(STDIN_FILENO + 1, &readfds, NULL, NULL, &tv); if (ret == -1) { fprintf(stderr, "select error\n"); return 1; } else if (!ret) { fprintf(stderr, "%d seconds elapsed.\n", TIMEOUT); return 0; } if (FD_ISSET(STDIN_FILENO, &readfds)) { char buf[BUFLEN + 1]; int len = read(STDIN_FILENO, buf, BUFLEN); if (len == -1) { fprintf(stderr, "read error\n"); return 1; } if (len != 0) { buf[BUFLEN] = ‘\0‘; fprintf(stdout, "read:%s\n", buf); } return 0; } else { fprintf(stderr, "This should not happen\n"); return 1; } }
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[2]; /* watch stdin for input */ fds[0].fd = STDIN_FILENO; fds[0].events = POLLIN; /* watch stdout for alibity to write */ fds[1].fd = STDOUT_FILENO; fds[1].events = POLLOUT; int ret = poll(fds, 2, TIMEOUT * 1000); if (ret == -1) { fprintf(stderr, "poll error\n"); return 1; } if (!ret) { fprintf(stdout, "%d seconds elapsed.\n", TIMEOUT); return 0; } if (fds[0].revents & POLLIN) { fprintf(stdout, "stdin is readable\n"); } if (fds[1].revents & POLLOUT) { fprintf(stdout, "stdout is writable\n"); } return 0; }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。