Upload
others
View
0
Download
0
Embed Size (px)
Citation preview
1
Lecture Topics
• Today: Processes and Threads
(Silberschatz, 3.1-3.3 and 4.1-4.4)
• Next: continued
1
Announcements
• Self-Study Exercise #3 posted
• Score for Project #1 sent via email today
• Computer Project #2 – due 1/23
• Computer Project #3 – due 1/30
2
2
Processes
• A process is a program in execution
• OS must support multitasking (multiple
processes in RAM, executing concurrently)
• OS interleaves execution of multiple
processes in order to maximize processor
utilization, yet still provide reasonable
response time
3
Process Elements
• A process consists of (at least):
– code and data for executing program
– program counter (PC) and status register (PSW)
– general-purpose registers
– set of OS resources (open files, signals received,
network connections, …)
• In other words, everything the OS needs to
run the program -- or to re-start it, if it’s
interrupted at some point
4
3
Example
Assume no virtual
memory: dispatcher
and three processes
are fully loaded into
memory
Program counter
currently points to an
address in Process B
5
Example (cont)
6
4
Example (cont)
Assume timer interrupt
after 6 clock cycles
Assume dispatcher is
at addresses 100-105
Assume dispatcher
selects next process
using "round robin"
7
Process Execution
• Typical execution sequence:
– created (PCB assigned)
– wait to use CPU
– execute machine language instructions on CPU
– wait for I/O to complete
– wait to use CPU
– execute machine language instructions on CPU
– wait for I/O to complete
– wait to use CPU
– execute machine language instructions on CPU
– terminated (PCB deassigned)8
5
Five-State Process Model
9
Process States
• New – created (waiting to be admitted to pool
of executable processes)
• Ready – waiting to be assigned to processor
• Running – using the processor
• Blocked – waiting for event (such as I/O
completion or signal)
• Exit – terminated (removed from pool of
executable processes)10
6
State Transitions
• Admit – OS moves process to pool of
executable processes
• Dispatch – OS allows process to use CPU
• Timeout – OS responds to timer interrupt
• Event wait – process issues system call
• Event occurs – OS responds to interrupt
• Release – Process issues “exit” system call
(or halts abnormally) 11
Previous Example
12
7
Process Creation
• User logs onto system
• Submission of a batch job
• OS creates process to provide a service
(ex: control print job)
• One process creates another process
(ex: parent process spawns child process)
13
Process Termination
• User logs off system
• Batch job terminates
• Process halts normally
• Process halts abnormally (ex: segmentation
fault, divide by zero)
• Parent process terminates child process
14
8
Process Execution
• Once process is in the Running state, the OS
can only regain control through an interrupt:
– process issues system call for I/O or similar task
(which may block the process)
– process issues system call to terminate
– process generates exception
– interrupt from timer
– interrupt from I/O device (not related to process in
the Running state) 15
Process Execution
• After the OS has regained control and is
using the CPU, it processes the interrupt,
then gives up control of the CPU
• If the interrupted process was moved out of
the Running state, the OS selects and
dispatches a different process
• If the interrupt was due to an event for a
different process (I/O completion), the OS
resumes the interrupted process16
9
Scenario #1
• Assume the process in the Running state
issues a system call to exit
– System call generates a software interrupt; kernel
saves context of the current process
– Kernel handles the system call and moves the
current process to the Exit state
– Kernel selects a different process from the pool of
processes in the Ready state and moves it to the
Running state, then restores its context
17
Scenario #2
• Assume the process in the Running state
generates an exception (fault)
– Exception generates a software interrupt; kernel
saves context of the current process
– Kernel handles the exception and moves the
current process to the Exit state
– Kernel selects a different process from the pool of
processes in the Ready state and moves it to the
Running state, then restores its context
18
10
Scenario #3
• Assume the process in the Running state
issues a system call to perform I/O
– System call generates a software interrupt; kernel
saves context of the current process
– Kernel handles the system call and moves the
current process to the Blocked state
– Kernel selects a different process from the pool of
processes in the Ready state and moves it to the
Running state, then restores its context
19
Scenario #4
• Assume the process in the Running state
issues a system call to access information in
its PCB
– System call generates a software interrupt; kernel
saves context of the current process
– Kernel handles the system call (extracts info from
PCB of current process)
– Kernel restores the context of the current process
20
11
Scenario #5
• Assume the time quantum for the process in
the Running state expires
– Timer generates a hardware interrupt; kernel
saves context of the current process
– Kernel handles the interrupt and moves the
current process to the Ready state
– Kernel selects a different process from the pool of
processes in the Ready state and moves it to the
Running state, then restores its context
21
Scenario #6
• Assume the process in the Running state is
interrupted by the completion of an I/O
operation for a different process
– I/O module generates a hardware interrupt; kernel
saves context of the current process
– Kernel handles the interrupt and moves the other
process from the Blocked state to the Ready state
– Kernel restores the context of the current process
22
12
Summary
• The OS gives up control of the CPU when it
moves a process from the Ready state to the
Running state (and restores its context)
• The OS regains control of the CPU whenever
there is an interrupt (hardware interrupt or
software interrupt)
• The OS consumes resources (CPU cycles
and memory)
23
Mode Switch
• User mode to kernel mode
– interrupt: during execution of current instruction;
reaction to asynchronous external event
– exception: caused by execution of current
instruction; reaction to exceptional condition
– trap (system call): explicit request by current
instruction; request service from OS
• Kernel mode to user mode
24
13
Mode Switch
• System call (other than exit):
• Processing similar for interrupts (although
control may return to a different process)
25
Process Switch
• Process currently in Running state
– save context (PC, other registers)
– move PCB to appropriate queue
– update PCB (state, accounting info)
• Process selected by OS
– update PCB (state, accounting info)
– move PCB out of Ready queue
– restore context (PC, registers)
26
14
Process Switch
27
Operating System Data Structures
• Information about
the current status
of each process
and resource
• Table for each
resource the OS
manages
• Data structures in
kernel memory
28
15
Process Management
• OS allocates a PCB (process control block)
when a process is created – a data structure
to hold information about that process
– Process ID number
– Current state
– Copy of control registers
– Copy of regular registers
– Accounting information
– Resource information
29
Process Control Block
• PCB assigned to
process when created
• OS maintains info in
PCB while process is
in existence
• OS moves PCBs
between queues to
manage system
30
16
Queueing Models: Single Queue
31
Queueing Models: Multiple Queues
32