Operating system Learning Notes process

Source: Internet
Author: User
Tags terminates types of tables

Definition of the process

1. A process under implementation;

2. An instance of the program being executed on the computer;

3. Entities that can be assigned to the processor and executed by the processor;

4. An activity unit with the following characteristics: Execution of a set of instruction sequences, a collection of current state and associated system resources;

An entity consisting of a set of elements, including two basic elements: program code and code-related datasets, which is called a process if the processor starts executing the code. At any point in the execution of a process, it can be uniquely represented as the following element:

  identifier : A unique indicator associated with a process used to distinguish between different processes;

  state : Execution state, blocking state, ready state, etc.;

  priority : Priority relative to other processes;

  Program Counter : The address of the next instruction that is executed in the program;

  Memory pointers : Pointers that include program code and process-related data, and pointers to other processes that share memory blocks;

  Context Data : The data of the processor registers when the process executes;

  I/O status information : Includes display of i/0 requests, I/O devices assigned to processes, list of files used by the process;

  Audit Information : including the use of the processor time, the sum of the number of clocks and so on;

This information is stored in a data structure called the Process Control block , which is created and managed by the operating system. The process control block contains sufficient information so that the operating system can interrupt the execution of a process, and when the process is interrupted, the operating system saves the program counter and processor registers to the corresponding location in the process control block, and the process state values (such as blocking states) change. The operating system can then set the state of the other process to run, loading the program counter and process context data of the process to be executed into the processor register so that other processes can start executing. Therefore, a process can be considered to consist of program code , related data , and Process Control blocks .

  

Creation of processes

When a new process needs to be added to the process queue, the operating system needs to establish a data structure to manage the process and assign it an address space in main memory.

Causes for process creation:

  

Termination of the process

Causes the process to terminate:

  

So the above behavior will send a service request to the operating system to terminate the process that made the request.

Five-State model

  Running state : The process is executing. If the computer has only one processor, then up to one process is in this state;

  Ready state : The process is ready to run, waiting for the processor to execute the opportunity;

  blocking states : processes cannot execute before certain events occur, such as waiting for the completion of i/0 operations;

  New State : The completed process has just been created and is not added to the execution list. Usually the process block has been created, but it is not loaded into main memory;

  exit State : For some reason, the execution of the process is terminated.

When the process is in the new state, the program code is not in main memory, nor does it allocate space for the program's data. The operating system may not run the process because of performance, main memory space limitations, process number limits, and so on.

When the process is in the exiting state, the process is no longer executed, but it is not immediately deleted, the table and other information related to the job is temporarily reserved by the operating system, which provides the time for the helper or support program to extract the required information. When this information is extracted, the operating system does not need to retain the data associated with the process, and then removes the process from the system.

Transitions of states

  

New state: Create a new process;

  new state-ready state : When the operating system can accept a process again, the process of a newly created state is converted to a ready state. Most operating systems have a certain limit on the number of active processes, preventing system performance degradation due to excessive active processes;

  ready state-run State : Scheduler or dispatcher is responsible for selecting a ready state of process execution;

  run state, exit State : The running process is completed naturally, or is terminated by the operating system;

  Run state-ready State : (1) The most common cause is the maximum time period for the "non-disruptive execution" of the processor to arrive;

(2) The process may be preempted due to priority reasons. Assuming that process A is executing, the higher-priority process B is in a blocking state. If process B waits for the time that occurs during process a during execution, process B becomes the ready state. Because process B has a higher priority than process A, the processor interrupts process a execution and executes process B;

(3) The process voluntarily releases the control of the processor;

  Block State : If an event requested by the process must wait, it goes into a blocking state. For example:

(1) The process requests a system of services, the system can not immediately service;

(2) The process requests a resource that is not immediately available, such as a file or a shared area in a virtual storage;

(3) or it is necessary to do some initialization work first;

(4) Inter-process communication, one process must wait for the input provided by another process;

  blocked state-ready state : The event that the process waits for has occurred;

  Ready State : The parent process terminates the execution of the child process, or when the parent process terminates, all related child processes are terminated;

  blocking state, exit State : Ibid.

Ready queue and blocking queue

All ready-state processes are placed in the ready queue, and a process execution is selected from the ready queue when the operating system needs to select a process execution. The ready queue can be designed as a simple, first-out approach. When the process is executed by the processor, it is either continued to be placed in the ready queue or in the blocking queue if it is not terminated. When a wait time occurs, the process that waits for the event in the blocking queue is transferred to the ready queue.

