fd是整数,可以找到对应的文件,fd的范围限于进程。究竟是如何找到呢:
#include <sys/stat.h> #include <fcntl.h> #include <stdlib.h> #include <stdio.h> #define F_LEN 1024 char filename[F_LEN]="temp"; int main(void) { int fd; char buf[F_LEN]; if((fd =open(filename, O_RDWR))< 0){ perror("open error"); exit(0); } snprintf(filename, F_LEN, "/proc/%ld/fd/%d",(long)getpid(), fd); if(readlink(filename, buf, F_LEN)< 0){ perror("readlink() "); exit(0); } printf("In this process fd=%d<->filename=%s\n", fd, buf); pause(); return 0; }
时间: 2024-11-05 14:42:59