Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

Batch Name : OM31

Subject Name : Operating System Concepts


=========================================================
===========

=> "If you are expert in C, you can become expert in any
porgramming language" by D. Ritchie.

Q. Why there is a need of an OS?

- there are 4 basic functions of computer:


1. data storage
2. data processing
3. data movement
4. control

Q. What is an OS?
- An OS is a "system software" (i.e. collection of system
programs) which acts as an interface between user and
hardware.

- An OS also acts as an interface between programs


(application & user programs) and hardware.

- to compile a program
$gcc program.c -> terminal

to execute program is the job of an OS:


$./program.out OR $.\a.exe press enter
- there are two types of interfaces provided by an OS in
the form of an application programs:

1. "CUI"/"CLI" Command User Interface / Command Line


Interface => user can interacts with an OS by means of
entering commands through command prompt/shell in a text
format.
e.g.
gcc
./program.out
cp
mv
cd - to change dir
g++
git pull
git clone
etc...
- In Windows, name of program which provides CUI =>
command prompt => "cmd.exe"
- In Linux, name of program which provides CUI => shell
- In MS DOS, name of program which provides CUI =>
"command.com"

2. "GUI" Graphical User Interface => user can interacts


with an OS by means of making an events like click on
buttons, left click, right click, double click
menu bar, menu list, exit etc......
- In Windows, name of program which provides GUI =>
"explorer.exe"
- In Linux, name of program which provides GUI =>
GNOME/KDE

"Linux" => Ubuntu Linux, Kali Linux, RedHat Linux Fedora


Linux -> Linux distro's etc...

"IDE" = Integrated Developement Environment => It is an


application software which is a collection of tools (i.e.
collection of an application programs like source code
editor, preprocessor, compiler, assembler, debugger
etc...), required for faster software developement.
e.g. visual studio c++, vs code editor, netbeans,
eclipse, android studio etc...

"editor" => it is an application program which can also


be used as a source code editor i.e. used to write source
code
e.g. notepad, vi editor, gedit etc....

- all header files contains only declaration of lib


functions
e.g. stdio.h file contains only declarations of standard
input output lib functions like printf(), scanf() etc...
- by means of executing #include (file inclusion
preprocessor directive) preprocessor includes contents of
header files into the source file.

"stdio.h"
printf()
scanf()

- definitions of all lib functions are exists in a lib


folder in precompiled object module format, linker links
object file of your program with precompiled object
modules of lib functions and it creates single executable
file.

- linker which links definitions of lib functions/binaries


during execution of a program is referred as a dynamic
linker.

Scenarion-1:
Machine-1: Windows -> program.c
Machine-2: Linux -> program.c -> compile + execute => YES

"Portability" => C Program written on one


machine/platform can be compiled and executed on any
other machine/platform

Scenarion-2:
Machine-1: Linux -> program.c -> program (executable
code)
Machine-2: Windows -> program.out (executable code) => NO

- In Linux dot extension doesnt matter

Q. Why an executable code created on one machine/platform


cannot be execute on any other machine/platform ???
- file format of an executable file in Linux is
ELF(Executable & Linkable Format), whereas file format of
an executable in Windows is PE(Portable Executable)
- When we execute a program, loader first verifies file
format of an executable file, if file format matches then
it checks magic number and if file format as well magic
number both matches then only it loads an executable file
from HDD into the main memory.

Q. What is a file format?


- file format is a way to store data & instructions of an
executable file in an organized manner.
Linux => ELF
Windows => PE

e.g. ELF file format divides an executable file logically


into sections
1. exe header
2. bss section
3. data section
4. rodata section
5. code/text section
6. symbol table

Q. What is a magic number?

int g_var=99;
static int i=1000;

100 - integer constant


1000L - long integer constant
12.45 - double contant
12.45f - float constant
'A' - char constant
"SunBeam" - string constant/string literal

sum(11,22);

int sum(int n1, int n2)//formal params


{
int res;//local var

res = n1 + n2;
return sum;
}

=> "Installation of an OS" => to install an OS on machine


is nothing but to store OS software (i.e. thousands of
system programs & application programs which are in a
binary format) onto the HDD.

kernel + other system programs + application programs in


binary format gets stored onto the HDD.

What is a Process?
- Program in execution is called as a process
- Running program is also called a process
- Running instance of a program is called as a process

- Program is a passive entity, whereas process is an


