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.
linux create a process的更多相关文章
- Java JVM、JNI、Native Function Interface、Create New Process Native Function API Analysis
目录 . JAVA JVM . Java JNI: Java Native Interface . Java Create New Process Native Function API Analys ...
- Linux Running State Process ".so"、"code" Injection Technology
catalog . 引言 . 基于so文件劫持进行代码注入 . 基于函数符号表(PLT)中库函数入口地址的修改进行代码注入 . PLT redirection through shared objec ...
- Linux 进程状态 概念 Process State Definition
From : http://www.linfo.org/process_state.html 进程状态是指在进程描述符中状态位的值. 进程,也可被称为任务,是指一个程序运行的实例. 一个进程描述符是一 ...
- Linux 进程状态标识 Process State Definition
From : http://www.linfo.org/process_state.html 译者:李秋豪 进程状态标识是指在进程描述符中状态位的值. 进程,也可被称为任务,是指一个程序运行的实例. ...
- Linux - create usergroup, user and Assigning permissions
第一步:登录已有的Linux系统,使用root账户,登录好以后,如下图: 这样,就登录到Linux系统中,而且是用root用户登录的 注意:如果,你想要创建用户和用户组,那么你当前登录的用户必须有ro ...
- <<Linux kernel development>> Process Management
The Process On modern operating systems,processes provide two virtualizations:a virtualized processo ...
- Bugzilla Error message: couldn't create child process: 720003: index.cgi
two steps is try to fix this issue. 1. Turn off the windowns firewall 2. Register the perl to the sy ...
- Linux Process VS Thread VS LWP
Process program program==code+data; 一个进程可以对应多个程序,一个程序也可以变成多个进程.程序可以作为一种软件资源长期保存,以文件的形式存放在硬盘 process: ...
- Linux中的task,process, thread 简介
本文的主要目的是介绍在Linux内核中,task,process, thread这3个名字之间的区别和联系.并且和WINDOWS中的相应观念进行比较.如果你已经很清楚了,那么就不用往下看了. LINU ...
随机推荐
- bzoj 3223 文艺平衡树 - Splay
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3884 Solved: 2235[Submit][Sta ...
- AD快捷键
* 在PCB电气层之间切换.在布线的过程中,按此键则换层并自动添加过孔并换层. Q 在公制和英制之间切换 J+C 定位到指定的元件处.在弹出的对话框内输入该元件的编号. G+G 设定栅格吸附尺寸. T ...
- 常用模块之 time,datetime,random,os,sys
time与datetime模块 先认识几个python中关于时间的名词: 时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量.我们运行“ty ...
- SRLTE,SGLTE,SVLTE,CSFB,VoLTE的区别【转】
本文转载自:https://blog.csdn.net/dangbochang/article/details/43851979 SRLTE——Single Radio LTE,俗称单待LTE. SG ...
- 一个改写MBR的例子
前言 想要对MBR类的病毒进行一下研究与学习,在此期间,看了很多资料,其中帮助最大的就是金龟子学姐和willj学长发表的文章.一个从源码与实现角度来讲了一下,另外一个从反病毒角度来分析. 功能描述 ...
- openwrt的编译系统是如何生成squashfs文件系统的
答:请看include/image.mk中的以下定义: define Image/mkfs/squashfs $(STAGING_DIR_HOST)/bin/mksquashfs4 $(call mk ...
- 2016年蓝桥杯B组C/C++省赛(预选赛)题目解析
2016年蓝桥杯B组C/C++ 点击查看2016年蓝桥杯B组省赛试题(无答案版) 第一题:煤球数目 题解 有一堆煤球,堆成三角棱锥形.具体: 第一层放1个, 第二层3个(排列成三角形), 第三层6个( ...
- centos 安装iftop
iftop是linux下的一个流量监控工具,用于查看实时网络流量.官网:http://www.ex-parrot.com/pdw/iftop/ 1.安装必须软件包yum install libpcap ...
- 批量启动application pool
在powershell中执行 Get-ChildItem IIS:\AppPools | where {$_.state -eq "Stopped"} | Start-WebApp ...
- IntelliJ IDEA 设置代码提示或自动补全的快捷键(Alt+/)
点击 文件菜单(File) –> 点击 设置(Settings… Ctrl+Alt+S), –> 打开设置对话框.在左侧的导航框中点击 KeyMap. 接着在右边的树型框中选择 Main ...