Process description and control operating system Chapter 2 knowledge points summary

Series of articles

Chapter One Operating System Overview
Chapter Two Process Scheduling

Chapter Two Process Description and Control

2.1 What is a process

Program code + related data + program control block PCB

When the processor begins to execute the code of a program, the entity that executes is called the process

2.1.1 Process and Process Control Block PCB

PCB(Process Control Block)

  • PCB is the basis of process interruption, it seems that it has never been interrupted after resuming execution
  • PCB is created and managed by OS
  • Tools that support multiple processes
PCB is a data structure for process management
PCB elements effect
id Identifier
state status
PC Program counter
memory pointer Memory pointer
context Context
I/O status I/O status
…… ……

2.1.2 Process VS program

The program is the script, the process is the performance

Basic characteristics of the process:
  • Dynamic ( essential characteristics ): the process has a life cycle
  • Concurrency ( important feature ): can be advanced with other processes
  • Independence: the address space of each process is independent of each other (logical address)
  • Asynchrony: the process advances at an unpredictable speed
Compared with the program:
1. 进程是正在计算机执行的实例
2. 进程=PCB+程序代码+数据
3. 程序是静态的,没有生命周期
4. 进程与程序并无一一对应关系,如一个dad()程序中涉及多个PV进程
5. 引入进程的概念是为了容易正确解释多道程序并发

2.2 Process status

Process trajectory

The sequence of instructions to be executed for the process, describing the behavior of a single instruction (guide)

Dispatcher

Switch the cpu from one process to another
Figure 1 Dispatcher

2.2.1 Two-state model

Figure 2 Two-state model

2.2.2 Process creation and termination

Process creation

The OS creates a PCB for managing the process.
OS allocates memory for the process

Reason for creation
  • New assignment <- old completed
  • User login <- literally
  • User requests service <- such as requesting printing
  • Process spawn <- such as fork()
Related

Process spawning: the process explicitly requests to create a process (child process)
parent process: the original process
child process: the new spawned process

