博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
进程创建时文件处理
阅读量:4152 次
发布时间:2019-05-25

本文共 2848 字,大约阅读时间需要 9 分钟。

 
static int copy_files(unsigned long clone_flags, struct task_struct *tsk){ struct files_struct *oldf, *newf; int error = 0;  /*  * A background process may not have any files ...  */ oldf = current->files; if (!oldf)  goto out;  if (clone_flags & CLONE_FILES) {  atomic_inc(&oldf->count);  goto out; }  newf = dup_fd(oldf, &error); if (!newf)  goto out;  tsk->files = newf; error = 0;out: return error;}
/* * Allocate a new files structure and copy contents from the * passed in files structure. * errorp will be valid only when the returned files_struct is NULL. */struct files_struct *dup_fd(struct files_struct *oldf, int *errorp){ struct files_struct *newf; struct file **old_fds, **new_fds; unsigned int open_files, i; struct fdtable *old_fdt, *new_fdt;  *errorp = -ENOMEM; newf = kmem_cache_alloc(files_cachep, GFP_KERNEL); if (!newf)  goto out;  atomic_set(&newf->count, 1);  spin_lock_init(&newf->file_lock); newf->resize_in_progress = false; init_waitqueue_head(&newf->resize_wait); newf->next_fd = 0; new_fdt = &newf->fdtab; new_fdt->max_fds = NR_OPEN_DEFAULT; new_fdt->close_on_exec = newf->close_on_exec_init; new_fdt->open_fds = newf->open_fds_init; new_fdt->full_fds_bits = newf->full_fds_bits_init; new_fdt->fd = &newf->fd_array[0];  spin_lock(&oldf->file_lock); old_fdt = files_fdtable(oldf); open_files = count_open_files(old_fdt);  /*  * Check whether we need to allocate a larger fd array and fd set.  */ while (unlikely(open_files > new_fdt->max_fds)) {  spin_unlock(&oldf->file_lock);   if (new_fdt != &newf->fdtab)   __free_fdtable(new_fdt);   new_fdt = alloc_fdtable(open_files - 1);  if (!new_fdt) {   *errorp = -ENOMEM;   goto out_release;  }   /* beyond sysctl_nr_open; nothing to do */  if (unlikely(new_fdt->max_fds < open_files)) {   __free_fdtable(new_fdt);   *errorp = -EMFILE;   goto out_release;  }   /*   * Reacquire the oldf lock and a pointer to its fd table   * who knows it may have a new bigger fd table. We need   * the latest pointer.   */  spin_lock(&oldf->file_lock);  old_fdt = files_fdtable(oldf);  open_files = count_open_files(old_fdt); }  copy_fd_bitmaps(new_fdt, old_fdt, open_files);  old_fds = old_fdt->fd; new_fds = new_fdt->fd;  for (i = open_files; i != 0; i--) {  struct file *f = *old_fds++;  if (f) {   get_file(f);  } else {   /*    * The fd may be claimed in the fd bitmap but not yet    * instantiated in the files array if a sibling thread    * is partway through open().  So make sure that this    * fd is available to the new process.    */   __clear_open_fd(open_files - i, new_fdt);  }  rcu_assign_pointer(*new_fds++, f); } spin_unlock(&oldf->file_lock);  /* clear the remainder */ memset(new_fds, 0, (new_fdt->max_fds - open_files) * sizeof(struct file *));  rcu_assign_pointer(newf->fdt, new_fdt);  return newf; out_release: kmem_cache_free(files_cachep, newf);out: return NULL;}

转载地址:http://ozhti.baihongyu.com/

你可能感兴趣的文章
双重指针——以0.12内核static inline void _sleep_on(struct task_struct **p, int state)为例
查看>>
VMWare虚拟机linux访问windows主机硬盘文件方法
查看>>
代码一致性
查看>>
有关操作系统保护模式中的 shl eax 4 这条语句的意思
查看>>
Ubuntu支持中文
查看>>
gitk安装
查看>>
GRUB引导linux3.0内核
查看>>
堆栈中的ss,bp,sp
查看>>
如何在linux终端下查询C函数
查看>>
如何连接远程 Ubuntu 桌面?
查看>>
win7远程控制ubuntu桌面
查看>>
控制寄存器(CR0,CR1,CR2,CR3)
查看>>
Linux内核如何计算链表的位置
查看>>
Linux中断执行过程
查看>>
C语言的函数指针的例子
查看>>
如何在Ubuntu下编辑PDF文件
查看>>
用gdb如何查看指定地址的内存内容?
查看>>
__builtin_expect详解
查看>>
关于vmlinux和bzImage
查看>>
分配task_struct时分配页面的大小的问题
查看>>