What is process?
Process can be seen as a dynamic instance of program, or say it's an instance of a computer program that is being executed./in execution.
Program其实就是指我们通常写的程序,这些都是静态的代码,当这些程序被执行的时候,它们就叫做process,进程。所以我们说,进程就是执行中的程序。
当程序运行的时候,操作系统会为这个程序提供它需要的资源。例如memory for storing data, system resource like file system access. 一个进程其实就是一个container, 其中存放了运行需要的资源,和这段运行的代码。
A process is defined as an entity which represents the basic unit of work to be implemented in the system.
When a program is loaded into the memory and it becomes a process, it can be divided into four sections ─ stack, heap, text and data. The following image shows a simplified layout of a process inside main memory. Program指的就是我们平时写的代码。
what does a process include
Stack:The process Stack contains the temporary data such as method/function parameters, return address and local variables.
Heap:This is dynamically allocated memory to a process during its run time.
Text:This includes the current activity represented by the value of Program Counter and the contents of the processor's registers. 储存处理器执行的代码。
Data: This section contains the global and static variables. 存储全局和静态变量。
States of Process
操作系统通常都支持多任务处理,即多个进程同时执行,这样的系统也叫做multi-processing, multi-tasking or time-sharing system。对于这种多任务同时处理,并不需要很多CPU,当CPU的个数小于运行的进程的数量的时候,就会通过改变进程的状态,来安排这些进程‘同时’运行。
这里‘同时’并不是真正的多个进程同时运行,而是通过快速切换多个进程,让用户觉得好像这些进程是同时在运行的,实际上并不是。每个进程占用CPU很短的时间,然后就放弃资源,让给下一个进程。或者当这个进程在等待I/O的时候或其他很慢的操作的时候,也会放弃CPU资源,进入wait/block/sleep状态,让给其他进程。
进程的三种状态
进程执行时的间断性,决定了进程可能具有多种状态。事实上,运行中的进程可能具有以下三种基本状态。
1)就绪状态(Ready):
进程已获得除处理器外的所需资源,等待分配处理器资源;只要分配了处理器进程就可执行。就绪进程可以按多个优先级来划分队列。例如,当一个进程由于时间片用完而进入就绪状态时,排入低优先级队列;当进程由I/O操作完成而进入就绪状态时,排入高优先级队列。
The process is waiting to be assigned to a processor. Ready processes are waiting to have the processor allocated to them by the operating system so that they can run. Process may come into this state afterStartstate or while running it by but interrupted by the scheduler to assign CPU to some other process.
2)运行状态(Running):
进程占用处理器资源;处于此状态的进程的数目小于等于处理器的数目。在没有其他进程可以执行时(如所有进程都在阻塞状态),通常会自动执行系统的空闲进程。
Once the process has been assigned to a processor by the OS scheduler, the process state is set to running and the processor executes its instructions.
3)阻塞状态(Blocked):
由于进程等待某种条件(如I/O操作或进程同步),在条件满足之前无法继续执行。该事件发生前即使把处理器资源分配给该进程,也无法运行。[3]
Process moves into the waiting state if it needs to wait for a resource, such as waiting for user input, or waiting for a file to become available.
*如果计算机有多个cpu,或者一个cpu中有多核(multi cores),那么我们就可以让多个进程真正的同时运行(simultaneously),每个进程的状态转变也遵循上面的规律。
Process Scheduling
什么时候进程被从cpu上移开,接下来哪个进程会被从queue里拿出来,放在cpu上执行是有一定规律的,这个规律取决于该操作系统用的什么scheduling algorithm。对于multiprogramming operating system来说,process scheduling是必不可少的一部分。
Process Scheduling Queues
The Operating System maintains the following important process scheduling queues −
Job queue− This queue keeps all the processes in the system.
Ready queue− This queue keeps a set of all processes residing in main memory, ready and waiting to execute. A new process is always put in this queue.
Device queues− The processes which are blocked due to unavailability of an I/O device constitute this queue.
程序用于描述进程要完成的功能,是控制进程执行的指令集;数据集合是程序在执行时所需要的数据和工作区;程序控制块(Program Control Block,简称PCB),包含进程的描述信息和控制信息,是进程存在的唯一标志。
进程具有的特征:
动态性:进程是程序的一次执行过程,是临时的,有生命期的,是动态产生,动态消亡的;
并发性:任何进程都可以同其他进程一起并发执行;
独立性:进程是系统进行资源分配和调度的一个独立单位;
结构性:进程由程序、数据和进程控制块三部分组成。
Most modern operating systems support multiple concurrent users(并行用户,即同一个时间有多个user), and by implication multiple concurrent processes(同一时间有多个进程). In such systems, the same program may be executed more than once at the same time, leading to several different processes all associated with the same program file. In the example, two of the users are executing the chrome command, resulting in two chrome processes.
现在很多操作系统通过实现同个时间多个进程同时运行,来支持多并行用户。在这样的系统里,可能有些程序会被同个时间多次执行,例如,两个用户同时都在执行chrome command,就会开启两个chrome 进程。
Most of the code that is executed in a process is that of the actual program. This is known as "user code". The user code of each process is well insulated from that of other processes. However the process may need the operating system to do some work for it. The process makes a call into the operating system, causing operating system code to run on its behalf. When this happens the process is said to be running in kernel mode.
一般来说, 应用程序是在User mode中执行程序,普通的数值计算或变量指派都可以在此模式完成,但是若要执行一些危及系统安全的指令(例如对磁盘写入资料),而这些指令是不准在User mode中执行的,强要执行那些特殊指令只会让系统给你一个错误信息而已,应用程序必须呼叫一些OS定义好的函数才能达成那些功能,例如printf(),这些OS事先定义好的函数我们称为system call(系统调用)。
当应用程序执行了system call,并不是傻傻地让应用程序想做什么就做什么,他们首先会严密地检查这个调用的应用程序的权限以及操作的内容(是否读取不属于自己的存储器范围,是否读写没有权限读写的文件,是否想把资料往错误的装置送过去......),若是有任何错误,system call将会停止执行并回传一个错误代号,让应用程序知道自己错在何处。相反地一切检查都没问题,system call将会通知CPU进入Kernel mode,并依照应用程序送过来的参数执行特权指令。当特权指令执行完毕,system call将会通知CPU返回User mode,并回到应用程序中。
为了不让程序任意存取资源,大部分的CPU架构都支持Kernel mode与User mode两种执行模式。当CPU运行于Kernel mode时,任务可以执行特权级指令,对任何I/O设备有全部的访问权,还能够访问任何虚拟地址和控制虚拟内存硬件;这种模式对应x86的ring0层,操作系统的核心部分,包括设备驱动程序都运行在该模式。当CPU运行于User Mode时,硬件防止特权指令的执行,并对内存和I/O空间的访问操作进行检查,如果运行的代码不能通过操作系统的某种门机制,就不能进入内核模式;这种模式对应于x86的ring3层,操作系统的用户接口部分以及所有的用户应用程序都运行在该级别。
*kernel:内核。kernel可以看作一台车子的发动机,如果说操作系统是一台车子的话。