0% found this document useful (0 votes)
13 views

Processing

...,..

Uploaded by

Michael Junior
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Processing

...,..

Uploaded by

Michael Junior
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 92

Operating Systems

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

OS is in charge of making sure the system


operates correctly and efficiently.
The Kernel

• Protection – The isolation of potentially misbehaving applications and users so that


they do not corrupt other applications or the OS itself.
The Kernel

• Protection – The isolation of potentially misbehaving applications and users so that


they do not corrupt other applications or the OS itself.

• Protection was required to achieve some of the goals of OS


• Reliability – preventing bugs from one program from affecting another
The Kernel

• Protection – The isolation of potentially misbehaving applications and users so that


they do not corrupt other applications or the OS itself.

• Protection was required to achieve some of the goals of OS


• Reliability – preventing bugs from one program from affecting another
• Security – preventing malicious software or users from infecting your system
The Kernel

• Protection – The isolation of potentially misbehaving applications and users so that


they do not corrupt other applications or the OS itself.

• Protection was required to achieve some of the goals of OS


• Reliability – preventing bugs from one program from affecting another
• Security – preventing malicious software or users from infecting your system
• Privacy – preventing users from accessing unauthorized data
The Kernel

• Protection – The isolation of potentially misbehaving applications and users so that


they do not corrupt other applications or the OS itself.

• Protection was required to achieve some of the goals of OS


• Reliability – preventing bugs from one program from affecting another
• Security – preventing malicious software or users from infecting your system
• Privacy – preventing users from accessing unauthorized data
• Fair resource allocation – preventing applications from hogging resources
The Kernel

• Protection – The isolation of potentially misbehaving applications and users so that


they do not corrupt other applications or the OS itself.

• Protection was required to achieve some of the goals of OS


• Reliability – preventing bugs from one program from affecting another
• Security – preventing malicious software or users from infecting your system
• Privacy – preventing users from accessing unauthorized data
• Fair resource allocation – preventing applications from hogging resources

• Implementing protection is the job of an OS’s Kernel


The Kernel

• 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

• The Kernel is the lowest level of software


running on the system
• Has full access to all of the machine hardware
• “Trusted” to do anything with the hardware
The Kernel

• The Kernel is the lowest level of software running on the system


• Has full access to all of the machine hardware
• “Trusted” to do anything with the hardware

• Everything else – considered “Untrusted”


• This software is run in a restricted environment with less than complete access
to the full power of the hardware
The Kernel

• The Kernel is the lowest level of software running on the system


• Has full access to all of the machine hardware
• “Trusted” to do anything with the hardware

• Everything else – considered “Untrusted”


• This software is run in a restricted environment with less than complete access
to the full power of the hardware

APP APP APP Untrusted

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

• Without protection an embedded virus could be forwarding keystrokes' to an attacker


Virtualization

• 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 …

• The virtual form is more general, powerful and easy-to-use.


• Sometimes, we refer to the OS as a virtual machine.
Running a Program

• A running program executes instructions.


• The processor fetches an instruction from memory.
• Decode: Figure out which instruction this is
• Execute: i.e., add two numbers, access memory, check a condition, jump to
function, and so forth.
• The processor moves on to the next instruction and so on.

CONTROL
ALU
UNIT
2: Decode 3: Execute

1: Fetch RAM 4: Store


A Process

• The execution of an application program with restricted rights


• The abstraction for protected execution provided by the OS
A Process

• The execution of an application program with restricted rights


• The abstraction for protected execution provided by the OS

• The process requires permission from the OS kernel to:


• access memory of other processes
• read and write to disk
• change hardware settings
A Process

• The execution of an application program with restricted rights


• The abstraction for protected execution provided by the OS

• The process requires permission from the OS kernel to:


• access memory of other processes
• read and write to disk
• change hardware settings

• 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

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

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

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?

• The OS provides an API for which we can use to create processes.

• An API is an Application Programming Interface, which is a software intermediary that


allows two applications to talk to each other
Process API

• These APIs are available on any modern OS.


• Create
Create a new process to run a program
• Destroy
Halt a runaway process
• Wait
Wait for a process to stop running
• Miscellaneous Control
Some kind of method to suspend a process and then resume it
• Status
Get some status info about a process
Process API - Process Creation

1. Load a program code into memory, into the address space of the process.
Programs initially reside on disk in executable format.

OS perform the loading process lazily.


Loading pieces of code or data only as they are needed during program
execution.
Process API - Process Creation

1. Load a program code into memory, into the address space of the process.
Programs initially reside on disk in executable format.

