zoukankan      html  css  js  c++  java
  • linux create a process

     When the system starts up it is running in kernel mode and there is, in a sense, only one process, the initial process. Like all processes, the initial process has a machine state represented by stacks, registers and so on. These will be saved in the initial processes task_struct  data structure when other processes in the system are created and run. At the end of system initialization, the initial process starts up a kernel thread (called init) and then sits in an idle loop doing nothing. Whenever there is nothing else to do the scheduler will run this, idle, process. The idle processes task_struct is the only one that is not dynamically allocated, it is statically defined at kernel build time and is, rather confusingly, called init_task .

    The init kernel thread or process has a process identifier of 1 as it is the system's first real process. It does some initial setting up of the system (such as opening the system console and mounting the root file system) and then executes the system initialization program. This is one of /etc/init/bin/init or /sbin/init depending on your system. The init program uses /etc/inittab as a script file to create new processes within the system. These new processes may themselves go on to create new processes. For example the getty process may create a login process when a user attempts to login. All of the processes in the system are descended from the init kernel thread.

    New processes are created by cloning old processes, or rather by cloning the current process. A new task is created by a system call (fork or clone)

    and the cloning happens within the kernel in kernel mode. At the end of the system call there is a new process waiting to run once the scheduler chooses it. A new task_struct  data structure is allocated from the system's physical memory with one or more physical pages for the cloned processes stacks (user and kernel). A new process identifier may be created, one that is unique within the set of process identifiers in the system. However, it is perfectly reasonable for the cloned process to keep its parents process identifier. The new task_struct  is entered into the task  vector and the contents of the old (current ) processes task_struct  are copied into the cloned task_struct .

    When cloning processes Linux allows the two processes to share resources rather than have two seperate copies. This applies to the processes files, signal handlers and virtual memory. When the resources are to be shared their respective count  fields are incremented so that Linux will not deallocate these resources until both processes have finished using them. So, for example, if the cloned process is to share virtual memory, its task_struct  will contain a pointer to the mm_struct  of the original process and that mm_struct  has its count  field incremented to show the number of current processes sharing it.

    Cloning a processes virtual memory is rather tricky. A new set of vm_area_struct  data structures must be generated together with their owning mm_struct  data structure and the cloned processes page tables. None of the processes virtual memory is copied a this point. That would be a rather difficult and lengthy task for some of that virtual memory would be in physical memory, some in the executable image that the process is currently executing and possibly some would be in the swap file. Instead Linux uses a technique called ``copy on write'' which means that virtual memory will only be copied when one of the two processes tries to write to it. Any virtual memory that is not written to, even if it can be, will be shared between the two processes without any harm occuring. The read only memory, for example the executable code, will always be shared. For ``copy on write'' to work, the writeable areas have their page table entries marked as read only and the vm_area_struct  data structures describing them are marked as ``copy on write''. When one of the processes attempts to write to this virtual memory a page fault will occur. It is at this point that Linux will make a copy of the memory and fix up the two processes page tables and virtual memory data structures.

  • 相关阅读:
    GIthub的小技巧
    C#中DateTime格式转换
    流程控制: if分支 while循环 for循环
    注释,输入,格式化输出,数据类型,运算符
    计算机原理简单了解
    day30
    day19
    day18 时间:time:,日历:calendar,可以运算的时间:datatime,系统:sys, 操作系统:os,系统路径操作:os.path,跨文件夹移动文件,递归删除的思路,递归遍历打印目标路径中所有的txt文件,项目开发周期
    day17跨文件夹导入模块,模块的两种被执行方式,包,直接使用包中模块,包的管理
    day16模块,导入模板完成的三件事,起别名,模块的分类,模块的加载顺序,环境变量,from...import语法导入,from...import *,链式导入,循环导入
  • 原文地址:https://www.cnblogs.com/lianghong881018/p/10303848.html
Copyright © 2011-2022 走看看