The OS manages all the resources in the computer system.
Some common examples of available operating systems are:
A good OS will always try to make the computer system run as efficiently as possible, meaning: do not waste any resource unless it is absolutely necessary.
I will only expose that part of the OS that is relevant for managing IO communication so that the CPU (a resource managed by the OS) is not wasted when a program cannot continue with its execution.
The different programs are stored at different locations in the computer memory.
The phrase "give control of the CPU to program X" means that the CPU will fetch and execute instructions pertaining to the program X.
(If you know how the CPU works, you should immediately conclude that the above sentence means that the program counter contains an address value that is in the range of the address locations occupied by program X).
The PCB at the head of the Ready Queue always pertains to the program that is currently executing (i.e., the CPU is fetching and executing instructions from that program).
A "process" is a program that is being executed by the computer
It is important to know the difference between a "process" and a "program".
|
The process state consists of all the information other than the program itself that is necessary to continue running the program if the program is halted at a particular point.
In other words: suppose you pull the plug on the computer that is running a program (say some video game). The process state is all the information that is necessary to allow you to restart the computer and continue with that program from the moment when you pull the plug.
Context:
|
The context infomation consists of the following information in the CPU:
|
In other words:
|
Saving context and restoring context:
|
BTW, in class, I often use the term "freeze a process" to mean "saving the context of the process" - I got that from some science fiction story which proposed to have humans who are suffering from currently incurable deseases to be frozen until the time that we have develop a cure for such deseases and at which time to "unfreeze" the bodies to be treated (don't ask me which science fiction book it was, too long ago...)
A process is ready if the program can continue its execution (from a state in which the context was saved) without resulting in execution errors.
In our discussion, a process is ready if it is not performing an IO operation.
(Because if the process is performing an IO operation - such as reading data from disk - then the data is not ready yet and it you allow such program to continue, the execution will not compute the correct value (based on the principle of GIGO - garbage in, garbage out - wrong data in, wrong data out).
|
Therefore, (because instructions modify registers), the current content of the registers, the PC and PSR are process state of program P1.
read(buffer, inputfile, numberOfBytes);in a high level programming language which will be translated into the following assembler instructions:
move.l #buffer D0 // Pass parameters move.l inputfile, D1 move.l numberOfBytes, D2 jsr OS_Read // Call subroutine in OS to // to handle the read IO transfer |
There exists many different kinds of service routines, ranging from routines to set the clock in the computer to aborting a program (the "kill" command in UNIX invokes such a service routine).
The service routines are called system calls (in UNIX) or supervisory calls by IBM.
A service routine is a program (subroutine) that performs a specific function.
In our example, the OS_Read service routine must perform a read operation on behave of the requesting program (P1). The OS_Read routine will looks like this:
OS_Read: (Uses D1 to tell the DMA to go to a particular part of the disk that contains the data for the specific input file) move.l D0, 2012 // Program Address register of DMA move.l D2, 2016 // Program Count register of DMA move.l #1, 2004 // Assumed: command code for IO write ..... (to be continued) <--------- point A |
We have seen these assembler instruction before (see click here). Recall that we have assumed that:
Therefore, program P1 is no longer ready.
Therefore, the OS must stop program P1, but:
|
From the previous discussion, you would conclude that you need to:
|
(PC = program counter, PSR = processor status register)
The place where you want to save the context information is the PCB pertaining to the program P1:
|
After saving P1's context, you must remove the PCB from the Ready Queue (because P1 is not ready and the Ready Queue only contains PCBs of processes that are ready).
The PCB must be entered into the device queue of the device that is servicing the IO request. The reason for this will be clear in part 2 where we discuss a technique to reclaim the CPU when the IO operation is finish.
Finally, a new ready program is started by restoring the context from a PCB structure in the Ready Queue.
OS_Read: (I will use psuedo code here because assembler code will be very tedious - I rely heavily on your knowledge of linked lists from CS170 and/or CS253) 1. Save the context into the first element of the ReadyQ 2. Program the DMA to perfrom the IO operation (Uses D1 to tell the DMA to go to a particular part of the disk that contains the data for the specific input file) move.l D0, 2012 move.l D2, 2016 move.l #1, 2004 ) 3. Remove the first element of the ReadyQ and insert it into the DMA queue 4. Restore the context using the values in the (new) first element of the ReadyQ (Note that this first element is actually the second element in the "old" ReadyQ because the first element has been removed in step 3. |
Notice that:
|
|