Processing
Processing
COMS(3010A)
Kernels and Processes
Branden Ingram
[email protected]
Office Number : ???
Recap
• What an OS is
• The Roles it plays
• Basic OS functionality
• The importance of OS
• OS Design similarities
Recap
• Responsible for
• Making it easy to run programs
• Allowing programs to share memory
• Enabling programs to interact with devices
• The kernel is the central module of an operating system (OS) responsible for
memory management, process and task management, and disk management.
The Kernel
• The kernel is the central module of an operating system (OS) responsible for
memory management, process and task management, and disk management.
• It is the part of the operating system that loads first, and it remains in main
memory.
The Kernel
• The kernel is the central module of an operating system (OS) responsible for
memory management, process and task management, and disk management.
• It is the part of the operating system that loads first, and it remains in main
memory.
The Kernel
OS KERNEL Trusted
HARDWARE
Do applications need to implement
protection?
Do applications need to implement
protection?
• Of course
Do applications need to implement
protection?
• Of course
• Just like the OS needs to safely execute untrusted software, so too does an application
• Web browser executing embedded JavaScript to draw a webpage
Do applications need to implement
protection?
• Of course
• Just like the OS needs to safely execute untrusted software, so too does an application
• Web browser executing embedded JavaScript to draw a webpage
• The OS takes a physical resource and transforms it into a virtual form of itself.
• Physical resource: Processor, Memory, Disk …
Virtualization
• The OS takes a physical resource and transforms it into a virtual form of itself.
• Physical resource: Processor, Memory, Disk …
CONTROL
ALU
UNIT
2: Decode 3: Execute
• Once again it’s the idea of the OS kernel mediating and checking a processes access to
hardware
The Process Abstraction
Machine
Instructions
Source Executable
Code Image Process
Programmer Data
Heap
Stack
Machine
Instructions
OS Kernel
Data
Heap
Stack
The Process Abstraction
Machine
Instructions
Source Executable
Code Image Process
Programmer Data
Heap
Stack
Designs and edits
some code
Machine
Instructions
OS Kernel
Data
Heap
Stack
The Process Abstraction
Machine
Instructions
Source Executable
Code Image Process
Programmer Data
Heap
Stack
Designs and edits Compiler
some code converts code
into a sequence
of machine Machine
instructions, as Instructions
well as static
data OS Kernel
Data
Heap
Stack
The Process Abstraction
Machine
Instructions
Source Executable
Code Image Process
Programmer Data
Heap
Stack
Designs and edits Compiler To run the
some code converts code program the OS
into a sequence copies the
of machine instructions and Machine
instructions, as data into physical Instructions
well as static memory
data OS Kernel
Data
Heap
Stack
The Process Abstraction
Machine Executable
Instructions instruction
Source Executable
Code Image Process
Programmer Data Static variables
Dynamically
Heap
allocated structures
Stack Local Variables
Heap
Stack
The Process Abstraction
Machine Executable
Instructions instruction
Source Executable
Code Image Process
Programmer Data Static variables
Dynamically
Heap
allocated structures
Stack Local Variables
Heap
Stack
Storage
The Process Abstraction
• Process is made up of :
Machine • Instructions
Instructions
• Data
Process
Data • Heap
Heap • Stack
Stack
How do we run a program?
1. Load a program code into memory, into the address space of the process.
Programs initially reside on disk in executable format.
1. Load a program code into memory, into the address space of the process.
Programs initially reside on disk in executable format.
Stack
How do we run multiple copies of the
same program?
• The OS can make multiple copies of the programs’
• instructions
• data Machine
Machine
Instructions
• stack Instructions
Process
• heap Data Process
Data
Heap
Heap
Stack
Stack
How do we run multiple copies of the
same program?
• The OS can make multiple copies of the programs’
• instructions
• data Machine
Machine
Instructions
• stack Machine
Instructions
Instructions Process
• heap Data Process
Data Process
Heap Data
Heap
StackHeap
Stack
Stack
How do we run multiple copies of the
same program?
• The OS can make multiple copies of the programs’
• instructions
• data Machine
Machine
Instructions
• stack Machine
Instructions
Machine
Instructions Process
• heap DataInstructions Process
Data Process
Heap Data Process
Heap Data
StackHeap
StackHeap
Stack
Stack
How do we run multiple copies of the
same program?
• The OS can make multiple copies of the programs’
• instructions
• data
• stack
• heap
• This means for each instance of a program there is a process in memory with its own
copy of the program
How do we keep track of all these
instances?
• The OS uses a data structure called a process control block (PCB) to keep track of the
various processes
CPU Machine
Instructions
Process
Program Data
Counter Heap
Stack
Direct Execution
OS Program
1. Create entry for process list
2. Allocate memory for program
3. Load program into memory
4. Set up stack with argc / argv
5. Clear registers
6. Execute call main()
7. Run main()
8. Execute return from main()
9. Free memory of process
10. Remove from process list
How do we prevent a process accessing
another?
• OpCode
CPU Machine
• BR Instructions
• LOAD Process
Data
• ADDA Program
Counter Heap
Stack
Machine
Instructions
Process
Data
Heap
Stack
How do we prevent a process accessing
another?
• OpCode Machine
• BR – branch to another instruction CPU Instructions
manipulating another.
Machine
Instructions
Process
Data
Heap
Stack
Kernel Checks
Heap
Stack
How can we speed up this approach?
CPU Machine
Instructions
Machine
Instructions
Process
Data
Heap
Stack
How can we speed up this approach?
Machine
Instructions
Process
Data
Heap
Stack
How can we speed up this approach?
CPU Mode
• In User Mode Register
Machine
• The processor checks instruction Instructions
Process
• In Kernel Mode Program Data
Counter
• The processor executes the instructions Heap
Stack
Machine
• Simple approach would be to have the kernel check each Instructions
instruction to see if it had permission. Process
Data
• The next question is how to safely transfer to and from our different modes
• These transitions are not rare
• Safe, Fast and Efficient
User to Kernel Mode
• We use the term trap to refer to any synchronous transfer of control from user
to kernel (less privileged to more)
Interrupts
• Asynchronous signal to the processor indicating some event occurred that the
processor should look at
Interrupts
• Asynchronous signal to the processor indicating some event occurred that the
processor should look at
• Timer Interrupts
• Since through process isolation we give the process the illusion of being fully
in control, we need a way to regain control
• For example when a program becomes non responsive and a user chooses to
close it
How does a kernel regain control from a
runaway process?
• Timer Interrupts
• Since through process isolation we give the process the illusion of being fully in
control, we need a way to regain control of the processor
• For example when a program becomes non responsive and a user chooses to
close it
• For example if you are typing, listening to music and downloading a file
• The OS needs to be able to switch between tasks smoothly
• This is handled by a device called a hardware timer
• The hardware timer is used to interrupt the processor after a certain delay
• After a specified delay, the CPU transfers control from the user process to
the kernel running in kernel mode
Different Interrupts
• Timer Interrupt
• Checks if current process is being responsive to user input, used to detect
infinite loops
• Switches between processes to ensure that each process gets a turn
Different Interrupts
• Timer Interrupt
OS @ boot Hardware
(kernel mode)
OS @ run Program
Hardware
(kernel mode) (user mode)
Process A
timer interrupt
save regs(A) to k-stack(A)
move to kernel mode
jump to trap handler
Different Interrupts
• Timer Interrupt
OS @ run Program
Hardware
(kernel mode) (user mode)
(Cont.)
Handle the trap
Call switch() routine
save regs(A) to proc-struct(A)
restore regs(B) from proc-struct(B)
switch to k-stack(B)
return-from-trap (into B)
restore regs(B) from k-stack(B)
move to user mode
jump to B’s PC
Process B
…
Different Interrupts
• Timer Interrupt
• Checks if current process is being responsive to user input, used to detect
infinite loops
• Switches between processes to ensure that each process gets a turn
• I/O requests
• A mouse triggers an interrupt every time a click is detected
Processor Exceptions
• Examples of exceptions
• Process attempts to perform privileged instruction
• Access memory outside of own memory region
• Division of integers by zero
• Writing to read-only memory
• In these cases the OS simply stops execution of the process and returns an
error code
System Calls
• Lastly, user processes can transition willing into kernel in order to request that
the kernel perform an operation on the user’s behalf
• A System call is any procedure provided by the kernel that can be called from
user level.
• Lastly, user processes can transition willing into kernel in order to request that
the kernel perform an operation on the user’s behalf
• A System call is any procedure provided by the kernel that can be called from
user level.
(Cont.)
• New Processes
• Resuming after an Interupt, Exception or System Call
• Context Switching
• User-level Upcalls
New Processes
• Upcalls are virtualized interrupts and exceptions which allows user programs
to receive asynchronous notifications of events
CPU Mode :
User 1. Instructions Machine
are processed Instructions
Program Process
Counter Data
Heap
Stack
Machine
Instructions
OS Kernel
Data
Heap
Stack
Summary
CPU Mode :
User 1. Instructions Machine
2. Interrupt Occurs
are processed Instructions
Program Process
Counter Data
Heap
Stack
Machine
Instructions
OS Kernel
Data
Heap
Stack
Summary
CPU Mode :
User 1. Instructions Machine
2. Interrupt Occurs
are processed Instructions
Program Process
Counter Data
Heap
Stack
Machine
Saved state of Instructions
the process
OS Kernel
Data
3. State of processor is saved
Heap
Stack
Summary
CPU Mode :
User 1. Instructions Machine
2. Interrupt Occurs
are processed Instructions
Program Process
Counter Data
Heap
Stack
Machine
Saved state of Instructions
the process
OS Kernel
Data
3. State of processor is saved
Heap
Stack
3. Stored in the
PCB in the stack
Summary
3. Mode Change
CPU Mode :
Kernel 1. Instructions Machine
2. Interrupt Occurs
are processed Instructions
Program Process
Counter Data
Heap
Stack
Machine
Saved state of Instructions
the process
OS Kernel
Data
3. State of processor is saved
Heap
Stack
3. Stored in the
PCB in the stack
Summary
Program Process
Counter Data
Heap
Stack
Machine
Saved state of Instructions
the process
OS Kernel
Data
2. State of processor is loaded
Heap
Stack
2. Loaded from
the PCB in the
stack
Summary
2. Mode Change
1. Interrupt Processed CPU Mode :
User Machine
Instructions
Program Process
Counter Data
Heap
Stack
Machine
Saved state of Instructions
the process
OS Kernel
Data
2. State of processor is loaded
Heap
Stack
2. Loaded from
the PCB in the
stack
Summary
2. Mode Change
1. Interrupt Processed CPU Mode :
User 3. Instructions Machine
are processed Instructions
Program Process
Counter Data
Heap
Stack
Machine
Saved state of Instructions
the process
OS Kernel
Data
2. State of processor is loaded
Heap
Stack
2. Loaded from
the PCB in the
stack