Transformation of the PCB [Posts] Detailed process state

Conversion process state and PCB Detailed

 

Skip to main content

There before Bowen processes and threads differences were described in detail and explains the purpose of the introduction process is to enable concurrent program execution, and can be described and control program executed concurrently. This blog, we've come to the state of the process to carry out a detailed analysis of the conversion. In addition to this paper, the state transition process, also interspersed with instructions to control the process.

Note: this post is the basic unit in the process are in the process of traditional operating systems, both the OS resource allocation, the OS is a separate unit of scheduling.

Before we start, we have processes and procedures to carry out a comparison:

  1. Program is static, and the process is dynamic.
  2. A software program can be used as long-term resources exist, but there is a certain process life cycle. Program is permanent, the process is temporary.
  3. Process more accurately describe concurrency, and the program can not;
  4. Processes with other processes to create functional, but the program does not;
  5. The same program can correspond to multiple processes;
  6. Process is an independent unit of resource allocation and scheduling systems, and the program is not.

Thus, from the above we can get some of the features of the process:

  1. Dynamic (generated by the process created by the scheduling and execution, and die by the withdrawal);
  2. Concurrency (stored in memory with a plurality of processes may be concurrently executed);
  3. Independence (independent unit of resource allocation and scheduling);
  4. Asynchrony (implementation process in an unpredictable speed forward).

Three basic state

Above we talked about, is a process life cycle, generated by the creation, performed by the scheduling, the revocation and die, and the memory can exist in multiple processes, multiple processes to share system resources during concurrent execution because the resource contention or other reasons, resulting in the process of presenting intermittent (asynchronous) during operation, therefore, the process will have multiple states during its life cycle.

First of all we are talking about are three basic states: ready state, execution state, blocking state. The three basic states there will be in each OS, it is also called the basic state, the following definitions are given to three basic states:

  1. Ready (Ready) state: we can see by Ready, in this state is already in the process is ready to run, and this time the process has allocated all the necessary resources in addition to the CPU, just get CPU (CPU how to allocate, Please pay attention to the follow-up blog post about the processor scheduling), it can be executed immediately. Word to describe the process in a ready state: everything is ready, only a strong CPU. Further there is a concept of the ready queue, a ready state in the process of this queue, waiting scheduler scheduling (allocation CPU).
  2. Execution (Running) states: Running, running, the process in this state is the CPU has received and is being implemented. For this state, in a single CPU OS, the same time only one process is in this state, and in the multi-CPU OS, you can have multiple (not to exceed the number of CPU) process while being executed.
  3. Blocking (Block) states: blocking, Baidu paraphrase: barriers can not be, can not be unblocked. Process in this state is because the event (I / O requests, application cache miss, waiting to receive data, etc.) in the process of implementation need to wait for some occurrence, temporarily unable to continue. We know that in a multi-batch system, in which case the process needs to release the processor, and select a new process is ready to perform. The ready queue corresponding to the process it is blocked in the blocking queue, some of the OS, for purposes of efficiency, depending on the cause of the obstruction, there will be more congestion queue.

Three basic process state transition diagram
The figure is a state transition diagram of the three basic processes, the conversion process will state often occurs in the course of operation. We can see from the figure, the ready state and the implementation of state can be converted to each other, but the execution state to the blocking state is one-way, because even if it is blocked process "smooth", but because the processor has been assigned to the other processes, the state of the process can only be switched to the ready state, and also the individual, because they can only wait for events to occur in the implementation, because each state switch occurs explained below:

  1. Ready state -> state execution: process gets CPU (selected scheduler);
  2. Execution state -> blocking state: the shared resource request to the OS (mutually exclusive synchronization) failed, waiting for some operation is completed, the new data has not been reached (I / O operation), waits for the arrival of the new task;
  3. Blocking state -> blocking state: the above four categories of wait events;
  4. Execution state -> ready state: allocated to the time slice execution process is completed (round robin algorithm), reaches a high priority process (preemptive scheduling).

Creating and final states

We know that when the process is created, it needs to allocate all the necessary resources in addition to the CPU, however, at this time if the OS can not allocate resources because of lack of resources to the process, then the process should be in what state? To meet these problems and meet the integrity of the process and enhance the flexibility of the state administration, usually for the process to add two common state in the OS: Creating and final states. Define two states are given below:

  1. Create a state: the process of creating the process, the resources required still can not be met, this time to create a work has not been completed, the process can not be scheduled for execution, this time is in the process of state creation.
  2. End state: the process finishes running normally or error caused the process to terminate the appearance, or OS is the end, or the parent process is terminated, then enter the final state. Into the final state of the process will no longer perform, but in the OS remains a record, which preserves the status code and timing statistics, wait for other processes to collect, once resources are collected to complete, OS removes the process immediately.

