OS UNIT-II (1)
OS UNIT-II (1)
Linus Torvalds has developed Linux OS, but actually, he is only responsible for the
development of the Linux kernel.
Complete Linux system = Kernel + GNU system utilities and libraries + other
management scripts + installation scripts.
What is Shell?
• A shell is a special user program that provides an interface for the user to use operating system services.
Shell accepts human-readable commands from users and converts them into something which the kernel
can understand. It is a command language interpreter that executes commands read from input devices
such as keyboards or from files. The shell gets started when the user logs in or starts the terminal.
Shell is broadly classified into two categories
• Command Line Shell
• Graphical shell
Command Line Shell
Shell can be accessed by users using a command line interface. A special program
called Terminal in Linux/macOS, or Command Prompt in Windows OS is provided
to type in the human-readable commands such as “cat”, “ls” etc. and then it is being
executed.
Graphical Shells
Graphical shells provide means for manipulating programs based on the graphical
user interface (GUI), by allowing for operations such as opening, closing, moving,
and resizing windows, as well as switching focus between windows. Window OS or
Ubuntu OS can be considered as a good example which provides GUI to the user for
interacting with the program. Users do not need to type in commands for every
action.
Types of shells
•Bourne shell (sh)
•C shell (csh)
•Korn shell (ksh)
•Bourne Again shell (bash)
• Developed at AT&T Bell Labs by Steve Bourne, the Bourne shell is regarded as the first UNIX
shell ever.
• It is denoted as sh.
• It gained popularity due to its compact nature and high speeds of operation.
• The complete path-name for the Bourne shell is /bin/sh and /sbin/sh. By default, it uses
the prompt # for the root user and $ for the non-root users.
Limitations
These different shells do the same job but understands different commands and provides different built in
functions.
• What is a terminal?
A program which is responsible for providing an interface to a user so that
he/she can access the shell. It basically allows users to enter commands and
see the output of those commands in a text-based interface.
Shell Scripting
• shells are interactive, which means they accept commands as input from users
and execute them.
• If we want to execute a bunch of commands repeatedly, we have to type all
commands each and every time in the terminal.
• To overcome this we can write commands in a file and execute them in shell to
avoid repetitive work. These files are called Shell Scripts or Shell Programs.)
• Shell scripts are similar to the batch file in MS-DOS. Each shell script is saved
with `.sh` file extension e.g., myscript.sh.
• A shell script has syntax just like any other programming language. If you have
any prior experience with any programming language like Python, C/C++ etc.
Why shell scripts?
Addition of 2 numbers
#! /bin/bash
echo "enter value of a:"
read a
echo "enter value of b:"
read b
c=`expr $a + $b`
echo “value of c is: $c”
O/p:
enter value of a:5
enter value of b:5
Value of c is 10
Shell program to find greatest of two numbers
#! /bin/bash
echo "Enter a number:"
read number
factorial=1
# Check if the number is non-negative
if [ $number -lt 0 ]; then
echo "Factorial is not defined for negative
numbers."
elif [ $number -eq 0 ]; then
echo "Factorial of 0 is 1"
else
# Call the factorial function
for(( i=1; i<=number; i++ )); do
factorial=$(( factorial *i ))
done
echo "Factorial of $number is
$factorial"
fi
shell script to check whether the number is palindrome or not:
#! /bin/bash
echo “enter n”
read n
num=0
on=$n
while [ $n -gt 0 ]; do
num=$(expr $num \* 10)
k=$(expr $n % 10)
num=$(expr $num + $k)
n=$(expr $n / 10)
done
if [ $num -eq $on ]; then
echo “palindrome”
else
echo “not palindrome”
fi
Print a greeting based on the time of day
#! /bin/bash
currenthour=$(date +%H)
if [ $currenthour -gt 5 -a $currenthour -lt 12 ]; then
greeting="Good morning"
elif [ $currenthour -gt 12 -a $currenthour -lt 18 ]; then
greeting="Good afternoon"
else
greeting="Good night"
fi
echo "$greeting have a nice day!"
Process
A process is a program at the time of execution.
Process Program
A Program is basically a collection of
The process is basically an instance
instructions that mainly performs a
of the computer program that is
specific task when executed by the
being executed.
computer.
A process has a shorter lifetime. A Program has a longer lifetime.
A Process requires resources such as A Program is stored by hard-disk and
memory, CPU, Input-Output devices. does not require any resources.
A process has a dynamic instance of A Program has static code and static
code and data data.
Basically, a process is the running On the other hand, the program is
instance of the code. the executable code.
Process State
Process State
As a process executes, it changes state.
Resource sharing
Parent and children share all resources.
Children share subset of parent’s resources.
Execution
Parent and children execute concurrently.
Parent waits until children terminate.
Operating System Concepts
Process Creation (Cont.)
Address space
Child duplicate of parent.
Child has a program loaded into it.
UNIX examples
fork system call creates new process
exec system call used after a fork to replace the process’ memory space with
a new program.
Process can’t share the same memory area. Threads can share same memory area.
System calls are requested to communicate each System calls are not required.
other.
Process is looselycoupled. Threads are tightly coupled.
It requires more resources to execute. Requires few resources to execute.
Multithreading
Advantages
• Thread switching does not require Kernel mode privileges.
• User level thread can run on any operating system.
• Scheduling can be application specific in the user level thread.
• User level threads are fast to create and manage.
Types Of Threads:
User Threads :
Thread creation, scheduling, management happen in user space by
Thread Library.
user threads are faster to create and manage.
If a user thread performs a system call, which blocks it, all the other
threads in that process one also automatically blocked, whole process is
blocked.
Disadvantages
In a typical operating system, most system calls areblocking.
Multithreaded application cannot take advantage ofmultiprocessing.
Kernel Threads:
kernel creates, schedules, manages these threads .
these threads are slower, manage.
If one thread in a process blocked, over all process need not be blocked.
Advantages
Kernel can simultaneously schedule multiple threads from the same process on
multiple processes.
If one thread in a process is blocked, the Kernel can schedule another thread of
the same process.
Kernel routines themselves can multithreaded.
Disadvantages
Kernel threads are generally slower to create and manage than the user threads.
Transfer of control from one thread to another within same process requires a
mode switch to the Kernel.
PROCESS SCHEDULING:
But in simple computers CPU sit idle until the I/O request granted.
scheduling is a important OS function. All resources are scheduled before
use.(cpu, memory, devices…..)
Such operating systems allow more than one process to be loaded into the
executable memory at a time and the loaded process shares the CPU using
Scheduling Objectives
Maximize throughput.
Enforce Priorities.
What are Scheduling Queues?
All processes, upon entering into the system, are stored in the Job Queue.
Processes waiting for a device to become available are placed in Device Queues.
There are unique device queues available for each I/O device.
Types of Schedulers
There are three types of schedulers available:
Long Term Scheduler
Short Term Scheduler
Medium Term Scheduler
P1 2 2 13 13-2= 11 11-2= 9 9
P2 5 6 19 19-5= 14 14-6= 8 8
P3 0 4 4 4-0= 4 4-4= 0 0
P4 0 7 11 11-0= 11 11-7= 4 4
P5 7 4 23 23-7= 16 16-4= 12 12
If the arrival time for processes are different process with short
burst time have to wait for the current process's execution to
finish.
0 3 7 8 12 16
0 2 4 5 7 11 16
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3