OS perform the loading process lazily.


Loading pieces of code or data only as they are needed during program
execution.

2. The program’s run-time stack is allocated.


Use the stack for local variables, function parameters, and return address.
Initialize the stack with arguments → argc and the argv array of main()
function
Process API - Process Creation

3. The program’s heap is created.


Used for explicitly requested dynamically allocated data.
Program request such space by calling malloc() and free it by calling free().
Process API - Process Creation

3. The program’s heap is created.


Used for explicitly requested dynamically allocated data.
Program request such space by calling malloc() and free it by calling free().

4. The OS do some other initialization tasks.


input/output (I/O) setup
Each process by default has three open file descriptors.
Standard input, output and error
Process API - Process Creation

3. The program’s heap is created.


Used for explicitly requested dynamically allocated data.
Program request such space by calling malloc() and free it by calling free().

4. The OS do some other initialization tasks.


input/output (I/O) setup
Each process by default has three open file descriptors.
Standard input, output and error

5. Start the program running at the entry point, namely main().


The OS transfers control of the CPU to the newly-created process.
How do we run multiple copies of the
same program?
• The OS can make multiple copies of the programs’
• instructions
• data Machine
Instructions
• stack Process
• heap Data
Heap

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

• Better way is to reuse memory where possible


• just store one instance of instructions
• This will be discussed in later lectures

Machine Machine Machine Machine


Instructions Instructions Instructions Instructions

Data Data Data Data


Heap Heap Heap Heap

Stack Stack Stack Stack


What is the difference between a
process and a program?
• Process is an instance of a program
• Just like an object is an instance of a class
What is the difference between a
process and a program?
• Process is an instance of a program
• Just like an object is an instance of a class

• 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

• The PCB stores :


• Where a process is stored in memory
• Where the executable image is on the disk
• Which user asked to execute the process
• Which privileges the process has
Process Control Block
// the information xv6 tracks about each process
// including its register context and state
struct proc {
char *mem; // Start of process memory
uint sz; // Size of process memory
char *kstack; // Bottom of kernel stack
// for this process
enum proc_state state; // Process state
int pid; // Process ID
struct proc *parent; // Parent process
void *chan; // If non-zero, sleeping on chan
int killed; // If non-zero, have been killed
struct file *ofile[NOFILE]; // Open files
struct inode *cwd; // Current directory
struct context context; // Switch here to run
process
struct trapframe *tf; // Trap frame for the
// current interrupt
};
How to handle the processing with a level of
control ?

CPU Machine
Instructions

Process
Program Data
Counter Heap
Stack
Direct Execution

• Just run the program directly on the CPU.

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

• LOAD – load a value into a register Process


Data
• ADDA – add a value to the accumulator Program
Counter Heap

• How do we prevent a process from Stack

manipulating another.
Machine
Instructions

Process
Data
Heap
Stack
Kernel Checks

• OpCode CPU Machine


• BR – branch to another instruction Instructions
• LOAD – load a value into a register Program Kernel Process
• ADDA – add a value to the accumulator Counter Data
Heap

• How do we prevent a process from Stack


manipulating another.
Machine
• Simple approach would be to have the kernel check each Instructions
instruction to see if it had permission. Process
Data

Heap

Stack
How can we speed up this approach?

CPU Machine
Instructions

Program Kernel Process


Counter Data
Heap
Stack

Machine
Instructions

Process
Data

Heap

Stack
How can we speed up this approach?

• Most instructions are perfectly safe CPU Machine


• So we only have to check those Instructions
instructions we could pose risk Program Kernel Process
Counter Data
Heap
Stack

Machine
Instructions

Process
Data

Heap

Stack
How can we speed up this approach?

• Most instructions are perfectly safe


• So we only have to check those instructions we could pose risk

• To accomplish this we implement the same checks on the hardware level


How can we speed up this approach?

• Most instructions are perfectly safe


• So we only have to check those instructions we could pose risk

• To accomplish this we implement the same checks on the hardware level

• This is called dual-mode


operation, represented by a single
bit in the processor which
represents its’ status
Dual Mode Operation

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

• Dual-Mode approach allows increased efficiency as we Heap

only check when we do not trust the process Stack


What instructions can’t a process
execute?
What instructions can’t a process
execute?
• Privileged Instructions
• Instructions available in kernel mode but not in user mode
• Change privilege levels
• Access memory
• Disable/Enable interrupts
Types of Mode Transfer

• 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

• 3 reasons for the kernel to take control


• Interrupts
• Processor Exceptions
• System Calls
User to Kernel Mode