Five basic state transition process

After includes creating and final states, the state of the conversion process as shown above. We can see from the figure, the transition state more than the creation of state to the ready state, execution state to terminate the state, two states reason to switch is given below:

  1. Creating state -> ready state: the resources needed to get the process of distribution;
  2. The implementation of state -> terminate state: running or end of the process error caused the process to terminate the appearance, or OS is the end or is terminated parent (or other termination rights process).

The following process for process creation and termination to be a clarification. The first is to create a process, the process is to create a more complex process, a process first need to apply for a blank process control block (Process Control Block, PCB, speaking in detail later), and to fill in for the PCB and control information management processes; and allocating the necessary resources for the process run; and finally the process into the ready state and inserted into the ready queue. Followed by termination of the process is divided into termination of the process in two steps: first, waiting for OS deal with the aftermath (stop the execution, the process is terminated descendants, to return resources, etc.), then it will be cleared PCB, PCB space and returned to the system.

​ 从上述进程的创建和终止的过程中,我们可以得知,创建态就是处于进程创建的第一步完成时的状态,终止态就是进程无法被调度或执行,但是还需等待别的进程来收集信息,也就是终止态同样处于第一步完成的状态,只有当进程归还了PCB后,才算是真正的被OS删除,生命周期才算结束。

挂起、激活操作

​ 在部分OS中,为了系统和用户观察和分析进程的需要,还引入了一个对进程的重要操作—挂起(Suspend),当对某个进程执行此操作是时,该进程将会被挂起,此时意味着该进程需要释放内存,调至到硬盘中(外存),也意味着此时该进程处于静止状态,无法接受调度或者执行。当该进程被执行激活(Active)操作时,才可将该进程从外存中重新导入到内存中。

​ 首先我们来分析下进程被挂起的原因:

  1. 终端用户的请求:当终端用户在自己的程序运行期间发现有可疑问题时,希望暂停自己的程序的运行,使之停止下来,以便用户研究其执行情况或对程序进行修改;
  2. 父进程的请求:有时父进程希望挂起自己的某个子进程,以便考察和修改子进程,或者协调各子进程间的活动;
  3. 负荷调节的需要:当实时系统中的工作负荷较重,已可能影响到对实时任务的控制时,可由系统把一些不重要的进程挂起,以保证系统能正常运行;
  4. 操作系统的需要:操作系统有时希望挂起某些进程,以便检查运行中的资源使用情况或进行记账。

​ 引入挂起之后,系统中的就绪状态和阻塞状态分别变为两种状态,这四种状态为:活动就绪、静止就绪、活动阻塞、静止阻塞,下面分别对这四种状态进行说明:

  1. 活动就绪(Readya):等价于就绪态,此时进程可以接受调度,获得处理机后可直接转为执行态;
  2. 静止就绪(Readys):进程被调至外存,无法接受调度;
  3. 活动阻塞(Blockeda):等价于阻塞态,当等待的事件发生后可以由活动阻塞变为活动就绪状态;
  4. 静止阻塞(Blockeds):进程仍然可以等待事件,当事件发生后,状态由静止阻塞变为静止就绪状态。

After the introduction of seven states suspend the process of conversion
​ 引入挂起操作后,OS中的状态变为7种,其中部分进程状态的变化如下:

  1. 活动就绪—>静止就绪:该进程被执行挂起原语Suspend;
  2. 活动阻塞—>静止阻塞:该进程被执行挂起原语Suspend;
  3. 执行态—>静止就绪:该进程被执行挂起原语Suspend;
  4. 静止就绪—>活动就绪:该进程被执行挂起原语Active;
  5. 静止阻塞—>活动阻塞:该进程被执行激活原语Active;
  6. 静止阻塞—>静止就绪:进程等待的事件发生;
  7. 活动阻塞—>活动就绪:进程等待的事件发生;
  8. 创建态—>活动就绪:在当前OS的性能和内存的容量均允许的情况下,完成进程的创建工作(三步);
  9. 创建态—>静止就绪:当前OS的当前资源状况和性能不允许,不分配给新建进程所需的资源(创建工作只完成第一步),此时进程处于静止就绪状态,被调至到外存中,不参与调度,此时创建工作尚未完成。

