The process
The definition of a process:
it is a running program
But we all know when every time we uesing the desktop or laptop where we might run a web browswer, mail program ,a game ,Wechat and so forth.
At the same time ,a tyical system may be running huandreds of processes.
The crux of the probblem:
How to provide the illusion of many CUPS
Although computer in general are only a few physical CPUS available
How OS provide the illusion of a nearly-endless supply of said CPUS?
The answer is by runnin one process ,then stoping it and running another ,and so forth.(Known as time sharing of the CPU)
Time sharing:https://www.britannica.com/technology/time-sharing
Mechanisms and polices
Mechanisms are low-level methods or protocols that implement a needed piece of functionality,like the time-sharing.
Policies are algorithms for making some kind of decision within the OS.
An important principle of operating system design and implementation is the searation of policies and mechanisms.
So how to comprehend the mechanisms and the policies ?
The mechanism determines the how to do ,whlie the policie determine what to do ?
That’s to say the low-level mechanisms needed to implement processes,and the higher-level polices required to schedule them in an intelligent way.
In many operating systems ,a common design paradigm is to separate high-level policies from their low-level mechanisms.
What constitutes a process
we have to understand its machine state
One obvious component of machine state that comprises a process is its memory.
Also part of the process’s machine state are registers ,many instructions explicitly read or update registers and thus clearly they are important to the execution of the process.
Process API
Here we give some idea if what must be included in any interface of an operating system.
- Create: An operating system must include some method to crate new processes.When you type a command into the shell,or doubule-click on an applicationg icon, the OS is invoked to create a new process to run the program you have indicated
- Destroy: As there is an interface for process creationg,systems also provide an interface to destroy processes focefully.Of course,many process will run and just exit by themselves when complete; when they don’t,however,the user may wish to kill them,and thus an interface to halt a runaway process is quite useful.
- Wait: Sometimes it is useful to wait for a process to stop running;thus some kind of waiting interface is often provided.
- Status: There are usually interfaces to get some status informationabout a process as well, such as how long it has run for, or what state it is in.
Process Creation : A Little More Detail
The first one , let us look the figure which is about the loading (from program to process)
Both disk and memory are used for storage,so why do that in the figure?
They are all used for computer storage, but memory is the use of current to achieve storage, and disk is the use of magnetic effects to achieve storage, and, from the perspective of storage capacity, memory is high-speed and high-price, while disk is low-speed and cheap。
What’s more,the CPU can only execute what is in memory.
Now you just need to remember this process.
Process States
a process can be in one of three states:
- Running: In the running state, a process is running on a processor.This means it is executing instructions.
- Ready: In the ready state, a process is ready to run but for some reason the OS has chosen not to run it at this given moment.
- Blocked: In the blocked state, a process has performed some kind of operation that makes it not ready to run until some other event takes place. A common example: when a process initiates an I/O request to a disk, it becomes blocked and thus some other process can use the processor.
Process: State Transitions figure
Tracing Process State: CPU Only
Tracing Process State: CPU and I/O
If can’t understand what the above three figures. Look the book “Operating Systems Three Easy Pieces”,I don’t think words are needed to explain it here
Data Structures
The OS is a program, and like any program, it has some key data structures that track various relevant pieces of information.
The xv6 Proc Structure
// the registers xv6 will save and restore
// to stop and subsequently restart a process
struct context {
int eip;
int esp;
int ebx;
int ecx;
int edx;
int esi;
int edi;
int ebp;
};
// the different states a process can be in
enum proc_state { UNUSED, EMBRYO, SLEEPING,
RUNNABLE, RUNNING, ZOMBIE };
// the information xv6 tracks about each process
// including its register context and state
struct proc {
char *mem; // Start of process memory
uint sz; // Size of process memory
char *kstack; // Bottom of kernel stack
// for this process
enum proc_state state; // Process state
int pid; // Process ID
struct proc *parent; // Parent process
void *chan; // If !zero, sleeping on chan
int killed; // If !zero, has been killed
struct file *ofile[NOFILE]; // Open files
struct inode *cwd; // Current directory
struct context context; // Switch here to run process
struct trapframe *tf; // Trap frame for the
// current interrupt
};
Summary
• The process is the major OS abstraction of a running program. At
any point in time, the process can be described by its state: the contents of memory in its address space, the contents of CPU registers(including the program counter and stack pointer, among others),and information about I/O (such as open files which can be read orwritten).
• The process API consists of calls programs can make related to processes. Typically, this includes creation, destruction, and other useful calls.
• Processes exist in one of many different process states, includingrunning, ready to run, and blocked. Different events (e.g., gettingscheduled or descheduled, or waiting for an I/O to complete) transition a process from one of these states to the other.
• A process list contains information about all processes in the system. Each entry is found in what is sometimes called a processcontrol block (PCB), which is really just a structure that containsinformation about a specific process.