Operating System Principles Chapter 3 Processes

Operating System Principles for Undergraduate Students Learning Record
Learning Record Family Bucket

3.1 Process concept

The operating system executes various programs

Batch processing system - job (job)

Time-sharing system - user program or task (Task)

Job: A set of computational steps assembled to run as a whole

task: process or thread

Jobs, Tasks, and Processes are basically interchangeable, but not with programs

Process: One run of a program on a data set is a program in execution, three ones are very important

3.2 Processes in memory

Processes include:

Code

Current activity:

  • Program Counter (PC) - Points to the address of the current instruction to be executed
  • Stack: store temporary data such as function parameters and temporary variables
  • Data (Data): global variables, processed files
  • Heap: dynamic memory allocation

3.2.1 The difference and connection between process and program

  • A process is an instance of a program, an execution of a program
  • A program can correspond to one or more processes, and a process can correspond to one or more programs
  • A program is the code portion of a process
  • Processes are active entities and programs are static (passive) entities
  • Process in memory, program in external memory

3.2.2 Process status

When the process is executed, the state changes

  • New (new): When creating a process
  • running: the instruction is being executed
  • Waiting (blocking): the process waits for something to happen
  • Ready: The process is waiting for a processor to be allocated
  • terminated: the process is finished

3.2.3 State transition

3.2.4 Process Control Block (PCB)

PCB can be understood as a structure in C language

The PCB contains information related to the process, including:

  • process status
  • program counter
  • CPU registers
  • CPU scheduling information
  • memory management information
  • Accounting information
  • I/O status information

in Windows asKPROCESS

3.2.5 CPU switching between processes

context switch

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-NSd06KJ9-1641211595071) (E:\Documents and PPT\Junior Course Study\Operating System\Pictures\Third Chapter\Process Switching.jpg)]

3.3 Process operation

The parent process creates child processes, which are created in turn to form a process tree

Resource Sharing: Three Cases

  • Parent process and child process share all resources
  • Child processes share a subset of the parent's resources
  • No resource sharing between parent process and child process

implement:

  • Parent process and child process execute concurrently
  • The parent process waits until the child process terminates

3.3.1 Process Creation

address space:

  • child copying parent
  • The child has a program called into

UNIX example:

  • The fork system call creates a new process
  • After fork replaces the memory space of the process with a new program, the exec system call is used

Atomic operation (interruptions are not allowed during the operation)

3.3.2 Process termination

The process executes the last item and exits (exit)

The parent process can abort the execution of the child process (abort)

3.3.3 Parent process and child process

fork() return value to differentiate

pid_tfork() return type ispid_t

The return value is 0, the child process is created successfully

The return value is greater than 0, the parent process

If the return value is less than 0, the creation failed

3.3.4 Waiting

The parent process blocks until the child process completes the task

call wait or waitpidsystem call

3.3.5 Primitives for Process Operations

Process creation is a primitive operation

The procedures or functions that the kernel or microkernel provides out-of-core calls are called primitives.

A primitive is a program written with machine instructions to complete a specific function, and no interruption is allowed during execution.

The primitive is the core of the operating system . It is not composed of a process but a group of program modules. It is an integral part of the operating system. It must be in the management state .

( a machine state , the program executed under the management state can execute privileged and non-privileged instructions, usually defined as the state of the operating system) to execute, and

And it is resident in memory, and some systems do not run in managed mode.

3.4 Inter-process communication

3.4.1 Co-process

Independent process: does not affect the execution of another process or be affected by the execution of another process

Co-process: may affect the execution of another process or be affected by the execution of another process

Advantages of process coordination

  • Information Sharing
  • accelerated computing
  • Modular
  • convenient

3.4.2 Interprocess communication

Two basic modes:

Shared memory and message passing mechanism

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-BBGPtA7f-1641211595075) (E:\Documents and PPT\Junior Course Study\Operating System\Pictures\Third chapter\Shared Memory and Messaging.png)]

3.4.3 Shared memory

fastest communication speed

A block of memory is shared between multiple processes

Communication is controlled by the application itself

Generally used for big data communication

Means of realization:

  • file mapping
  • map
  • clipboard

There is a problem: the need for access control between processes

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-VXu02nrG-1641211595077) (E:\Documents and PPT\Junior Course Study\Operating System\Pictures\Third Chapter\Shared Memory.png)]