• 3 reasons for the kernel to take control


• Interrupts – Asynchronous event
• Processor Exceptions – Synchronous event
• System Calls – Synchronous event

• Asynchronous event = triggered by external events


• Synchronous event = triggered by process execution
User to Kernel Mode

• 3 reasons for the kernel to take control


• Interrupts – Asynchronous event
• Processor Exceptions – Synchronous event
• System Calls – Synchronous event

• Asynchronous event = triggered by external events


• Synchronous event = triggered by process execution

• 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

• As the process executes instructions it will check if an interrupt has occurred


• If Yes = completes or stalls processing current instruction, saves current
execution state then starts executing a interrupt handler in the kernel

• If No = continues with current instruction processing

• For different interrupts we have different handlers


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

• 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

• Additionally, the OS needs to regain control in normal operation as well

• 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)

initialize trap table


remember address of …
syscall handler
timer handler
start interrupt timer
start timer
interrupt CPU in X ms

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

• Hardware event caused by a user program behavior that causes a control


transfer from user to kernel mode

• As with interrupts ; completes or stalls processing current instruction, saves


current execution state then starts executing a exception handler in the kernel
Processor Exceptions

• Hardware event caused by a user program behavior that causes a control


transfer from user to kernel mode

• As with interrupts ; completes or stalls processing current instruction, saves


current execution state then starts executing a exception handler in the kernel

• 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.

• As with interrupts/Exceptions ; saves current execution state then starts


executing a pre-defined handler in the kernel
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.

• As with interrupts/Exceptions ; saves current execution state then starts


executing a pre-defined handler in the kernel

• Examples of System Calls


• Create (fork) / terminate processes
• Wait command
• Create/Delete files
• Get/Set DateTime
• Get/Set File permissions
System Calls
OS @ boot Hardware
(kernel mode)

initialize trap table


remember address of …
syscall handler

OS @ run Hardware Program


(kernel mode) (user mode)
Create entry for process list
Allocate memory for program
Load program into memory
Setup user stack with argv
Fill kernel stack with reg/PC
return-from -trap
restore regs from kernel stack
move to user mode
jump to main
Run main()

Call system
trap into OS
System Calls
OS @ run Hardware Program
(kernel mode) (user mode)

(Cont.)

save regs to kernel stack


move to kernel mode
jump to trap handler
Handle trap
Do work of syscall
return-from-trap
restore regs from kernel stack
move to user mode
jump to PC after trap

return from main
trap (via exit())
Free memory of process
Remove from process list
Kernel to User

• There are several types of transitions from kernel to user

• New Processes
• Resuming after an Interupt, Exception or System Call
• Context Switching
• User-level Upcalls
New Processes

• To start a New Process


• Kernel copies program into memory
• Sets program counter to the start of the process
• Switches to user mode
Resuming

• To resume a process after the kernel finishes handling the interrupt


• Restores the program counter to the instruction of the interrupted program
• Restores the registers
• This information is found in the saved state
• Switches to user mode
Context Switches

• To switch to another process after receiving an interrupt


• Kernel saves current state of execution of the current process
• Kernel then resumes another process by loading its current state
• Switches to user mode
Context Switches
1 # void swtch(struct context **old, struct context *new);
2 #
3 # Save current register context in old
4 # and then load register context from new.
5 .globl swtch
6 swtch:
7 # Save old registers
8 movl 4(%esp), %eax # put old ptr into eax
9 popl 0(%eax) # save the old IP
10 movl %esp, 4(%eax) # and stack
11 movl %ebx, 8(%eax) # and other registers
12 movl %ecx, 12(%eax)
13 movl %edx, 16(%eax)
14 movl %esi, 20(%eax)
15 movl %edi, 24(%eax)
16 movl %ebp, 28(%eax)
17
18 # Load new registers
19 movl 4(%esp), %eax # put new ptr into eax
20 movl 28(%eax), %ebp # restore other registers
21 movl 24(%eax), %edi
22 movl 20(%eax), %esi
23 movl 16(%eax), %edx
24 movl 12(%eax), %ecx
25 movl 8(%eax), %ebx
26 movl 4(%eax), %esp # stack is switched here
27 pushl 0(%eax) # return addr put in place
28 ret # finally return into new ctxt
User-level Upcalls

• Upcalls are virtualized interrupts and exceptions which allows user programs
to receive asynchronous notifications of events

• It is Kernel Interrupt handling except at user level


Summary

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

1. Interrupt Processed CPU Mode :


Kernel 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 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

You might also like