文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>linux process creating

linux process creating

时间:2005-11-06  来源:redog

这一节主要介绍了,内核创建一个子进程的过程。介绍了创建一个进程需要的十八个步骤。

Creating Processes

Unix operating systems rely heavily on process creation to statify user requests.As an example,the shell process creats a new
process that executes another copy of the shell whenever the user enters a command.
  Traditional unix systems treat all processes in the same way:resources owned by the parent process are duplicated,and a copy
is granted to the child process.this approach makes process creation very slow and inefficient.sinace it requires copying
the entire address space of the parent process.the child process rarely needs to read or modify all the resources already owned by the
parent;in many cases,it issues an immediate execve()and wipes out the address space so carefully saved.
  Modern Unix kernels solve this problem by introducing three different mechanisms:
  The Copy on write technique allows both the parent and the child to read the same physical pages,whenever either one tries
to write on a physical page,the kernel copies its contents into a new physical page that is assigned to the writing process
the implementation of this technique in linux is fully explained in chapter 7
  Lightweight processes allows both the parent and the child to share many per-process kernel data stuctures,like the paging
tables(and therefore the entire user mode address space)and the open file talbes.
  The vfork()system call creats a process that shares the memory address space of its parents.to prevent the parent form overwriting
  data needed by the child,the parent's execution is blocked until the child exits or executes a new program.we'll learn more about the
