Lecture 03 - Processes
Lecture 03 - Processes
Program code
A set of data
Process Control Block
Contains the process elements
uniquely identifying the process
Created and managed by the OS
contains sufficient information that
allow to interrupt a running
process and later resume
execution
Allows OS to support multiple
processes
Dr. Noha Adly Operating Systems – CS x61 7
Process Control Block (PCB)
Information associated with each process
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
Dr. Noha Adly Operating Systems – CS x61
Process Execution
Consider three
processes being
executed
Dispatcher is a small
program which switches
the processor from one
process to another
All are in memory (plus
the dispatcher)
10
Timeout
I/O
emacs tcsch
ps
pid = 9204 pid = 4005
pid = 9298
UNIX examples
fork() system call creates new process
exec() system call used after a fork() to
replace the process’ memory space with a
new program
26
Dr. Noha Adly Operating Systems – CS x61
Using Two Queues
But when an event occurs the dispatcher must scan the entire
Blocked Queue to see which process is waiting for the event
Reason Comment
Swapping The OS needs to release sufficient main memory to
bring in a process that is ready to execute OR increase
the memory allocated to other Ready processes
Blocked Blocked/Suspend make room for another process that is not blocked
OR if currently running/ready process requires more main memory
Blocked/SuspendReady/Suspend: when the event occurs
Ready/SuspendReady: When there are no ready processes in main memory, or
if a suspended process has a higher priority
Ready Ready/Suspend: if that is the only way to free up a sufficiently large
block of main memory OR suspend a lower priority ready process for a higher
priority blocked process expected to unblock soon
New Ready/Suspend and New Ready: OS creates PCB for process and
move to Ready if enough main memory or ready/Suspend otherwise
Blocked/Suspend Blocked: seem to be poor design. But useful in case of high
priority blocked process expected to unblock soon
Running Ready/Suspend: free some main memory.
Any State Exit
Dr. Noha Adly Operating Systems – CS x61 33
Processes and Resources
OS manages the use of system resources by processes
Processes (P1, . . ., Pn,) created and exist in memory
Each process needs access to certain system resources
P1 is running; part in main memory and has control of 2 I/O
devices
P2 in main memory; blocked waiting for I/O device allocated to P1
Pn has been swapped out and suspended
Q: what information does OS need to control processes
and manages resources for them
To manage processes /
resources, OS must
have information about
the current status of
each process and
resource
Tables are constructed
for each entity OS
manage
Memory
I/O
File
process
User-Visible Registers
Typically, 16 to 32 registers that can be referenced by user
programs directly
Control and Status Registers
Program counter: contains the address of the next instruction
Condition codes: Result of the most recent arithmetic or logical
operation (e.g., sign, zero, carry, equal, overflow)
Status information: interrupt enabled/disabled flags, execution
mode etc.
Stack Pointers
each process has one or more LIFO system stacks which is used to store
parameters and calling addresses for procedure and system calls.
Dr. Noha Adly Operating Systems – CS x61 42
Program Status Word (PSW)
All processor designs include a PSW register
PSW contain condition codes plus other status information
A good example of a PSW is x86 EFLAGS register
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
I
V V
I A V R N O O D I T S Z A P C
0 0 0 0 0 0 0 0 0 0 I I 0 0 0 1
D C M F T P F F F F F F F F F
P F
L
Ready
Blocked
64
Process size Enables the operating system to know how much space to allocate
the process.
User identifiers The real user ID identifies the user who is responsible for the
running process. The effective user ID may be used by a process
to gain temporary privileges associated with a particular program;
while that program is being executed as part of the process, the
process operates with the effective user ID.
Process identifiers ID of this process; ID of parent process. These are set up when the
process enters the Created state during the fork system call.
Event descriptor Valid when a process is in a sleeping state; when the event occurs,
the process is transferred to a ready-to-run state.
P_link Pointer to the next link in the ready queue (valid if process is ready
to execute).