Part II: Process Management

Source: Internet
Author: User
Tags mutex numeric value posix semaphore terminates time limit

Part II: Process Management

process : A process can be seen as a program being executed. The process requires a certain amount of resources to complete the even more task.
A process is a unit of work in most systems. Such a system has a set of processes that make up the operating system process code, the user process executes the user code, and all processes can execute concurrently.

Chapter III: Process 3.1 Process Concepts

A program is a passive entity, and a process is an activity entity that has a program counter to represent the next command to execute and a collection of related resources.

status of the process :

    1. NEW: The process is being created
    2. Run: instruction is being executed
    3. Wait: The process waits for an event to occur (such as I/O completion or signal received)
    4. Ready: Process waits for the processor to be allocated
    5. Terminate: Process completes execution

Important : Only one process can run on one processor at a time, but multiple processes can be in a ready or waiting state.

Process block Control BLOCK,PCB: Each process is represented within the operating system by a Process control block that contains many information related to a particular process.

process status : status can include new, ready, run, wait, stop
Program Counter : The counter represents the address of the next instruction that the process will execute.
CPU Registers : Depending on the architecture of the computer, the number and type of registers are different. He includes accumulators, index registers, stack pointers, general registers, and other condition code information registers.
CPU scheduling information : including process priority, dispatch queue pointers, etc.
Memory Management Information : Depending on the memory system used by the operating system, this information includes the base and boundary register values, the page table, or the segment table.
accounting information : Includes CPU time, actual usage time, time limit, accounting data, number of jobs or processes
I/O status information : Includes the list of assigned process I/O devices, the list of open files, and so on.

3.2 Process Scheduling

The purpose of the multi-channel program is to have the process running at any time, thus maximizing the CPU utilization.

Process scheduling : Select an available process to execute on the CPU.

    1. Dispatch queue: When a process enters the system, it is added to the job queue, which includes all processes in the system. resides in memory-ready, waiting for the running process to be saved in the ready queue. This queue is usually implemented with a linked list, with the head node pointing to the first of the list and the pointer of the last PCB block. Each PCB contains a pointer field to the next PCB of the ready queue.

      The list of processes waiting for a specific I/O device is called a device queue.

      The common notation for discussing process scheduling is the queue graph.

      When a process is assigned to the CPU and executed, the situation may occur:

      • The process may issue an I/O request and be placed in the I/O queue.
      • The process may create a new child process and wait for the end of the period
      • The process may forcibly release the CPU due to an outage and be put back into the ready queue.
    2. Dispatch program

      The long-term scheduler (long-term scheduler) or the Job scheduler (Job Shceduler) selects processes from this pool and loads the memory to prepare for execution.

      The short-term dispatcher (short-term scheduler) or the CPU scheduler selects the process from the ready-to-execute process and allocates the CPU to it.

      context Switching : Switching the CPU to another process requires saving the state of the current process and recovering another process state.

3.3 Process Operations
    1. Process creation

      During execution, the process can create several new processes by creating a process system call. Most operating systems identify processes based on a unique identifier (process Identifier,pid), which is usually a numeric value.

      When a process creates a new process, there are two possible types of executions:

      1. The parent process executes concurrently with the child process
      2. Parent process waits until one or all of the child processes have finished executing

      the difference between concurrency and parallelism :?

      The address space of the new process also has two possibilities:

      1. A child process is a replica of the parent process (with the same program and data as the parent process)
      2. Child process loads another new program

In the UNIX operating system, each process has a unique identifier. A new process can be created through the fork () system call. The new process is made by copying the address space of the original process. This mechanism allows the parent process to communicate with the child process conveniently. Two processes (both parent and child) continue to execute instructions that lie after the system calls fork (). However, there is a difference: for a new child process, the return value of the system call Fork () is 0, and for the parent process, the return value is the process identifier of the child process (not 0).

Typically, after the system calls fork (), a process causes the system to call EXEC () to replace the process memory space with a new program. The system calls exec () to load the binary into memory (eliminating the memory mapping of the program that originally contained the system call to exec ()) and begins execution. In this way, two processes can communicate with each other and execute according to their respective methods.

    1. Process termination

      When the process finishes executing the final statement and the system calls exit () to request that the operating system remove itself, the process terminates. At this point, the process can return a status value (usually an integer) to the parent process (by calling wait () through the system).

