OS Lectures
OS Lectures
Systems
Lecture#1
INTRODUCTION TO OPERATING SYSTEM
What is an Operating System?
Address Bus
Data Bus
Control BUS
Computer Organization
HD
Address Bus D
Data Bus
I/O
Computer Organization
Address Bus HD
D
Data Bus
Control BUS
USB
Othe
r Tim
er
Computer Organization
Address Bus HD
D
Data Bus
Control BUS
USB
Othe
r Tim
er
Boot Process
Multiprogramming
Multitasking
Multiprocessing
Multithreading
Multiprogramming
Multiple programs are loaded into memory
One program executes at a time
On I/O call control is taken from the running process
and given to another.
Control is taken only if a program makes I/O call or
terminates
Multitasking
Multiple programs are loaded into memory
One program executes at a time
On I/O call control is taken from the running
program and given to another.
Time slice is allocated to each program
When time slice expires, control is taken from
running program and transfer to another
Multiprocessing
Multiple programs are loaded into memory
CPU
Multiple programs execute at a time depending 0
upon the number of CPUs available
If the number of programs is equal to the number of
CPU
CPUs then there is True parallel processing
1
If the number of program exceeds number of
available CPUs then multitasking takes place
CPU
2
CPU
3
Protection violations
Memory Protection violation
I/O Protection violation
CPU Protection violation
Memory Protection
There are multiple programs
residing in the memory
One program changes the
variables of other programs. xxxxx
xxx
There should be a check on it.
I/O Protection
One program may access I/O devices
directly bypassing all security
checks.
Accesses files that belongs to
another user.
Using In/Out instructions
CPU protection
A control may stuck in an infinite loop.
While(1)
{
}
Dual-Mode of Operation
Kernel Mode
In kernel mode, CPU executes all instructions.
Even if the harmful operation is performed
Kernel of OS executes in kernel-mode
User Mode
In user mode, CPU executes only non-privileged instructions.
Instructions which are safe to execute.
User processes execute in user-mode.
ALU instructions
JMP branch within the limit
Memory Protection (Solution)
Every program residing in memory is
assigned a limit.
If a program exceeds its limit in user
mode, it is trapped.
A program can’t change its limit in
user-mode.
I/O Protection Solution
I/O instructions can’t be issued in user-mode.
Issuing I/O instructions in user-mode generate
a trap.
CPU Protection (Solution)
Hardware Interrupts
Generated by I/O devices
Software Interrupts
Generated programmatically
E.g. int 0x21
Exceptions
Trap Control is return to the next instruction which caused
exception
Fault Control is return to the same instruction which caused
exception
Double Fault Exception within exception
How hardware Interrupts are
generated?
How software Interrupts are
generated?
DX Data for kernel
Ah Service#
int #num
How Interrupts are handled
System Call
return
}
Why API is preferred over System
Call
System calls are complex to invoke
Lecture#4
Program
int a=5;
int b=10;
main()
{
int c;
c = a + b;
cout<<c;
return 0;
}
Program Process
How to boost performance
Solution
By creating process
int fork ()
Lecture#5
Creating Child Process
{ P1
printf(“Hello \n”);
fork();
printf(“World \n”);
P2
}
Child
Pid_t fork()
• Creates new process
• Fork() is called once and
returned twice
• Returns 0 to the child
process
• Returns ID of child
process to parent
process (>1)
Lecture#6
Output of a program
Output
5-state process diagram
NE Termina
W te
admitted
NE Termina
W te
admitted
NE Termina
W te
admitted
Every process has a descriptor table & its PCB contains a pointer to that
table.
First 3 descriptors in descriptor table have default meanings that can be
changed.
Writing to Descriptor
Reading from Descriptor
Inter-Process Communication
Pipes
Anonymous Pipes (Used for IPC between processes having same ancestor)
Named Pipes (FIFOs)
Shared Memory
Sockets
Lecture#9
Anonymous Pipes
int pipe (int fd[2)
Creates a pipes and places the descriptors of write & read ends to fd[1]
and fd[0] respectively.
On success zero is returned. -1 is returned on error.
After Fork()
Synchronization mechanism in
Pipe
0 0
ls | sort
1 1
ls sort
Lecture#11 code: ls / -R | grep ‘std’
| more
Lecture#12
Paren
t
Fork()
Fork()
Child
Why to create copy of a process?
After Fork(), a child process is created which is a
copy of its parent process.
The child process contains the same code as of
parent process.
Why to replicate process?
So that different parts of a program come to
execution.
Each process has 1 flow of execution.
Can we achieve the same without replicating
address space?
POSIX Threads
Portable Operating System Interface
for Unix (POSIX)
Threads
It is flow of execution
It is a basic unit of CPU utilization
Threads
Creating POSIX Threads
Joining threads
Example
Lecture#13
User Level Threads (Many to One
Model)
User-level threads are easy
to create/cancel and manage.
Portable
Limitation of User-Level Threads
if one user thread performs IO
whole process is blocked.
Creation/Cancellation
and synchronization involves
kernel intervention through system
call.
Not portable
Creation/Cancellation
& Synchronization is slower than
that of User-level threads.
Code: Passing Arguments to Threads
Code: Returning Value from Threads
Class Activity (11 September
2020) Lecture 16
Write a multithreaded program to sort an array of 1000 elements.
All threads must be extended from the same function
Lecture-18
Solution to Critical Section
Problem (Template)
Valid Solution of Critical Section
Problem
Mutual Exclusion
One thread at time should enter in its critical
section.
Progress (Prevents Deadlock)
a) In decision making, threads willing to enter their critical sections
should participate
b) Decision should be made in finite amount of time
flag[2] = {0,0};
Do //thread#1 Do //Thread#2
{ {
flag[0]=true; flag[1]=true;
while(flag[1]); while(flag[0]);
critical section critical section
flag[0]=false; flag[1]=false;
remainder section remainder section
} while(1) } while(1)
Algorithm-2
turn = 0;
while(turn != 0); //entry section while(turn != 1);//entry section
//critical section
//critical section
turn = 1; //exit section
turn = 0;//exit section
//remainder section<-==========
//remainder section
Algorithm-3