When an event occurs, the operating system scans the entire blocking queue and searches for the process that waits for the event. If the number of processes in the blocking queue in the system is large, scanning the entire queue can be very inefficient. One solution is to specify a queue for each event, and when an event occurs, simply move all the processes in the corresponding queue to the ready queue.

If the operating system supports process priority scenarios, it is also very efficient to specify separate queues based on different priority levels.

  

The process being suspended

In a system without virtual memory, all running processes must be fully loaded into main memory. Because the processor is much faster than I/O, it is common for all processes in memory to wait for I/O even for systems designed for multi-channel programs, most of the time the processor is idle.

One solution is to swap, which is to transfer part or all of a process in main memory to disk. When there is no ready-to-state process in main memory (that is, the operating system does not have a process to execute), the operating system will swap the blocked process out to the Suspended queue in the disk, which temporarily saves the process queue that is swapped out from main memory. The operating system then takes out a process in the Suspended queue, or accepts a new process and loads it into main memory execution.

Because the interchange is a disk I/O operation, the problem can also be exacerbated. But disk I/O operations are relatively the fastest I/O in the system, so in general, switching can improve performance.

To accommodate the switching strategy of the operating system, we add a suspend state to the process. When all the processes in main memory are in a blocking state, the operating system puts one of the processes into a suspend state and swap it out to disk. The freed space in main memory can be swapped into another process to execute in main memory. Therefore, according to the above reasoning, the suspended state process is in a blocking, here is a problem is: if the operating system to complete a swap operation, it is necessary to select a pending process execution, and if the suspended process is in a blocking state, then the process of switching into a suspended is meaningless, The pending process must wait for the event to occur before it can be executed. Therefore, in order to differentiate so these different situations, it is necessary to add 2 states:

  block/Suspend state : The process is in the secondary and waits for an event;

  Ready/Suspended state : The process in the secondary storage, loaded into main memory can be executed;

  

New State transitions:

    Blocking /Suspend state : If there is no ready process, at least one blocking process is swapped out, freeing up space for the unblocked process. If a running process, or a ready process requires more storage space, the operating system may swap out the process even if there is a ready process;

    Block/Suspend state-ready/Suspend State : The waiting event occurs;

    Ready /Suspend state-ready state: If there is no ready state in main memory, the operating system is swapped into a process execution;

    Ready-to-ready /suspended states : Usually the operating system does not tend to suspend the ready process. This conversion occurs when: (1) If the main memory needs enough space, only a ready process can be suspended, (2) a high-priority blocking process will soon be ready, the operating system may suspend a low-priority ready process, rather than a high-priority blocking process;

    new configuration-ready/suspend or new state-ready state : When a new process is created, it either joins the ready queue or joins the ready/Suspended queue. Regardless of the operation, the operating system must establish tables to manage processes and assign address spaces to processes. With this strategy, there is often not enough space allocated to the new process in main memory, so the new state-to-ready/suspend-state conversion is used. On the other hand, the operating system creates the process as late as possible to reduce the overhead of the operating system and then executes the process creation task when the system is blocked by the blocking process;

    Blocking /Hang-up states : This is a rare situation because it doesn't make sense to swap a blocked process for primary storage. However, if the blocking/suspending process priority is higher than any process in the Ready/Suspended queue, and the operating system considers the blocking event to occur quickly, it is reasonable to swap in the process;

    Run state-ready/Suspended State : (1) The running time period of the process is full, (2) the blocking/suspending queue, a higher priority process is not blocked, it is possible to preempt the running process, convert it to ready/suspend state, and free some main memory space;

    various states, exit states : The running process terminates naturally, or an error condition is forced to terminate; a process can also be terminated by the parent process, or the parent process terminates. Processes can be converted to exit states in any state;

Process description

In order to manage processes and resources, the operating system must have information about the current state of each process and resource. The way to use this is to construct and maintain information tables for each entity that you manage. The operating system maintains four different types of tables: memory, I/O, files, and processes.

  

  Memory Table : Used to track main memory and secondary storage (virtual RAM). Some parts of main memory are reserved for the operating system, and the remainder is a process that can be used by processes that are saved in a secondary storage using some type of virtual memory or a simple switching mechanism.

Include the following information:

Main memory assigned to the process;

The secondary storage assigned to the process;

Any of the protected properties of main memory blocks and virtual block blocks, such as those processes that can access certain shared memory areas;

