Review: what's the problem...
 

Problem description:

  • When a program performs an input (IO) operation, the program cannot continue running:

      • Because the input data will take time to be read from an input device into the memory !

      • If the program continues execution, the program will operate with invalid data - the data is not yet available !

  • The computer system must pause the current running program that has performed an input operation !!!

Review:   How to pause a running program (= current process) ?

  • Fact:

      • The CPU will always execute the instruction execution cycle

          • I.e.: the CPU must (always) execute an instruction from some program !!!

      • Pausing the current running program will also mean:

          • Run another (previously paused) program !!!       

  • In the last set of slides, we learned:

      • The context switching operation will (1) pause the current process and (2) restart/resume a previously paused process to run again

Review: context information
 

  • A program consists of computer instructions

  • A process = is a program that is being executed by the computer

  • A process consists of:

      1. The program instructions (assumed to be stored in memory for simplicity)

      2. The context information consisting of the values in the general purpose registers , PSR and LR (= PC)


  • The context information of the current process (= runs on CPU) is contained in the registers inside the CPU

  • The context information of a paused process is stored (saved) in its Process Control Block (PCB) ( Wikipedia)

Background information:   Operating System

  • Operating System = a very complex computer program that manages all the resources inside a computer

  • Sample operating systems:

      • Microsoft Windows
      • MacOS (they have very colorful names, like Leopard, Tiger, etc)
      • Linux (e.g.: Ubuntu, Debian,...)

  • A computer user (like you) always interacts with a computer through its Operating System

    Without an Operating System, a computer is practically useless !!!

  • An important task performed by the Operating System:

      • Manage the processes running inside the computer (this subsystem of the operating system is called the process manager)

The process manager of the Operating System
 

  • The process manager of the Operating System manages processes (= running programs)

  • The Operating System is a program and it is contained in the memory:

           

The process manager of the Operating System
 

  • Recall that for simplicity, we assumed that the program instructions of the running programs (= processes) are always kept in the memory:

           

The process manager of the Operating System
 

  • The context information of the running programs (= processes) are stored in PCB objects managed by the the process manager:

           

  • The processes can be categorized in to 2 broad categories: see next page...

Ready and not ready processes
 

  • Processes within the computer system can be categorized into:

      • Ready process:

          • A ready process is not waiting for any "event" to complete and it can become the current process

      • Not-ready process:

          • A not-ready process is waiting for some "event" (e.g.: an IO operation to finish) to complete before it can continue its execution

          • Such a process cannot become the current process !

Ready processes and the Ready Queue
 

  • The process manager of the Operating System organizes the PCBs of Ready Processes into a "Ready" queue:

           

    The Ready queue contains the PCB objects of only the Ready Processes.

    It's also known as the Run Queue (Wikipedia)

The Current Process
 

  • The PCB of current process (= the process that is currently running and using the CPU) is always at the head of the ready queue:

           

    The CPU will always fetch and execute instructions from the current process - so PC must point to an instruction in the current process' code area !

Not-ready processes and the Device Queues
 

  • A process that have request an IO operation to an IO device is not "ready" (to run), and it's PCB entered into the "wait" queue of the associated DMA

           

  • The process manager maintains a waiting queue of processes for each IO device

    The queue contains the PCB objects of waiting (= Not-Ready) processes is called a Device Queue

