标签:相同 clu 复制 中断 状态 turn known The 进程
1 #include <unistd.h> 2 #include <fcntl.h> 3 int fcntl(int fd, int cmd); 4 int fcntl(int fd, int cmd, long arg); 5 int fcntl(int fd, int cmd, struct flock * lock);
1 struct flcok 2 { 3 short int l_type; /* 锁定的状态*/ 4 short int l_whence;/*决定l_start位置*/ 5 off_t l_start; /*锁定区域的开头位置*/ 6 off_t l_len; /*锁定区域的大小*/ 7 pid_t l_pid; /*锁定动作的进程*/ 8 };
文件状态标志设置
io.h
1 #ifndef __IO_H__ 2 #define __IO_H__ 3 4 extern void copy(int fdin, int fdout); 5 6 extern void set_fl(int fd, int flag); 7 extern void clr_fl(int fd, int flag); 8 #endif
io.c
1 #include <sys/types.h> 2 #include <sys/stat.h> 3 #include <fcntl.h> 4 #include <unistd.h> 5 #include "io.h" 6 #include <string.h> 7 #include <errno.h> 8 #include <stdlib.h> 9 #include <stdio.h> 10 #include <fcntl.h> 11 12 13 #define BUFFER_LEN 1024 14 15 /* 文件的读写拷贝 */ 16 void copy(int fdin, int fdout) 17 { 18 char buff[BUFFER_LEN]; 19 ssize_t size; 20 21 // printf("file length: %ld\n", lseek(fdin, 0L, SEEK_END));//将文件定位到文件尾部,偏移量为0L 22 // lseek(fdin, 0L, SEEK_SET);// 定位到文件开头 23 24 while((size = read(fdin, buff, BUFFER_LEN)) > 0) { //从 fdin 中读取 BUFFER_LEN 个字节存放入 buff 中 25 // printf("current: %ld\n", lseek(fdin, 0L, SEEK_CUR)); 26 27 if(write(fdout, buff, size) != size) { 28 fprintf(stderr, "write error: %s\n", strerror(errno)); 29 exit(1); 30 } 31 } 32 if(size < 0) { 33 fprintf(stderr, "read error:%s\n", strerror(errno)); 34 exit(1); // 相当于 return 1; 35 } 36 } 37 38 39 void set_fl(int fd, int flag) 40 { 41 int val; 42 43 //获得原来的文件状态标志 44 val = fcntl(fd, F_GETFL); 45 if(val < 0) { 46 perror("fcntl error"); 47 } 48 49 //增加新的文件状态标志 50 val |= flag; 51 52 //重新设置文件状态标志(val 为新的文件状态标志) 53 if(fcntl(fd, F_SETFL, val) < 0) { 54 perror("fcntl error"); 55 } 56 } 57 58 void clr_fl(int fd, int flag) 59 { 60 int val; 61 62 val = fcntl(fd, F_GETFL); 63 if(val < 0) { 64 perror("fcntl error"); 65 } 66 //清除指定的文件状态标志(设置为0) 67 val &= ~flag; 68 if(fcntl(fd, F_SETFL, val) < 0) { 69 perror("fcntl error"); 70 } 71 }
file_append.c
1 #include <sys/types.h> 2 #include <sys/stat.h> 3 #include <fcntl.h> 4 #include <unistd.h> 5 #include <string.h> 6 #include <errno.h> 7 #include <stdlib.h> 8 #include <stdio.h> 9 #include <fcntl.h> 10 #include "io.h" 11 12 int main(int argc, char *argv[]) 13 { 14 if(argc < 3) { 15 fprintf(stderr, "usage: %s content destfile\n", argv[0]); 16 exit(1); 17 } 18 19 int fd; 20 int ret; 21 size_t size; 22 23 fd = open(argv[2], O_WRONLY); 24 25 //设置追加的文件状态标志 26 set_fl(fd, O_APPEND); 27 28 //清除追加文件状态标志 29 //clr_fl(fd, O_APPEND); 30 /* 31 fd = open(argv[2], O_WRONLY | O_APPEND); 32 if(fd < 0){ 33 perror("open error"); 34 exit(1); 35 } 36 37 */ 38 /* 39 //定位到文件尾部 40 ret = lseek(fd, 0L, SEEK_END); 41 if(ret == -1) { 42 perror("lseek error"); 43 close(fd); 44 exit(1); 45 } 46 */ 47 sleep(10); //睡眠 10s 48 49 //往文件中追加内容 50 size = strlen(argv[1]) * sizeof(char); 51 if(write(fd, argv[1], size) != size) { 52 perror("write error"); 53 close(fd); 54 exit(1); 55 } 56 57 return 0; 58 }
编译调试与 file_append 例子中相同
1 #include <unistd.h> 2 #include <sys/ioctl.h> 3 int ioctl(int fd, int cmd, …)
从键盘 IO 设备中,读取键盘的数据
1 #include <sys/types.h> 2 #include <sys/stat.h> 3 #include <fcntl.h> 4 #include <unistd.h> 5 #include <string.h> 6 #include <errno.h> 7 #include <stdlib.h> 8 #include <stdio.h> 9 #include <fcntl.h> 10 #include <sys/ioctl.h> 11 #include <linux/input.h> 12 13 int main(int argc, const char *argv[]) 14 { 15 int fd = -1; 16 char name[256] = "Unknown"; 17 struct input_event event;//事件源 18 int ret = 0; 19 20 if((fd = open("/dev/input/event1", O_RDONLY)) < 0) { 21 perror("open error"); 22 exit(1); 23 } 24 25 //EVIOCGNAME 宏的作用是获得 IO 设备的名称 26 if(ioctl(fd, EVIOCGNAME(sizeof(name)), name) < 0) { 27 perror("evdev ioctl error\n"); 28 exit(1); 29 } 30 31 printf("The device says its name is %s\n", name); 32 33 //读写打开的设备文件 34 while(1) { 35 ret = read(fd, &event, sizeof(event)); 36 if(ret < 0) { 37 perror("read event error\n"); 38 } 39 40 if(EV_KEY == event.type) { 41 //如果事件是一个按键码 42 printf("key code is %d\n", event.code); 43 44 if(event.code == 16) { 45 //按 q 退出此应用程序 46 break; 47 } 48 } 49 } 50 51 close(fd); 52 53 return 0; 54 }
标签:相同 clu 复制 中断 状态 turn known The 进程
原文地址:https://www.cnblogs.com/kele-dad/p/9033766.html