S4 The Process
S4 The Process
CPUs?
• CPU virtualizing
• The OS can promote the illusion that many virtual CPUs exist.
• Time sharing: Running one process, then stopping it and running another
• The potential cost is performance.
1
A Process
A process is a running program.
• Comprising of a process:
• Memory (address space)
• Instructions
• Data section
• Registers
• Program counter
• Stack pointer
2
Process API
• These APIs are available on any modern OS.
• Create
• Create a new process to run a program
• Destroy
• Halt a runaway process
• Wait
• Wait for a process to stop running
• Miscellaneous Control
• Some kind of method to suspend a process and then resume it
• Status
• Get some status info about a process
3
Process Creation
1. Load a program code into memory, into the address space of the
process.
• Programs initially reside on disk in executable format.
• OS perform the loading process lazily.
• Loading pieces of code or data only as they are needed during program execution.
4
Process Creation (Cont.)
3. The program’s heap is created.
• Used for explicitly requested dynamically allocated data.
• Program request such space by calling malloc() and free it by calling
free().
5
Loading: From Program To Process
CPU Memory
code
static data
heap
stack
Process
Loading:
code Takes on-disk
static data program
heap and reads it into the
Program address space of
process
Disk
6
Process States
• A process can be one of three states.
• Running
• A process is running on a processor.
• Ready
• A process is ready to run but for some reason the OS has chosen not to run it at this
given moment.
• Blocked
• A process has performed some kind of operation.
• When a process initiates an I/O request to a disk, it becomes blocked and thus some
other process can use the processor.
7
Process State Transition
Deschedule
Running d Ready
Scheduled
Blocked
8
Data structures
• The OS has some key data structures that track various relevant
pieces of information.
• Process list
• Ready processes
• Blocked processes
• Current running process
• Register context
9
Process Concept
• An operating system executes a variety of programs:
• Batch system – jobs
• Time-shared systems – user programs or tasks
• Textbook uses the terms job and process almost interchangeably
• Process – a program in execution; process execution must progress in sequential
fashion
• Multiple parts
• The program code, also called text section
• Current activity including program counter, processor registers
• Stack containing temporary data
• Function parameters, return addresses, local variables
• Data section containing global variables
• Heap containing memory dynamically allocated during run time
10
Process Concept
(Cont.)
• 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
11
Process in Memory
12
Process State
• As a process executes, it changes state
• 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
13
Diagram of Process State
14
Process Control Block
(PCB)
Information associated with each process
(also called task control block)
• Process state – running, waiting, etc
• Program counter – location of instruction to next
execute
• CPU registers – contents of all process-centric registers
• CPU scheduling information- priorities, scheduling
queue pointers
• Memory-management information – memory
allocated to the process
• Accounting information – CPU used, clock time
elapsed since start, time limits
• I/O status information – I/O devices allocated to
process, list of open files
15
CPU Switch From Process to
Process
16