Reason for termination
  • Homework completed
  • timeout
  • No memory available
  • No access
  • Out of available memory
  • Access to protected files
  • Not operable
  • Timeout (can't wait)
  • I/O failure such as read and write failure
  • ……
  • The parent process is terminated
Five-state model

Figure 3 Five-state model

Queuing model
  1. Single blocking queue: All blocking queues are in the same queue, and the entire queue needs to be traversed after the event.
  2. Multi-blocking queue: different blocking conditions correspond to different queues.
    Similarly, multiple ready queues can be maintained according to different priorities

2.2.4 Processes that are suspended

To solve the problem of limited memory, part or all of the temporarily blocked processes are swapped out to the suspension queue of the external memory

After suspension

  • OS schedules new processes
  • Fetch a new process from the suspended queue
Hang

Processes that are blocked, ready or even executing in the memory are released to the external memory and no longer participate in cpu competition

Seven-state model

Figure 4 Seven-state model

Suspended process

Can not run immediately, in external storage, will not swap in by itself, may be waiting or not waiting for conditions

Reason for suspension
  • Exchange (to make room)
  • User request
  • Parent process request
  • OS request
  • Timing: execute regularly, hang while waiting

2.3 Process description

2.3.1 OS control structure

OS uses tables to maintain and manage resources

  • Memory table: used to track memory and external memory

  • Memory and external storage allocated to the process

  • Store protected information out of memory

  • Manage external storage information

  • I / O table

  • I/O status (available/allocated to XX process)

  • I/O data source and destination memory

  • File table

  • Does the file exist

  • File storage location

  • Current state

  • Other attributes

  • Process table characteristics
  • Used to manage the process

  • There are indirect references to memory, I/O\files

  • Itself can be accessed by the OS (nonsense -_-)

2.3.2 Process control structure

Process image: a collection of program, data, stack (save the calling address) and PCB
Information in PCB
  • Process identification information: the digital identifier stored in the process
    • Process id
    • Parent process id
    • User id
  • Processor status information: cpu register content
    • User visible register
    • Control and status registers (PC, PSW, interrupt enable/disable flag, execution mode)
    • Stack pointer (point to the top of the stack of the process)
  • Process control information: other information required by the operating system to schedule the process
    • Process state state: "running"......
    • Process priority priority
    • Event ID: Process waiting for event
    • Scheduling related information: different scheduling algorithms may require space to store data
    • Data structure: used to support queues, rings, pointers
    • Process communication: communication between unrelated processes
    • Process privilege
    • Storage management: pointer to virtual memory segment table or page table
    • Resource ownership and usage

2.4 Process control

2.4.1 Execution Mode

Kernel mode function
  • Process management
  • PCB
  • create、delete、schedule
  • shift
  • Synchronize
  • Storage management
  • distribution
  • exchange
  • Page segmentation
  • I/O device management
  • I/O buffer
  • Allocate I/O
Kernel support function
  • Interrupt handling
  • Clock interrupt
  • Bookkeeping
Two modes supported by OS and their characteristics
User mode
  • Low priority
  • User program execution
System mode/Kernel mode
  • More priority
  • Run in the kernel
  • Privileged instruction
  • Privileged memory space access

Note: The current mode is identified by a special bit in PSW, and the two modes are also to protect the OS data structure from being tampered with

2.4.2 Process creation

OS behavior:
allocate id -> allocate space -> init pcb (id, stack, state, priority...) -> insert ready/suspend queue -> initialize other data structures

2.4.3 Process switching

Interrupt
  • Normal interrupt
  • timeout: the process time slice is exhausted
  • I/O interrupt: I/O is ready, OS decides which process to wake up
  • Memory failure (page missing): blocking the current process
  • Trap
  • Process error or exception
Reasons for process switching
  • Normal interrupt: reaction to external events
  • Pitfall: handling exceptions
  • System call: call the kernel program
Interrupt process

save context(P1)–>renew pcb(P1)–>move pcb(P1) in queue–>change process(P1->P2)–>renew pcb(P2)–>renew storage、data structure–>reload context(P1)

Note: Interruption does not necessarily lead to process switching
  • An example: After the I/O is interrupted, the system decides to continue execution by the previous process (for example, because the process has a higher priority)
Mode switch

User mode <==> Kernel mode

the reason
  • System call: call kernel service
  • Interrupt: call interrupt handler
Mode switching and process switching

Mode switching does not necessarily lead to process switching, process switching must be mode switching

2.4.4 OS execution

  1. Kernel without process: the kernel is independent of the process (separate kernel)
  2. The operating system in the user process: the process and the kernel function are executed simultaneously (OS function execute with process)
  3. Process-based operating system: kernel function executes with separate process at the same level (OS function execute with separate process)
    Insert picture description here

2.5 UNIX SVR4 process management

fork() is used to create a process: pid=fork();

Pay attention to three points:
  1. After fork() creates the child process, the parent and child processes continue to run separately
  2. The fork() of the child process returns pid=0, and the fork() of the parent process returns the id of the created child process
  3. The sequence of advancement of the father and son process is unpredictable

Give three examples to help understand:


Example 1: How many hello worlds are output by the following code?

#include <stdio.h>
#include <unistd.h>
int main()
{
    int i;
    for (i=0;i<3;i++)
        fork();
    printf("hello, world\n");
    return 0;
} 

A fork() is understood as: the old process remains unchanged, continues to run, a new process is generated
3 fork(), there will be 2^3 processes, so 8 hello world will be printed


Example 2: What is the output of the following code?

void main(void) {
int i;
static char buffer[10];
if (fork()==0)     
   strcpy(buffer, “Child\n”);
else
   strcpy(buffer, “Parent\n”);
 
for (i=0; i < 5; ++i) {
  sleep(1);
  write(1, buffer, sizeof(buffer));
}
} 

5 Child, 5 Parent, but the order is not necessarily


Example 3: What is the output of the following code?

#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
int global = 4;
void main(void) {
int  pid; 
int vari = 5;
printf(“before fork\n”);
if ((pid = fork()) < 0) {
      printf(“fork error\n”);
      exit(0);
  } else if (pid == 0) {
	  global++;
	  vari--;
    }
 printf(“global=%d,vari=%d\n”, global, vari);
}

Parent process: global=4, vari=5
Child process: global=5, vari=4

2.6 Thread

The process is divided into two components
  • The basic unit of scheduling
  • Independent unit of resource

The thread is independent as the basic scheduling unit and is scheduled by the OS

Multithreading

The ability to support multiple concurrent paths within a single process
Multithreading

Single thread

A process has only one thread
Single thread

A thread is an entity in the process, a unit of independent scheduling and dispatch

2.6.1 Process vs Thread

A process can have one or more threads
Suspend or terminate a process = Suspend or terminate all threads inside

Thread
  • Execution state state: derived, blocked, unblocked, ended
  • Context
  • Stack
  • Local variable
  • Shared resources and memory
    Single threadMultithreading

Multi-threaded has one more tcb (threaded "pcb") than single-threaded

Thread advantages
  1. Short creation time
  2. Short time-consuming termination
  3. Short switching time (in the same process)
  4. Improve the communication efficiency between different execution programs
Thread synchronization
  • All threads in a process share the process data and space
  • Modification of data by one thread will affect other threads

2.6.2 Thread Classification

User-Level Thread ULT (User-Level Thread)
  • The thread core is not visible and managed by the application
  • The process is blocked, the thread is not necessarily blocked; the process is running, the thread may also be blocked
  • advantage
  • Thread switching does not need to switch kernel mode
  • Scheduling strategy can be customized by application
  • Can run on any kind of OS
  • Disadvantage
  • All threads of the process will be blocked when the thread system is called
  • Not suitable for multi-core systems
  • How to improve the disadvantages
  • Jacketing technology: replace system calls by application-level calls
    ULT
Kernel level thread KLT (Kernel level Thread)
  • The kernel manages threads, the application does not manage threads
  • advantage
  • Multiple threads can be assigned to multiple CPUs
  • When a thread is blocked, other threads in the same process will not be blocked
  • The kernel itself can also use multithreading
  • Disadvantage
  • Thread switching requires mode switching

KLT

Hybrid method (ULT+KLT)
  • Thread is created at user level
  • Thread scheduling and synchronization are completed by the application
  • One or more user threads are mapped to kernel threads (there are fewer kernel threads)
    MIX

Guess you like

Origin blog.csdn.net/qq_44722674/article/details/111189670