3.4 interprocess communication (interprocess COMMUNICATION,IPC)

Two basic modes of inter-process communication: Shared memory and message delivery

    1. Shared Memory System

      interprocess communication using shared memory requires the communication process to establish a shared memory region. Typically, a piece of shared memory area resides in the address space of the process that generated the shared memory segment. Other processes that want to communicate using this shared memory segment must place this on their own address space.

Eg:posix, which represents the portable operating system interface (Portable Operating system Interface, abbreviated to POSIX), the POSIX standard defines the interface standards that the operating system should provide for the application. is the generic term for the series of API standards that IEEE defines for software to run on a variety of UNIX operating systems, formally referred to as IEEE 1003, and the International standard name is ISO/IEC 9945.

The POSIX standard is intended for software portability at the source code level. In other words, a program written for a POSIX-compliant operating system should be compiled and executed on any other POSIX operating system, even from another vendor.

    1. Message delivery system

      Messaging tools: Send messages, receive messages, communicate lines

      1. Naming: Direct communication, indirect communication (email)
      2. Synchronization: Message delivery can be either blocked or non-blocking (also known as synchronous or asynchronous)
      3. Buffering: Regardless of whether the communication is direct or indirect, the information exchanged by the communication process resides in the temporary queue, in brief, there are three ways to implement the queue:

        0 Capacity : The maximum length of the queue is 0, so no messages in the line are waiting. That is, the send must be blocked until the recipient receives the message.

        Limited capacity : The queue length is limited n; therefore, only n messages can reside in the queue, and if a new message is sent to a queue that is not full, the message may be placed in the queue (or the message is copied or a pointer to the message is saved), and the sender can continue execution without waiting. But the line capacity is limited. If the line is full, the sender must be blocked until the space in the queue is available.

        Unlimited capacity : The queue length can be unlimited, so no matter how many messages can wait in it, never block the sender.

Eg:mach [Mɑk],mach is an operating system kernel developed by Carnegie Mellon University to support operating system research, and is an IPC system based on shared memory.

Chapter 4th Thread 4.1 Overview

thread : is the basic unit used by the CPU, which consists of thread ID, program counter, register collection and stack. It shares code snippets, data segments, and other operating system resources with other threads that belong to the unified process.

Advantages of Multithreading:

    1. High degree of responsiveness
    2. Shared resources: The default shares the memory and resources of the process to which they belong.
    3. Economy: Threads can share resources, creating and replacing faster than process blocks.
    4. The use of multi-processing architectures: the ability to take advantage of multiprocessor architectures so that each process can run on different processors in parallel.
4.2 Multi-Threading model

Thread support: User thread of user layer and kernel thread of kernel layer;

The relationship model between the user thread and the kernel thread:

    1. Many-to-one model : Maps Many user-level threads to a kernel thread. Thread management is performed in user space and is therefore highly efficient. However, if a thread executes a blocking system call, the entire process is blocked. Also, because only one thread can access the kernel at any time, multiple threads cannot run concurrently on multiple processors. (solaries)
    2. Single-to-one model : Maps each user thread to a kernel thread. The model can allow another thread to continue executing when one thread executes a blocking system call, so it provides better concurrency than a multiple-to-one model ; He also allows multiple threads to run concurrently on multiprocessor systems. But the downside is that each user thread created needs to create a corresponding kernel thread, which has a large overhead and limits the number of threads supported by the system. (Linux and Windows)
    3. Many-to-many models: multiplexing many user threads onto the same number or a smaller number of kernel threads.
4.3-Wire libraries

Thread Libraries : Provides programmers the API to create and manage threads.

Implementation method:

    1. Provides a library in user space that does not have kernel support.
    2. Executes a kernel-level library that is directly supported by the operating system.

The three main line libraries currently used are:

    1. POSIX Pthread (user-or kernel-level)
    2. Win32 (Kernel level)
    3. Java: User-level
4.4 Multi-Threading Problem 4.4.1 system call fork () and exec ()

In Unix, fork () is used in two forms, one to replicate all threads, and the other to replicate only those threads that call the system call Fork ().

System call EXEC (): If a thread invokes the system call exec (), the program specified by the EXEC () parameter replaces the entire process, including all threads.

