Download Operating Systems Processes and more Lecture notes Process Control in PDF only on Docsity!
Operating Systems
Processes
Lecture 3
Michael O’Boyle
Overview
• Process
• Process control block
• Process state
• Context switch
• Process creation and termination
What’s “in” a process?
• A process consists of (at least):
- An address space, containing
- the code (instructions) for the running program
- the data for the running program (static data, heap data, stack)
- CPU state, consisting of
- The program counter (PC), indicating the next instruction
- The stack pointer
- Other general purpose register values
- A set of OS resources
- open files, network connections, sound channels, …
• In other words, everything needed to run the program
- or to re-start, if interrupted
A process’s address space (idealized)
0x 0xFFFFFFFF address space code (text segment) static data (data segment) heap (dynamic allocated mem) stack (dynamic allocated mem) PC SP
Representation of processes by the OS
• The OS maintains a data structure to keep track of a
process’s state
- Called the process control block (PCB) or process descriptor
- Identified by the PID
• OS keeps all of a process’s execution state in (or linked
from) the PCB when the process isn’t running
- PC, SP, registers, etc.
- when a process is unscheduled, the state is transferred out of the hardware into the PCB
- (when a process is running, its state is spread between the PCB and the CPU)
The PCB
• The PCB is a data structure with many, many fields:
- process ID (PID)
- parent process ID
- execution state
- program counter, stack pointer, registers
- address space info
- UNIX user id, group id
- scheduling priority
- accounting info
- pointers for state queues
• In Linux:
- defined in task_struct (include/linux/sched.h)
- over 95 fields!!!
PCBs and CPU state
• When a process is running, its CPU state is inside the CPU
- PC, SP, registers
- CPU contains current values
• When the OS gets control because of a …
- Trap: Program executes a syscall
- Exception: Program does something unexpected (e.g., page fault)
- Interrupt: A hardware device requests service
the OS saves the CPU state of the running process in that
process’s PCB
PCBs and CPU state
• When the OS returns the process to the running state
- it loads the hardware registers with values from that process’s PCB
- eg general purpose registers, stack pointer, instruction pointer
• The act of switching the CPU from one process to another
is called a context switch
- systems may do 100s or 1000s of switches/sec.
- takes a few microseconds on today’s hardware
- Still expensive relative to thread based context switches
• Choosing which process to run next is called scheduling
Process execution states
• Each process has an execution state, which indicates what
it’s currently doing
- ready: waiting to be assigned to a CPU
- could run, but another process has the CPU
- running: executing on a CPU
- it’s the process that currently controls the CPU
- waiting (aka “blocked”): waiting for an event, e.g., I/O completion, or a message from (or the completion of) another process - cannot make progress until the event happens
• As a process executes, it moves from state to state
- UNIX: run top , STAT column shows current state
- which state is a process in most of the time?
Process states and state transitions
running ready blocked trap or exception (I/O, page fault, etc.) interrupt (unschedule) dispatch / schedule interrupt (I/O complete) You can create and destroy processes create terminate
State queues
• There may be many wait queues, one for each type of wait
(particular device, timer, message, …)
head ptr tail ptr firefox (1365) emacs (948) ls (1470) cat (1468) firefox (1207) head ptr tail ptr Wait queue header Ready queue header These are PCBs
PCBs and state queues
• PCBs are data structures
- dynamically allocated inside OS memory
• When a process is created:
- OS allocates a PCB for it
- OS initializes PCB
- (OS does other things not related to the PCB)
- OS puts PCB on the correct queue
• As a process computes:
- OS moves its PCB from queue to queue
• When a process is terminated:
- PCB may be retained for a while (to receive signals, etc.)
- eventually, OS deallocates the PCB
init pid = 1 sshd pid = 3028 login pid = 8415 kthreadd pid = 2 sshd pid = 3610 pdflush pid = 200 khelper pid = 6 tcsch pid = 4005 emacs pid = 9204 bash pid = 8416 ps pid = 9298