vfork()system call in the following section.
 
 
  The clone(),fork(),and vfork() system calls
 
  Lightweight processes are created in linux by using a function named _clone(),which makes use of four parameters:
 
  fn:
     specific a function to be executed by the new process;when the function returns,the child terminates,the function
     returns an integer,which represents the exit code for the child process.
  arg:
     pointer to data passed to the fn()function.
  flags
     miscellaneous information.the low byte specifices the signal number bo be sent to the parent process when the child
     terminates;the SIGCHLD signal is generally selected.the remaining 3 bytes encode a group of clone flags,which specify
     the resources shared between the parent and the child process,the flags,when set,have the following meanings:
  child_stack
     specifies the user mode stack pointer to be assigned to the esp register of the child process,if it is equal to 0,the
     kernle assigns to the child the current parent stack pointer.thus the parent and child temporarily share the same user
     mode stack.but thanks to the copy on write mechanism,the usually get separate copies of the user mode stack as soon as one
     tries to change the stack.however,this parameter must have a non-null value if the child process shares the same address
     space as the parent
 
  the clone()system call receives only the flags and the child_stack parameters; the new process always start its execution
  form the instruction following the system call invocation.when the system call returns to the _clone()function,it determines
  whether it is in the parent or the child and forces the child to exectue the fn() function.
 
  the traditional fork()system call is implemented by linux as a clone()whose first parameter specific a SIGCHLD signal and all
  the clone flags cleard and whose second parameter is 0
 
  the old vfork() system call ,described in the previous section,is implemented by linux as a clone()whose first parameter
  specifies a SIGCHLD signal and the flags CLONE_VM and CLONE_VFORK and whose second parameter is equal to 0.
 
  when either  a clone(),fork(),or vfork()system call is issued,the kernel invokes the do_fork() function,which executes the following steps:
  
   1.if the CLONE_PID flag has been specified,the do_fork()function checks whether the PID of the parent process is not null
     ;if so ,it return an error code.only the swapper process is allowed to set CLONE_PID;this is required when initializing
     a mulitiprocessor system.
   2. the alloc_task_struct() function is invoked in order to get a news 8kb union task_union memory area to store the process
      descriptor and the kernel mode stack of the new process.
   3. the function follows the current pointer to obtain the parent preocess descriptor and copies it into the new process
      descriptor in the memory area just allocated.
   4.A few checks occurs to make sure the user has the resources necessary to start a new process.First the function checks whether
     current->rlim[RLIMIT_NPROC].rlim_cur is smaller than or equal to the current number of processes owned by the user;if so
     an error code is returned.the fucntion gets the current number of process owned by the user from a per-user
     data structure named user_struct.this data structure can be found through a pointer in the user field of the process descriptor
   5.the find_empty_process()function si invoked,if the owner of the parent process is not the superuser,this fucntion checks whether
     nr_tasks is samller than NR_TASKS_MIN_TASKS_LEFT_FOR_ROOT.if so,find_empty_process()invokes get_free_taskslot()to find
     a free enty in the task array.otherwise,it returns an error.
   6.the function write the new process descriptor pointer into the previously obtained task entry and sets the tarray_ptr field of the
     process descriptor to the address of that entry
   7.if the parent process makes use of some kernel modules.the function increments the corresponding reference counters.
     Each kernel module has its own reference counter,which indicates how many processes are using it.A module cannot be removed
     unless its reference counter is null.
   8.the function the updates some of the flags included in the flags field that hava been copied from the parent process:
     a.it clears the PF_SUPERPRIV flag,which indicates whether the process has used any of its superuser privileges.
     b.it clears the PF_USEDFPU flag
     c.it clears the PF_PTRACED flag unless the CLONE_PTARCE parameter flag is set.when set,the CLONE_PTRACE flag means that
     the parent process is being traced with the ptrace()function,so the child should be traced too.
     d.it clears PF_TRACESYS flag unless.once again,the CLONE_PTRACE parameter flag is set.
     e.it sets the PF_FORKNOEXEC flag.which indicates that the child process has not yet issued an execve()system call.
     f.it sets the PF_VFORK flag according to the values of the CLONK_VFORK flag.this specifies that the parent process
     must be woken up whenever the process issues an execve()system call or terminates.
   9.now the function has taken almost everything that it can use from the parent process;the rest of its activities focus on
     setting up new resources in the child and letting the kernel know that this new process has been born.first the function
     invokes the get_pid()function to obtain a new PID, which will be assigned to the child process.
   10.the function the updates all the process descriptor fields that cannot be inherited from the parent process,such as the
      fields that specify the process parenthood relationship.
   11.unless specified differently by the flags parameter,it invokes copy_files(),copy_fs(),copy_sighand(),and copy_mm()to
      create new data structures and copy into them the values of the corresponding parent process data structures.
   12.it invokes copy_thread() to initialize the kernel mode stack of the child process with the values contained in the
      cpu registers when the clone()call was issued.however,the function force the value into the field corresponding to the eax register.
      the tss.esp field of the TSS of the child process is initialized with the base address of the kernel mode stack,and the
      address of an assembly language function is stored in the tss.eip field.
   13.it uses the SET_LINKS macro to insert the new process descriptor in the process list.
   14.it uses the hash_pid()function to insert the new process descriptor in the pidhash hash table.
   15.it increments the values of nr_tasks and current->user->count.
   16.it set the state field of the child process descriptor to TASK_RUNNING and then invokes wake_up_process()to insert the child in the runqueue list.
   17.if the CLON_VFORK flag has been specified,the function suspends the parent process until the child release its memory address space
      in order to do this, the process descriptor includes a kernel semaphore called vfork_sem
   18.it returns the PID of the child,which will be eventually be read by the parent process in user mode
  
   Now we have a complete child process in the runnable state,but it isnot actually running,it is up to the scheduler to decide
   when to give the CPU to this child,at some future process swithc,the schedule will bestow this favor of the child's process
   descriptor,in particular,esp will be loaded with tss.esp,and eip will be loaded with the address of ret_from_fork().this assembly
   language function,in turn,invokes the ret_from_sys_call() function,which reloads all other process will then start its execution
   right at the end of the fork(),vfork(),or clone() systemcall.

相关阅读 更多 +
排行榜 更多 +
宝宝情商养成宝宝巴士

宝宝情商养成宝宝巴士

休闲益智 下载
燥热手机版

燥热手机版

飞行射击 下载
巨人狙击手安卓版

巨人狙击手安卓版

飞行射击 下载