4.4.2 Thread Cancellation

Thread Cancellation : A task that terminates a thread before it is finished.

Target thread Cancellation :

    1. Asynchronous cancellation (asynchronous cancellation): A thread terminates the target thread immediately.
    2. Deferred cancellation (deferred cancellation): The target thread constantly checks whether it should be terminated, which allows the target thread to have an opportunity to terminate itself in an orderly manner.
4.4.3 Signal Processing

signal : Used in UNIX to notify a process that a particular event has occurred.

The signal is divided into synchronous signal and asynchronous signal .

Signal handlers: Default handlers and custom handlers.

Signal sending:

    1. Sends a signal to the thread to which the signal is applied.
    2. Sends a signal to each thread within the process.
    3. Sends a signal to some fixed thread within the process.
    4. Specifies a specific thread to receive all signals from the process.
4.4.4 thread Pool

Thread Pool : The main idea is to create a certain number of threads at the beginning of the process and put them into the pool to wait for work. When the server receives the request, he wakes up a thread in the pool (if there is a thread that can be used) and passes it to the request to be processed. Once the thread has completed the service, it will return to the pool to wait for work. If there are no threads available in the pool, then the server waits until there are empty threads.

4.4.6 Scheduler Activation

lightweight process (LWP): A multi-threaded communication between the kernel and the line libraries, in many implementations of the many-to-many model and the two-level model system to set up an intermediate data structure between the user and the kernel thread. For user line libraries, LWP behaves as a virtual processor that an application program can dispatch a user thread to run. Each LWP is connected to the kernel thread, and the kernel thread is dispatched to the physical processor by the operating system. If the kernel thread is blocked, the LWP also blocks, and the corresponding user thread that is connected to the LWP is blocked.

Dispatch activator (Scheduler activation) : The kernel provides a set of virtual processors (LWP) to an application that can dispatch user threads to an available virtual processor.

4.5 Operating System Instances 4.5.1 Windows XP Threads

A thread typically contains:

    • A thread ID to uniquely identify the thread.
    • A set of registers to represent the processor state.
    • A user stack for threads to run in user mode, and a kernel stack for threads to run in kernel mode.
    • A private storage area that is used by various run-time libraries and dynamic-link libraries.

A collection of registers, stacks, and private stores are often referred to as contexts .

4.5.2 Linux Threads

Linux provides system call fork () with traditional process replication capabilities, as well as the ability to create threads with System Invoke Clone (), which does not differentiate between processes and threads.

When Clone () is called, it is passed a set of flags to determine how much sharing between the parent task and the child task occurs.

5th Chapter CPU Scheduling

CPU scheduling is the basis of a multi-channel program operating system. By switching CPUs between processes, the operating system can increase the throughput rate of the computer.

5.1 Basic Concepts

multi-Channel program design : The road Program Design technology is in the computer memory at the same time stored several independent programs, so that they under the control of the management program, interspersed with each other, two or more than two programs in the computer system in the beginning to the end of the state. This corresponds to a single-channel program, which allows only one program to run in the computer's memory.

5.1.2 CPU Scheduler

Each time the CPU is idle, the operating system must select a process from the ready queue to execute. Process selection is performed by a short-term dispatcher or CPU scheduler. The scheduler selects a process that can execute from memory and allocates CPUs for it.

5.1.3 preemption Scheduling

Four environments in which CPU scheduling decisions occur:

    1. When a CPU switches from a running state to a wait state (I/O request or call wait)
    2. When a process switches from a running state to a ready state (when an outage occurs)
    3. When a process switches from a wait state to a ready state (I/O completion)
    4. When a process terminates.
      1 and 4 do not select only control room non-preemption (with non-preemptive scheduling, once the CPU allocates a process, the process will always use the CPU until the process terminates or switches to the wait state.)
5.1.4 Dispatch Program

Dispatch Program (dispatcher) : The dispatcher is a module that gives control of the CPU to a process selected by the short-term scheduler. Its features include:

    • Toggle Context
    • Switch to User mode
    • Turn back to the appropriate location in the user program to restart the program.
5.2 Scheduling criteria

Cpud Scheduling Algorithm Guidelines:

    • CPU Usage
    • Throughput
    • Turnaround time
    • Wait time
    • Response time
5.3 Scheduling algorithm

