進程狀態與轉換


本文主要講述進程相關狀態及其轉換。本來是要翻譯的,但由於某些好處,熱愛計算機的人應該明白,本文還是直接上英文吧。相關術語早晚應該都可以知道的,因此可以現在查閱查閱。

Introduction of Process Management

Program vs Process

A process is a program in execution. For example, when we write a program in C or C++ and compile it, the compiler creates binary code. The original code and binary code are both programs. When we actually run the binary code, it becomes a process.

A process is an ‘active’ entity, as opposed to a program, which is considered to be a ‘passive’ entity. A single program can create many processes when run multiple times; for example, when we open a .exe or binary file multiple times, multiple instances begin (multiple processes are created).

What does a process look like in memory?

process

Text Section:A Process, sometimes known as the Text Section, also includes the current activity represented by the value of the Program Counter.
Stack: The Stack contains the temporary data, such as function parameters, returns addresses, and local variables.
Data Section: Contains the global variable.
Heap Section: Dynamically allocated memory to process during its run time.
Refer this for more details on sections.

Attributes or Characteristics of a Process

A process has following attributes.

1. Process Id:    A unique identifier assigned by the operating system
2. Process State: Can be ready, running, etc.
3. CPU registers: Like the Program Counter (CPU registers must be saved and 
                  restored when a process is swapped in and out of CPU)
4. Accounts information:
5. I/O status information: For example, devices allocated to the process, 
                           open files, etc
6. CPU scheduling information: For example, Priority (Different processes 
                               may have different priorities, for example
                               a short process may be assigned a low priority
                               in the shortest job first scheduling)

All of the above attributes of a process are also known as the context of the process.
Every process has its own program control block(PCB), i.e each process will have a unique PCB. All of the above attributes are part of the PCB.

States of Process:

A process is in one of the following states:

1. New: Newly Created Process (or) being-created process.

2. Ready: After creation process moves to Ready state, i.e. the 
          process is ready for execution.

3. Run: Currently running process in CPU (only one process at
        a time can be under execution in a single processor).

4. Wait (or Block): When a process requests I/O access.

5. Complete (or Terminated): The process completed its execution.

6. Suspended Ready: When the ready queue becomes full, some processes 
                    are moved to suspended ready state

7. Suspended Block: When waiting queue becomes full.

process-states

Context Switching

The process of saving the context of one process and loading the context of another process is known as Context Switching. In simple terms, it is like loading and unloading the process from running state to ready state.

When does context switching happen?

  1. When a high-priority process comes to ready state (i.e. with higher priority than the running process)
  2. An Interrupt occurs
  3. User and kernel mode switch (It is not necessary though)
  4. Preemptive CPU scheduling used.

Context Switch vs Mode Switch

A mode switch occurs when CPU privilege level is changed, for example when a system call is made or a fault occurs. The kernel works in more a privileged mode than a standard user task. If a user process wants to access things which are only accessible to the kernel, a mode switch must occur. The currently executing process need not be changed during a mode switch.
A mode switch typically occurs for a process context switch to occur. Only the kernel can cause a context switch.

CPU-Bound vs I/O-Bound Processes:

A CPU-bound process requires more CPU time or spends more time in the running state.
An I/O-bound process requires more I/O time and less CPU time. An I/O-bound process spends more time in the waiting state.

Exercise:

  1. Which of the following need not necessarily be saved on a context switch between processes? (GATE-CS-2000)
    (A) General purpose registers
    (B) Translation lookaside buffer
    © Program counter
    (D) All of the above

Answer (B)

Explanation:
In a process context switch, the state of the first process must be saved somehow, so that when the scheduler gets back to the execution of the first process, it can restore this state and continue. The state of the process includes all the registers that the process may be using, especially the program counter, plus any other operating system-specific data that may be necessary. A translation look-aside buffer (TLB) is a CPU cache that memory management hardware uses to improve virtual address translation speed. A TLB has a fixed number of slots that contain page table entries, which map virtual addresses to physical addresses. On a context switch, some TLB entries can become invalid, since the virtual-to-physical mapping is different. The simplest strategy to deal with this is to completely flush the TLB.

  1. The time taken to switch between user and kernel modes of execution is t1 while the time taken to switch between two processes is t2. Which of the following is TRUE? (GATE-CS-2011)
    (A) t1 > t2
    (B) t1 = t2
    (C ) t1 < t2
    (D) nothing can be said about the relation between t1 and t2.

Answer: ©
Explanation: Process switching involves mode switch. Context switching can occur only in kernel mode.

References:

http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/3_Processes.html
http://cs.nyu.edu/courses/spring11/G22.2250-001/lectures/lecture-04.html

