OS Unit 1 & 2 Notes
OS Unit 1 & 2 Notes
Steps:
1. Power ON
2. CPU will move BIOS in ROM
3. BIOS will be executed.(POST)
4. Power on self Test.
One of the purposes of an operating system is to hide the peculiarities of specific hardware devices from the
user.
Secondary-Storage Management Generally speaking, systems have several levels of storage, including
primary storage, secondary storage and cache storage. Instructions and data must be placed in primarystorage
or cache to be referenced by a running program.
vi. Security and Protection
The operating system uses password protection to protect user data and similar other techniques. it also
prevents unauthorized access to programs and user data. The operating system provides various techniques
which assure the integrity and confidentiality of user data. Following security measures are used to protect
user data:
Protection against unauthorized access through login.
Protection against intrusion by keeping firewall active.
Protecting the system memory against malicious access.
Displaying messages related to system vulnerabilities.
Fourth Generation
With the development of LSI (Large Scale Integration) circuits, chips, operating system entered in the
personal computer and the workstation age. Microprocessor technology evolved to the point that it b+ecomes
possible to build desktop computers as powerful as the mainframes of the 1970s.
Advantages of Time-Sharing OS
Each task gets an equal opportunity.
Fewer chances of duplication of software.
CPU idle time can be reduced.
Disadvantages of Time-Sharing OS
Reliability problem.
One must have to take care of the security and integrity of user programs and data.
Data communication problem.
Multi-Programming Operating System
Disadvantages of RTOS
Limited Tasks: Very few tasks run at the same time and their concentration is very less on a few
applications to avoid errors.
Use heavy system resources: Sometimes the system resources are not so good and they are
expensive as well.
Complex Algorithms: The algorithms are very complex and difficult for the designer to write on.
• Process Control
These system calls deal with processes such as process creation, process termination etc.
• File Management
These system calls are responsible for file manipulation such as creating a file, reading a file, writing
into a file etc.
• Device Management
These system calls are responsible for device manipulation such as reading from device buffers,
writing into device buffers etc.
• Information Maintenance
These system calls handle information and its transfer between the operating system and the user
program.
• Communication
These system calls are useful for interprocess communication. They also deal with creating and
deleting a communication connection.
Types Of System Calls
wait()
In some systems, a process may wait for another process to complete its execution. This happens
when a parent process creates a child process and the execution of the parent process is suspended
until the child process executes. The suspending of the parent process occurs with a wait() system
call. When the child process completes execution, the control is returned back to the parent process.
exec()
This system call runs an executable file in the context of an already running process. It replaces the
previous executable file. This is known as an overlay. The original process identifier remains sinc e
a new process is not created but data, heap, stack etc. of the process are replaced by the new
process.
• fork()
Processes use the fork() system call to create processes that are a copy of themselves. This is one of
the major methods of process creation in operating systems. When a parent process creates a child
process and the execution of the parent process is suspended until the child process executes. When
the child process completes execution, the control is returned back to the parent process.
• exit()
The exit() system call is used by a program to terminate its execution. In a multithreaded
environment, this means that the thread execution is complete. The operating system reclaims
resources that were used by the process after the exit() system call.
• kill()
The kill() system call is used by the operating system to send a termination signal to a process that
urges the process to exit. However, kill system call does not necessary mean killing the process and
can have various meanings.
Shell script
A shell script is a computer program designed to be run by the Unix/Linux shell which could be one of the
following:
A shell is a command-line interpreter and typical operations performed by shell scripts include file
manipulation, program execution, and printing text.
Shell Scripts
The basic concept of a shell script is a list of commands, which are listed in the order of execution. A good
shell script will have comments, preceded by # sign, describing the steps.
There are conditional tests, such as value A is greater than value B, loops allowing us to go through massive
amounts of data, files to read and store data, and variables to read and store data, and the script may include
functions.
Shell scripts and functions are both interpreted. This means they are not compiled.
Shell commands
1. In
By using this command a hard link(actual file on Linux) is created to existing file
2. wc
Command for word count. With this we can find no of lines, word count, byte & characters
count in the files
3. unmask
newly created file permissions are restricted by using “mask” The unmask cmd is used to set this
mask.
Grep: globle regular expression print
grep displays the entire line which has the matched string.
4. cut
it cuts out the sections from each line of files and writes the result to standard output
Using a variable: A variable is used to store in some value so that the whole value does not need to
Cat: This Shell Scripting command is used to show the file contents. It is one of the very important
Grep: Another very important command which is used to search for a particular text within the file
and generate the output for you related to the pattern being matched.
Greater than symbol (>): This is used if you want to save the text written on the console to be saved
present in a particular directory. This shell scripting command is often appended with other
Piping ( | ): This is another very basic command of Linux based operating system that is used to fetch
the output received from one command straight away into another. This symbol called pipe can most
often be seen along with the grepping command. At some places, this piping can also be said to be
chaining.
Lesser than symbol (<): If you wish to take the input from files directly then < symbol will be used.
Pwd: This is another very basic Linux command which is used to tell the present working directory
and hence the name pwd. It is most essential when you need to know which subfolder you are in, of a
folder.
Mkdir and cd: These two commands go hand in hand. Mkdir, on one hand, is responsible to create
or make a directory, on the other hand, a cd is used to change a directory by traversing in it. These
monitoring the system status, usage of memory, running process on a system, kill or terminate multiple
processes, can interact directly with operating system and made changes at system level so that it will reflect
after the system reboot, can develop scripts to perform during the boot operation of the system.
Data Backup
Shell scripts can be used to automate the data backup process by creating a Korn job and schedule the shell
script to execute data backup commands and perform the task in an easy way along with the timestamp will
be stored so it will be easy to use them later when we want to use the backup of a particular day, data
archiving, restoring the system to a particular date can be done by using shell scripts, we can monitor the
system status while doing backup operation, restore operation. We can do the system restore by using the
backup data which we stored previously by using shell scripts we can achieve all the operations.
3. Programming
Shell scripts can be used by programmers to develop programs, scripts to perform system-level tasks, finding
patterns in a file, processing the data in a file based on the user inputs, etc. can be done using shell scripts.
Shell scripts can be used like other programming languages as we have built-in functions such as for loop,
4. Automation
Shell scripting can be used to automate several daily tasks, repetitive tasks, etc. If we want to execute the
same command multiple times then we can use shell script built-in functions like for loop, while loop etc. If
we want to find a pattern in a file we can use sed/awk and the same thing we can apply when we want to find
Operations on Processes
System must provide mechanisms for:
● Process creation,
● Process termination,
Process Creation
Parent process create children processes, which, in turn create other processes, forming a tree of processes
● Generally, process identified and managed via a process identifier (pid)
● Resource sharing options
● Parent and children share all resources
● Children share subset of parent’s resources
● Parent and child share no resources
● Execution options
● Parent and children execute concurrently
● Parent waits until children terminate
Process Termination
Some operating systems do not allow child to exists if its parent has terminated. If a process terminates, then
all its children must also be terminated.
● cascading termination. All children, grandchildren, etc. are terminated.
● The termination is initiated by the operating system.
● The parent process may wait for termination of a child process by using the wait()system call.
Process State
As a process executes, it changes state. The state of a process is defined in part by the current activity of that
process. Each process may be in one of the following states:
Process state
process number
program counter
Registers
memory limits
.
.
Process State
This specifies the process state i.e. new, ready, running, waiting or terminated.
Process Number
This shows the number of the particular process.
Program Counter
This contains the address of the next instruction that needs to be executed in the process.
Registers
This specifies the registers that are used by the process. They may include accumulators, index registers, stack
pointers, general purpose registers etc.
Accounting information
The time limits, account numbers, amount of CPU used, process numbers etc. are all a part of the PCB
accounting information.
Schedulers
Short-term scheduler (or CPU scheduler) – selects which process should be executed next and
allocates CPU
Sometimes the only scheduler in a system
Short-term scheduler is invoked frequently (milliseconds) ⇒ (must be fast)
● Long-term scheduler (or job scheduler) – selects which processes should be brought into the ready queue
Long-term scheduler is invoked infrequently (seconds, minutes) ⇒ (may be slow)
The long-term scheduler controls the degree of multiprogramming
1. Preemptive
2. Non Preemptive Scheduling
1. Preemptive Scheduling
Preemptive Scheduling is a CPU scheduling technique that works by dividing time slots of CPU to
a given process.
The time slot given might be able to complete the whole process or might not be able to it.
When the burst time of the process is greater than CPU cycle, it is placed back into the ready queue
and will execute in the next chance. This scheduling is used when the process switch to ready state.
Algorithms that are backed by preemptive Scheduling are round-robin (RR), priority, SRTF
(shortest remaining time first)(also called as SJF Preemtive)
Non-preemptive Scheduling is a CPU scheduling technique the process takes the resource (CPU
time) and holds it till the process gets terminated or is pushed to the waiting state.
No process is interrupted until it is completed, and after that processor switches to another process.
Algorithms that are based on non-preemptive Scheduling are non-preemptive priority, and shortest
Job first.
CPU Scheduling
CPU scheduling is the basis of multiprogramming operatingsystems. By switching the CPU among
processes, the operatingsystem can make the computer more productive.
Scheduling Criteria
Different CPU scheduling algorithms have different properties and may favor one class of processes over
another. In choosing which algorithm to use in a particular situation, we must consider the properties of the
various algorithm.
Scheduling Algorithms
2. Shortest-Job-First Scheduling
3. Priority Scheduling
4. Round-Robin Scheduling
P1 24
P2 3
P3 3
P1 P2 P3
0 24 27 30
2. Shortest-Job-First Scheduling
The shortest job first (SJF) or shortest job next, is a scheduling policy that selects the waiting process wi th
the smallest execution time to execute next. SJN, also known as Shortest Job Next (SJN).
Algorithm:
Sort all the processes according to the arrival time.
Then select that process that has minimum arrival time and minimum Burst time.
After completion of the process make a pool of processes that arrives afterward till the completion of the
previous process and select that process among the pool which is having minimum Burst time.
P2 8
P3 7
P4 3
P4 P1 P3 P2
0 3 9 16 24
3. Priority Scheduling
Priority scheduling is a non-preemptive algorithm and one of the most common scheduling algorithms in
batch systems. Each process is assigned first arrival time (less arrival time process first) if two processes
have same arrival time, then compare to priorities (highest process first). Also, if two processes have same
priority then compare to process number (less process number first). This process is repeated while all
process get executed.
Implementation –
First input the processes with their arrival time, burst time and priority.
First process will schedule, which have the lowest arrival time, if two or more processes will have lowest
arrival time, then whoever has higher priority will schedule first.
Now further processes will be schedule according to the arrival time and priority of the process. (Here we
are assuming that lower the priority number having higher priority). If two process priority are same then
sort according to process number.
Note: In the question, They will clearly mention, which number will have higher priority and which
number will have lower priority.
Once all the processes have been arrived, we can schedule them based on their priority.
P1 10 3
P2 1 1
P3 2 3
P4 1 4
P5 5 2
P2 P5 P1 P3 P4
4. Round-Robin Scheduling
A round-robin scheduling algorithm is used to schedule the process fairly for each job a time slot or
quantum and the interrupting the job if it is not completed by then the job come after the other job which is
arrived in the quantum time that makes these scheduling fairly.
1. Shared Memory
2. Message passing
Figure 1 below shows a basic structure of communication between processes via the shared memory
method and via the message passing method.
An operating system can implement both methods of communication. First, we will discuss the shared
memory methods of communication and then message passing. Communication between processes using
shared memory requires processes to share some variable, and it completely depends on how the
programmer will implement it. One way of communication using shared memory can be imagined like
this: Suppose process1 and process2 are executing simultaneously, and they share some resources or use
some information from another process. Process1 generates information about certain computations or
resources being used and keeps it as a record in shared memory. When process2 needs to use the shared
information, it will check in the record stored in shared memory and take note of the information generated
by process1 and act accordingly. Processes can use shared memory for extracting information as a record
from another process as well as for delivering any specific information to other processes.
Let’s discuss an example of communication between processes using the shared memory method.
What is a Thread?
A thread is a path of execution within a process. A process can contain multiple threads.
Process vs Thread
The primary difference is that threads within the same process run in a shared memory space, while
processes run in separate memory spaces.
Threads are not independent of one another like processes are, and as a result threads share with other
threads their code section, data section, and OS resources (like open files and signals).
But, like process, a thread has its own program counter (PC), register set, and stack space.
Advantages of Thread over Process
1. Responsiveness:
If the process is divided into multiple threads, if one thread completes its execution, then its output can be
immediately returned.
Context switch time between threads is lower compared to process context switch. Process context
switching requires more overhead from the CPU.
if we have multiple threads in a single process, then we can schedule multiple threads on multiple
processor. This will make process execution faster.
4. Resource sharing:
Resources like code, data, and files can be shared among all threads within a process.
Note: stack and registers can’t be shared among the threads. Each thread has its own stack and registers.
5. Communication:
Communication between multiple threads is easier, as the threads shares common address space. while in
process we have to follow some specific communication technique for communication between two
process.
Types of Threads
There are two types of threads.
User Level Thread
Kernel Level Thread
1. User Threads
These threads are implemented and used in the user library. They cannot be created using the system.
While doing thread switching, if the OS is called there will be distractions.
The user thread avoids all distractions and does not interrupt the kernel system.
These are implemented on the systems that do not support multithreading as it is a single thread
process. These are simply represented with a single, register, counter and stack.
The user threads do not create any separate tasks for creation. The switching is also fast as there is no
OS intervention.
There is no coordination between threads and kernel and hence if one thread breaks, the entire
process is blocked.
2. Kernel Threads
Kernel manages the threads and knows each and every thread. This is a multithreading type.
The kernel manages a table to track the threads in each process. Also, there is a separate table to
track the processes and update whenever the changes are made.
OS makes the changes in the thread while creating and managing them. Knowledge of threads is
shared with the kernel and hence the time for each process is scheduled according to the execution.
Kernel threads are used for applications that break in between the process.
Kernel threads are slow when compared with user threads. The thread control block is needed to
control the tasks.
In a specific implementation, the user threads must be mapped to kernel threads, using one of the following
strategies.
Many-To-One Model
In the many-to-one model, many user-level threads are all mapped onto a single kernel thread.
Thread management is handled by the thread library in user space, which is very efficient.
However, if a blocking system call is made, then the entire process blocks, even if the other user
threads would otherwise be able to continue.
Because a single kernel thread can operate only on a single CPU, the many-to-one model does not
allow individual processes to be split across multiple CPUs.
Green threads for Solaris and GNU Portable Threads implement the many-to-one model in the past,
but few systems continue to do so today.
One-To-One Model
The one-to-one model creates a separate kernel thread to handle each user thread.
One-to-one model overcomes the problems listed above involving blocking system calls and the
splitting of processes across multiple CPUs.
However the overhead of managing the one-to-one model is more significant, involving more
overhead and slowing down the system.
Most implementations of this model place a limit on how many threads can be created.
Linux and Windows from 95 to XP implement the one-to-one model for threads.
Many-To-Many Model
The many-to-many model multiplexes any number of user threads onto an equal or smaller number
of kernel threads, combining the best features of the one-to-one and many-to-one models.
Users have no restrictions on the number of threads created.
Blocking kernel system calls do not block the entire process.
Processes can be split across multiple processors.
Individual processes may be allocated variable numbers of kernel threads, depending on the
number of CPUs present and other factors.
Threading Issues
Some of the issues related to thread are
1. The Fork () and EXEC() system Call
2. Thread Cancellation
3. Signal Handling
4. Thread Pool