avtive entity.

- If any OS wants to become active, initially atleast its


core program i.e. kernel must be loaded from HDD into the
main memory, and to load kernel from HDD into the main
memory is done by one special program which is in HDD
named as "bootstrap program" and this process is called
as "booting".

- While booting, bootstrap program locates the kernel and


load it into the main memory.

- Components which are there onto the motherboard


referred as "Core Computer System" => [ CPU, RAM, ROM,
Cache Memory etc ...]

- Devices which are connected to the motherboard


externally through ports are referred as "Peripheral
Devices/Peripherals"
e.g. [ HDD, IO Devices etc... ]

- "Bootable Device": if any storage device contains one


special program called as bootstrap program in its first
sector i.e. in a boot sector (usually size of 1 sector =
512 bytes) then it is referred as a bootable device, and
if boot sector of any storage device is empty then it is
referred as non-bootable.
e.g. HDD, PD, CD, DVD

there are two steps of booting:


1. machine (h/w) boot:
BIOS => ROM Memory
POST =>
2. system boot

=> UNIX, Windows, Linux, MAC OS X, iOS, Android, Palm OS,


RTOS etc....

=> Open Source OS means kernel source code of that OS is


freely available onto internet.
e.g. Linux (vmlinuz - kernel source code is freely
available on internet).

# DAY-02:

- there are 2 major subsystems of any OS:


1. process control subsystem
2. file subsystem

- In UNIX => "File has Space and Process has Life"


- In UNIX, whatever that can be stored is considered as a
file, whereas whatever is running/active is considered as
a process.
- UNIX treats all devices as a file:
- In UNIX devices can be catagorised into two catagories:
1. "character devices": devices from which data gets
tranferes char by char i.e. byte by byte are referred as
character devices and UNIX treats all char devices as
"char special device file".

e.g. keyboard, printer, monitor etc...

2. "block devices" : devices from which data gets


tranferes block by block i.e. sector by sector are
referred as block devices and UNIX treats all block
devices as "block special device file".
e.g. storage devices
- When data gets copied from HDD to PD or vice-versa,
UNIX treats this operation as copying of data from one
file into another file.

"buffer cache" => it is a purely is software technique in


which kernel uses few portion of main memory as a cache
memory in which most recently accessed disk contents can
be kept to achieve max throughput in min hardware
movement.

Q. What are system calls?

DS :
slll.c => porgram

functions:
main() -> calling function => client function

services:
create_node()
add_last()
add_first()
add_at_pos()
delete_first()
delete_last()
delete_at_pos()
display_list()
Kernel => Program
System Calls => Functions

- System calls are the functions of kernel program defined


in C, C++ & assembly language which provides interface of
services made available by the kernel for user programs.

to open a file/to create a new file => OS


to write data into a file => OS
to read data from a file => OS

- fopen() C lib function => open() sys call => kernel


function - to open a file or to create a new file
- fwrite()/fprintf()/printf()/fputs()/fputc() C lib
functions => write() sys call - to write data into a file
- fread()/fscanf()/scanf()/fgets()/fgetc() C lib
functions => read() sys call
- to read data from file

- In UNIX there are total 64 system calls


- In Linux there are more than 300 system calls
- In Windows there are more than 3000 system calls

e.g.
- In UNIX, fork() sys call is used to create a new
process/child process.
- In Linux, fork() & clone() sys call used to create a
new process/child process.
- In Windows, CreateProcess() sys call is used to create
a new process/child process.
- Irespective of any OS there are 6 catagories of system
calls:

- exit() C lib function internally makes call to _exit()


sys call which is used to terminate a process.
- wait() sys call is used to suspend a process
"pid" => process id - an OS identifies each process
uniquely by its pid.
getpid() sys call returns pid of calling process
getppid

- stat() sys call is used to get info about the file

//program to do sum of 2 numbers - user defined code


#include<stdio.h>

int main(void)
{
//local vars definition
int n1, n2, res;

//executable instructions
printf("enter n1 & n2 : ");//write() sys call -
system defined code
scanf("%d %d", &n1, &n2);//read() sys call - system
defined code

res = n1 + n2;

printf("res = %d\n", res);//write() sys call - system


defined code

return 0;//successful termination


}