CPU dispatch processing is the issue of selecting a process from the ready queue and allocating the CPU to it.

scheduling algorithms include:

  • first-come-first service scheduling (first-come,first-served (FCFS) scheduling algorithm): FIFO queues can be used for easy implementation. FCFS scheduling code is simple and easy to understand. But its average wait time is longer.
  • Minimum Job Priority scheduling (Shortest-job-first (SJF) scheduling algorithm): When the CPU is idle, it is assigned to processes with the shortest CPU interval. The SJF scheduling algorithm proves to be the best, because for a given set of processes, the average wait time for SJF is minimal. The difficulty with SJF is the inability to know the length of the next CPU interval. One approach is to approximate SJF and predict. The SJF may be preemptive or non-preemptive.
  • Priority scheduling (scheduling algorithm): Each process has a priority associated with it. Priority scheduling can be preemption or non-preemption. But the main problems are: infinite blocking (indefinite blocking) or starvation (starvation).
  • Rotation Method (ROUND-ROBIN,RR): Scheduling algorithm is specially designed for time-sharing system. It is similar to FCFS scheduling, but increases preemption to switch processes. Define a smaller time unit called the time slice (quantum, or slice). The time slice is usually 10-100ms. The ready queue is used as the loop queue. CPU Scheduler loop-ready queue, the performance of the CPU.RR algorithm that allocates no more than one time slice per process is largely dependent on the size of the time slice. In extreme cases, if the time slice is very large, then the RR algorithm is the same as the FCFS algorithm. If the time slice is small, then the RR algorithm is called the processor share, in theory, n processes for the user has its own processor, the speed of the real processor speed 1/n.
  • Multi-level queue scheduling (multilevel queue scheduling algorithm): divides the ready queue into multiple Independiente columns. Depending on the properties of the process, such as memory size, process priority, process type, a process is permanently assigned to a queue. Each queue has its own scheduling algorithm. There must be a dispatch between the queue and the queue, usually with a fixed-priority preemption schedule.
  • Multilevel Feedback queue scheduling algorithm (mutilevel feedback queue scheduling algorithm): In contrast to multi-level queuing scheduling, it allows processes to move between queues. The main idea is to differentiate processes based on the characteristics of the CPU interval. If the process uses too much CPU time, it is transferred to the lower priority queue. In addition, in lower priority queues, the forward-waiting time is transferred to the higher priority queue.
5.4 Multi-processor scheduling 5.4.1 Multi-Processor scheduling method

Asymmetric Multiprocessor (asymmetric multiprocessing): In a multiprocessor, the method of CPU scheduling is to have one processor (master server) handle all scheduling decisions, I/O processing and other activity, and other processor values to execute user code.

symmetric multiprocessor (symmetric MULTIPROCESSING,SMP): That is, each processor is self-dispatching. All processes may be in a common ready queue, or each processor has its own private-ready process queue. In any case, the schedule checks the common-readiness queue by each processor and chooses a process to execute.

5.4.2 Processor Affinity

Processor Affinity : Due to the high cost of invalidating or refactoring the cache, the majority of SMP systems try to avoid moving a process from one processor to another, trying to make a process run on the same processor, known as processor affinity.

5.4.3 is responsible for peace

load Balancing (balanceing): manages to distribute the workload evenly across all processors in the SMP system.

There are usually two ways to load peace:

    • Push Migration: A specific task periodically checks the load on each processor, and if it finds an imbalance, that is, by moving the process from the overloaded processor to (or pushed to) an idle or less busy processor, the average low load is distributed. Occurs when the idle processor pushes (pull) a waiting task from a busy processor
    • Pull migration: Pulling miggration occurs when the idle processor pushes a wait task from a busy processor.
5.4.4 Symmetric multithreading

Multiple physical processors are available, and the SMP system allows several threads to run concurrently. Another approach is to provide multiple logical (rather than physical) processors to implement. This approach is known as symmetric multithreading (SMT), which is called Hyper-Threading (hyperthreading) technology on Intel processors.

The idea of SMT is to generate multiple logical processors on the same physical processor, and the next operating system renders a multi-logical processor attempt, even if the system has only a single processor.

5.5 Thread Scheduling

One of the differences between user threads and kernel-wise threads is how they say they are scheduled.

