Background information:    Process

  • A computer program is a passive collection of instructions stored in a file on disk

  • Process = a running program
                  = a computer program that is being executed

  • A computer system can have multiple processes:

  • Goal of studying a process

    • Understand how to pause a running program (= process)

Background information:    Process

Difference between a process and a program: (from Wikipedia)

Program = static and makes no progress
Process = dynamic (being run on a CPU) and has a process state

Background information:    Current Process

  • Current Process = the process that is currently using the CPU to execute its instructions

    I.e.: the process from which the CPU is fetching and executing instruction is the current process

  • Example:

    Note:

    • The PC always points to an instruction inside the current process !

Background information:   progress in program execution

  • The are many processes in a computer system

  • For a single core CPU computer, there is exactly one current process

    • Let's assume the computer hss a single core CPU for simplicity

    (In multi-processor systems, the number of current processes is equal to the number of CPUs)

  • Only the current process makes progress in its program execution (because the CPU only executes instructions fetched from the current process)

    • All other processes are paused !!!


  • Important fact from CS255:

    • The current process is using CPU registers in its execution !!

    This fact is important for how to pause the current process !

Background information:   switching current process
 

  • Fact:

      • The computer system (Operating System) has the ability to switch current processes

    Example: the state before a "context switch" operation

The process (program) P2 is currently running (= making progress)

Background information:   switching current process
 

  • Fact:

    • The computer system (Operating System) has the ability to switch current processes

    Example: the state after a "context switch" operation

The process P2 has temporally stopped running !!

Background information:   switching current process
 

  • Fact:

      • The computer system (Operating System) has the ability to switch current processes

    The old process can be switch back and resume its execution:

The process (program) P2 will resume from the paused program location !!!

How to switch the current process ?
 

What does it take to switch the current process:

  1. We must stop the current process (= the currently running program) "carefully":

      • We must stop it in such a way that the stopped process can be restarted (= resumed) at a later time !!!

    How to stop the current process:

    • We must save the necessary information in order for the current process to resume

    • The data structure used to save the process' information is called the Process Control Block (PCB)

  2. After stopping the current process, we must restart a (previously stopped) process (this process will become the new current process)

How can we stop a running program so that it can be restarted at a later time ???

Suppose the process P1 is current running (current process):

Question: what data does the computer system need to have to restart a running program ??

How can we stop a running program so that it can be restarted at a later time ???

Obvious answer:   the computer instructions of process P1

Assumption: we keep all instructions of the process in RAM.   Anything else we need ?

( Note:   the OS can save the running program on disk to free up some memory if needed)

How can we stop a running program so that it can be restarted at a later time ???

Another data needed:   the location of the current computer instruction (where to resume)

Solution: we save the PC in a Process Control Block data structure     Anything else we need ?

Note: we will see later that we actually need to save the LR register (it calls the OS to perform IO)

How can we stop a running program so that it can be restarted at a later time ???

Finally:   the values in the general purpose registers and the PSR (temporal results !)

Solution: we also save all these registers in the Process Control Block data structure

Process context (= context information) and context switching
 

  • Process context (or context information) = all information needed to pause a running (current) process so it can be restarted at a later time

  • The context information consists of:

      • LR (the PC was saved as the return address in the LR register)
      • PSR
      • All the general purpose registers


  • Context switching consists of the following operations:

      1. Saving the context information of the current process into its PCB (Process Control Block)

      2. Load the context information from another PCB (Process Control Block) into the general purpose registers, PSR and LR in the CPU

    Note that:   the LR register is loaded with a new return address !!!

The effect of a context switching operation
 

The effect achieved by a context switching operation is very remarkable:

  1. Saving the context information of the current process into its PCB (Process Control Block)

      • This step in the context switching operation will pause the current process in a manner that it can resume at a later time from the paused location

  2. Load the context information stored inside another PCB (Process Control Block) into the general purpose registers, PSR and PC

      • This step in the context switching operation will resume a previously paused process so it becomes the new current (running) process

The effect of a context switching operation - graphically explained

Before a context switching operation:

After a context switching operation:

DEMO: demo/Co-process/main.c

Analogy of context switching

  • Imagine a number of people is sharing a single bedroom living space where you cannot have 2 or more people living/using in the living space at the same time

  • The context switching technique is like using stasis pods:

               

  • Exactly 1 person is awake from stasis at any one time (= the current process)

    All other persons are in stasis (= all other processes are not running and do not make progress !!)