OS——process(Part One)
OS——Process(Part One)
A. What is process?
1.Process
– a program in execution; process execution must progress in sequential fashion.
Program is passive entity stored on disk (executable file), process is active.
– Program becomes process when executable file loaded into memory
- Execution of program started via GUI mouse clicks, command line entry
of its name, etc - One program can be several processes
– Consider multiple users executing the same program
2.A process includes:
– program code, known as text section
– data section, contains global variables.
– heap, containing memory dynamically allocated during run time.
– process stack, contains temporary data
• function parameters, return value
• return addresses
• local variables
– current activity, represented by the value of the program counter and the contents of the processor’s register.
3.Process State
As a process executes, it changes state
A process may be in one of the following states:
– new: The process is being created.
– running: Instructions are being executed.
– waiting: The process is waiting for some event to occur.
– ready: The process is waiting to be assigned to a processor.
– terminated: The process has finished execution.
Only one process can be running on any processor at any time, many processes may be ready and waiting
- if there is progress which is ready, there must be some progress running. So,there can be at most (n-1) progresses on ready state when
there are n progresses. The waiting ranges from 0 to n which can all
be waiting in the IO queue. - one state must follow the flow
4.PCB (Process Control Block, task control block)
- Each process is represented in the OS by a PCB
- PCB contains:
– Process state – ready, running, waiting, etc.
– Program counter – the address of the next instruction to be executed
– CPU registers – contents of all process-centric registers, e.g. index registers, stack pointers, general-purpose registers, condition-code, etc.
– CPU scheduling information- priorities, scheduling queue pointers
– Memory-management information – memory allocated to the process
– Accounting information – the amount of CPU time used, clock time elapsed since start, time limits, account numbers, etc.
– I/O status information – the list of I/O devices allocated to process, a list of open files, and so on.
– Process number – Identifiers, e.g. Identifier of this process , Identifier of its parent process, User identifier.
B.Progress Scheduling
1.queue
The objective of multiprogramming – to maximize CPU utilization.
The objective of time sharing – users can interact with each program while it is running.
– switch the CPU among processes frequently process scheduler, selects an available process for execution on the CPU.
On a single-processor system,
– Only one running process at most.
– the rest will have to wait until the CPU is free and can be rescheduled.
Job queue – set of all processes in the system.
Ready queue – set of all processes residing in main memory, ready and waiting to execute.
Device queues – set of processes waiting for an I/O device.
Representation of Process Scheduling
Queueing diagram represents queues, resources, flows
2.scheduling
- Long-term scheduler (or job scheduler) – selects which processes
should be brought into the ready queue which controls the number of
processes in memory, the degree of multiprogramming - Short-term scheduler (or CPU scheduler) – selects which process
should be executed next and allocates CPU.
- Primary distinction: execution frequency - Medium term scheduling is responsible for swapping (交换/倒换)
- swapping out: to reduce degree of multiprogramming, removing processes in ready or running states from main memory and into swapping section on disks the process that is swapped out is delayed to execute
- swapping in: reintroducing the processes on the disk into memory into memory, restarting their executing
3.Context Switch
The context is represented in the PCB of the process.
When CPU switches to another process, the system must save the state of the old process and load the saved state for the new process.
Known as a context switch.
– Save context of processor including program counter and other
registers
– Update the PCB of the process that is currently running
– Move PCB to appropriate queue - ready, waiting
– Select another process for execution (–scheduling)
– Update the PCB of the process selected
– Update memory-management data structures
– Restore context of the selected process
Context-switch time is overhead; the system does no useful work while switching.
Context-switch times dependent on hardware support.
C.Operations on Processes ——Process Creation & Process Termination
1.Process Creation
- reason:
• Submission of a batch job
• User logs on
• Created to provide a service such as printing
• Process creates another process - step
Assign a unique process identifier
Allocate space for the process
Initialize process control block (PCB)
Set up appropriate linkages
Create or expand other data structures
Resource sharing
– Parent and children share all resources.
– Children share subset of parent’s resources.
– Parent and child share no resources. - Execution
– Parent and children execute concurrently.
– Parent waits until some or all of its children terminate. - Address space
– Child is a duplicate of the parent.
– Child has a program loaded into it. - UNIX examples
– each process is identified by its process identifier, an unique integer
– system call fork creates new process
– system call exec used after a fork to replace the child process’ memory space with a new program.
In this part ,I don’ t understand the linux example of progress creating , then I do it myself .
Most of us must think it will only output two lines. But, the result is
four ! but why?
After fork,OS will duplicate a new child progress which is the same as his father. But they only share the same code paragraph ,and their data space is independent. They run at the same time and keep the step.