Q. What is an Interrupt?
- An interrupt is a signal recieved by the CPU from an IO
device due to which it stops an execution of one
job/process and start executing another job/process.
- interrupts which are sent by an IO devices to the CPU
are referred as hardware interrupts.

- Whenever system call gets called the CPU switches from


user defined code into system defined code, hence system
calls are also called as software interrupts
or trap.
- "Process Control Subsystem":

Process Management:

Q. Why RAM is also called as Main Memory?


- for an execution of any program RAM is must, hence RAM
is also called as Main Memory.

- Kernel gets loaded into the main memory while booting


the system, it remains present inside main memory till we
do not shutdown system, few part of main memory will
always occupied by the kernel, so portion of the main
memory which

- "execution context" => info about data & instructions


of currently executing process by the CPU can be kept
temporarily into the CPU registers, and collectively this
info is referred as an execution context, copy of it also
can be kept into the PCB of that process.

- "swap area" => it is a portion of HDD kept reserved by


an OS at the time installation which can be used later as
an extension of the main memory in which inactive running
programs can be kept temporarily.

upon process submission => PCB for that gets created into
main memory inside kernel space.

=> after process submission it may active or it may be


inactive

"active running program" => if for a process PCB is there


into the main memory inside kernel space and program also
there into the main memory inside user space.

"inactive running program" => if for a process PCB is


there into the main memory inside kernel space but
program is not there into the main memory inside user
space, it can be kept temporarily into the swap area
if for any process PCB is not there into the main memory
inside kernel space
=> process is terminated

+ Features of an OS:
1. multi-programming
2. multi-tasking

3. multi-threading
4. multi-processor
5. multi-user

# day-01:
step-1: start ingination
step-2: start bike either by kick or click
step-3: need to press cluch fully
step-4: change gear from neutral to 1st
.
.
.

# day-20:
step-1: start ingination
step-2: start bike either by kick or click
step-3: need to press cluch fully
step-4: change gear from neutral to 1st
.
.
.

"responsiveness to stimuli":
reaction time given by the brain to all your actions is
so quick

- thread is the smallest execution unit of a process


- thread is lightweight process
- thread is the smallest indivisible part of a process

process => thread


element => atom

- "multi-threading" -> system in which the CPU can


execute multiple threads which are of either same process
or are of different processes simultaneusly.
(i.e. one after another), the speed at which the CPU
executes multiple threads which of either same process or
are of diff processes simultaneuosly, it seems that the
CPU is executing multiple threads at a time.

"uni-processor system" -> system can run on such a


machine in which only 1 CPU is there.

4. "multi-processor" => system can run on such a machine


in which more than one CPU's are connected in a closed
circuit.

5. "multi-user" => system in which more than one users


can logged in at a time
Server OS =>

- to keep track on all running programs an OS maintains


few data structures called as kernel data structures,
there are such 3 kernel data structures:
1. job queue
2. ready queue
3. waiting queue

queue => it is a linear/basic data structure which is a


collection/list of logically related similar type data
elements

- there are 4 cases in which CPU scheduler must gets


called:
1. running -> terminated : due to an exit
2. running -> waiting : due an io request
3. running -> ready : due an interrupt
4. waiting -> ready : due an io request completion

- there are 2 types of CPU scheduling:


1. "non-preemptive" : under non-preemptive type of CPU
scheduling control of the CPU released by the process by
its own i.e. voluntarily.
e.g. above case-1 & case-2

2. "preemptive" : under preemptive type of CPU scheduling


control of the CPU taken away forcefully from process.
e.g. case-3 & case-4

- there are basic 4 cpu scheduling algorithms:


1. fcfs (first come first served) cpu scheduling
2. sjf (shortest job first) cpu scheduling
3. round robin cpu scheduling
4. priority cpu scheduling

waiting time
response time
turn-around-time

1. fcfs (first come first served) cpu scheduling:

ready queue => 100 jobs

P9 - larger process
- larger process has got lowest priority

multi-programming system and in this system during


runtime as well processes are getting submitted into the
ready queue, and processes which are are getting
submitted into the ready queue during runtime having CPU
burst time smaller than burst time of P9
# OS_DAY-04:

Processes/Jobs Priority Values


P1 3
P2 4
P3 1
P4 2

P3 has got highest priority => P4 => P1 => P2

raedy queue => 100 jobs


P3 - very low priority

- multi-level queue scheduling

- processes which are running in the system can be


