CS3101 Lecture 2

Process Description and Control

How are processes represented and controlled by the OS?

All modern OS is built around the concept of process

OS fundamental task: Process Management

Process:

  • A program in execution
  • An instance of a program running on a computer
  • The entity that can be assigned to and executed on a processor
  • A unit of activity characterized by the execution of a sequence of instructions, a current state, and an associated set of system resources

Process execution: a dispatcher - a small program which switches the processor from one process to another

All the process are in memory

The sequence of instructions that execute for a process is called a trace of the process

timeout---------

 


Process states which characterize the behavior of processes

Processes moved by the dispatcher of the OS to the CPU then back to the queue until the task is completed

 

Five-State Process Model

In ready, process has been loaded into main memory and is awaiting execution on a CPU

Blocked = Waiting, Processes may be blocked (e.g., waiting for an I/O operation).

Multiple blocked queues

Suspended state

Processor is faster than I/O, so all processes could be waiting for I/O -> processor could be idle. Therefore, swap these processes to disk to free up more memory or use on other processes. Blocked state becomes suspend state when swapped.

One Suspend State

When the event occurs, the process is potentially available for execution

Reason for Swapping: The OS needs to release sufficient main memory to bring in a process that is ready to execute

Two Suspend State

Notice: Blocked/Suspend and Ready/Suspend are in the disk, Ready and Blocked are in the memory

 


Data structures used to manage processes

OS manages the use of system resources by processes.

For the OS to manage processes and resources, it must have information about the current status of each process and resource. Tables are constructed for each entity the OS manages

-OS control Tables

-Memory Tables

Memory tables are used to keep track of both main (real) and secondary (virtual) memory.

Must include this information:

  • Allocation of main memory to processes
  • Allocation of secondary memory to processes
  • Protection attributes of blocks of main or virtual memory
  • Information needed to manage virtual memory

-I/O Tables

  • Used by the OS to manage the I/O devices and channels of the computer.
  • At any given time, an I/O device may be available or assigned to a particular process.
  • If an I/O operation is in progress, the OS needs to know:
    • The status of the I/O operation

    • The location in main memory being used as the source or destination of the I/O transfer

-File Tables

provide information about:

  • Existence of files
  • Location on secondary memory
  • Current status
  • Other attributes

-Process Tables

To manage and control a process, there is one entry for each process in the process table. Each entry points to a process image containing:

  • User data, The modifiable part of user space
  • User Program, The program to be executed
  • Stack, Each process has one or more last-in first-out(LIFO) stacks associated with it. A stack is used to store parameters and calling address for procedure and system calls.
  • Process Control Block, Data needed by the OS to control the process

 

Process Control Block (PCB)

Each process has associated with it a number of attributes that are used by the OS for process control. The attributes are stored in a data structure called a process control block (PCB), created and managed by the OS. It contains sufficient information so that it is possible to interrupt a running process and later resume its execution.

Information in a PCB:

  • Process identification
    • Each process is assigned a unique numeric identifier
  • Processor state information
    • Consists of processor registers’ content
  • Process control information
    • The additional information needed by the OS to control and coordinate the various active processes

 

Structure of Process Images in Virtual Memory

Process List Structure

 

Role of PCB

  • PCB is the most important data structure in an OS
    • Contains all of the information about a process that is needed by the OS

    • Read and/or modified by virtually every module in the OS such as scheduling, resource allocation, interrupt processing and performance monitoring

    • Defines the state of the OS

  • PCB requires protection
    • A faulty routine could damage PCBs, which could destroy the OS’s ability to manage the affected processes

    • Any design change to the PCB could affect many modules of the OS 

 


Ways in which the OS uses these data structures to control process execution

Most processors support at least two modes of execution to protect the OS and key OS tables from interference by user programs:

  • User mode
    • Less-privileged mode
    • User programs typically execute in this mode
  • System mode (control mode or kernel mode) 
    • More-privileged mode
    • Kernel of the operating system (central module of an OS)
      • Loads first when the system starts
      • Resides in memory (in a protected area) all the time

Process Creation

  1. Assigns a unique process identifier and adds a new entry to the process table
  2. Allocates space for the process (process image) Initializes process control block
  3. Sets up appropriate linkages such as putting the new process in the Ready list
  4. Creates or expands other data structures such as an accounting file for performance assessment 

Process Switching

When CPU switches to another process, the system must save the state of the old process and load the saved state for the new process 

Process-switch time is considered as overhead (the system does no useful work while switching)

When to Switch Processes

System calls: Mostly accessed by programs via a high-level Application Programming Interface (API).

Process Switching steps:

  1. Save context of processor including program counter and other registers
  2. Update the PCB of the process currently in the Running state
  3. Move the PCB of this process to appropriate queue – ready; blocked; ready/suspend
  4. Select another process for execution
  5. Update the PCB of the process selected
  6. Update memory management data structures
  7. Restore context of the processor to that which existed at the time the selected process was last switched out.

 

 

The occurrence of an interrupt does not necessarily mean a process switch. It is possible that, after the processor switches from user mode to kernel mode in order to execute the interrupt handler (which may include privileged instructions), the currently running process will resume execution. In such a mode switching case, only need to save/restore the processor state information

 


 

Discuss process management in UNIX

Unix

  • System processes run in kernel mode 
    • executes operating system code to perform administrative and housekeeping functions

  • User processes
    • operate in user mode to execute user programs and utilities

    • operate in kernel mode to execute instructions that belong to the kernel

    • enter kernel mode by issuing a system call, when an exception is generated, or when an interrupt occurs

Process Creation

Process creation is by means of the kernel system call, fork( ).

This causes the OS, in kernel mode, to:

  1. Allocate a slot in the process table for the new process.
  2. Assign a unique process ID to the child process.
  3. Make a copy of the process image of the parent, with the exception of any shared memory.
  4. Increment the counters for any files owned by the parent, to reflect that an additional process now also owns those files.
  5. Assign the child process to the Ready to Run state.
  6. Return the ID number of the child to the parent process, and a 0 value to the child process.

After Creation

After creating the process, the Kernel can do one of the following, as part of the dispatcher routine:

  • Stay in the parent process - Control returns to user mode at the point of the fork call of the parent
  • Transfer control to the child process  - The child process begins executing at the same point in the code as the parent, namely at the return from the fork call
  • Transfer control to another process  - Both parent and child are left in the Ready to Run state

 

posted @ 2018-01-25 10:15  Charonnnnn  阅读(345)  评论(0编辑  收藏  举报