Processes
Processes
A process is a program in execution; process
execution must progress in sequential fashion. Can
be characterized by its trace.
The OS interleaves the execution of several
processes to maximize processor utilization
A process requires resources, which are managed
by the operating system
OS supports Inter-Process Communication (IPC)
and user creation of processes
2
Trace of Process
Sequence of
instructions
that execute
for a process
Dispatcher
switches the
processor
from one
process to
another
3
Time slice ~= 1/10th of a second
4
Process state models
The OS is responsible for interleaving the
execution of the processes.
It is important to understand the possible
behaviors of all the processes and how to react to
them.
The behavior of processes is described by means of
the process state model.
5
Two-State Process Model
Process may be in one of two states
Running
Not-running
6
Queuing Diagram
Enter
Queue Dispatch
Processor
Exit
Pause
(a) Queuing diagram
Dispatcher
Program that assigns the processor to one process or
another
Prevents a single process from monopolizing
processor time
7
Reason for Process Creation
8
Reasons for Process Termination
9
Reasons for Process Termination
10
5 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.
11
Diagram of Process State
12
Example for 3 Processes
13
OS Control Structures
Process Control Block (PCB)
Information associated with each process.
Process ID
Process state
Program counter (PC)
CPU registers (Context data)
CPU scheduling information (Priority)
Memory-management information (Memory pointer)
Accounting information (Time, acct. #)
I/O status information (requests, devices assigned)
15
Process Control Block
Contains the process attributes
Created and manage by the operating system
Allows support for multiple processes
Process = Program code + Data + PCB
16
Suspending Processes
17
Two Suspended States
18
Process 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.
19
Ready Queue, I/O Device Queues
20
Process Scheduling
21
Schedulers
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)
22
Process Switching
Definition
The activity that occurs when the OS kernel switches
between processes in an effort to share the CPU among
competing, runable processes
Actions
Save contents of hardware registers (PC, SP, ...)
Load PC with location of resume point
Load hardware registers with new context if full context
switch
23
Process Switching
Process-switch time is overhead
CPU utilization affected by quantum and process-switch
time
Quantum: Time-slice (max CPU interval) given to a
process
Time dependent on hardware support
Types
Voluntary: Process blocks
Involuntary: End of time quantum or higher-priority,
runable process gets control of CPU
24
CPU Process Switch
25
Mode switch vs Process switch
When a mode switch occurs, the PCB of the
process that was running is still marked Running
and is not placed in the Blocked or Ready queue
However, depending on what caused the mode
switch, a process switch could follow
26
Modes of execution
User mode:
Some parts of virtual address space can not be accessed
Some instructions (e.g., memory management) can not
be executed
Kernel mode:
Can access kernel address space
Is a fixed part of the virtual address space of every process
System call puts user into kernel mode
27
Mode, Space and Context
Process Context
Kernel Context
System call
Application
System calls
Exceptions
(user space)
(user + system space)
X
not allowed
User Mode
Interrupts,
System tasks
(user space)
Kernel Mode
More privileges
Interrupt occurs
28
Execution of the OS
29
Execution within user processes
30
Unix Process Creation
Parents create children
results in a tree of processes
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 children terminate
31
Unix Process Creation
Address space
Child duplicate of parent
Child has a program loaded into it
UNIX examples
fork system call creates new process
exec family of system calls used after a fork to replace
the process’ memory space with a new program
32
Unix Process Creation (fork)
Allocate new PID and proc structure
Init child’s process control block (PCB)
Copy parent’s registers to child’s hardware context
Copy parent’s process image to child’s
Mark child runnable and give to scheduler
Return PID = 0 to child
Return child PID to parent
33
Unix Process Creation (fork)
Effects of fork
Child gets a copy of its parent’s memory
BUT changes made by child are not reflected in parent
EXCEPT a child can affect the I/O state of parent
File descriptors can point to same file table entry
WARNING: Parent should “fflush(stdout)” before fork if
using printf()
34
exec Operations
Use to overlay process image with new program
Find executable file (argv[0])
Copy exec args and env variables to kernel space
Free memory currently allocated to process image
Allocate new memory for process image
Copy exec args and env variables into new space
Begin executing in main()
35
exec Functions
Six exec functions:
execl
execv
execle
execve
execlp
execvp
l list of arguments
v argv[] vector
p file name
e envp[] vector instead of
inheriting environment of parent
36
waitpid
pid_t waitpid(pid_t pid, int *status, int options);
Unlike wait(2), waitpid doesn’t have to block
wait(2) blocks until one of its children terminates
37
waitpid – pid parameter
pid == -1: Wait for any child to terminate
pid > 0: Wait for child whose PID equals pid
pid == 0: Wait for any child whose PGID equals the
PGID of process pid
pid < -1: Wait for any child whose PGID = abs(pid)
38
waitpid – status parameter
Contains exit status, signal number, and flags
WIFEXITED(status)
True if child terminated normally
Ex: printf(“exit = %d\n”, WEXITSTATUS(status));
WIFSIGNALED(status)
True if child terminated and didn’t catch signal
Ex: printf(“signo = %d\n”, WTERMSIG(s));
WIFSTOPPED(status)
True if child is STOPPED
Ex: printf(“signo = %d\n”, WSTOPSIG(s));
39
waitpid – options parameter
options = 0
No special handling
options = WNOHANG
Don’t block if child is not available and return 0
Ex:
rc = waitpid(pid, &status, WNOHANG);
if (rc == -1) { . . . error . . . }
else if (rc == 0) { . . . no child available . . .}
else { . . . rc should equal pid . . . }
40
waitpid options
options = WUNTRACED
Return status of child if child stopped and status has not
already been returned (assumes job control support)
41
© Copyright 2026 Paperzz