​ 从上面的状态我们可以看到,处于执行态的进程一样会被挂起,而且引入了挂起操作后,进程不会长期的处于创建状态,如果能分配到资源,状态会变为活动就绪态,如果无法分配资源,则进入静止就绪状态,当资源充足后,在将进程调入内存并为之分配资源,完成创建工作。

进程控制块PCB

​ 上面讲了这么多的进程的状态的切换,那么OS到底是根据什么来进行进程的控制和管理呢?比如进程的状态信息保存在哪里、进程对应的程序和数据是如何保存的、进程是如何保存和恢复CPU现场信息(上下文切换)的,还有其他等等操作是如何实现的?其实这些操作都需要依托于一种数据结构—进程控制块PCB。PCB是OS为了系统的描述和管理进程的运行,在内核中为每个进程专门定义的一个数据结构。PCB作为进程实体(程序、数据、PCB)的一部分,记录了OS所需的用于描述进程当前情况以及管理进程运行的全部信息,是OS中最重要的记录型数据结构。

​ **PCB的作用是使一个在多道程序环境下不能独立运行的程序(含数据),成为一个能独立运行的基本单位,一个能与其它进程并发执行的进程。**因此PCB的作用非常重要,我们讲进程的状态切换是必须要讲PCB的。首先,我们对PCB中存储的信息进行介绍:

  1. 进程标识符:用于唯一地标识进程。一个进程通常包含两种标识符:外部标识符和内部标识符。外部标识符一般由创建者(用户)提供,用来方便记忆;内部标识是为了方便OS对进程的使用,通常是Linux系统中查看到的Pid;
  2. CPU状态:CPU状态信息也称处理机的上下文,主要由CPU的各种寄存器(通用寄存器、指令计数器、程序状态字PSW、用户栈指针)中的内容组成的。进程在执行的过程中,正在处理的许多信息都放在寄存器中,如果需要发生切换,这些信息就需要保存在该进程的PCB中,以便可以再次执行时可以快速的恢复CPU的状态;
  3. 进程调度信息:在OS进行调度(从就绪队列中选取进程分配CPU)时,必须要知道进程的状态和相关的调度信息,主要包括:进程状态、优先级、进程调度所需的其他信息(等待时间、已经执行的时间等)、事件(等待发生的时间,即阻塞的原因);
  4. 进程的控制信息:只用于进程控制所必需的信息,主要包括:程序和数据的地址(所在内存或外存的首地址)、进程同步和通信机制、资源清单(进程运行期间所需的全部资源,CPU除外,还有一张已经分配给该进程的资源清单,主要用于避免死锁)、链接指针(指向本PCB所在队列中的下一个进程的PCB的首地址)。

​ 通过PCB所保存的信息,我们是不是也就知道了上面讲到的问题(进程如何进行控制、程序和数据如何保存、CPU上下文如何切换和恢复)的答案了,都是需要使用PCB中保存的信息。下面我们根据PCB中存储的信息,来对PCB作用做一个具体的分析:

  1. As a sign of stand-alone basic unit: after when a program (containing data) equipped with PCB, says he became a process, a legitimate and can operate independently of a basic unit in a multi-channel programming environment, it has made a right OS services. This is why when the system creates a process that needs to be created for a PCB, and with one of the binding process, the system is to perceive the process according to the PCB, PCB revoked returned to the OS, the process will disappear along a;
  2. You can achieve intermittent operation mode: a basic characteristic of which is the process - asynchronous, since the PCB holds context information during process execution on the CPU, so when the process is scheduled to perform again, you can quickly restore its CPU-site information.
  3. Information management needs of process: process throughout the life cycle of, OS are based on control and management of PCB implementation process.
  4. Information needed to process scheduling: OS PCB according to the state stored in the process information to be inserted into a corresponding succession queue, and performs scheduling according to the stored priority PCB, waiting for the event and the like;
  5. Achieve synchronization and communication with other processes: PCB and having a queue pointer for regional communication process communication or the like.

Through the above description, we have seen important point of the PCB, although a process of PCB holds so much information, but a PCB size is about 1KB, a small amount of body can also ensure that the kernel can be stored in a certain the number of the PCB.


Went to the separator below concludes this article, this article is to collate all the bloggers themselves and with their own understanding summarize, if there is something wrong, please correct me criticism, of course, if you have any doubts you can comments.

The full text is more than 4700 characters, the original is not easy, at the time of writing this blog, really put a lot of effort, look at a lot of documents, if this article help you, please leave a like, table support.

Guess you like

Origin www.cnblogs.com/jinanxiaolaohu/p/11777030.html