OS Review Chapter1: Introduction
OS Review Chapter 2: Computer-System Structures
OS Review Chapter 3: Operating-System Structures
OS Review Chapter 4: Process
OS Review Chapter 5: Thread
Chapter 4: Processes
Introduce process-> describe CPU activity-> study CPU activity-> improve CPU utilization
What is a Process-> One execution process of the program
Process – a program in execution; process execution must progress in sequential fashion.
A process includes:
◆ program counter
Where did the program go?
◆ contents of the processor’s registers
State
◆ stack
Local variables
◆ data section
Global Variables
Process in Memory
text
: Program (binary)
heap
: Dynamically allocated memory
Process State (process life cycle)
Process Control Block (PCB)
PCB storage place:内存 内核段
Information associated with each process:
Process state
Program counter where to run the program (context)
CPU registers (context)
CPU scheduling information
Memory-management information related addresses accessed by the program
Accounting information audit CPU time
I/O status information
Process Scheduling Queues
Job queue – set of all processes in the system.
Ready queue – set of all processes residing in main memory, ready and waiting to execute. All processes that can be executed on CPu are processes scheduled by the scheduler
Device queues – set of processes waiting for an I / O device. Processes in the waiting state
Process migration between the various queues.
Schedulers
Long-term scheduler (or job scheduler) – selects which processes should be loaded into memory for execution. 决定进入队列的进程 (may be slow)The long-term scheduler controls the degree of multiprogramming.
Short-term scheduler (or CPU scheduler) – selects which process should be executed next and allocates CPU. 决定CPU执行的进程 (must be fast).
Processes can be described as either:
- I/O-bound process
- CPU-bound process
Context Switch
What is a process context?
The context of a process includes the values of CPU registers, the process state, the program counter, and other memory/file management information.
After the CPU scheduler selects a process (from the ready queue) and before allocates CPU to it, the CPU scheduler must :
- save the context of the currently running process
- put it into a queue
- load the context of the selected process
- let it run
Operations on Processes
Process Creation
Parent process create children processes, which, in turn create other processes, forming a tree of processes.
- Resource sharing
- Execution
- The address space of the child process and the parent process are different
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
int main(int argc,char *argv[])
{
int i,id1,id2;
for(i=1;i<2;i++)
{
id1=fork();
id2=fork();
if(id1==0||id2==0)
fork();
}
printf(" I am %d\n",getpid());
}
I am 3696
I am 3697
I am 3698
I am 3699
I am 3701
I am 3702
I am 3700
Process Termination
Process executes last statement and asks the operating system to delete it (exit).
- Output data from child to parent (via wait).
- Process’ resources are deallocated by OS.
Parent may terminate execution of children processes (abort).
- Child has exceeded allocated resources.
- Task assigned to child is no longer required.
- Parent is exiting. (Operating system does not allow child to continue if its parent terminates)
Cooperating Processes
Advantages of process cooperation :
◆Information sharing
◆Computation speed-up
◆Modularity
◆Convenience
Producer-Consumer Problem
Context
OS Review Chapter1: Introduction
OS Review Chapter 2: Computer-System Structures
OS Review Chapter 3: Operating-System Structures
OS Review Chapter 4: Process
OS Review Chapter 5: Thread