# 系統程式設計 - `llseek(3)` [TOC] ## 課程影片 ### 系統程式設計 2018 - W3 1 {%youtube KhnUbPf44_w %} ## 例子 ```c #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdlib.h> #include <unistd.h> #include <stdio.h> #define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) #define FILE_FLAG (O_CREAT | O_WRONLY | O_TRUNC) char buf1[] = "abcdefghij"; char buf2[] = "ABCDEFGHIJ"; int main(void) { int fd; if ((fd = open("file.hole", FILE_FLAG, FILE_MODE)) < 0) perror("creat error"); if (write(fd, buf1, 10) != 10) perror("buf1 write error"); /* offset now = 10 */ if (lseek(fd, 40, SEEK_SET) == -1) perror("lseek error"); /* offset now = 40 */ if (write(fd, buf2, 10) != 10) perror("buf2 write error"); /* offset now = 50 */ exit(0); } ``` ### `lseek` ```c 1d... 0.889 us | post_ttbr_update_workaround(); 1d... | do_el0_svc() { 1d... | el0_svc_common.constprop.0() { 1.... | invoke_syscall() { 1.... | __arm64_sys_lseek() { 1.... | __fdget_pos() { 1.... 2.093 us | __fget_light(); 1.... 6.112 us | } 1.... | ext4_llseek() { 1.... 2.056 us | generic_file_llseek_size(); 1.... 6.222 us | } 1.... + 18.796 us | } 1.... + 23.000 us | } 1d... + 27.111 us | } 1d... + 28.482 us | } ```