OS: Note of Chapter 3
OS Management of Application Execution
- Resources are made available to multiple applications, the processor is switched among applications so all will appear to be progressing. The processor and I/O devices can be used efficiently.
If there is only one program or only several sequential programs, the OS is factually not needed.
Then in fact the OS is designed for the programs can be interrupted and resumed, typically for multiple tasks running simultaneously.
Process Elements
- While the program is executing, this process can be uniquely characterised by a structure, called PCB (Process Control Block). In the primitive Linux, it is factually called TCB (Task Control Block).
- Alongside the operating system is more and more complicated, the PCB structure is more and more complicated as well.
Process Models
2-State Process Model
- States: Running, Not Running
- How to change states?
- Running -> Not Running: Time slice expired, I/O request, etc.
- When the time slice expired, it will be pushed back to a queue awaiting the next dispatch.
- Dispatch: The process is selected to run.
- Not Running -> Running: Dispatching, I/O completion, etc.
5-State Process Model
- States: New, Ready, Running, Blocked, Exit
- New: The process is being created.
- Ready: The process is ready to run (can be dispatched).
- Running: The process is running (using the PC).
- Blocked: The process is waiting for an event to occur.
- Exit: The process has finished.
Why Interrupt is necessary?
The CPU is too fast, and the I/O devices are too slow relatively. And there’s only 1 CPU, but have to serve for plenty of processes.
The 5-state model is better, because in the 2-state model, the process is either running or not running, but the not-running state may be not ready, the dispatch request may be a waste of time.
The dispatcher is also a program, so it can also have instances, aka. process.
Between the CPU running any 2 processes which are not the dispatcher, the dispatcher is running.
- Queue structure: There are 2 queues for the processes in the ready and blocked states, respectively. The dispatcher will select the process from the ready queue to run, and event wait pushes the process to the blocked queue, and the timed-out process will be pushed back to the ready queue. When a blocked process is unblocked by event, it will be pushed back to the ready queue.
- There is also a disadvantage: The blocked queue is usually long, meaning that CPU should traverse the whole block, which is contradictory to the objective of increasing the efficiency of the CPU.
- The solution is use more queues, to categorise by event. When an event waits, this process is pushed back to the respective queue, and when the event occurs, the process popped from the respective queue as well, reducing the time of traversing the queue.
Suspend
- The queues have limited space, so the processes in the queue may be OOM (out of memory). The solution is to suspend the blocked processes to the disk, aka. swap out, leaving some more memory space for the new or ready processes.
- Also it may happen when the ready queue is full, the situation is almost the same, but the ready processes are swapped to another suspend queue.
- The running processes can also be suspended, Then this is the 7-state model.
4 Foundamental Problems of OS
Until now, we have introduced the 4 foundamental problems of OS. An OS should manage memory, devices, files, and processes, then the 4 problems are the management of these 4 resources.
Structure of Process Images
A process occupies some computer hardware resources, and this consists of PCB, the user stack, the private address space (programs, data) and the shared address space (shared libraries, etc.).
User stack
- The user stack is used to store the next PC for function calling. When call another function, the next PC is pushed to the user stack, and when the function returns, the next PC position is popped to the PC pointer again.
- So the user stack is for only 1 process, the process cannot push the next PC to another process’s user stack, either the system kernel’s stack.
Final Words
So, eventually, learning OS need the thinking to dive into the hardware’s view, and then the software’s view, finally the user’s. Stay in the high position of a computer system can never understand essentially how the OS works.