Process Schedulers in Operating System

There are three types of process scheduler.

  1. Long Term or job scheduler: It brings the new process to the ‘Ready State’. It controls Degree of Multi-programming, i.e., number of process present in ready state at any point of time.It is important that the long-term scheduler make a careful selection of both IO and CPU bound process.

  2. Short term or CPU scheduler: It is responsible for selecting one process from ready state for scheduling it on the running state. Note: Short-term scheduler only selects the process to schedule it doesn’t load the process on running.
    Dispatcher is responsible for loading the process selected by Short-term scheduler on the CPU (Ready to Running State) Context switching is done by dispatcher only. A dispatcher does the following:

- Switching context.
- Switching to user mode.
- Jumping to the proper location in the newly loaded program.
  1. Medium-term scheduler: It is responsible for suspending and resuming the process. It mainly does swapping (moving processes from main memory to disk and vice versa). Swapping may be necessary to improve the process mix or because a change in memory requirements has overcommitted available memory, requiring memory to be freed up.

States of a Process in Operating Systems

States of a process are as following:
在這裏插入圖片描述

  • New (Create) – In this step, the process is about to be created but not yet created, it is the program which is present in secondary memory that will be picked up by OS to create the process.
  • Ready – New -> Ready to run. After the creation of a process, the process enters the ready state i.e. the process is loaded into the main memory. The process here is ready to run and is waiting to get the CPU time for its execution. Processes that are ready for execution by the CPU are maintained in a queue for ready processes.
  • Run – The process is chosen by CPU for execution and the instructions within the process are executed by any one of the available CPU cores.
  • Blocked or wait – Whenever the process requests access to I/O or needs input from the user or needs access to a critical region(the lock for which is already acquired) it enters the blocked or wait state. The process continues to wait in the main memory and does not require CPU. Once the I/O operation is completed the process goes to the ready state.
  • Terminated or completed – Process is killed as well as PCB is deleted.
  • Suspend ready – Process that was initially in the ready state but were swapped out of main memory(refer Virtual Memory topic) and placed onto external storage by scheduler are said to be in suspend ready state. The process will transition back to ready state whenever the process is again brought onto the main memory.
  • Suspend wait or suspend blocked – Similar to suspend ready but uses the process which was performing I/O operation and lack of main memory caused them to move to secondary memory.
    When work is finished it may go to suspend ready.

CPU and IO Bound Processes:

If the process is intensive in terms of CPU operations then it is called CPU bound process. Similarly, If the process is intensive in terms of I/O operations then it is called IO bound process.

Types of schedulers:

  1. Long term – performance – Makes a decision about how many processes should be made to stay in the ready state, this decides the degree of multiprogramming. Once a decision is taken it lasts for a long time hence called long term scheduler.
  2. Short term – Context switching time – Short term scheduler will decide which process to be executed next and then it will call dispatcher. A dispatcher is a software that moves process from ready to run and vice versa. In other words, it is context switching.
  3. Medium term – Swapping time – Suspension decision is taken by medium term scheduler. Medium term scheduler is used for swapping that is moving the process from main memory to secondary and vice versa.

Multiprogramming – We have many processes ready to run. There are two types of multiprogramming:

  • Pre-emption – Process is forcefully removed from CPU. Pre-emption is also called as time sharing or multitasking.
  • Non pre-emption – Processes are not removed until they complete the execution.

Degree of multiprogramming

The number of processes that can reside in the ready state at maximum decides the degree of multiprogramming, e.g., if the degree of programming = 100, this means 100 processes can reside in the ready state at maximum.

cause

In process states, under what conditions do the following occur: running to ready, running to waiting, and waiting to ready?

  • Running to Ready
  1. When the time slice of the running process expires
  2. Even if the time slice of the running process has not expired and there is a process with higher priority in the ready queue, the running process is preempted.
  3. When an interrupt arrives on the cpu, the process running on the cpu gets preempted.
  4. The transition from running to ready can happen only in preemptive scheduling.
  • Ready to Waiting
  1. Process is waiting for some I/O event to happen
  2. Process is waiting to obtain a contended semaphore
  3. Process is waiting for it’s child to complete/terminate via wait()
  4. Process is waiting for a signal.
  • Waiting to Ready
  1. I/O event has completed
  2. The semaphore on which the process was waiting is free
  3. Child has completed its task
  4. Process has received the signal on which it was waiting.

參考:

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

http://www.sci.brooklyn.cuny.edu/~jniu/teaching/csc33200/files/0924-ProcessConceptAndState.pdf
http://www.personal.kent.edu/~rmuhamma/OpSystems/os.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章