Operating System Principles learning notes--process management

Source: Internet
Author: User
Tags mutex semaphore

Tags: data structure primitive storage algorithm IO job2012-09-01 16:50 6603 People read Comments (1) favorite reports Classification:"OS" (4)

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Process Management

  • Points:
    • Fundamentals: Process Description and control
    • Policy: Process scheduling
    • Implementation: Mutual exclusion and synchronization
    • Avoidance: Deadlock and starvation
    • Solution: Several classic questions
  • The introduction of processes
    • Sequential execution of programs
      • Source code program, target program and executable program
      • Program execution: Edit, compile, link, execute
      • Structure of the program: order, branching, looping structure
      • Characteristics of program execution: sequential, closed, reproducible
    • Program Concurrency execution
      • Multi-Channel Program design Technology: Concurrent execution of multiple programs
      • Characteristics of program concurrency execution: Intermittent, non-closed, not reproducible
      • Issues raised by concurrent execution:
        • Coordinate the order of execution of each program: The calculation must wait for the input data not all in memory
        • Multiple executing programs share system resources, and programs can affect each other and even affect the output
        • Select those, how many programs go into memory execution
        • In-memory execution program who executes first, who executes after
        • How is memory allocated efficiently?
    • The concept of a process
      • Definition: A program that can execute concurrently, running on a data set
      • Request, own resource ~ schedule (thread)
      • Program: Static concept, is a collection of instructions and data, can be stored for a long time
      • Process-to-program correspondence
        • A program can correspond to one process or multiple processes
        • A process can correspond to a program, or a program
    • Characteristics of the process
      • Dynamic nature
      • Concurrency of
      • Independence
      • of Asynchrony
    • Problems caused by the introduction of processes
      • Increased space overhead: building data structures for processes
      • Additional time overhead: manage and coordinate, track, fill and update data structures, switch processes, protect the site
      • More difficult to control: coordinate multiple processes to compete and share resources to prevent and resolve multiple integrations due to competing resources
      • The competition for processors is particularly pronounced
    • Structure of the process
      • Composition (process image): program, data set, Process control block PCB
      • The PCB is the only sign that the process exists. The PCB is created when the process is created, and when the process is finished, the system will revoke its PCB
  • Pcb
    • Process identity information: internal to the process (the system assigns it the label) and external identifiers (name of a person)
    • Processor status information: General register value, instruction counter value, program status Word PSW value, user stack pointer value
    • Process scheduling information: Process status, process priority, additional information for process scheduling
    • Additional Information: program and data pointers, process synchronization and communication mechanisms, resource lists, connection pointers
  • How the PCB is organized
    • Single queue: The PCB of all processes is organized into a single queue through the linked list. Applies to systems with a very large number of processes. such as the Windows operating system
    • Table structure (lookup efficiency is higher)
      • The PCB is organized into different tables by process state: Ready process table, Execution process table (in multi-machine system) and blocking process table
      • The system separately records the starting address of each PCB table.
    • PCB multistage Queue
  • Status of the process
    • Process execution trajectory: the sequence of instructions executed by the process to observe the process of the processing machine execution
    • Two-State model:
      • Execute, not executed
      • Not all processes are in a ready state as long as "not executed", some need to be blocked, wait for IO to complete, and "not executed" can be divided into ready and blocked
    • Five states of the process
      • Execution running: Occupancy processor (in a single processor environment, only one process occupies a processor at a time)
      • Ready to go: ready to execute
      • Block blocked: Waits for an event to occur before it can be executed, such as waiting for I/O completion
      • NEW: The process has been created but not accepted as an executable process by the OS
      • Terminate terminated: Released by OS from execution state due to stop or cancel
      • Some systems allow the parent process to terminate its child processes under any circumstances. If a parent process is terminated, its child processes must terminate
  • Problem: Multiple processes competing for memory resources
    • Tight Memory resources
    • No ready process, processor idle: I/O speed is much slower than processor speed, may occur all process blocking wait I/O
  • Workaround
    • Swap technology: Swap out part of the process to external memory to free up memory space
    • Adopt Virtual Storage technology: Each process can load only part of the program and data (Storage Management Section)
  • Swap technology, Exchange technology
    • Swap the processes that are temporarily not running in memory, or data and programs that are temporarily unused, to external memory to free up enough memory space to swap the processes and data that are already running, or the processes needed by the process, into memory
  • Why the process hangs
    • Process all blocked, processor idle
    • Heavy system load, memory space tension
    • The needs of the operating system. The operating system may need to suspend a background process or some service process, or some process that causes a system failure
    • End-User Requests
    • Request from parent process
  • Characteristics of the suspended process
    • cannot be executed immediately
    • It is possible to wait for an event to occur, and if so, the blocking condition is independent of the suspend condition, and even if the blocking event occurs, the process cannot execute
    • The process to suspend is: itself, parent process, OS
    • Only suspend his process to convert it from a pending state to a different state
  • Suspend and block
    • Whether the blocking process can only be suspended
    • How to activate a pending process
    • Two concepts: Whether the process waits for an event, is blocked or not, whether the process is swapped out of memory, hangs or not
    • 4 Combinations of states:
      • Ready: Process in memory, ready to execute
      • Blocking: Process in memory, waiting for event
      • Ready/Pending: Process is in-store and can be executed as long as it is in memory
      • Blocking/suspending: process is in-store, waiting for event
    • There are two types of processes that can be scheduled to be executed by the processor:
      • The newly created process
      • Or swap in a previously suspended process
      • Usually in order to avoid increasing the system load, the system is swapped into a previously suspended process execution
  • Control of the process
    • Two modes of execution:
      • System mode (also known as System State), control mode, or kernel mode
        • have higher privileges
        • Run system-specific instructions, including instructions for read-write control registers, basic IO instructions, and storage-management-related instructions and some specific memory areas
        • Processor in kernel mode and its instructions, registers, and memory are fully controlled and protected
      • User mode (or user state)
        • Lower privileges
        • Users typically run in user mode
    • Mode switching
      • User--System: User executes to a system call and enters operating system kernel execution
      • System-User: Performs the function of system call and returns to the user program
      • Special case: When the program executes to the closing sentence, switches to system mode and no longer returns to the user program
    • Operating system kernel
        • Hardware-based first-layer software expansion, providing the most basic functions of the operating system, is the basis of operating system work;
        • In modern operating systems, in order to reduce the overhead of the system itself, it tends to be closely related to hardware (such as interrupt handlers, device drivers, etc.), basic, public, high-frequency modules (such as clock management, process scheduling, etc.), as well as key data structures independent of the memory, and data protection for them. This part is often referred to as the operating system kernel.
        • The ability of the user to access the operating system through system calls, which are eventually implemented through the operating system kernel.
        • Generally, the operating system kernel functions can be divided into resource management functions and support functions in a nutshell.
          • Resource management:
            • Process Management: Process creation and termination, scheduling, state transitions, synchronization and communication, management PCB
            • Storage management: Assigning address space for processes, swap, segment/page management
            • IO Device Management: Cache management, assigning IO channels and devices to processes
          • Support function:
            • Interrupt Handling
            • Statistics
            • Monitoring
            • Clock management
            • Primitive Language (Primitive): Atomic manipulation
              • Creation and termination of processes
              • Blocking and waking of processes
              • Process suspend and activate
              • Process switching
                • Process switching & Mode switching
                  • Process switching: An operation between a role and a process
                  • Mode switching: An operation referenced internally by a process that is referenced when the user program and the transfer system call, or vice versa
                  • Process switching will cause mode switching, or vice versa
  • Process scheduling
    • Scheduling refers to the process of selecting a suitable individual in a queue, according to a method (algorithm).
    • The key to scheduling is the need for some method or algorithm, good scheduling algorithm is conducive to the selection of suitable individuals
    • Scheduling target
      • Fair sex
      • Processor utilization
      • Increase system throughput
      • Minimizing the response events of a process
    • Scheduling principle:
      • Meet the user's requirements:
        • Response Time: (consider making the overwhelming majority of users ' requests available within the response time, often used to evaluate the performance of timeshare systems)
        • Turnaround time: The time interval at which the job is submitted to the system and the completion of the job, evaluating the performance of the batch processing system
        • Deadline: In real-time systems, the latest time a task must start executing, or the latest time it must be completed, is commonly used to evaluate the performance of real-time systems
      • Meet the needs of the system:
        • System throughput
        • Processor utilization
        • Balanced use of all types of resources
        • Fairness and Priority
    • Dispatch mode:
      • Non-deprivation mode
        • Execute or request IO to block yourself
        • Not conducive to the "timeliness" of high demand for time-sharing and realtime systems, mainly for batch processing systems
      • Deprivation mode
        • The operating system can interrupt the execution of the current process when a new process arrives, or a high priority is blocked into the planning that insert ready queue, or in a time-slice-based system, the time slice runs out and the new process execution is dispatched.
        • In this way, more interrupts are generated, mainly for real-time systems with higher real-time requirements and higher performance-demanding batch processing systems.
    • Scheduling type
      • Batch scheduling, time-sharing scheduling, realtime scheduling and multiprocessor scheduling
      • Long-range scheduling (external memory to memory):
        • Long-term scheduling
        • Also known as advanced scheduling or job scheduling, it is the scheduled job or user program to create processes, allocate the necessary system resources, and the newly created into the planning that the insert ready queue, waiting for short-range scheduling
        • Some systems that use switching technology insert newly created processes into a ready/Suspended queue, waiting for a medium-range dispatch
        • In a batch system, after the job enters the system, it resides on the disk and organizes the batch processing queue, called the fallback queue. Long-range Scheduling select one or more jobs from this queue to create a process for it
        • Questions to consider:
          • Select how many go into memory--depending on the degree of the multi-channel program, which is the number of processes allowed to run concurrently in memory
          • Select those jobs: depending on the long-range scheduling algorithm
      • Medium-range scheduling (between external memory memory between processes)
        • Also known as intermediate scheduling
        • When the memory space is tight, or the processor cannot find an executable ready process, it needs to select a process (blocking or ready state) to swap out to external memory, free up memory space for another process, and when the memory space is more abundant, the process of selecting a pending state from external memory to memory (swap in);
        • Purpose: In order to provide the utilization of memory and the throughput of the system
        • Medium-range scheduling is only available for operating systems that support process hangs
      • Short-range dispatch (in-memory):
        • Also become a process scheduler, or a low-level scheduler that determines the readiness of the process in the queue to get the processor
        • Short range dispatch runs most frequently
        • Almost all modern operating systems have short-range scheduling functions
      • IO scheduling (similar tracks)
  • Process scheduling algorithm
    • FCFS (first come first service): also suitable for three kinds of scheduling
      • The non-deprivation dispatch mode, realizes simple, seemingly fair
      • Note: It may take a long time to wait for a short-running process or IO-type process to enter the queue
      • Unfair to the segment process
    • Short process first (improvements to FCFS)
      • Non-deprivation.
      • Difficult to accurately predict the execution time of a process
      • may lead to long process starvation
      • The use of non-deprivation scheduling method, not considering the urgency of the process, not suitable for time-sharing system and transaction processing system
    • Time-Slice Rotation scheduling method
      • A dramatic increase in the number of users
      • Time slice size can affect processing performance
        • Process switching adds additional overhead to the system
        • Too long, too short, not good.
        • Consider the system maximum number of users, response time, system efficiency and other factors
      • For short, computationally-based processes are more advantageous
      • process that is not suitable for IO type
      • One of the improvements: the process of IO blocking time can be organized separately into a ready queue, the time slice of the queue process can be set a little bit, and priority scheduling
    • Priority-based scheduling algorithm
      • Prioritize the process
        • The importance of the process completion function
        • The urgency of the process completion function
        • To balance the use of system resources, specify the process (job) priority
        • The degree to which a process consumes resources, for example, a short process (or job) can be given a higher priority
      • Static and Dynamic priority
        • Dynamic Priority:
          • Shortest remaining time preferred (deprivation type)
        • High response ratio is preferred
          • Process priority is proportional to wait time
          • Difficult to accurately estimate the tone execution time of the process
          • Calculate response ratio increases system overhead
    • Feedback Scheduling method
      • Scheduling based on execution history rather than in the future will solve this problem
      • The scheduling method of scheduling is adjusted according to the execution history of the process, which combines the methods of priority and time-slice rotation scheduling.
      • Facilitates interactive short process or batch jobs, they generally need only one or several time slices to complete
      • Could be a sharp increase in turnaround time for long processes
      • If new processes continue to come in, it may also be long process sea long-term hunger phenomenon
      • Different time slices can be set for each queue, and the lower the priority time slice the longer
  • Real-time systems (real-time system)
    • Be able to respond to external events in a timely manner love your request to complete the processing of the event within a specified time and to control all real-time tasks in a coordinated and consistent running computer system
    • Divided into real-time control system and real-time information processing system
    • Real-Time Control system:
      • Systems that require real-time control
      • Mainly used in production process control. (Automatic temperature control, weapon control, missile guidance, autonomous driving)
    • Real-time Information processing system
      • A system for real-time processing of information
      • A short time for the user to make the right answer (airplane booking, information retrieval)
    • Real-time tasks (real-time Task)
      • A specific process, often repeated, that is frequently called a task in a real-time system.
      • Periodic division
        • Recurring Real-time tasks
          • Periodic control of an external event
        • Non-recurring real-time tasks
          • Must contact a deadline (start deadline, completion deadline)
      • Requirements for the cut-off time
        • Hard real-time tasks (missing unpredictable results, potentially catastrophic)
        • Soft Real-time task (missed the impact won't be too big)
    • Goal:
      • Hard real-time tasks must be completed, soft real-time tasks as far as possible
      • Fairness and minimum time response time requirements are no longer important
    • algorithm:
      • Real-time demand is not very high
        • based on time slice rotation scheduling algorithm
        • priority-based scheduling algorithm
        • first time-to-deadline scheduling algorithm, which is the most recent implementation of the priority scheduling deadline When task
      • speed monotonic scheduling algorithm (RMS)
        • prioritizes priority based on task cycle size, with the shortest priority being the highest priority with
          • task cycle (period), A task reaches the time range between the next task arrival
          • Task speed (rate), which is the countdown of the period (in seconds) to the end of the task cycle, which indicates the hard cutoff time of the task, and the task should not be executed more than the task Period
          • takes the task speed as parameter, then the priority function is a monotonically increasing function
          • widely used in industrial real-time system of periodic task scheduling
  • Thread
    • Threading is introduced to reduce the overhead incurred by the system during concurrent execution of the program, making the system more concurrency-
    • Two basic properties
      • A process is an independent unit that has resources
      • It is also a basic unit that can be independently dispatched.
    • by process to thread
      • Objective: Both to increase the concurrency of the process and to reduce the additional overhead of the system
      • Implementation: Separating the resource request and scheduling attributes of the process, that is, the process as the request and owner of the resource, but not as the basic unit of the dispatch, thus creating a thread concept
      • The thread itself essentially does not own system resources and has only a few private resources necessary to run, and the thread can share all the resources of the process with other threads belonging to one process
      • All threads in a process share the status of the process
      • Three basic states: ready to perform blocking
      • Generally does not have a pending state
    • A process can create and revoke multiple threads, and multiple threads of the same process can execute concurrently
    • The operations of a thread include:
      • Derivation (SPawn)
      • Block (block)
      • Unblocking (Unblock)
      • End (finish)
    • Thread blocking does not necessarily cause blocking of the process
    • Type:
      • User-level threads (completed by the application) and kernel-level threads
      • When one of the threads in a process waits for the input data of another thread to block, the entire process is not blocked, that is, the process remains in execution state, and one of its threads is executing.
      • When a thread is blocked by IO, the kernel needs to start the system IO, the control goes from the user level to the system kernel level, which often causes the whole process to block, then a process switch occurs, and the process scheduler re-dispatches another ready process execution.
      • Mixed mode
  • Mutex and synchronization of processes
    • Question: How do I reconcile multiple processes to system resources, such as memory space, external devices, and other competing and sharing? How to solve the problem that multiple processes occur because of competing resources, set system instability, failure, and so on.
    • Concurrency control:
      • Competitive resources
        • Dead lock
        • Some resources must be used mutually exclusive-critical resources
        • The code that accesses the critical resource is called the critical section.
        • At any moment, only one process is allowed to enter the critical section, which enables the process to mutually exclusive access to critical resources
    • Mutex Use critical section
      • When a process needs to use a critical resource, it is implemented by obtaining access to the critical section.
      • First, in the entry area to determine whether you can enter the critical section, if you can enter, you must set the critical section use flag, prevent other subsequent process into the critical section. The subsequent process, by looking at the flag of the critical section, knew that it was not able to enter the critical section and went into the blocking queue, blocking itself
      • When the process in the critical zone is completed, the critical section is rolled out, the critical section is modified in the exit area, and a process in the blocking queue is awakened, allowing it to enter the critical section
      • It is important to ensure that the "critical zone use Flag" is a global variable that can be shared by all processes in the system, and that changes to the flag by the process must be mutually exclusive
    • Principle of use of critical area
      • Only one process is allowed to enter the critical section at a time (busy waiting)
      • The process can only stay within the critical zone for a limited time and must not allow other processes to wait indefinitely outside the critical zone (limited wait)
      • If the critical section is idle, let it enter as soon as there is a process request (idle let in)
      • The process of entering the critical zone, cannot be blocked for a long time in the critical area waiting for an event, must exit the critical section within a certain period (right to wait)
      • Cannot limit the progress of process execution and the number of processors
    • Competing resources can cause deadlocks
    • Competing resources can cause hunger
    • Concurrency control-co-ordination
      • Multiple processes often need to co-modify some shared variables, tabular file database, etc., collaborate to complete some functions
      • It is important to ensure that they are correct when modifying shared variables, ensuring that the data is of the complete type
      • Shared collaboration also involves mutually exclusive deadlocks and starvation, when more emphasis on writing to the data must be mutually exclusive
      • Consistency of data must be ensured (bank deposit withdrawal balance)
      • Generally, the data consistency is ensured by transaction processing, and the process of entering the critical section must complete the modification of some columns data at once.
      • Only after the process exits the critical section, other processes are allowed to enter the critical section for data modification to ensure data consistency.
    • concurrency Control-Communication collaboration
      • When a process is cooperating in communication, the processes need to establish a connection, and the communication process needs to be synchronized and coordinated. Processes communicate in many ways, including messaging, pipelines, shared storage, and so on.
      • When you implement process communication through messaging, there is no need for mutual exclusion because there are no shared resources, but deadlocks and starvation can still occur
      • Communication deadlock with hunger
    • Mutually exclusive and synchronous resolution strategies
      • Software approach
        • By the process itself, by executing the corresponding program directives, to achieve synchronization with other processes in mutual exclusion, without the need for specialized programming language or operating system support
        • It is difficult to properly control synchronization and mutual exclusion between processes, and may significantly increase the additional overhead of the system
      • Hardware methods
        • Control synchronization and mutual exclusion by shielding interrupts or by using specialized machine directives
        • Reduces system overhead, requires too strong hardware constraints, and can cause process starvation and deadlock, without becoming a common solution
      • Signal Volume method (emphasis)
        • Special support by the operating system, or specialized programming languages, including semaphore methods, process methods, and message delivery methods
        • General methods
      • Pipe-Path method
      • Message Delivery Methods
    • Software approach
      • Dekker algorithm
      • Peterson algorithm
      • Initial assumption: Control two mutual exclusion into the critical section, you can let two processes turn into the critical section
        • Guaranteed mutual exclusion.
        • appear busy and other phenomena
        • A must wait for B to use the next time.
      • First-time improvements
        • Sets the status flag for the critical section, indicating whether the critical section is available, and when idle, any process can enter
        • Process failure problem in critical section
        • Cannot guarantee mutex
      • Second improvement
        • Pre-stated wish to enter the critical section
        • The deadlock will be busy waiting.
      • Third-time improvement
        • Make them express their attitude and understand "humility"
      • Dekker Mutex algorithm
      • Peterson method
    • Hardware algorithms
        • masking interrupts
          • constraints too high, too expensive
        • dedicated machine directives
          • Test and set directives
          • Exchange directives
          • Advantages: Simple, easy to prove and suitable for mutual exclusion of multiple processes in the multiprocessor system of a single processor system and shared memory;
          • Disadvantages : Busy and other phenomena still exist, but are acceptable, such as busy; starvation may occur
          • may cause deadlock
        • semaphore method
          • instance: Semaphore
          • Two or two processes can be Signaling to cooperate can force a process to temporarily stop executing (blocking wait) at a certain location, knowing that it is subject to a forward signal (awakened)
          • corresponding to a variable called semaphore, often defined as a record-type variable s, where one field is integer and the other field is a queue , whose element is the blocking process waiting for semaphores
          • semaphore definition
          • defines two atomic operations on semaphores: Wait (s) and signal (s); The early two primitives are called P (s) and V (s)
          • semaphore type: Mutex semaphore and resource semaphore
          • mutex semaphore used to request or release the use of resources, often initialized to 1
          • resource semaphore for application or return Resource, which can be initialized to a positive integer greater than 1, indicates the number of available
          • wait operations on a class of resources in the system to request resources (or rights to use), and the process may block itself when executing the wait primitive
          • Signal actions are used to free up resources (or to return resource rights), the process executes the signal primitive, it is the responsibility to wake up a blocking process
          • the physical meaning of the semaphore S.count represents the number of available resources
  • Producer/Consumer Issues
    • The producer and consumer must be mutually exclusive into the buffer. That is, at some point only one entity (producer or consumer) is allowed to access the buffer, the producer is mutually exclusive to the consumer and any other producer.
    • Producers cannot write data to full buffers, nor can consumers take data in an empty buffer, where producers and consumers must synchronize
  • Reader/writer questions
    • Readers first
    • Readers first
  • Pipe mode (object-oriented approach) (less application)
    • Tube structure
    • Implementing mutual exclusion with a pipe process
  • Message delivery
    • How the Process communicates
    • Common process Communication methods
      • Shared storage-based approach
      • How messages are delivered
        • Two primitives for communication between processes
          • Send (Destination,message)
          • Receive (Sourse,message)
        • Synchronization of Message Delivery
        • Three modes of synchronization
        • Blocking receive
        • Addressing in message delivery
        • Indirect addressing
        • Implement mutex with message delivery
        • Attention
        • Message delivery addresses producer consumer issues
      • Mail method

Operating System Principles learning notes--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.