Any information required to manage virtual memory;

  I/O table : Manages I/O devices and channels in the computer. At any point in time, an I/O device is either available or is already assigned to a particular process. If I/O operations are in progress, the operating system needs to know the operational status of I/O and the primary cells of the source and destination for I/O delivery.

  File Table : Information about whether a file exists, the location of the file in the secondary, the current state, and other properties. The information in the file table may be maintained and used by the file management system, or it may be managed and maintained by the operating system itself;

  Process Table : management process;

The location of the process

The process consists of one or a set of executed programs, as well as the local variables associated with those programs, global variables, any defined constants, and the execution of the program usually involves a stack to track the procedure calls and the parameters between the procedures; Finally, there are many properties associated with each process that the operating system uses to control the process. The collection of properties is referred to as the Process Control block. A collection of programs, data, stacks, and properties is called a process image.

  

The location of the process image depends on using a memory management scheme. For the simplest case, the process image is saved in an adjacent or contiguous block of storage, and the process image is saved in the secondary store. So the operating system manages the process, and at least part of its process image must be in main memory. In order to execute this process, the entire process image must be loaded into main memory or virtual RAM. Therefore, the operating system needs to know the location of each process on disk, and for processes in main memory, the operating system needs to know its location in main memory.

The paging hardware of modern operating systems allows the process of partially resident memory to be supported using discontinuous physical memory. At any time, a portion of the process image can be in main memory, and the remainder can be in the secondary storage. Therefore, the process tables maintained by the operating system must indicate the location of each page in each process image.

Properties of the process

  process identification Number : Each process is assigned a unique numeric identification number, which can be used as an index in the main process table, or the corresponding table can be located through the process identification number. For example, the memory tables can be organized to provide a mapping of main memory, indicating that the area is assigned to that process. I/O tables and file tables also have similar references. Process identification numbers can be used to inform the target of a particular communication in the operating system when the processes communicate with each other. When a process creates another process, the identification number can be used to specify the parent and child processes for each process.

  

  Processor status information : includes the contents of the processor's registers. When the process is running, its information is in the register. When the process is interrupted, all the register information must be saved so that the process resumes execution when the information is recoverable. The type and number of registers involved depends on the design of the processor, typically, the register group includes user-visible registers, control and status registers, and stack pointers. All processor designs include one or a set of registers, commonly referred to as the program status Word (PSW).

  

  Process Control Information : Additional information required by the operating system to control and coordinate the activity process. The process image structure in virtual memory, including the process control block, the user stack, the process private address space, and the address space that is shared with other processes. Whether the address of the process image is contiguous depends on the operating system's memory management scheme and the schema of the organization control structure.

  

The role of the Process control block

Process Control blocks are the most important data structures in the operating system, and each module in the operating system can read and modify them. To provide protection for the process Control block, the operating system is specifically handled by a processing routine whose task is to protect the process control block, which is the only arbiter that reads and writes these blocks.

Process Control

Execution mode: Certain instructions of the operating system, including instructions to read and change control registers such as the program status Word, instructions for raw I/O directives and memory management, and access to parts of the memory area, must be in system mode (also known as control mode or Kernel mode ) to execute. The corresponding is the user mode , the user program is usually run in this mode.

  

Two modes are used to protect the operating system and operating system data tables (such as process tables) from being affected by user programs. How does the processor differentiate the execution mode of the current process? Typically, the program status word has one representation of the execution pattern, which changes as certain events occur. For example, when a user program invokes a system service or interrupts a system routine, the execution mode bit is set to kernel mode, and the execution mode bit is set to user mode when the user process is returned from the system service.

Process creation

When the operating system creates the process, the following steps are performed:

1. Assign a process identification number to the new process . Add a new table entry in the main process table;

2. allocate space for the process . Include all elements in the process image:

The operating system needs to know how much space the private user address space (Programs and data) and user stacks require. This can use default values depending on the type of process, or it can be set according to user requests when the job is created. If the parent process creates a child process, the parent process can pass the desired value to the operating system as part of the request. If an address space is shared by the new process, it must be established correctly.

Allocating space to process control blocks;

3. Initialize the Process control block .

The process flag number includes the process ID, the parent process ID, and so on;

The processor state information is mostly initialized to 0, except for the program counter (which is placed as the program entry address) and the stack pointer (defining the process stack boundary);

The initialization standard default value of the Process control information and the properties requested for the process. For example, the process state is typically initialized to ready or ready/suspended, and the priority defaults to the lowest priority, unless a higher priority is displayed, and the process initially does not own any resources (i/0 devices, files) unless the request is displayed or inherited from the parent process;

4. set the correct link . For example, a new process is placed in a ready or ready/Suspended queue;

5. Create or augment other data structures . For example, the operating system may keep an audit file for each process for performance evaluation, etc.;

