// 我们来看一下复制files_struct的函数dup_fs int dup_fs(struct files_struct *to, struct files_struct *from) { // cprintf("[dup_fs]\n"); assert(to != NULL && from != NULL); assert(files_count(to) == 0 && files_count(from) > 0); if ((to->pwd = from->pwd) != NULL) { vop_ref_inc(to->pwd); // 增加引用就可以了 } int i; struct file *to_file = to->fd_array, *from_file = from->fd_array; for (i = 0; i < FILES_STRUCT_NENTRY; i ++, to_file ++, from_file ++) { if (from_file->status == FD_OPENED) { /* alloc_fd first */ to_file->status = FD_INIT; fd_array_dup(to_file, from_file); } } return 0; }
void files_closeall(struct files_struct *filesp) { // cprintf("[files_closeall]\n"); assert(filesp != NULL && files_count(filesp) > 0); int i; struct file *file = filesp->fd_array; //skip the stdin & stdout for (i = 2, file += 2; i < FILES_STRUCT_NENTRY; i ++, file ++) { if (file->status == FD_OPENED) { fd_array_close(file); } } }
//Called when a proc exit void files_destroy(struct files_struct *filesp) { // 用于销毁文件 // cprintf("[files_destroy]\n"); assert(filesp != NULL && files_count(filesp) == 0); if (filesp->pwd != NULL) { // 如果当前目录不为空 vop_ref_dec(filesp->pwd); } int i; struct file *file = filesp->fd_array; for (i = 0; i < FILES_STRUCT_NENTRY; i ++, file ++) { if (file->status == FD_OPENED) { fd_array_close(file); // 关闭每一个文件 } assert(file->status == FD_NONE); } kfree(filesp); }
// get_fd_array - get current process's open files table static struct file * get_fd_array(void) { struct files_struct *filesp = current->filesp; assert(filesp != NULL && files_count(filesp) > 0); return filesp->fd_array; }