A specific example is the process problem of producers and consumers

3.4.4 Message Passing

Application of Message Passing in Microkernel

Remote communication cannot use memory sharing (the most direct communication between two different computers)

Two operations:

  • Send a message: send(message)
  • Receive message: receive(message)

If P and Q want to communicate, they need:

  • Establish a communication connection
  • Exchange messages via send/receive

Realization of communication connection

  • Physical (eg, shared memory, hardware bus)
  • logical (eg, logical properties)

direct communication

Processes must be explicitly named

  • send (P, message) – send a message to process P
  • receive(Q, message) – receive a message from process Q

Characteristics of the communication connection

  • The connection is automatically established
  • A connection is associated with exactly one pair of communicating processes
  • There is a connection between each pair of communicating processes
  • Connection can be unidirectional, but usually bidirectional

indirect communication

In the process of message delivery, the message is delivered through the intermediary mailbox

If the process uses a mailbox to transmit information, then the process name of the sender should be stored in the mailbox

MOOC unit work

1. Why does the process need to have its own PCB? Please give an example to talk about the role of PCB in the process of running.

PCB is an indispensable data structure for a process and the only sign that a process exists in the system.

effect:

  1. The sign of the PCB as a base unit for independent operation
  2. PCB can realize discontinuous operation mode
  3. PCB provides information needed for process management
  4. The PCB provides the information needed for process scheduling
  5. PCB realizes synchronization and communication with other processes

2. Please talk about the relationship between parent process and child process from the perspective of process creation and process termination

create:

The execution sequence of the parent process is prior to the child process. Taking the fork() function under Linux as an example, the child process will execute the logic after the pid = fork() statement

The process ID of the parent process and the child process are different

termination:

A parent process can abort the execution of a child process.

The parent process monitors the child process. During the execution of the child process, the parent process will not die. Call the wait function to check the status of the child process. The parent process will not terminate its own program until the child process terminates.

3. Process state transition diagram

(1) What are the typical events that cause various state transitions?

‎ Ready to run: the currently running process is blocked, and the scheduler chooses a process with the highest priority to occupy the processor

​ Run to ready: the time slice of the current running process has run out or is interrupted

​ Run to blocking: request I/O operation

​ Blocked to ready: I/O operation completes, wakes up by interrupt handler

‌(2) When we observe some processes in the system, we can see that a state transition generated by a certain process can cause another process to make a state transition. Under what conditions does the occurrence of transition 3 immediately cause transition 1 to occur?

‎ The current process is blocked, and the scheduler selects a process with the highest priority to occupy the processor

‌(3) Try to explain whether the following causal transformation will occur:

‌ a) Does transition 2 cause transition 1

​ ‎Can happen. The priority of the currently running process decreases, and the scheduler selects a process with the highest priority to occupy the processor

‌ b) Does transition 3 cause transition 2

​ ‎Impossible. A process occupying the CPU cannot enter two states at the same time; in a single-CPU system, after state 3 occurs, the CPU does not execute the process, so state transition 2 does not occur

‌ c) Does transition 4 cause transition 1

Generally irrelevant, but when the ready queue is empty, after a process is woken up and transferred to the ready queue, the scheduler makes the process occupy the processor

(4) All possibilities that a state transition of one process causes a state transition of another process

run to ready causes ready to run

run to block causes ready to run

run to finish causes ready to run

4. Describe the process by which the kernel performs context switching between two processes.

Take two processes P1and P2as an example, when switching from process P1to process P2:

First save P1the context of the old process PCB1to , which will be PCB1added to the queue

P2Then modify the information of the process control block of the new process PCB2, P2set the address space for the process, use PCB2the information, and execute the processP2

Finally, restore the context of the new process, execute the process P2, and complete the context switch

5. What is direct communication? What is indirect communication? Please give an example of each and discuss their respective strengths and weaknesses

Direct Communication: A process in which two processes communicate in such a way that they directly indicate the receiver or sender of the communication

Example: Communication Link

Advantages: fast communication speed and high efficiency

Disadvantage: only one-to-one communication

Indirect communication: the sending and receiving of messages between two processes through media mailboxes (or ports)

Example: Mailbox media delivery of information

Advantages: one-to-many, many-to-one communication

Disadvantage: Increased system overhead for user space and kernel space communication

Guess you like

Origin blog.csdn.net/weixin_45788387/article/details/122291751