Process switching

Reason for process switching:

  

Two types of system interrupts: interrupts and traps . The difference is that the former refers to the occurrence of some type of external event that is not related to the currently running process, such as the completion of an I/O operation, or an error or exception condition that is associated with a process that is currently running, such as illegal file access.

For normal interrupts , the processor control is first transferred to the interrupt processor, which does some basic auxiliary work, and then goes to interrupt-related operating system routines. For example:

    Clock Interrupt : The time that is currently running exceeds the maximum run time period (time slice, the maximum time a process can be executed before it is interrupted);

    I/O interrupts : If an I/O activity occurs, the operating system translates the corresponding blocking process into a ready state process, and the operating system decides whether the currently running process will continue to execute, or a higher priority ready-state process execution;

    Memory Failure : When the processor accesses a virtual memory address, and the address unit does not exist in main memory, the operating system must transfer the block (segment or page) containing this address unit from the secondary to main storage. After an I/O request is issued for a memory call, the operating system may perform a process switch to execute another process. A memory failure process is placed in a blocking state, and when the required block is called into memory, the process is placed in the ready state;

For traps , the operating system determines whether the error and exception conditions are fatal. If it is, the currently running process is set to exit and a process switch occurs, otherwise the behavior of the operating system depends on the type of error and the design of the operating system, which may be attempting to recover or notify the user, or it may be a process switchover or continuation of the execution of the current process.

  system call : If a running process executes an I/O request instruction, such as opening a file, this call will cause the operating system to go to the system code routine to execute. Typically, system calls block running processes.

Mode switching

If a processor is interrupted, the processor performs the following actions:

1. Set the program counter to the start address of the interrupt processor;

2. Switch the processor mode from user mode to kernel mode, is the interrupt processing code can execute the privileged instruction;

The data in the Processor state information section of the Process Control block of the interrupted process must be saved, including program counters, other processor registers, and stack information;

What interrupts the processor will usually do:

1. Send an answer to the entity (I/O module) where the interrupt occurred;

2. If the interrupt is related to an I/O event, the interrupt processor detects the error condition;

3. If an error occurs, the interrupt processor sends a signal to the process requesting the I/O operation;

4. If the clock is interrupted, the processor control is transferred to the dispenser;

5. The interruption is not always accompanied by a switchover of the process, it is possible to continue to execute the interrupted process after the interrupt processor execution is complete;

A complete process switching process

1. Save the processor context, including program counters and other registers;

2. Update the Process control block of the process in the running state, including the process state to other States;

3. Move the Process control block to the appropriate queue;

4. Select another process to execute;

5. Update the Process control block for the selected process, including the status of the update process as a running state;

6. Update the data structure of memory management;

7. The recovery processor in the selected process has recently switched out of the running state context, by loading the program counter and other register values to achieve;

Therefore, process switching requires more work than mode switching.

Execution of the operating system

The operating system runs in the same way as normal software, which means that the processor executes a program;

The operating system often releases control and relies on the processor to restore control;

Various ways the operating system operates:

  no process kernel : In many older operating systems, one common way is to execute the operating system kernel outside of all processes. In this way, when the currently running process is interrupted or a system call is generated, the schema context of the process is saved and control is transferred to the kernel. The operating system has its own memory area and system stack to control procedure calls and returns. The operating system can perform any expected function and restore the context of the interrupted process, which can lead to the continuation of the interrupted process. Alternatively, the operating system can completely save the functionality of the process environment and continue to dispatch and dispatch another process, depending on the cause of the outage and the current situation.

In either case, the key point is that the concept of a process only applies to the user program, and the operating system code is executed only as a standalone entity running in privileged mode.

  

  executing in a user process : In a smaller machine (PC, workstation), the common approach is to execute all operating system software in the context of the user process as a set of routines called by the operating system software as a user process. At any time, the operating system manages all process images, each of which includes not only the user process's own process control block, the user stack, the private user address space (Programs and data), but also the program, data, and stack areas of the kernel program.

A typical process image structure. When the process is in kernel mode, a separate kernel stack is used to manage calls/returns. The code and data for the operating system are in the shared address space and are shared by all user processes.