IO operations = making a service call to the Operating System

  • IO operations (e.g.: Java's System.out.print( ) and Scanner.next( ) methods) are subroutine calls to "service routines" inside the Operating System:

           

    Example:   the Scanner object contains buffer variables and the call in.next( ) will call OS_Read(buffer) in the OS to read the next input into its buffers

What actions are taken by the Operating System in an IO operation ?
 

Recall that when a program performs an input operation,

  // Computer program (e.g.: Java)

  i = inputDev.next( ); // Performs an input operation

  i = i + 1; // Must use correct input value !!!
  

the program must wait until the data has been read into the memory before the program can continue execution

  • A program that performs an Input Operation cannot continue running until the IO operation is completed !!!

 

We will now study how to pause the current process that has performed an input (IO) operation

What actions are taken by the Operating System in an IO operation ?

The method call inpuDev.next( ) is translated by the compiler to call a service routine (say: OS_Read) inside the Operating System (with a buffer inside the Scanner object):

What actions are taken by the Operating System in an IO operation ?

The OS service routine OS_Read() will initialize the DMA to perform the IO operation:

  OS_Read: (input params: R0 = length, R1 = buffer address in Scanner object)


     // Program the DMA to READ 'length' (R0) bytes 
     // into memory location R1
  2. Write to DMA length  register = R0
     Write to DMA address register = R1
     Write to DMA command register = READ (1)








 

Remember:   the current process must be paused !! (Because the input is not ready)

What actions are taken by the Operating System in an IO operation ?

We paused the current process by saving its context information in its PCB:

  OS_Read: (input params: R0 = length, R1 = buffer address in Scanner object)

  1. Save the context information from the CPU registers
     into the first element of the ReadyQ

  2. Write to DMA length  register = R0
     Write to DMA address register = R1
     Write to DMA command register = READ (1)








  

What actions are taken by the Operating System in an IO operation ?

We move the PCB of the blocked process to the DMA-Queue (device queue):

  OS_Read: (input params: R0 = length, R1 = buffer address in Scanner object)

  1. Save the context information from the CPU registers
     into the first element of the ReadyQ

  2. Write to DMA length  register = R0
     Write to DMA address register = R1
     Write to DMA command register = READ (1)

  3. Remove the first PCB (= running process) of the ReadyQ 

     Insert it into the DMA queue




  

This represents that fact that the process is waiting for the DMA to finish

What actions are taken by the Operating System in an IO operation ?

The OS_Read( ) routine then load the context information of the first "ready-to-run" process in the Ready queue:

  OS_Read: (input params: R0 = length, R1 = buffer address in Scanner object)

  1. Save the context information from the CPU registers
     into the first element of the ReadyQ

  2. Write to DMA length  register = R0
     Write to DMA address register = R1
     Write to DMA command register = READ (1)

  3. Remove the first PCB (= running process) of the ReadyQ 

     Insert it into the DMA queue

  4. Load the context information in the new first element
     of the Ready Queue into the CPU registers


Recall that:   the first PCB in the ready queue will always be the (new) current process.

What actions are taken by the Operating System in an IO operation ?

At last, the OS_Read subroutine will return: (where will it jump to ?)

  OS_Read: (input params: R0 = length, R1 = buffer address in Scanner object)

  1. Save the context information from the CPU registers
     into the first element of the ReadyQ

  2. Write to DMA length  register = R0
     Write to DMA address register = R1
     Write to DMA command register = READ (1)

  3. Remove the first PCB (= running process) of the ReadyQ 

     Insert it into the DMA queue

  4. Load the context information in the new first element
     of the Ready Queue into the CPU registers

  5. "Return" (mov pc, lr)

Hint:   the link register LR contains the (paused) location of the new ready process !

What actions are taken by the Operating System in an IO operation ?

I will show you the effect of the OS_Read routine using a series of figures

This is the initial state when the current process has just called: inputDev.next( ).

What actions are taken by the Operating System in an IO operation ?

The bl OS_Read instruction will start running the OS_Read routine code:

Recall that bl instruction will save the return address into the LR register (=part of the context information)

What actions are taken by the Operating System in an IO operation ?

(1) Save the context information (registers !) from CPU into first element in the Ready Queue

This step enables the Operating System to pause the process P1 and restart P1 at a later time

What actions are taken by the Operating System in an IO operation ?

(2) Start the DMA to transfer data from IO device to P1's buffer:

Now the process P1 cannot continue !!! (Because the data transfer is not yet completed)

What actions are taken by the Operating System in an IO operation ?

(3) Remove P1's PCB from the Ready Queue and insert it into the DMA (device) queue:

The step enable the Operating System to find P1's PCB when the DMA transfer is done

What actions are taken by the Operating System in an IO operation ?

(4) Use P2's PCB (1st PCB in the Ready Queue) to update the registers in the CPU:

The step enable the Operating System to find P1's PCB when the DMA transfer is done

What actions are taken by the Operating System in an IO operation ?

(5) OS_Read (is a subroutine) returns:

Notice that LR points to an instruction inside the process P3 !!!

What actions are taken by the Operating System in an IO operation ?

Result of the InputDev.next( ) input operation:

Notice that: (1) P3 is new current process and (2) the IO data transfer is not yet completed

One unsolved problem: how to restart a process after its IO operation is completed ???

Suppose the input operation completes:   P1 can now continue...

Notice that: P1 is not in the Ready Queue (only processes in the Ready Queue will be executed)