Title: Processes: Description and Control
1ProcessesDescription and Control
2Processes
- A process is a program in execution process
execution must progress in sequential fashion.
Can be characterized by its trace. - A process requires resources, which are managed
by the operating system - The OS interleaves the execution of several
processes to maximize processor utilization - OS supports InterProcess Communication (IPC) and
user creation of processes
3Process
4Process Traces
5Running System
6Simple Two-State Model
7Queuing Diagram
Dispatch
Queue
Exit
Enter
Pause
(a) Queuing diagram
- Dispatcher
- Program that assigns the processor to one process
or another - Prevents a single process from monopolizing
processor time
85 Process States
- As a process executes, it changes state
- New The process is being created.
- Running Instructions are being executed.
- Waiting or blocked The process is waiting for
some event to occur. - Ready The process is waiting to be assigned to
a process. - Terminate or Exit The process has finished
execution.
9Diagram of Process State
10Example for 3 Processes
11Suspending Processes
12Two Suspended States
13OS Control Structures
Memory Tables
Process Image
Memory
I/O Tables
User data User program System stack PCB
I/O
File
File Tables
Processes
Primary Table
Process 1
Process 2
Process N
14Process Control Block (PCB)
- Information associated with each process.
- Process state
- Program counter
- CPU registers
- CPU scheduling information
- Memory-management information
- Accounting information
- I/O status information
15Process Scheduling Queues
- 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. - Process migration between the various queues.
16Ready Queue, I/O Device Queues
17Process Scheduling
18Schedulers
- Long-term scheduler job scheduler
- selects which processes should be brought into
the ready queue. - invoked infrequently (seconds, minutes)
- controls the degree of multiprogramming
- Medium-term scheduler
- allocates memory for process.
- invoked periodically or as needed.
- Short-term scheduler CPU scheduler
- selects which process should be executed next and
allocates CPU. - invoked frequently (ms)
19CPU Context Switch
20Context Switch
- 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. - Context-switch time is overhead the system does
no useful work while switching. - Time dependent on hardware support.
21Process Creation
- Parents create children results in a tree of
processes. - Resource sharing
- Parent and children share all resources.
- Children share subset of parents resources.
- Parent and child share no resources.
- Execution
- Parent and children execute concurrently.
- Parent waits until children terminate.
22Process Creation (Cont.)
- Address space
- Child duplicate of parent.
- Child has a program loaded into it.
- UNIX examples
- fork system call creates new process
- execve system call used after a fork to replace
the process memory space with a new program.
23Process Termination
- Process executes last statement (exit).
- Output data from child to parent (via wait).
- resources deallocated by operating system.
- Parent may terminate execution of children
processes (abort). - Child has exceeded allocated resources.
- Task assigned to child is no longer required.
- Parent is exiting.
- Operating system does not allow child to continue
- Cascading termination.
24Cooperating Processes
- Independent process cannot affect or be affected
by the execution of another process. - Cooperating process can affect or be affected by
the execution of another process - Advantages of process cooperation
- Information sharing
- Computation speed-up
- Modularity
- Convenience
25Typical UNIX System
26A Traditional UNIX Kernel
execution environment
application
trap
libraries
user
System call interface
kernel
System Services
File subsystem
dispatcher
IPC
Process control subsystem
Buffer cache
Scheduler
Interrupt
Exceptions
Memory
hardware
27Basic Concepts and Terminology
- Two privilege levels
- user and system mode
- kernel space protected requires special
instruction sequence to change from user to
kernel mode - Process has "protected" address space
- all process share same kernel space
- per process state (u_area) in kernel
- per process kernel stack
- kernel is re-entrant
- system versus process context
28Mode, Space and Context
Privileged
user
kernel
mode
context
process
Application (user code)
System calls Exceptions
kernel
X not allowed
Interrupts, System tasks
system space
29The UNIX Kernel
- loaded at boot time and initializes system,
- creates initial system processes.
- remains in memory and manages the system
- Resource manager/mediator -
- process is key abstraction
- Time share (time-slice) the CPU,
- coordinate access to peripherals,
- manage virtual memory.
- Performs privileged operations.
- provides synchronization primitives.
- Well defined entry points
- syscall, exceptions or interrupts.
30Entry into the Kernel
- Synchronous - kernel performs work on behalf of
the process - System call interface (UNIX API) central
component of the UNIX API - Hardware exceptions - unusual action of process
- Asynchronous - kernel performs tasks that are
possibly unrelated to current process. - Hardware interrupts - devices assert hardware
interrupt mechanism to notify kernel of events
which require attention (I/O completion, status
change, real-time clock etc) - System processes - scheduled by OS
- swapper and pagedaemon.
31Trap or Interrupt Processing
- Hardware switches to kernel mode, using the
per/process kernel stack. - HW saves PC, Status word and possibly other state
on kernel stack. - Assembly routine saves any other information
necessary and dispatches event. - On completion a return from interrupt (RFI)
instruction is performed.
32Interrupt handling
Asynchronous event. Name three? Must be serviced
in system context, What does this mean? Must not
block, Why? What happens to the currently
running process?
33Interrupt handling - some details
- Multiple interrupt priority levels (ipl),
traditionally 0-7. - User processes and kernel operate at the lowest
ipl level. - Higher level Interrupts can preempt lower
priority ones. - The current ipl level may be set while executing
critical code in order to block (or mask) higher
priority interrupts.
34Exception handling
- Synchronous to the currently running process.
- What is an example?
- Must run in the current processes context.
- Why?
- An Interrupt may occur during the processing of
an exception or trap. - Could this be a problem?
35Software Interrupts
- Interrupts typically have the highest priority in
a system. - Software interrupts are assigned priorities above
that of user processes but below that of
interrupts. - Software interrupts are typically implemented in
software. - Examples are callout queue processing and network
packet processing.
36System Call Interface
- System call API
- Why do we need one?
- Implemented as a set of assembly language stubs
- Trap into kernel dispatch routine which invokes a
high-level system call. - User privileges are verified and any data is
copied into kernel (copyin()). - On return, copy out any user data (copyout()),
check for an Asynchronous System Trap (signal,
preemption, etc).
37Traditional UNIX Synchronization
- Kernel is re-entrant.
- only one thread/processes active at any given
time (others are blocked). - Nonpreemptive.
- Blocking operations
- Masking interrupts
38Blocking Operations
- When resource is unavailable (possibly locked),
process sets flag and calls sleep() - sleep places process on a blocked queue, sets
state to asleep and calls swtch() - when resource released wakeup() is called
- all sleeping processes are woken and state set to
runnable (placed on the runnable queues). - When running, process must verify resource is
available. Why?
39Process Scheduling
- Preemptive round-robin scheduling
- fixed time quantum
- priority is adjusted by nice value and usage
factor. - Processes in the kernel are assigned a kernel
priority (sleep priority) which is higher than
user priorities. - Kernel 0-49, user 50-127.
40Signals
- Asynchronous events and exceptions
- Signal generation using the kill() system call
- Default operation or user specific handlers
- sets a bit in the pending signals mask in the
proc structure - All signals are handled before return to normal
processing - System calls can be restarted
41The Process
- Fundamental abstraction
- Executes a sequence of instructions
- Competes for resources
- Address space - memory locations accessible to
process - virtual address space memory, disk, swap or
remote devices - System provides features of a virtual machine
- shared resources (I/O, CPU etc)
- dedicated registers and memory
42The UNIX Process
- Most created by fork or vfork
- well defined hierarchy one parent and zero or
more child processes. The init process is at the
root of this tree - different programs may be run during the life of
a process by calling exec - processes typically terminate by calling exit
43UNIX Process States
fork
system call, interrupt
return
fork
swtch()
exit
swtch()
wait
sleep()
continue
stop
wakeup
stop
stop
continue
wakeup
44Simpler State Diagram
45Process Context
- Address Space
- text, data, stack, shared memory ...
- Control information (u area, proc)
- u area, proc structure, maps
- kernel stack
- Address translation maps
- Credentials
- user and group ids
- Environment variables
- variablevalue
- typically stored at bottom of stack
46Process Context (cont)
- Hardware context
- program counter
- stack pointer
- processor status word
- memory management registers
- FPU registers
- Machine registers saved in u area's process
control block (PCB) during context switch to
another process
47Process Address Space
0xffffffff
Kernel address space
0x7fffffff
48Big Picture
kernel memory
Kernel stack/u area
Kernel stack/u area
Kernel stack/u area
Data
Data
Data
Text (shared)
Text (shared)
Text (shared)
49Control Information
- U area.
- Part of user space (above stack).
- typically mapped to a fixed address.
- contains info needed with running.
- Can be swapped
- Proc
- contains info needed when not running.
- Not swapped out.
- traditionally fixed size table
50U area and Proc structures
- U Area
- PCB - HW context
- pointer to proc
- real/effective ids
- args, return values or errors to current syscall.
- Signal info
- file descriptor table
- controlling terminal vnode
- Proc structure
- process ID and group
- u area pointer
- process state
- queue pointers - scheduler, sleep, etc.
- Priority
- memory management info
- flags
51User Credentials
- Every user is assigned a unique user id (uid) and
group id (gid). - Superuser (root) has uid 0 and gid 0
- every process both a real and effective pair of
Ids. - effective id gt file creation and access,
- real id gt sending signals.
- Senders real/effective receivers real
52Process creation
- fork()
- create a new process
- copy of parents virtual memory
- parent return value is childs PID
- child return value is 0
- exec()
- overwrite with new program
53Process Termination
- exit() is called
- closes open files
- releases other resources
- saves resource usage statistics and exit status
in proc structure - wakeup parent
- calls swtch
- Process is in zombie state
- parent collects, via wait(), exit status and usage