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

OS Lectures

Uploaded by

Mazhar Ali
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

OS Lectures

Uploaded by

Mazhar Ali
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 98

Operating

Systems
Lecture#1
INTRODUCTION TO OPERATING SYSTEM
What is an Operating System?

 First See Computer system organization


Computer Organization
Computer Organization

Address Bus

Data Bus

Control BUS
Computer Organization

HD
Address Bus D

Data Bus

Control BUS USB

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

1. First address loads to the address bus is 0xFFFF0, called reset


address
2. ROM is mapped at 0xFFFF0

3. First program executes from ROM, called bootstrap


Loads the first sector of the bootable device to 0x07C00, called boot
address

4. Transfer Control to 0x07C00


Whatever written in boot sector executes
Need of Operating System

 Whatever is in boot sector will be executed.


 Boot sector is of 512B only
 Loading first sector to RAM is the responsibility of ROM.

 There is a need of a utility that let us choose a program which is


to be executed.
 Provides user interface
 Provides programmer interface
 For reading input
 For displaying characters on screen
 Access files
Operating System

 It is a resource organizer that creates an environment where


other software can run.

 Without Operating System


 A program has to
 Read key from keyboard buffer itself
 Write character patterns directly to video memory
 Must implement file system
 Write driver of every hardware it intend to use
 In nutshell: It will do everything an OS does.
Lecture#2
Terminologies

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

 Timer, a controller, generates interrupts at fixed intervals.


 Interrupts transfer control to the kernel in kernel mode.
Lecture#3
Interrupts

 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

DX  Data for kernel


Ah  Service#
int #system call num
Application Programmer Interface

Print (char* str)


{
DX  str
Ah  0x9
int 0x21

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

 You have an array of 1000 elements and you need to search a


number from it.
 How would you speedup searching?
 How would you exploit multiprocessing?

 Solution
 By creating process
int fork ()
Lecture#5
Creating Child Process

int main() Parent

{ 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

Rea CPU assign Exit


RUN
dy
Time slice over

I/O Complete I/O call


Wait
Process Control Block
Lecture#7
Short-term Scheduler (CPU
Scheduler)

NE Termina
W te
admitted

Rea CPU assign


Short-term RUN Exit
dy scheduler
Time slice over

I/O Complete I/O call


Wait
Long-term Scheduler (Job
Scheduler)

NE Termina
W te
admitted

Long- Rea CPU assign Exit


RUN
term dy
Schedul Time slice over
er
I/O Complete I/O call
Wait
Parent process waits for child
process to terminate
Orphan & Zombie Process
 An orphan is child process that is reclaimed by init when the original
parent process terminates before the child.
 A Zombie is created when a parent process does not use the wait system
call after a child dies to read its exit status,
Lecture#8
Exec
•The exec() family of functions replaces the current
process image with a new process image. ​
int execl(const char *path, const char *arg, ...);​

int execlp(const char *file, const char *arg, ...);​

int execv(const char *path, char *const argv[]);​

int execvp(const char *file, char *const argv[]);​


Standard Descriptors in every
process

 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​

Read() is blocked if pipe is empty​


Write() is blocked if pipe is full. (Pipe has limited capacity e.g. 4KB)​

Read() on a pipe whose write end has closed returns 0 (EOF). ​
Write() on a pipe whose read end has closed generates a signal named SIGPIPE​
Closing un-used ends of pipe
 When a process is terminated, all
of its descriptors are closed.​

 Writing to pipe whose all read ends are


closed throw a signal SIGPIPE​

 Reading from a pipe whose all write
ends are closed return EOF or 0
instantly preventing it from getting
blocked. ​

 Writer/Reader process’s own unused
pipe end will prevent it from throwing
SIGPIP/returning EOF. ​

 It’s better to close unused ends in
Lecture#10
Descriptor Table After Pipe()
Descriptor Table After Fork()
Making Stdin a copy of fds[0]
 ls | sort

0 0

ls | sort

1 1

ls sort
Lecture#11 code: ls / -R | grep ‘std’
| more
Lecture#12
Paren
t

Child Child Child Fork()

Fork()

Fork()

Child Child Child

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

 Can be created on OS have no


native support for threads.​

 Portable
Limitation of User-Level Threads
 if one user thread performs IO
whole process is blocked.​

 User threads can’t exploit


multiprocessing.​

 User thread are not given separate


time slice. They have to share the
time slice given to one kernel-level
thread they are mapped on.​
Kernel Level Threads (One to One
Model)​
 Each kernel thread can be
scheduled on different CPU​

 Each kernel thread is assigned


separate CPU time (time slice)​

 If one thread blocks, control


is transferred to other thread. ​
Limitation of Kernel-Level Threads

 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​

Bounded waiting (Prevents Starvation)​


There should be a bound on how many times a thread can re-enter its
critical section while other has requested to enter its critical section.​
Algorithm-1

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

flag[2] = {0,0}; turn = 0;​


Do //flag[0] turn=0 //thread#​1 Do //flag[1] turn=1​//thread#2
{​ {​
flag[0]=true;​ flag[1]=true;​
turn=1;​ turn=0;​
while(flag[1] && turn==1){ }​ while(flag[0] && turn==0);​
​ ​
critical section​ critical section​
​ ​
flag[0]=false; ​ flag[1]=false; ​
​ ​
remainder section​ remainder section​
} while(1) ​ } while(1) ​

Hardware Solution

Context switching is done by timer interrupt. If we disable the


timer interrupt then time slice won’t get expired, hence no
synchronization issue. ​
How to disable interrupts​
CLI (Clear Interrupt Flag)​
How to do that in a High level language​
“asm(cli;)”​
Would it work?​
If no, Why?​
Hardware Solution

 CPU provides some instructions in its ISA that can execute


atomically. ​
 Atomically means its execution can’t be interrupted once it
is issued even on multi-core system.​
 Such instructions once issued are committed.​
Atomic Instruction
Use of Atomic Instruction
Dinning Philosopher

You might also like