When an interrupt, trap or system call occurs, the processor is placed in kernel mode and control is given to the operating system. To do this, you need to save the schema context and switch to a mode, and then switch to an operating system routine, but it still executes in the user process, so this way does not require a process switch, just a mode switch. After the operating system has completed its operation, if it is necessary to continue running the current process, another mode switch is made to recover the interrupted program in the current process. A bit of this approach is that there is no need for two process switching, just a mode switch. If you determine that a process switch needs to occur instead of returning the currently executing program, control is transferred to the process switch routine, which may or may not be executed in the current process. For example, the current process must be placed in a non-running state, and another process will be designated as a running process. Such a process can logically be seen as executing in an environment outside of all processes. A process can save its state information, select another process from the ready state process, and turn control over to this process. This is a mixed case, because at the critical time, the code of the shared operating system is executed in the user process. Even so, user code still does not interfere with the operating system routines.

The concepts of processes and programs are different, and they are not a one-to-one relationship. In a process, both the user program and the operating system program are likely to execute, and the operating system's programs are the same in different user processes.

Process-based operating system: implements the operating system as a set of system processes. In this case, the primary kernel functions are organized into separate processes, and there may be some process switching code that executes outside of any process. Advantages of this approach:

Using the principle of program design, the operating system is modularized and the most concise interface between modules is used;

Some non-critical operating system functions can be separated into separate processes, which can run at a specified priority and alternate with other processes under the control of the dispatcher;

The operating system is implemented as a set of processes and is useful in multiprocessor and multiprocessor environments.

 

UNIX Systems

In Unix System V, most operating systems are executed in a user process environment, so two modes are required: User mode and kernel mode. UNIX uses two types: system processes and user processes. System processes run in kernel mode, executing operating system code to manage functions and internal processing such as memory allocation and process switching, user processes running in user mode, execution of user programs and utilities, and switching to kernel mode if the kernel instructions are to be executed. When an exception, interrupt, or system call is generated, the user process enters kernel mode.

UNIX systems have a total of 9 process states.

  

UNIX uses two run states to differentiate whether the process is running in user mode or in kernel mode. Preempted state: When a process runs in kernel mode, the kernel completes the task of preparing to return control to the user program, which can be preempted. At this point, the kernel may preempt the current process and run another high-priority process. At this point, the process goes to the preempted state, but in order to allocate processing, the process of the preempted state is in the same queue as the ready state. When the process is running in kernel mode, it cannot be preempted, so UNIX is not suitable for real-time processing.

There are two special processes in UNIX.

Process 0 is created at the time of the system, it is a predefined data structure, is loaded at startup time, is the exchange process;

Process 1 is created by process 0, is the initial process, and is the ancestor of all other processes. When the system logs on to the user, process 1 creates a user process for the user. The user process can then create a child process that forms a process tree. Therefore, any application is composed of a set of related processes;

Unix Process Description: user-level context , Register context , and system-level context .

  

User-level context: Includes the basic part of the program, which is generated directly from the compiled target file. The user program is divided into two areas of text and data, the body area is read-only and is used to save program instructions. When the process executes, the processor uses the user stack for procedure calls and returns, as well as for passing parameters. A shared memory area is a memory area that is shared with other processes and has only one physical copy, but by using virtual memory, the shared memory area seems to be in their respective address space for each shared process.

Register context: When the process is not running, the state information of the processor is stored in the register context.

System-Level Context: Contains additional information that is required by the operating system management process, which consists of static and dynamic parts. Static parts are fixed in size, while dynamic parts are variable in size throughout the life cycle.

Dynamic parts in the static section:

      Process Table Entry : It is part of the process table maintained by the operating system, and each process corresponds to a portion of the table.

    

      U (user) zone : Contains the additional process control information required by the kernel to execute in the context of the process.

    

      This process area table : Memory management system use.

Dynamic part of System-level context: kernel stack . When the process runs in kernel mode, the kernel stack contains information that must be saved and recovered when the procedure is called and interrupted.

The UNIX system calls Fork() through the kernel system to create the process. When a process produces a fork request, the following steps are performed:

1. Assign an empty item to the process table for the new process;

2. Assign a unique process identification number to the child process;

3. Create a logical copy of the parent process context, excluding the shared area;

4. Increase the counters for files owned by the parent process to indicate that another process also owns the files;

5. The handle process is set to the ready state;

6. Returns the process number of the child process to the parent process, or 0 if it is a child process;

All of the above operations are done in kernel mode, and you may continue with one of the following three actions (which can be considered part of the allocator routine) after completion:

1. Continue execution in the parent process. If the fork is returned that is not 0, returns the parent process in user mode to the fork call to continue execution;

2. Processor control is given to the child process. If the value returned from the fork is 0, the execution of the code begins in the child process, and the same is done from the fork call;

3. Control is transferred to another process, the parent process and the child process are all set to the ready state;

    

    

Operating system Learning Notes process

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.