您的位置:首页 > 运维架构 > Linux

Linux System Programming note 2 ——File I/O

2014-06-06 09:05 316 查看
1.
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *name, int flags);
int open(const char *name, int flags, mode_t mode);
flags: O_RDONLY, O_RDWR, O_WRONLY, O_APPEND, O_ASYNC(available only for FIFOs, pipes, sockets, and terminals, not for regular files), O_CLOEXEC, O_CREAT, O_DIRECT, O_DIRECTORY, O_EXCL, O_LARGEFILE, O_NOATIME+,
O_NOCTTY, O_NOFOLLOW, O_NONBLOCK, O_SYNC, O_TRUNC
2.
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int creat(const char *name, mode_t mode);
3.

#include <unistd.h>

ssize_t read(int fd, void *buf, size_t len);

ssize_t ret;
while (len != 0 && (ret = read(fd, buf, len)) !=0){
if (ret == -1){
if (errno == EINTR)
continue;
perror("read");
break;
}

len -= ret;
buf +=ret;
}


4. 
EBADF
     The given file descriptor is invalid or is not open for reading.
EFAULT
     The pointer provided by buf is not inside the calling process's address space.
EFBIG
     The write would have made the file larger than per-process maximum file, or internal implementation, limilts.
EINVAL
     The file descriptor is mapped to an object that does not allow reading.
EIO
     A low-level I/O error occurred.
ENOSPC
     The filesystem backing the given file descriptor does not have sufficient space.
EPIPE
     The given file descriptor is associated with a pipe or socket whose reading end is closed. the process will also receive a SIGPIPE signal. The default action for the SIGPIPE signal is to terminate the receiving
process.
5.

#include <unistd.h>

ssize_t write(int fd, const void *buf, size_t count);

ssize_t ret, nr;

while(len != 0 && (ret = write(fd, buf, len)) != 0) {
if (ret == -1) {
if (errno == EINTR)
continue;
perror ("write");
break;
}

len -= ret;
buf += ret;
}


6.

#include <unistd.h>
int fsync(int fd);

#include <unistd.h>
int fdatasync(int fd);

if (fsync (fd) == -1) {
if (errno == EINVAL){
if (fdatasync(fd) == -1)
perror("fdatasync");
} else {
perror("fsync");
}
}


7. 
#include <unistd.h>

void sync(void);

It is often recommended to invoke sync() multiple times to ensure that all  data is safely on disk. Linux, however, does wait until all buffers are committed.

Therefore a single sync() is sufficient.

8.
#include <unistd.h>
int close(int fd);

9.
#include <sys/types.h>
#include <unistd.h>

off_t lseek(int fd, off_t pos, int origin);

origin: SEEK_CUR, SEEK_END, SEEK_SET

10.
#define _XOPEN_SOURCE 500
#include <unistd.h>
ssize_t pread(int fd, void *buf, size_t count, off_t pos);
ssize_t pwrite(int fd, const void *buf, size_t count, off_t pos);

11.
#include <unsitd.h>
#include <sys/types.h>

int ftruncate (int fd, off_t len);
int ftruncate(const char *path, off_t len);

12.
#include <sys/select.h>

int select (int n,
               fd_set *readfds,
               fd_set *writefds,
               fd_set *exceptfds,
               struct timeval *timeout);

FD_CLR(int fd, fd_set *set);
FD_ISSET(int fd, fd_set *set);
FD_SET(int fd, fd_set *set);
FD_ZERO(fd_set *set);

#include <sys/time.h>
struct timeval {
     long tv_sec;
     long tv_usec;
};

Example for select():

#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

#define TIMEOUT 5
#define BUF_LEN 1024

int main(void)
{
struct timeval tv;
fd_set readfds;
int ret;

FD_ZERO(&readfds);
FD_SET(STDIN_FILENO, &readfds);

tv.tv_sec = TIMEOUT;
tv.tv_usec = 0;

ret = select(STDIN_FILENO + 1, &readfds, NULL, NULL, &tv);

if (ret == -1){
perror("select");
return 1;
}else if (!ret){
printf("%d seconds elapsed.\n", TIMEOUT);
return 0;
}

if (FD_ISSET(STDIN_FILENO, &readfds)){
char buf[BUF_LEN + 1];
int len;

len = read(STDIN_FILENO, buf, BUF_LEN);
if (len == -1){
perror("Read");
return 1;
}

if (len){
buf[BUF_LEN] = '\0';
printf("read: %s\n", buf);
}

return 0;
}

fprintf(stderr, "This should not happen!\n");
return 1;
}


13.
#define _XOPEN_SOURCE 600
#include <sys/select.h>

int pselect(int n,
                fd_set *readfds,
                fd_set *writefds,
                fd_set *exceptfds,
                const struct timespec *timeout,
                const sigset_t *sigmask);
FD_CLR(int fd, fd_set *set);
FD_ISSET(int fd, fd_set *set);
FD_SET(int fd, fd_set *set);
FD_ZERO(fd_set *set);

#include <sys/time.h>

struct timespec{
     long tv_sec;
     long tv_nsec;
};

14.
#include <poll.h>

int poll (struct pollfd *fds, nfds_t nfds, int timeout);

#include <poll.h>
struct pollfd {
     int fd;
     short events;
     short revents;
};

15.
#define _GNU_SOURCE
#include <poll.h>

int ppoll (struct pollfd *fds, nfds_t nfds, const struct timespec *timeout, const sigset_t *sigmask);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息