catagorised into 2 catagories:
1. "background processes" => processes with which user
cannot interacts
2. "foreground process" => processes with which user can
interacts

2 friends are PreCAT @SunBeam Hinjwadi

Hinjwadi - Wakad => 10 km


PG @ Wakad ->

common bike -

own bikes -

2 friends SunBeam Building

1 - ground floor
2 - 5th floor

medium/network => network service provider companies jio,


idea, airtel

voice call =>

"related processes" => processes which are of same parent


"non-related processes" => processes which are of
different parent's

=> by using unnamed pipe only related processes can


communicates.
=> by using named pipe only related as well as non-
related processes can communicates.

# way-1 => voice call - voice data


# way-2 => video call - voice + video data
# way-3 => text message - text data
# way-4 => missed calls - predefined meaning - "signals"

1 missed call
2 missed calls
3 misssed calls
.
.

3 Students A B C seated on one desk and they are sharing


one common notebook

- if i asked to all of them to write something in that


same notebook, on same page and same line at a time =>

- decide their order:


A
B
C

Line => dbda


"data inconsistency"

"race condition" => if two or more processes are trying


to access same resource at a time race condition occurs
- to avoid race condition an OS has to do co-ordination

- there are 2 synchronization tools out of which any one


tool can be used to avoid critical section problem.
1. semaphore:
binary semphore
counting semaphore/classic semaphore
2. mutex object

- binary semaphore & mutex object can be used if resource


can be acquired by only one process at a time.

+ "Memory Management":
Main Memory => RAM

Max CPU utilization => multi-tasking/time sharing =>


multi-programming
=>

SunBeam => Krishna Hall -> "225" Seats => 1-225 =>
Physical Seat Numbers

Website => 1 Month PreCAT Batch of 1000 capacity will be


starting from 1st May
to 31st May 2022 => YES

25th April - 1000 addmissions are full => 1-1000 =>


logical seat numbers

Batch of 1000 students will be divided into 5 equal size


sub batches
in each bacth => first 25 seats are reserved for faculties
from seat number 26-225 => 200 seats are available for
students

batch-1: 1-200 - 6 TO 8
batch-2: 201-400 - 8 TO 10
batch-3: 401-600 - 10 TO 12
batch-4: 601-800 - 12 TO 2
batch-5: 801-1000 - 2 TO 4

PG Facility => -> "Sahyadri" => 1000

1st May =>


bacth-1: 1->26, 2->27, 3->28, ......, 200->225
batch-2: 201->26, 202->27, 203->28, ....., 400->225
.
.
.

# day-02:
batch-1:

- When any process is requesting for memory, there are 2


methods by which memory allocated for it, there are 2
memory allocation methods:
1. "contiguos memory allocation": process can complete
its execution only if memory gets allocated for it in a
contiguos manner.
- further there 2 schemes under contiguos allocation
method:
i. fixed sized partitioning scheme
ii. variable/dynamic sized partitioning scheme

2. "non-contiguos memory allocation":

- deadlock and deadlock handling methods:


- memory management:

RAM => 4 GB [ 1 GB : Kernel Space + 3 GB : User Space ]

Process P9 => 6 GB

Virtual Memory = Maim Memory : 4 GB + Swap Area : 8 GB =


12 GB

Is it possible for an OS to complete an execution of such


a process which is having size bigger than size of main
memory itself ? ==> YES

"Virtual Memory Management": in this technique, an OS not


only manages physical memory it also manages virtual
memory (i.e. Swap Area which physical not a main memory
it is portion of HDD) with it.

Virtual Memory Management = Paging + Swapping

- Big size process in its logical memory space gets


divided into pages, and not all pages of that process
resides into the main memory at once, only active pages
of it can be kept into the main memory, whereas inactive
pages of it can be kept into the swap area, and swapping
of pages can be done by one system program called as
"swapper" between swap area and main memory and an
execution of that process will be completed page by page
i.e. pagewise

- swapper process does swapping of pages in between main


memory and swap area, variants of swapping in virtual
memory management is referred as "pagein" &
"page out"

- page of any process can be loaded into the main memory


by swapper only after it is requesting by that process
i.e. only after demanding by the process, hence it is
also referred as "demand paging", and page which is never
requested will never gets loaded into the main memory it
is referred as "pure demand paging", in this case swapper
process is also called as "lazy swapper".

- no. of pages of Process having size 6 GB > no. of