When executing many-to-one and many-to-many models, the line libraries dispatches user-level threads to run on a valid LWP, known as the process contention range (Process-contention scope,pcs), because CPU contention occurs between threads that belong to the same process.

In order to decide which kernel thread is to be dispatched to the CPU, the kernel uses a system competitive range (System-contention Scope,scs). Using SCS scheduling method, the competing CPU occurs in all the threads of the system.

5.6 Operating System Instances

Solaris: Takes priority-based thread scheduling.

Windos XP: Priority-based, preemptive scheduling algorithm to dispatch threads.

Linux scheduling: Preemptive, priority-based algorithms with two independent priority ranges: from the real-time range of 0~99 and from the Nice range of 100~140. It maps to the global
Priority, where the lower the value indicates the higher the priority level. Linux assigns a longer slice of time to higher priority, and assigns a segment of time slices to lower priority levels.

The 6th Chapter process synchronization

Multiple processes concurrently access and manipulate the same data and the execution results are related to the particular order in which the access occurs, known as the race condition (race condition). To avoid competitive conditions, it is necessary to ensure that only one process can manipulate variables for a period of time and must be synchronized in some form.

6.2 Critical Area

Critical Area (critial section) Characteristics:

    • Mutex (mutual exclusion): If process P is executed in its critical section, no other process can execute in its critical section.
    • forward (progress): If no process is executed within its critical section and there are entry procedures into the critical section, only those processes that are no longer in the remaining area can participate in the selection to determine who can enter the critical section at the next, and this option cannot be indefinitely postponed.
    • Limited Wait (bounded waiting): A request to enter a critical section from a process knows the maximum number of times that other processes are allowed to enter a critical section until the request is allowed.

Two methods for dealing with critical area problems within the operating system:

    • preemption Kernel (preemptive kernel): preemption kernel allows processes that are in kernel mode to be preempted.
    • Non-preemption kernel (nonpreemptive kernel): a process that is in kernel mode is not allowed to be preempted.
6.4 Hardware Synchronization

lock : A lock is obtained before entering the critical section, while the Exit critical section is a release lock.

Many modern operating systems provide special hardware instructions to allow Atomic (non-disruptively) checking and modifying the contents of a child or exchanging two words.

6.5 Signal Volume

signal Volume (semaphore) : A synchronization tool whose semaphore s is an integer variable that can only be accessed by two standard atomic operations: Wait () and signal () except for the initial barbarian.

6.8 Synchronizing Instances
    1. Solaris: To control access to critical sections, Solaris provides adaptive mutex, condition variables, semaphores, read-write locks, and turnstiles.
    2. Linux: preemption kernel, provides spin lock and semaphore for kernel lock.
7th Chapter Deadlock

Deadlock (Deadlock): In a multi-channel program environment, multiple processes may compete for a certain amount of resources. A process requests a resource, and if the resource is not available at this time, the process goes into a wait state. If the requested resource is occupied by another waiting process, the wait process may no longer be in its state.

In normal mode, a process can only use resources in the following order:

    1. Application
    2. Use
    3. Release
7.2 Deadlock Characteristics

When a deadlock occurs, the process can never be completed, and system resources are blocked for use, preventing other jobs from starting to execute.

7.2.1 Requirements
    1. Mutually exclusive
    2. Occupy and wait
    3. Non-preemption
    4. Loop wait
7.2.2 Resource allocation diagram

The deadlock problem can be described more precisely by a forward graph called a system resource allocation diagram.

If the distribution diagram has no loops, then the system has no process deadlock. If the allocation graph has loops, then there may be deadlocks.

7.3 Deadlock Handling methods
    1. Protocols can be used to prevent or avoid deadlocks;
    2. The system can be allowed to enter a deadlock state, then detect it and restore it;
    3. Can nurse this problem, think that deadlock can not occur within the system;

Deadlock Prevention (deadlock prevention) : is a set of methods to ensure that at least one necessary condition is not established;

deadlock avoidance (deadlock avoidance): requires the operating system to obtain additional information about the process requesting resources and using resources in advance.

7.7 Deadlock Recovery
    1. Process termination: terminates all deadlock processes; only one process is terminated at a time until the deadlock cycle is canceled;
    2. Resource preemption: (1) Select a victim; (2) rollback

Part II: Process Management

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.