标签:style blog class code java c
struct flock { short l_type; /* F_RDLCK, F_WRLCK, F_UNLCK */ off_t l_start; /* offset in bytes, relative to l_whence */ short l_whence; /* SEEK_SET, SEEK_CUR, SEEK_END */ off_t l_len; /* length in bytes, 0 means lock to EOF */ pid_t l_pid; /* returned with F_GETLK */ };
#include <fcntl.h> int lock_reg(int fd, int cmd, int type, off_t offset, int whence, off_t len) { struct flock lock; lock.l_type = type; lock.l_start = offset; lock.l_whence = whence; lock.l_len = len; int ret = fcntl(fd, cmd, &lock); return ret; }
如果两个进程相互等待对方持有并且锁定的资源时,则这两个进程处于死锁状态
3. select
#include <select.h> int select(int maxfds, fd_set* readfds, fd_set* writefds, fd_set* exceptfds, struct timeval* timeout);
中间三个参数 readfds, writefds, exceptfds是指向描述符集的指针,每个描述符集存放在一个fd_set中:
这三个参数指针任意一个可以为空指针,表示对相应状态并不关心。如果三个指针都是空指针,则select提供了比sleep更精确的计时器,因为sleep只能等待整数秒,
而select的struct timeval可以精确到微秒
int FD_ISSET(int fd, fd_set* set); void FD_CLR(int fd, fd_set* set); void FD_SET(int fd, fd_set* set); void FD_ZERO(fd_set* set);
select的第一个参数maxfds是三个描述符集中最大的fd数值加1,也可以将此参数设置为FD_SETSIZE,表明最大的描述符数
fd_set readset, writeset; FD_ZERO(&readset); FD_ZERO(&writeset); FD_SET(0, &readset); FD_SET(3, &readset); FD_SET(1, &writeset); FD_SET(2, &writeset); select(4, &readset, &writeset, NULL, NULL);
因为描述符编号从0开始,所以要在最大描述符编号值加1,第一个参数实际上就是要检查的描述符数(从描述符0开始)
int readn(int fd, char* ptr, size_t n) { size_t nleft = n; int nread = 0; while (nleft > 0) { if ((nread = read(fd, ptr, nleft)) < 0) { if (nleft == n) { return -1; } else { break; } } else if (nread == 0) { break; } nleft -= nread; ptr += nread; } return n - nleft; }
int writen(int fd, char* ptr, size_t n) { size_t nleft = n; int nwrite = 0; while (nleft > 0) { if ((nwrite = write(fd, ptr, nleft)) < 0) { if (nleft == n) { return -1; } else { break; } } else if (nwrite == 0) { break; } nleft -= nwrite; ptr += nwrite; } return n - nleft; }
#include <sys/mman.h> void* mmap(void* addr, size_t len, int prot, int flag, int fd, off_t off);
#include <unistd.h> #include <string.h> #include <sys/mman.h> #include <fcntl.h> #include <sys/types.h> #include <stdio.h> int main(int argc, char* argv[]) { int fdin = 0; int fdout = 0; char* src = NULL; char* dst = NULL; struct stat statbuf; if (argc != 3) { fprintf(stderr, "usage: %s <fromfile> <tofile>\n", argv[0]); return 1; } if ((fdin = open(argv[1], O_RDONLY)) < 0) { fprintf(stderr, "cannot open %s for reading\n", argv[1]); } if ((fdout = open(argv[2], O_RDWR | O_CREAT | O_TRUNC)) < 0) { fprintf(stderr, "cannot creat %s for writing\n", argv[2]); } if (fstat(fdin, &statbuf)) { fprintf(stderr, "fsat error\n"); } if (lseek(fdout, statbuf.st_size - 1, SEEK_SET) == -1) { fprintf(stderr, "lseek error\n"); } if (write(fdout, "", 1) != 1) { fprintf(stderr, "write error\n"); } if ((src = mmap(0, statbuf.st_size, PROT_READ, MAP_SHARED, fdin, 0)) == MAP_FAILED) { fprintf(stderr, "mmap error for input\n"); } if ((dst = mmap(0, statbuf.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fdout, 0)) == MAP_FAILED) { fprintf(stderr, "mmap error for output\n"); } memcpy(dst, src, statbuf.st_size); munmap(src, statbuf.st_size); munmap(dst, statbuf.st_size); return 0; }
APUE 学习笔记(九) 高级I/O,布布扣,bubuko.com
标签:style blog class code java c
原文地址:http://www.cnblogs.com/wwwjieo0/p/3726498.html