frames
- total no. of pages of all submitted processes >>>> no.
of frames

- there are quite good chances that more frequently all


frames becomes full, and in this case if any process is
requesting for any of its new page and if that page is
not present into the main memory is called "page fault"

- when page fault occurs, means requested page of any


process is not present into the main memory, it may
present into the swap area, in this case as there is no
free frame available so there is a need to remove any
existing page from the main memory and can be kept temp
into swap area so that requested page can be loaded into
that free frame, so we can say there is a need of page
replacement i.e. there is need to decide which page
should removed from main memory for loading new page from
swap area, and to do this there are certain algorithms
referred as "page replacement algorithms".

- there are total 5 page replacement algorithms:


1. "fifo (first in first out) page replacement algorithm" =>
in this algo, page which was inserted first into the main
memory gets removed first and requested page will be
loaded into that free frame.
- this algo can be implemented by using fifo queue

2. "optimal page replacement algorithm" => in this algo,


page which will not used in near future will be removed
first and requested page will be loaded into that free
frame.
- as any OS dont know in advanced that which page will
not used near future so this algo is practically not
feasible, but it is used to set bechmark for other page
replacement algorithms (on paper this algo is provably
efficient).
3. "lru (least recently used) page replacement algorithm"
=> in this algo, least recently used page will be removed
from the main memory and requested page will be loaded
into that free frame.
- this algo can be implemented by using stack

- following algo follows counting based approach:


4. "lfu (least frequently used) page replacement
algorithm"
5. "mfu (most frequently used) page replacement
algorithm"

+ "thrashing": if any process spends more time on paging


rather than execution, this high paging activity is
called as thrashing.

- In all modern OS memory management has been implemented


in a combined form:
Segmentation + Paging => Swapping.

+ "File Management":
Q. What is a File ?
User point view:
- File is a named collection of logically related
data/information.
- File is basic storage unit
- File is like a container which contains logically
related data/information/records.

System point view:


- File is a stream of bits/bytes i.e. collection of 0's &
1's
text file / video file / image file / source file etc... =>
stream of bits/bytes

- In Windows OS => type of data into the file can be


identified by its dot extension
.txt
.doc
.xls
.rar
.mp4
.mp3
.jpeg
.exe
.c / .cpp / .java

=> In Linux OS dot extension doesnt matters, in Linux


there are 7 types of files
- In Linux text file, video file, audio file, source file,
image file etc... will be considered as "regular file".

file has 2 things:


1. "data" => actual file contents which are exists inside
the file
2. "meta data" => information about that file like name of
the file, type of the file, parent folder location, size of
the file, access perms, time stamp etc...
can be stored into one structure called as "FCB (File
Control Block)".

- Process => PCB (Process Control Block) - structure


which contains info about process (runtime) => Kernel
Space.
- In UNIX FCB is called as iNode
- File => FCB (File Control Block) - structure which
contains info about file => HDD permanenetly and when an
OS is accessing that file its FCB contents/iNode
gets loaded into the main memory temporarily.
- FCB/iNode mainly contains:
- inode number (unique identifier of a file)
- name of the file
- type of the file
- size of the file
- access perms
- time stamps
etc...

- accounting information system calls :


getpid() - to get pid of calling process
getppid() - to get pid of parent of calling process
stat() - to get information about file
- When any new file gets created one structure gets
created for that file in which all information of that file
can be kept => FCB/iNode.

- no. of iNodes = no. of files


- 10,000 files = 10,000 iNodes

- 10K iNodes of 10K files, and millions of bytes of data


of those 10K files gets stored onto HDD permanently.

+ "filesystem" => it is a way to store data onto any


storage device (e.g. HDD, PD, CD, DVD etc...) in an
organized manner so that it can be accessed efficiently.

e.g.
Windows => NTFS(New Technology FileSystem),
FAT/FAT16/FAT32/ (File Allocation Table)

Linux => ext, ext2, ext3 (Extended File System)


MAC OS => HFS (Heirarchical FileSystem)
UNIX => UFS (UNIX File System).

- When we format any storage device new filesystem gets


created on it
- To format any storage device is nothing but to create a
new filesystem on it, while creation of new filesystem data
may or may not gets erased from it.

- By using some data recovery tools we can recover data


from HDD even after its formatting, but any data recovery
tool do not gives garuantee.

You might also like