OS Notes
OS Notes
OPERATING
SYSTEMS
Name:
Roll Number:
Class:
OPERATING SYSTEMS
Unit -1: Introduction:
Operating Systems Objectives and Functions, Computer System Architecture, Operating
System Structure, Operating System Operations. Evolution of Operating Systems, Types of
Operating System, Simple, Batch, Multi programmed, time shared, Parallel, Distributed
Systems, Real-Time Systems, Operating System Services.
Examples:
Understand the operating system objectives, functions and its structure.
Exercises:
DOS Internal & External Commands
Web Resources:
[Link]
[Link]
[Link]
UNIT – II
Write the program to implement CPU scheduling algorithm for first come first serve
Scheduling
Write a program to implement a CPU scheduling algorithm for shortest job first scheduling.
Write a ‘C’ program to perform priority scheduling.
Write a program to implement CPU scheduling for Round Robin Scheduling.
Web Resources:
[Link]
[Link]
UNIT – III
Memory Management and Virtual Memory –
Logical & physical Address Space, Swapping, Contiguous Allocation, Paging-Structure of Page
Table Segmentation, Virtual Memory, Demand Paging, Performance of Demanding Paging
Page Replacement, Page Replacement Algorithms, Allocation of frames.
Examples:
Description on new methods used in Memory management in the present-day Operating
Systems.
Exercises:
Memory management with fixed partitioning technique. (MFT)
Memory management with variable partitioning technique. (MVT)
Web Resources:
[Link]
[Link]
UNIT – IV
UNIX
Introduction to Unix:- Architecture of Unix, Features of Unix , Unix Commands – PATH, man,
echo, printf, script, passwd, uname, who, date, stty, pwd, cd, mkdir, rmdir, ls, cp, mv, rm, cat,
more, wc, lp, od, tar, gzip.
Examples:
Description on how UNIX treats regular files and directories differently from other operating
systems.
Exercises:
Getting hands-on on basic UNIX Commands.
Write a program using the following system calls of UNIX OS fork, exec, getpid, exit, wait,
close, opendir, readdir.
Execute various file/directory handling commands in UNIX.
Specific Resources: (web)
[Link]
[Link]
UNIT – V
Shell programming:
Ordinary and environment variables. The profile. Read and read only commands. Command
line arguments. exit and exit status of a command. Logical operators for conditional
execution. The test command and its shortcut. The if, while, for and case control statements.
Simple shell program examples.
Examples:
Description on Shell Commands & Shell Programming to manage files
Exercises:
Write a Simple shell script for basic arithmetic and logical calculations.
Write a shell script to display a list of users currently logged in.
Write a shell script to delete all the temporary files.
Write a shell script to search an element from an array using binary searching.
Write a shell script to determine whether a given number is a prime number or not
Write a shell script to print the first n Fibonacci numbers.
Execute various system administrative commands
Web Resources:
[Link]
[Link]
TEXT BOOK:
UNIT – 1
An Operating System (OS) is the software that manages all the hardware and software
resources of a computer. It acts as an intermediary between the user and the computer
hardware, making the system easier to use and ensuring efficient resource utilization.
Examples of operating systems:
Windows
macOS
Linux
Android
iOS
The OS is crucial for the proper functioning of any computer system, enabling users to interact
with the computer and run applications smoothly.
Computer is an electronic machine that makes performing any task very easy. In computer,
the CPU executes each instruction provided to it, in a series of steps, this series of steps is
called Machine Cycle, and is repeated for each instruction. One machine cycle involves
fetching of instruction, decoding the instruction, transferring the data, executing the
instruction. Most systems use a single general-purpose processor (PDAs through
mainframes). So, based on the processors used in computer systems, they are categorized
into the following systems.
1. Single-processor system
2. Multiprocessor system
3. Clustered Systems
Most systems use a single processor. On a single processor system, there is one main CPU
capable of executing a general-purpose instruction set, including instructions from user
processes. A single processor system contains only one processor. So only one process can be
executed at a time and then the process is selected from the ready queue. Single processor
system can be further described using the diagram below:
As in the above diagram, there are multiple applications that need to be executed.
However, the system contains a single processor and only one process can be executed at a
time.
Multiprocessor Systems:
Most computer systems are single processor systems i.e. they only have one processor.
However, multiprocessor or parallel systems are increasing in importance nowadays. These
systems have multiple processors working in parallel that share the computer clock, memory,
bus, peripheral devices etc. It is also known as Parallel systems or tightly coupled systems.
Increased reliability: If one processor fails, the system can often continue operating using the
remaining processors. This redundancy ensures that critical tasks are not interrupted and
improves the system's overall reliability.
The ability to continue providing service proportional to the level of surviving hardware is
called graceful degradation. Some systems go beyond graceful degradation and are called
fault tolerant, because they can suffer a failure of any single component and still continue
operation.
In asymmetric clustering, one machine is in hot-standby mode while the other is running the
applications. The hot-standby host machine does nothing but monitor the active server. If
that server fails, the hot-standby host becomes the active server. If that server fails, the hot-
standby host becomes the active server. In symmetric mode, two or more hosts are running
applications, and are monitoring each other. This mode is obviously more efficient, as it uses
all of the available hardware. It does require that more than one application be available to
run.
In clustered systems, where multiple nodes share resources and collaborate on tasks, Distributed
Lock Managers (DLMs) play a crucial role in maintaining data consistency and preventing conflicts.
Focus: The design and organization of the hardware components of a computer system.
Scope: Includes:
Goal: To define the functional behavior and performance characteristics of the hardware.
Focus: The design and organization of the software components of the operating system.
Scope: Includes:
Kernel: Core components (process management, memory management, file system, device
drivers).
User Interface: How users interact with the system (e.g., command-line, GUI).
System Services: Functions provided to applications (e.g., file I/O, network communication).
Goal: To provide an efficient, reliable, and user-friendly interface between applications and the
underlying hardware.
Key Differences:
Hardware vs. Software: Computer architecture deals with hardware design, while OS structure deals
with software organization.
Relationship: OS structure is built upon the foundation of computer architecture. The OS must
interact with and utilize the hardware resources defined by the architecture.
In essence:
Operating system structure defines how software utilizes and manages that hardware.
Time sharing requires an interactive computer system, which provides direct communication
between the user and the system. The user gives instructions to the operating system or to a
program directly, using an input device such as a keyboard, mouse, touch pad, or touch
screen, and waits for immediate results on an output device. Accordingly, the response time
should be short—typically less than one second.
A time-shared operating system allows many users to share the computer simultaneously.
Since each action or command in a time-shared system tends to be short, only a little CPU
time is needed for each user. A program loaded into memory and executing is called a
process. When a process executes, it typically executes for only a short time before it either
finishes or needs to perform I/O.
Time sharing and multiprogramming require that several jobs be kept simultaneously in
memory. If several jobs are ready to be brought into memory, and if there is not enough room
for all of them, then the system must choose among them. Making this decision involves job
scheduling. If several jobs are ready to run at the same time, the system must choose which
job will run first. Making this decision is CPU scheduling. In a time-sharing system, the
operating system must ensure reasonable response time. This goal is sometimes
accomplished through swapping, whereby processes are swapped in and out of main
memory to the disk.
A more common method for ensuring reasonable response time is virtual memory, a
technique that allows the execution of a process that is not completely in memory. The main
advantage of the virtual-memory scheme is that it enables users to run programs that are
larger than actual physical memory.
Dual-Mode Operation:
Dual-mode operation is a fundamental concept in operating systems that ensures
a clear separation of privilege levels between user processes and the operating system kernel.
It's a crucial mechanism for maintaining system security, stability, and efficient resource
management.
At the very least we need two separate modes of operation user mode and kernel
mode. A bit, called the mode bit is added to the hardware of the computer to indicate the
current mode: kernel (0) or user (1) with the mode bit we are able to distinguish between a
task that is executed on behalf of the operating system and one that is executed on behalf of
the user, When the computer system is executing on behalf of a user application, the system
is in user mode. However, when a user application requests a service from the operating
system (via a system call), it must transition from user to kernel mode to fulfil the request. At
system boot time, the hardware starts in kernel mode. The operating system is then loaded
and starts user applications in user mode. Whenever a trap or interrupt occurs, the hardware
switches from user mode to kernel mode (that is, changes the state of the mode bit to 0).
Thus, whenever the operating system gains control of the computer, it is in kernel mode. The
system always switches to user mode (by setting the mode bit to 1) before passing control to
a user program.
1. Mainframe Systems
Reduce setup time by batching similar jobs Automatic job sequencing –
automatically transfers control from one job to another. First rudimentary
operating system. Resident monitor
initial control in monitor
control transfers to job
when job completes control transfers pack to monitor
2. Batch Processing Operating System:
This type of OS accepts more than one jobs and these jobs are batched/ grouped together
according to their similar requirements.
This is done by computer operator. Whenever the computer becomes available, the batched
jobs are sent for execution and gradually the output is sent back to the user.
It allowed only one program at a time.
This OS is responsible for scheduling the jobs according to priority and the resource required.
During execution, the job may have to wait for some tasks, such as an I/O operation, to
complete.
In a multiprogramming system, the operating system simply switches to another job and
executes.
When that job needs to wait, the CPU is switched to another job, and so on.
When the first job finishes waiting and it gets the CPU back.
As long as at least one job needs to execute, the CPU is never idle.
Multiprogramming operating systems use the mechanism of job scheduling and CPU
scheduling.
It uses CPU scheduling, memory management, disc management and security management.
Examples: CTSS, MULTICS, CAL, UNIX etc.
5. Distributed Systems:
replicating& they spread via worm or virus mechanisms. They can disrupt entire companies
or even world-wide networks.
E.g. Windows 98, Windows 2000, Linux.
It is a special purpose OS in which there are rigid time requirements on the operation of a
processor. A real time OS has well defined fixed time constraints. Processing must be done
within the time constraint or the system will fail. A real time system is said to function
correctly only if it returns the correct result within the time constraint. These systems are
characterized by having time as a key parameter.
One set of operating-system services provides functions that are helpful to the user
Communications – Processes may exchange information, on the same computer or between
computers over a network Communications may be via shared memory or through message
passing (packets moved by the OS)
Error detection – OS needs to be constantly aware of possible errors May occur in the CPU
and memory hardware, in I/O devices, in user program. For each type of error, OS should
take the appropriate action to ensure correct and consistent computing Debugging facilities
can greatly enhance the user’s and programmer’s abilities to efficiently use the system
Another set of OS functions exists for ensuring the efficient operation of the system itself via
resource Sharing
Resource allocation - When multiple users or multiple jobs running concurrently, resources
must be allocated to each of them.
Many types of resources - Some (such as CPU cycles, main memory, and file storage) may
have special allocation code, others (such as I/O devices) may have general request and
release code
Accounting - To keep track of which users use how much and what kinds of computer
resources
Protection and security - The owners of information stored in a multiuser or networked
computer system may want to control use of that information, concurrent processes should
not interfere with each other
Protection involves ensuring that all access to system resources is controlled.
Security of the system from outsiders requires user authentication, extends to defending
external I/O devices from invalid access attempts.
EXERCISES:
DOS commands are instructions given to the Disk Operating System to perform specific tasks. They
are broadly classified into two categories:
1. Internal Commands:
Residency: Built into the [Link] file, they are loaded into memory when the system boots.
Examples:
2. External Commands:
Accessibility: Require the system to locate and load the corresponding file from the disk.
Performance: Generally slower than internal commands due to the disk access involved.
Examples:
Examples cls, dir, cd, copy, del format, chkdsk, fdisk, tree
UNIT – 2
PROCESS CONCEPTS
A batch system executes jobs whereas a timeshared system has user programs or tasks. Even
on a single user system such as Microsoft Windows, a user may be able to run several
programs at one time: a word processor, a Web browser, and an e-mail package. And even if
the user can execute only one program at a time, the operating system may need to support
its own internal programmed activities, such as memory management. In many respects, all
these activities are similar, so we call all of them processes.
The terms Job and process are used almost interchangeably in this text. Although we
personally prefer the term process, much of operat1ng-system theory and terminology was
developed during a time when the major activity of operating systems was job processing. It
would be misleading to avoid the use of commonly accepted terms that include the word job
(such as job scheduling) simply because process has superseded job.
We emphasize that a program by itself is not a process; a program is a passive entity, such as
a file containing a list of instructions stored on disk (often called an executable file), whereas
a process is an active entity, with a program counter specifying the next instruction to execute
and a set of associated resources. A program becomes a process when an executable file is
loaded into memory. Two common techniques for loading executable files are double-clicking
an icon representing the executable file and entering the name of the executable.
The process, from its creation to completion, passes through various states. The minimum
number of states is five. The names of the states are not standardized although the process
may be in one of the following states during execution.
1. New
A program which is going to be picked up by the OS into the main memory is called a new
process.
2. Ready
Whenever a process is created, it directly enters in the ready state, in which, it waits for the
CPU to be assigned. The OS picks the new processes from the secondary memory and put all
of them in the main memory.
The processes which are ready for the execution and reside in the main memory are called
ready state processes. There can be many processes present in the ready state.
3. Running
One of the processes from the ready state will be chosen by the OS depending upon the
scheduling algorithm. Hence, if we have only one CPU in our system, the number of running
processes for a particular time will always be one. If we have n processors in the system then
we can have n processes running simultaneously.
4. Block or wait
From the Running state, a process can make the transition to the block or wait state
depending upon the scheduling algorithm or the intrinsic behaviour of the process. When a
process waits for a certain resource to be assigned or for the input from the user then the OS
move this process to the block or wait state and assigns the CPU to the other processes.
5. Completion or termination
When a process finishes its execution, it comes in the termination state. All the context of the
process (Process Control Block) will also be deleted the process will be terminated by the
Operating system.
6. Suspend ready
A process in the ready state, which is moved to secondary memory from the main memory
due to lack of the resources (mainly primary memory) is called in the suspend ready state. If
the main memory is full and a higher priority process comes for the execution then the OS
have to make the room for the process in the main memory by throwing the lower priority
process out into the secondary memory. The suspend ready processes remain in the
secondary memory until the main memory gets available.
7. Suspend wait
Instead of removing the process from the ready queue, it's better to remove the blocked
process which is waiting for some resources in the main memory. Since it is already waiting
for some resource to get available hence it is better if it waits in the secondary memory and
make room for the higher priority process. These processes complete their execution once
the main memory gets available and their wait is finished.
Each process is represented in the operating system by a process control block (PCB) also
called a task control block. A PCB is shown in figure. It contains many pieces of information
associated with a specific process, including these:
Process state. The state may be new, ready running, waiting, halted, and so on. Program
counter. The counter indicates the address of the next instruction to be executed for this
process. CPU registers. The registers vary in number and type, depending on the computer
architecture. They include accumulators, index registers, stack pointers, and general-purpose
registers, plus any condition-code information. Along with the program counter, this state
information must be saved when an interrupt occurs, to allow the process to be continued
correctly afterward CPU-scheduling information. This information includes a process priority,
pointers to scheduling queues, and any other scheduling parameters Memory-management
information. This information may include such information as the value of the base and limit
registers, the page tables, or the segment tables, depending on the memory system used by
the operating system. Accounting information. This information includes the amount of CPU
and real time used, time limits, account numbers, job or process numbers, and so on. I/O
status information. This information includes the list of I/O devices allocated to the process,
a list of open files, and so on.
Processes that executing concurrently in the operating system may be either independent
processes or cooperating processes. if a process cannot affect or be affected by the other
processes executing in the system then the process is said to be independent. So, any process
that does not share any data with any other process is independent. A process is said to be
cooperating if it can affect or be affected by the other processes executing in the system. So,
it is clear that, any process which shares its data with other processes is a cooperating process.
[Link] passing
2.1.5 Threads
A thread is a flow of execution through the process code, with its own program counter that
keeps track of which instruction to execute next, system registers which hold its current
working variables, and a stack which contains the execution history.
A thread shares with its peer threads few information like code segment, data segment and
open files. When one thread alters a code segment memory item, all other threads see that.
A thread is also called a lightweight process. Threads provide a way to improve application
performance through parallelism. Threads represent a software approach to improving
performance of operating system by reducing the overhead thread is equivalent to a classical
process.
Each thread belongs to exactly one process and no thread can exist outside a process. Each
thread represents a separate flow of control. Threads have been successfully used in
implementing network servers and web server. They also provide a suitable foundation for
parallel execution of applications on shared memory multiprocessors. The following figure
shows the working of a single-threaded and a multithreaded process.
Types of Thread
Threads are implemented in following two ways −
User Level Threads − User managed threads.
Kernel Level Threads − Operating System managed threads acting on kernel, an operating
system core.
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.
Disadvantages
In a typical operating system, most system calls are blocking.
Multithreaded application cannot take advantage of multiprocessing.
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 be multithreaded.
Disadvantages
Kernel threads are generally slower to create and manage than the user threads.
Transfer of control from one thread to another within the same process requires a mode
switch to the Kernel.
As processes enter the system, they are put into a job queue, which consists of all processes
in the system. The processes that are residing in main memory and are ready and waiting to
execute are kept on a list called the ready queue. This queue is generally stored as a linked
list. ready-queue header contains pointers to the first and final PCBs in the list. Each PCB
includes a pointer field that points to the next PCB in the ready queue.
The system also includes other queues. When a process is allocated the CPU, it executes for
a while and eventually quits, is interrupted, or waits for the occurrence of a particular event,
such as the completion of an I/0 request. Suppose the process makes an I/O request to a
shared device, such as a disk. Since there are many processes in the system, the disk may be
busy with the I/0 request of some other process. The process therefore may have to wait for
the disk. The list of processes waiting for a particular I/0 device is called a device queue.
A new process is initially put in the ready queue. It waits there until it is selected for execution,
or is dispatched. Once the process is allocated the CPU and is executing, one of several events
could occur:
The process could issue an I/0 request and then be placed in an I/0 queue.
The process could create a new subprocess and wait for the subprocess's termination.
The process could be removed forcibly from the CPU, as a result of an interrupt, and be put
back in the ready queue.
2.2.2 Schedulers
There are three types of process schedulers:
1. Long Term or Job Scheduler
It brings the new process to the ‘Ready State’. It controls the Degree of Multi-programming,
i.e., the number of processes present in a ready state at any point in time. It is important that
the long-term scheduler make a careful selection of both I/O and CPU-bound processes. I/O-
bound tasks are which use much of their time in input and output operations while CPU-
bound processes are which spend their time on the CPU. The job scheduler increases
efficiency by maintaining a balance between the two. They operate at a high level and are
typically used in batch-processing systems.
Short-Term or CPU Scheduler
It is responsible for selecting one process from the ready state for scheduling it on the running
state. Note: Short-term scheduler only selects the process to schedule it doesn’t load the
process on running. Here is when all the scheduling algorithms are used. The CPU scheduler
is responsible for ensuring no starvation due to high burst time processes.
The dispatcher is responsible for loading the process selected by the Short-term scheduler on
the CPU (Ready to Running State) Context switching is done by the dispatcher only. A
dispatcher does the following:
Switching context.
Switching to user mode.
Jumping to the proper location in the newly loaded program.
3. Medium-Term Scheduler
It is responsible for suspending and resuming the process. It mainly does swapping (moving
processes from main memory to disk and vice versa). Swapping may be necessary to improve
the process mix or because a change in memory requirements has overcommitted available
memory, requiring memory to be freed up. It is helpful in maintaining a perfect balance
between the I/O bound and the CPU bound. It reduces the degree of multiprogramming.
Generally, Speed is lesser than Speed is the fastest among all Speed lies in between both
short term scheduler of them. short and long-term
schedulers.
It controls the degree of It gives less control over how It reduces the degree of
multiprogramming much multiprogramming is multiprogramming.
done.
It can re-enter the process into It selects those processes It can re-introduce the process
memory, allowing for the which are ready to execute into memory and execution
continuation of execution. can be continued.
Context switching only allows a single CPU to handle multiple processes requests parallelly
without the need for any additional processors.
So the context switching of two processes, the priority-based process occurs in the ready
queue of the process control block. These are the following steps.
The state of the current process must be saved for rescheduling.
The process state contains records, credentials, and operating system-specific information
stored on the PCB or switch.
The PCB can be stored in a single layer in kernel memory or in a custom OS file.
A handle has been added to the PCB to have the system ready to run.
The operating system aborts the execution of the current process and selects a process from
the waiting list by tuning its PCB.
Load the PCB’s program counter and continue execution in the selected process.
Process/thread values can affect which processes are selected from the queue, this can be
important.
Preemptive scheduling
CPU-scheduling decisions may take place under the following four circumstances:
1) When a process switches from the running state to the waiting state (for example, as the
result of an I/0 request or an invocation of wait for the termination of one of the child
processes)
2) When a process switches from the running state to the ready state (for example, when an
interrupt occurs)
3) When a process switches from the waiting state to the ready state (for example, at
completion of I/0)
4) When a process terminates
Disadvantages of Preemptive
Limited computational resources must be used.
Suspending the running process, change the context, and dispatch the new incoming process
all take more time.
The low-priority process would have to wait if multiple high-priority processes arrived at the
same time.
Non-Preemptive scheduling
Non-preemptive Scheduling is used when a process terminates, or a process switches from
running to the waiting state. In this scheduling, once the resources (CPU cycles) are allocated
to a process, the process holds the CPU till it gets terminated or reaches a waiting state. In
the case of non-preemptive scheduling does not interrupt a process running CPU in the
middle of the execution. Instead, it waits till the process completes its CPU burst time, and
then it can allocate the CPU to another process.
Advantages
It has a minimal scheduling burden.
It is a very easy procedure.
Less computational resources are used.
It has a high throughput rate.
Dispatcher
Another component involved in the CPU-scheduling function is the dispatcher. The dispatcher
is the module that gives control of the CPU to the process selected by the short-term
scheduler. This function involves the following:
Switching context
Switching to user mode
Jumping to the proper location in the user program to restart that program The dispatcher
should be as fast as possible, since it is invoked during every process switch. The time it takes
for the ispatcher to stop one process and start another running is known as the dispatch
latency.
Scheduling criteria
Different CPU-scheduling algorithms have different properties, and the choice of a particular
algorithm 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 algorithms.
Many criteria have been suggested for comparing CPU-scheduling algorithms. Which
characteristics are used for comparison can make a substantial difference in which algorithm
is judged to be best. The criteria include the following:
CPU utilization. We want to keep the CPU as busy as possible. Conceptually, CPU utilization
can range from 0 to 100 percent. In a real system, it should range from 40 percent (for a lightly
loaded system) to 90 percent (for a heavily used system).
Throughput. If the CPU is busy executing processes, then work is being done. One measure
of work is the number of processes that are completed per time unit, called throughput. For
long processes, this rate may be one process per hour; for short transactions, it may be ten
processes per second.
Turnaround time. From the point of view of a particular process, the important criterion is
how long it takes to execute that process. The interval from the time of submission of a
process to the time of completion is the turnaround time. Turnaround time is the sum of the
periods spent waiting to get into memory, waiting in the ready queue, executing on the CPU,
and doing I/0.
Waiting time. The CPU-scheduling algorithm does not affect the amount of time during which
a process executes or does I/0; it affects only the amount of time that a process spends
waiting in the ready queue. Waiting time is the sum of the periods spent waiting in the ready
queue.
Response time. In an interactive system, turnaround time may not be the best criterion.
Often, a process can produce some output fairly early and can continue computing new
results while previous results are being output to the user. Thus, another measure is the time
from the submission of a request until the first response is produced. This measure, called
response time, is the time it takes to start responding, not the time it takes to output the
response. The turnaround time is generally limited by the speed of the output device.
Scheduling Algorithm
1. First Come First Serve:
FCFS considered to be the simplest of all operating system scheduling algorithms. First come
first serve scheduling algorithm states that the process that requests the CPU first is allocated
the CPU first and is implemented by using FIFO queue.
Characteristics of FCFS:
FCFS supports non-preemptive and preemptive CPU scheduling algorithms.
Tasks are always executed on a First-come, First-serve concept.
FCFS is easy to implement and use.
This algorithm is not much efficient in performance, and the wait time is quite high.
Advantages of FCFS:
Easy to implement
First come, first serve method
Disadvantages of FCFS:
FCFS suffers from Convoy effect.
The average waiting time is much higher than the other algorithms.
FCFS is very simple and easy to implement and hence not much efficient.
Example
Let's take an example of The FCFS scheduling algorithm. In the Following schedule, there are
5 processes with process ID P0, P1, P2, P3 and P4. P0 arrives at time 0, P1 at time 1, P2 at
time 2, P3 arrives at time 3 and Process P4 arrives at time 4 in the ready queue. The processes
and their respective Arrival and Burst time are given in the following table.
The Turnaround time and the waiting time are calculated by using the following formula.
Turn Around Time = Completion Time - Arrival Time
Waiting Time = Turnaround time - Burst Time
Characteristics of SJF:
Shortest Job first has the advantage of having a minimum average waiting time among all
operating system scheduling algorithms.
Example In the following example, there are five jobs named as P1, P2, P3, P4 and P5. Their
arrival time and burst time are given in the table below.
Since, No Process arrives at time 0 hence; there will be an empty slot in the Gantt chart from
time 0 to 1 (the time at which the first process arrives).
According to the algorithm, the OS schedules the process which is having the lowest burst
time among the available processes in the ready queue.
Till now, we have only one process in the ready queue hence the scheduler will schedule this
to the processor no matter what is its burst time.
This will be executed till 8 units of time. Till then we have three more processes arrived in the
ready queue hence the scheduler will choose the process with the lowest burst time.
Among the processes given in the table, P3 will be executed next since it is having the lowest
burst time among all the available processes.
So that's how the procedure will go on in shortest job first (SJF) scheduling algorithm.
Priority Scheduling:
Preemptive Priority CPU Scheduling Algorithm is a pre-emptive method of CPU scheduling
algorithm that works based on the priority of a process. In this algorithm, the editor sets the
functions to be as important, meaning that the most important process must be done first. In
the case of any conflict, that is, where there are more than one processor with equal value,
then the most important CPU planning algorithm works on the basis of the FCFS (First Come
First Serve) algorithm.
Characteristics of Priority Scheduling:
• Schedules tasks based on priority.
• When the higher priority work arrives while a task with less priority is executed, the higher
priority work takes the place of the less priority one and
• The latter is suspended until the execution is complete.
• Lower is the number assigned, higher is the priority level of a process.
Example
In the Example, there are 7 processes P1, P2, P3, P4, P5, P6 and P7. Their priorities, Arrival
Time and burst time are given in the table.
We can prepare the Gantt chart according to the Non Preemptive priority scheduling.
The Process P1 arrives at time 0 with the burst time of 3 units and the priority number 2. Since
No other process has arrived till now hence the OS will schedule it immediately. Meanwhile
the execution of P1, two more Processes P2 and P3 are arrived. Since the priority of P3 is 3
hence the
Meanwhile the execution of P3, All the processes get available in the ready queue. The
Process with the lowest priority number will be given the priority. Since P6 has priority
number assigned as 4 hence it will be executed just after P3.
After P6, P4 has the least priority number among the available processes; it will get executed
for the whole burst time.
Since all the jobs are available in the ready queue hence All the Jobs will get executed
according to their priorities. If two jobs have similar priority number assigned to them, the
one with the least arrival time will be executed.
From the GANTT Chart prepared, we can determine the completion time of every process.
The turnaround time, waiting time and response time will be determined.
1. Turn Around Time = Completion Time - Arrival Time
2. Waiting Time = Turn Around Time - Burst Time
• • Round robin seems to be fair as every process gets an equal share of CPU.
• • The newly created process is added to the end of the ready queue.
Example:-
Process Synchronization
The main objective of process synchronization is to ensure that multiple processes access
shared resources without interfering with each other and to prevent the possibility of
inconsistent data due to concurrent access. To achieve this, various synchronization
techniques such as semaphores, monitors, and critical sections are used.
In a multi-process system, synchronization is necessary to ensure data consistency and
integrity, and to avoid the risk of deadlocks and other synchronization problems. Process
synchronization is an important aspect of modern operating systems, and it plays a crucial
role in ensuring the correct and efficient functioning of multi-process systems.
A cooperating process is one that can affect or be affected by other processes executing in
the system. Cooperating processes can either directly share a logical address space (that is,
both code and data) or be allowed to share data only through files or messages. The former
case is achieved through the use of threads.
Example:
The code for the producer process can be modified as follows:
[Link] waiting. There exists a bound, or limit, on the number of times that other
processes are allowed to enter their critical sections after a process has made a request to
enter its critical section and before that request is granted.
Synchronization Hardware
Problems occur when two processes running concurrently share the same data or same
variable. The value of that variable may not be updated correctly before its being used by a
second process. Such a condition is known as Race Around Condition. There are a software as
well as hardware solutions to this problem. In this article, we will talk about the most efficient
hardware solution to process synchronization problems and its implementation.
There are three algorithms in the hardware approach of solving Process Synchronization
problem:
1. Test and Set
2. Swap
3. Unlock and Lock
The TestAndSet () instruction can be defined as shown in Figure 6.4. The important
characteristic of this instruction is that it is executed atomically. Thus, if two TestAndSet ()
instructions are executed simultaneously (each on a different CPU), they will be executed
sequentially in some arbitrary order.
If the machine supports the TestAndSet () instruction, then we can implement mutual
exclusion by declaring a Boolean variable lock, initialized to false. The structure of process P
The Swap() instruction, in contrast to the TestAndSet () instruction, operates on the contents
of two words; it is defined as shown in Figure 6.6. Like the TestAndSet () instruction, it is
executed atomically. If the machine supports the Swap () instruction, then mutual exclusion
can be provided as follows. A global Boolean variable lock is declared and is initialized to false.
In addition, each process has a local Boolean variable key. The structure of process P;
These data structures are initialized to false. To prove that the mutualexclusion requirement
is met, we note that process P; can enter its critical section only if either waiting [i] == false or
key == false. The value of key can become false only if the TestAndSet () is executed. The first
process to execute the TestAndSet () will find key== false; all others must wait. The variable
waiting [i] can become false only if another process leaves its critical section; only one waiting
[i] is set to false, maintaining the mutual-exclusion requirement.
to read and write) the database. We distinguish between these two types of processes by
referring to the former as readers and to the latter as writers. Precisely in OS we call this
situation as the readers-writers problem. Problem parameters:
• One set of data is shared among a number of processes.
• Once a writer is ready, it performs its write. Only one writer may write at a time.
• If a process is writing, no other process can read it.
• If at least one reader is reading, no other process can write.
• Readers may not write and only read.
Dining-Philosophers Problem
The Dining Philosopher Problem states that K philosophers seated around a circular table with
one chopstick between each pair of philosophers. There is one chopstick between each
philosopher. A philosopher may eat if he can pickup the two chopsticks adjacent to him. One
chopstick may be picked up by any one of its adjacent followers but not both. This problem
involves the allocation of limited resources to a group of processes in a deadlock-free and
starvation-free manner.
operations, are employed to address these challenges in operating systems and concurrent
programming.
UNIT – 3
INTRODUCTION
Memory is the important part of the computer that is used to store the data. Its management
is critical to the computer system because the amount of main memory available in a
computer system is very limited. At any time, many processes are competing for it. Moreover,
to increase performance, several processes are executed simultaneously. For this, we must
keep several processes in the main memory, so it is even more important to manage them
effectively.
LOGICAL ADDRESS
A logical address, also known as a virtual address, is an address generated by the CPU during
program execution. It is the address seen by the process and is relative to the program’s
address space. The process accesses memory using logical addresses, which are translated by
the operating system into physical addresses. An address that is created by the CPU while a
program is running is known as a logical address. Because the logical address is virtual—that
is, it doesn’t exist physically—it is also referred to as such. The CPU uses this address as a
reference to go to the actual memory location. All logical addresses created from a program’s
perspective are referred to as being in the “logical address space”. This address is used as a
reference to access the physical memory location by CPU. The term Logical Address Space is
used for the set of all logical addresses generated by a program’s perspective.
MMU
The physical hardware of a computer that manages its virtual memory and caching functions
is called the memory management unit (MMU). The MMU is sometimes housed in a separate
Integrated Chip (IC), but it is typically found inside the central processing unit (CPU) of the
computer. The MMU receives all inputs for data requests and decides whether to retrieve the
data from ROM or RAM storage.
PHYSICAL ADDRESS
A physical address is the actual address in the main memory where data is stored. It is a
location in physical memory, as opposed to a virtual address. Physical addresses are used by
the Memory Management Unit (MMU) to translate logical addresses into physical addresses.
The user must use the corresponding logical address to go to the physical address rather than
directly accessing the physical address. For a computer program to function, physical memory
space is required. Therefore, the logical address and physical address need to be mapped
before the program is run. The term “physical address” describes the precise position of
necessary data in a memory. Before they are used, the MMU must map the logical address to
the physical address. This is because the user program creates the logical address and believes
that the program is operating in this logical address. However, the program requires physical
memory to execute. All physical addresses that match the logical addresses in a logical
address space are collectively referred to as the “physical address space”.
SWAPPING
To increase CPU utilization in multiprogramming, a memory management scheme known as
swapping can be used. Swapping is the process of bringing a process into memory and then
temporarily copying it to the disc after it has run for a while. The purpose of swapping in an
operating system is to access data on a hard disc and move it to RAM so that application
programs can use it. It’s important to remember that swapping is only used when data isn’t
available in RAM. Although the swapping process degrades system performance, it allows
larger and multiple processes to run concurrently. Because of this, swapping is also known as
memory compaction. The CPU scheduler determines which processes are swapped in and
which are swapped out. Consider a multiprogramming environment that employs a priority-
based scheduling algorithm. When a high- priority process enters the input queue, a low-
priority process is swapped out so the high-priority process can be loaded and executed.
When this process terminates, the low priority process is swapped back into memory to
continue its execution.
Swapping has been subdivided into two concepts: swap-in and swap-out.
• Swap-out is a technique for moving a process from RAM to the hard disc.
• Swap-in is a method of transferring a program from a hard disc to main memory, or
RAM.
Advantages
• If there is low main memory so some processes may have to wait for much long but
by using swapping process do not have to wait long for execution on CPU.
• It utilizes the main memory.
• Using only single main memory, multiple process can be run by CPU using swap
partition.
• The concept of virtual memory starts from here and it utilize it in better way.
• This concept can be useful in priority-based scheduling to optimize the swapping
process.
Disadvantages
• If there is low main memory resource and user is executing too many processes and
suddenly the power of system goes off there might be a scenario where data get erase
of the processes which are took parts in swapping.
• Chances of number of page faults occur
• Low processing performance
Contiguous Allocation
✓ Multiple-partition allocation.
✓ Hole – block of available memory; holes of various size are scattered
throughout memory
✓ When a process arrives, it is allocated memory from a hole large enough to
accommodate it
Contiguous memory allocation is one of the efficient ways of allocating main memory
to the processes. The memory is divided into two partitions. One for the Operating
System and another for the user processes. Operating System is placed in low or high
memory depending on the interrupt vector placed. In contiguous memory allocation
each process is contained in a single contiguous section of memory
.
Memory protection
Memory protection is required to protect Operating System from the user processes
and user processes from one another. A relocation register contains the value of the
smallest physical address for example say 100040.
The limit register contains the range of logical address for example say 74600. Each
logical address must be less than limit register. If a logical address is greater than the
limit register, then there is an addressing error and it is trapped. The limit register
hence offers memory protection. The MMU, that is, Memory Management Unit maps
the logical address dynamically, that is at run time, by adding the logical address to
the value in relocation register. This added value is the physical memory address
which is sent to the memory. The CPU scheduler selects a process for execution and a
dispatcher loads the limit and relocation registers with correct values. The advantage
of relocation register is that it provides an efficient way to allow the Operating System
size to change dynamically.
Memory allocation
There are two methods namely, multiple partition method and a general fixed
partition method. In multiple partition method, the memory is divided into several
fixed size partitions. One process occupies each partition. This scheme is rarely used
nowadays. Degree of multiprogramming depends on the number of partitions. Degree
of multiprogramming is the number of programs that are in the main memory. The
CPU is never left idle in multiprogramming. This was used by IBM OS/360 called MFT.
MFT stands for Multiprogramming with a Fixed number of Tasks. Generalization of
fixed partition scheme is used in MVT. MVT stands for Multiprogramming with a
Variable number of Tasks. The Operating System keeps track of which parts of memory
are available and which is occupied. This is done with the help of a table that is
maintained by the Operating System. Initially the whole of the available memory is
treated as one large block of memory called a hole. The programs that enter a system
are maintained in an input queue. From the hole, blocks of main memory are allocated
to the programs in the input queue. If the hole is large, then it is split into two, and
one half is allocated to the arriving process and the other half is returned. As and when
memory is allocated, a set of holes in scattered. If holes are adjacent, they can be
merged. Now there comes a general dynamic storage allocation problem. The
following are the solutions to the dynamic storage allocation problem.
➢ First fit: The first hole that is large enough is allocated. Searching for the holes
starts from the beginning of the set of holes or from where the previous first
fit search ended.
➢ Best fit: The smallest hole that is big enough to accommodate the incoming
process is allocated. If the available holes are ordered, then the searching can
be reduced.
➢ Worst fit: The largest of the available holes is allocated.
First and best fits decrease time and storage utilization. First fit is generally faster.
Fragmentation
The disadvantage of contiguous memory allocation is fragmentation. There are two types of
fragmentation, namely, internal fragmentation and External fragmentation.
Internal fragmentation
When memory is free internally, that is inside a process but it cannot be used, we call that
fragment as internal fragment. For example say a hole of size 18464 bytes is available. Let the
size of the process be 18462. If the hole is allocated to this process, then two bytes are left
which is not used. These two bytes which cannot be used forms the internal fragmentation.
The worst part of it is that the overhead to maintain these two bytes is more than two bytes.
External fragmentation
All the three dynamic storage allocation methods discussed above suffer external
fragmentation. When the total memory space that is got by adding the scattered holes is
sufficient to satisfy a request but it is not available contiguously, then this type of
fragmentation is called external fragmentation.
The solution to this kind of external fragmentation is compaction. Compaction is a method by
which all free memory that are scattered are placed together in one large memory block. It is
to be noted that compaction cannot be done if relocation is done at compile time or assembly
time. It is possible only if dynamic relocation is done, that is relocation at execution time.
One more solution to external fragmentation is to have the logical address space and physical
address space to be non-contiguous. Paging and Segmentation are popular non contiguous
allocation methods.
Paging
A computer can address more memory than the amount physically installed on the system.
This extra memory is actually called virtual memory and it is a section of a hard that's
set up to emulate the computer's RAM. Paging technique plays an important role in
implementing virtual memory. Paging is a memory management technique in which process
address space is broken into blocks of the same size called pages (size is power of 2, between
512 bytes and 8192 bytes). The size of the process is measured in the number of pages.
Similarly, main memory is divided into small fixed-sized blocks of (physical) memory called
frames and the size of a frame is kept the same as that of a page to have optimum utilization
of the main memory and to avoid external fragmentation.
Paging Hardware
Address Translation
Page address is called logical address and represented by page number and the offset.
A data structure called page map table is used to keep track of the relation between a page
of a process to a frame in physical memory.
When the system allocates a frame to any page, it translates this logical address into a physical
address and create entry into the page table to be used throughout execution of the program.
When a process is to be executed, its corresponding pages are loaded into any available
memory frames. Suppose you have a program of 8Kb but your memory can accommodate
only 5Kb at a given point in time, then the paging concept will come into picture.
When A computer runs out of RAM, the operating system (OS) will move idle or unwanted
pages of memory to secondary memory to free up RAM for other processes and brings them
back when needed by the program.
This process continues during the whole execution of the program where the OS keeps
removing idle pages from the main memory and write them onto the secondary memory and
bring them back when required by the program.
The two memory access problem can be solved by the use of a special fast-lookup hardware
cache called associative memory or translation look-aside buffers (TLBs)
Memory Protection
Segmentation
Memory-management scheme that supports user view of memory A program is a collection
of segments A segment is a logical unit such as: main program Procedure function method
object local variables, global variables common block stack symbol table arrays
Segmentation Architecture
• base – contains the starting physical address where the segments reside in memory
• Segment-table base register (STBR) points to the segment table’s location in memory
• Protection
• read/write/execute privileges
• Protection bits associated with segments; code sharing occurs at segment level
Segmentation Hardware
Virtual Memory
Virtual Memory is a space where large programs can store themselves in form of pages while
their execution and only the required pages or portions of processes are loaded into the main
memory. This technique is useful as large virtual memory is provided for user programs when
a very small physical memory is there.
In real scenarios, most processes never need all their pages at once, for following reasons:
Error handling code is not needed unless that specific error occurs, some of which are
quite rare.
Arrays are often over-sized for worst-case scenarios, and only a small fraction of the arrays
are actually used in practice.
Demand Paging
A demand paging is similar to a paging system with swapping. When we want to execute a
process, we swap it into memory. Rather than swapping the entire process into memory.
When a process is to be swapped in, the pager guesses which pages will be used before the
process is swapped out again Instead of swapping in a whole process, the pager brings only
those necessary pages into memory. Thus, it avoids reading into memory pages that will not
be used in anyway, decreasing the swap time and the amount of physical memory needed.
Hardware support is required to distinguish between those pages that are in memory and
those pages that are on the disk using the valid-invalid bit scheme. Where valid and invalid
pages can be checked checking the bit and marking a page will have no effect if the process
never attempts to access the pages. While the process executes and accesses pages that are
memory resident, execution proceeds normally
Fig. Transfer of a paged memory to continuous disk space
Access to a page marked invalid causes a page-fault trap. This trap is the result of the
operating system's failure to bring the desired page into memory. Initially only those pages
are loaded which will be required the process immediately. The pages that are not moved
into the memory are marked as invalid in the page table. For an invalid entry the rest of the
table is empty. In case of pages that are loaded in the memory, they are marked as valid along
with the information about where to find the swapped out page. When the process requires
any of the page that is not loaded into the memory, a page fault trap is triggered and following
steps are followed,
1. The memory address which is requested by the process is first checked, to verify the
request made by the process.
6. The instruction that caused the page fault must now be restarted from the beginning.
There are cases when no pages are loaded into the memory initially, pages are only loaded
when demanded by the process by generating page faults. This is called Pure Demand Paging.
The only major issue with Demand Paging is, after a new page is loaded, the process starts
execution from the beginning. It is not a big issue for small programs, but for larger programs
it affects performance drastically
What is dirty bit?
When a bit is modified by the CPU and not written back to the storage, it is called as a dirty
bit. This bit is present in the memory cache or the virtual storage space.
Page Replacement
As studied in Demand Paging, only certain pages of a process are loaded initially into the
memory. This allows us to get more number of processes into the memory at the same time.
but what happens when a process requests for more pages and no free memory is available
to bring them in. Following steps can be taken to deal with this problem:
1. Put the process in the wait queue, until any other process finishes its execution thereby
freeing frames.
2. Or, remove some other process completely from the memory to free frames.
3. Or, find some pages that are not being used right now, move them to the disk to get free
frames. This technique is called Page replacement and is most commonly used. We have some
great algorithms to carry on page replacement efficiently.
When the page that was selected for replacement and was paged out, is referenced again, it
has to read in from disk, and this requires for I/O completion. This process determines the
quality of the page replacement algorithm: the lesser the time waiting for page-ins, the better
is the algorithm.
A page replacement algorithm looks at the limited information about accessing the pages
provided by hardware, and tries to select which pages should be replaced to minimize the
total number of page misses, while balancing it with the costs of primary storage and
processor time of the algorithm itself. There are many different page replacement algorithms.
We evaluate an algorithm by running it on a particular string of memory reference and
computing the number of page faults,
Reference String
The string of memory references is called reference string. Reference strings are generated
artificially or by tracing a given system and recording the address of each memory reference.
The latter choice produces a large number of data, where we note two things.
For a given page size, we need to consider only the page number, not the entire address.
If we have a reference to a page p, then any immediately following references to page p will
never cause a page fault. Page p will be in memory after the first reference; the immediately
following references will not fault.
For example, consider the following sequence of addresses − 123,215,600,1234,76,96
If page size is 100, then the reference string is 1,2,6,12,0,0 FirstInFirstOut(FIFO)algorithm
Oldest page in main memory is the one which will be selected for replacement.
Easy to implement, keep a list, replace pages from the tail and add new pages at the head.
• In the Second Chance page replacement policy, the candidate pages for removal are
consider in a round robin matter, and a page that has been accessed between consecutive
considerations will not be replaced.
The page replaced is the one that - considered in a round robin matter - has not been accessed
since its last consideration.
• Implementation:
• If the second chance bit is ONE, reset its second chance bit (to ZERO) and continue.
• If the second chance bit is ZERO, replace the page in that memory frame.
• The following figure shows the behavior of the program in paging using the Second
Chance page replacement policy:
We can see notably that the bad replacement decision made by FIFO is not present in Second
chance!!!
o There are a total of 9 page read operations to satisfy the total of 18 page requests - just as
good as the more computationally expensive LRU method !!!
NRU (Not Recently Used) Page Replacement Algorithm - This algorithm requires that each
page have two additional status bits 'R' and 'M' called reference bit and change bit
respectively. The reference bit(R) is automatically set to 1 whenever the page is referenced.
The change bit (M) is set to 1 whenever the page is modified. These bits are stored in the PMT
and are updated on every memory reference.
When a page fault occurs, the memory manager inspects all the pages and divides them into
4 classes based on R and M bits.
Class 1: (0,0) − neither recently used nor modified - the best page to replace.
Class 2: (0,1) − not recently used but modified - the page will need to be written out before
replacement.
Class 3: (1,0) − recently used but clean - probably will be used again soon.
Class 4: (1,1) − recently used and modified - probably will be used again, and write out will be
needed before replacing it.
This algorithm removes a page at random from the lowest numbered non-empty class
Allocation of Frames
An important aspect of operating systems, virtual memory is implemented using demand
paging. Demand paging necessitates the development of a page-replacement algorithm and
a frame allocation algorithm. Frame allocation algorithms are used if you have multiple
processes; it helps decide how many frames to allocate to each process.
There are various constraints to the strategies for the allocation of frames:
• You cannot allocate more than the total number of available frames.
• At least a minimum number of frames should be allocated to each process. This constraint
is supported by two reasons. The first reason is, as less number of frames are allocated, there
is an increase in the page fault ratio, decreasing the performance of the execution of the
process. Secondly, there should be enough frames to hold all the different pages that any
single instruction can reference.
Frame allocation algorithms – The two algorithms commonly used to allocate frames to a
process are:
1. Equal allocation: In a system with x frames and y processes, each process gets equal
number of frames, i.e. x/y. For instance, if the system has 48 frames and 9 processes,
each process will get 5 frames. The three frames which are not allocated to any
process can be used as a free-frame buffer pool.
• Advantage: All the processes share the available frames according to their needs,
rather than equally.
Global vs Local Allocation – The number of frames allocated to a process can also
dynamically change depending on whether you have used global replacement or local
replacement for replacing pages in case of a page fault.
1. Local replacement: When a process needs a page which is not in the memory, it
can bring in the new page and allocate it a frame from its own set of allocated
frames only.
• Advantage: The pages in memory for a particular process and the page fault
ratio is affected by the paging behavior of only that process.
• Disadvantage: A low priority process may hinder a high priority process by not
making its frames available to the high priority process.
2. Global replacement: When a process needs a page which is not in the memory, it
can bring in the new page and allocate it a frame from the set of all frames, even
if that frame is currently allocated to some other process; that is, one process can
take a frame from another.
• Advantage: Does not hinder the performance of processes and hence results in
greater system throughput.
UNIT – 4
What is Unix?
The Unix operating system is a set of programs that act as a link between the computer and
the user.
The computer programs that allocate the system resources and coordinate all the details of
the computer's internals is called the operating system or the kernel.
Users communicate with the kernel through a program known as the shell. The shell is a
command line interpreter; it translates commands entered by the user and converts them
into a language that is understood by the kernel.
• Unix was originally developed in 1969 by a group of AT&T employees Ken Thompson,
Dennis Ritchie, Douglas McIlroy, and Joe Ossanna at Bell Labs.
• There are various Unix variants available in the market. Solaris Unix, AIX, HP Unix and
BSD are a few examples. Linux is also a flavor of Unix which is freely available.
• Several people can use a Unix computer at the same time; hence Unix is called a
multiuser system.
• A user can also run multiple programs at the same time; hence Unix is a multitasking
environment.
Unix Architecture
Here is a basic block diagram of a Unix system –
The main concept that unites all the versions of Unix is the following four basics −
• Kernel: The kernel is the heart of the operating system. It interacts with the hardware
and most of the tasks like memory management, task scheduling and file
management.
• Shell: The shell is the utility that processes your requests. When you type in a
command at your terminal, the shell interprets the command and calls the program
that you want. The shell uses standard syntax for all commands. C Shell, Bourne Shell
and Korn Shell are the most famous shells which are available with most of the Unix
variants.
• Commands and Utilities: There are various commands and utilities which you can
make use of in your day to day activities. cp, mv, cat and grep, etc. are few examples
of commands and utilities. There are over 250 standard commands plus numerous
others provided through 3rd party software. All the commands come along with
various options.
• Files and Directories: All the data of Unix is organized into files. All files are then
organized into directories. These directories are further organized into a tree-like
structure called the filesystem.
10. Accounting: UNIX keeps an account of jobs created by the user. This feature enhances
the system performance in terms of CPU monitoring and disk space checking. It allows
you to keep an account of disk space used by each user, and the disk space can be
limited by each other. You can assign every user a different disk quota. The root user
can perform these accounting tasks using various commands such as quota, df, du,
etc.
UNIX COMMANDS
1. PATH
• Purpose: Defines the directories where the system searches for executable programs.
• Usage:
o Displaying the current PATH: echo $PATH
o Modifying the PATH: (Temporarily) export
PATH=$PATH:/path/to/new/directory
▪ (Permanently) Edit your shell's configuration file (e.g., .bashrc,
.bash_profile) and add the new directory to the PATH variable.
2. man
• Purpose: Displays the manual pages for commands.
• Usage: man <command_name> (e.g., man ls)
3. echo
• Purpose: Displays text on the terminal.
• Usage: echo "Hello, world!"
o Can also be used for variable expansion: echo $VARIABLE_NAME
4. printf
• Purpose: More versatile than echo for formatted output.
• Usage:
o printf "The value of pi is %.2f\n" 3.14159 (formatted output)
o Supports format specifiers (e.g., %d for integers, %s for strings, %f for floats)
5. script
• Purpose: Records a terminal session to a file.
• Usage:
Page | 81 Lecturer Name : Kavya
Department of DS, DA and AI Subject: Operating System
• Usage:
o cd /home (change to the home directory)
o cd .. (go to the parent directory)
o cd ~ (go to the home directory)
13. mkdir
• Purpose: Creates a new directory.
• Usage: mkdir new_directory_name
14. rmdir
• Purpose: Removes an empty directory.
• Usage: rmdir directory_name
15. ls
• Purpose: Lists files and directories in the current directory.
• Usage:
o ls -l (long listing)
o ls -a (show hidden files)
16. cp
• Purpose: Copies files or directories.
• Usage:
o cp source_file destination_file
o cp -r source_directory destination_directory (recursive copy)
17. mv
• Purpose: Moves or renames files or directories.
• Usage:
o mv source_file destination_file
o mv source_file destination_directory
18. rm
• Purpose: Removes files or directories.
• Usage:
o rm file_name
UNIT – 5
Shell Programming
Ordinary and Environment Variables in Shell Programming
In shell programming, variables serve as symbolic names for data values. They allow for
greater flexibility, maintainability, and reusability of scripts. Two primary types of variables
exist: ordinary and environment variables.
Ordinary Variables
• Scope: These variables have a limited scope, typically confined to the current shell or
a specific script.
• Declaration: Declared using the = operator. For example:
my_variable="Hello, World!"
• Access: Accessible only within the current shell or script where they are defined.
• Lifetime: Exist only for the duration of the current shell or script execution.
• Example:
#!/bin/bash
my_name="John Doe"
echo "My name is: $my_name"
./my_script.sh
Key Differences
export
Declaration variable_name="value" variable_name="value"
.profile:
In shell programming, the .profile file is a script that gets executed when a user logs into a
system using a login shell. It's used to customize the user's environment by setting
environment variables, defining aliases, and running other commands.
Here's what you need to know about .profile:
Location:
• The .profile file is located in the user's home directory ($HOME).
• It's a hidden file, so you need to use ls -a to see it.
Purpose:
• Customize the environment: You can set environment variables like PATH, EDITOR,
and PS1 to customize your shell experience.
• Define aliases: Create shortcuts for frequently used commands.
• Run commands: Execute any commands you want to run automatically at login.
When it's executed:
• .profile is executed only for login shells.
Example .profile:
# Set the PATH environment variable
export PATH=$PATH:/usr/local/bin
# Define an alias
alias ll='ls -l'
# Run a command
echo "Welcome, $USER!"
Editing .profile:
• You can edit your .profile file using a text editor like nano, vim, or emacs.
• Changes usually take effect in the next login shell. You can also run source ~/.profile to
apply the changes immediately in the current shell.
Important considerations:
• Be careful when editing .profile, as incorrect settings can cause problems with your
shell environment.
• Use comments (#) to explain what each line does.
• Test your changes thoroughly before making them permanent.
read Command
• Purpose: The read command is used to read input from the user, typically from the
standard input (usually the keyboard). This input can then be stored in a variable for
later use within the script.
• Syntax: read variable_name
• This basic syntax reads a single line of input from the user and stores it in the specified
variable_name.
• Options:
o -p "prompt": Displays a custom prompt to the user before reading input.
read -p "Enter your name: " name
• -r: Prevents backslash escapes from being interpreted.
• -s: Reads input silently (without echoing characters to the terminal).
• Example:
#!/bin/bash
read -p "Enter your age: " age
echo "Your age is: $age"
• This script prompts the user to enter their age, stores the input in the age variable,
and then displays the user's age.
readonly Command
• Purpose: The readonly command is used to declare a variable as read-only. Once a
variable is declared as read-only, its value cannot be changed or unset within the
current shell session.
• Syntax:
readonly variable_name
• This declares the existing variable variable_name as read-only.
readonly variable_name=value
• This declares a new read-only variable named variable_name and assigns it the
specified value.
• Example:
#!/bin/bash
readonly PI=3.14159
echo "The value of PI is: $PI"
• This script declares the variable PI as read-only with the value 3.14159. Any attempt
to change the value of PI within the script will result in an error.
In Summary
The read command is essential for interacting with users and obtaining input dynamically. The
readonly command provides a mechanism to protect the values of important variables from
accidental or unintended modifications, enhancing script robustness and security.
Conclusion
Command-line arguments are a fundamental part of shell scripting, allowing for greater
flexibility and customization. By effectively using these arguments, you can create powerful
and versatile scripts that can adapt to various situations.
if [ ! -f "[Link]" ]; then
echo "Error: [Link] not found."
exit 1
fi
In this example, if the file "[Link]" does not exist, the script will print an error message
and immediately exit with an exit status of 1, indicating an error.
Exit Status
• Definition: Every command executed in a shell returns an exit status code.
• Convention:
o 0 (zero): Generally indicates successful execution of the command.
o Non-zero values: Typically indicate an error condition. The specific meaning
of non-zero values can vary depending on the command.
• Accessing Exit Status: The special variable $? holds the exit status of the last
command executed.
• Example:
ls -l [Link]
echo $?
• If "[Link]" exists, the ls command will succeed, and $? will be 0. If the file does
not exist, ls will fail, and $? will be a non-zero value.
Using Exit Status in Scripts
Exit status codes are crucial for controlling script execution flow:
• Conditional Execution:
if [ $? -eq 0 ]; then
echo "Command executed successfully."
else
echo "Command failed."
Fi
Error Handling:
if ! command; then
echo "Command failed."
exit 1
fi
Chaining Commands:
command1 && command2
# command2 is executed only if command1 succeeds
command1 || command2
# command2 is executed only if command1 fails
Conclusion
The exit command and exit status are fundamental concepts in shell scripting. By
understanding and utilizing them effectively, you can create robust and reliable scripts that
can handle errors gracefully and control their execution flow based on the success or failure
of individual commands.
if(( ! $a == "true" ))
then
echo "a" was initially false.
else
echo "a" was initially true.
Fi
Shell scripting is a powerful tool for automating tasks and streamlining workflows. One of the
essential commands in Shell is the "test" command, which allows you to evaluate conditions
and make decisions within your scripts. In this tutorial, we will dive into the world of the test
command and explore how you can leverage it to create more versatile and reliable Shell
scripts.
The EXPRESSION can be a combination of various operators and operands, which we will
explore in the following sections.
File-related Expressions
The test command can be used to check the status of files and directories. Some common file-
related expressions include:
Expression Description
-f FILE Checks if the file is a regular file (not a directory or symbolic link)
Expression Description
Expression Description
STRING1 =
Checks if STRING1 is equal to STRING2
STRING2
if [ -f "[Link]" ]; then
echo "[Link] exists"
else
echo "[Link] does not exist"
fi
In this script, the test command is used to check if the file [Link] exists. If the expression [ -f
"[Link]" ] is true (the file exists), the script will print "[Link] exists". Otherwise, it will print
"[Link] does not exist".
The if, while, for and case control statements in shell programming
if statement
This block will process if specified condition is true.
Syntax:
if [ expression ]
then
statement
fi
Example
#Initializing two variables
a=10
b=20
while loop:
Here the command is evaluated and based on the resulting loop will execute, if the command
is raised to false then the loop will be terminated that.
Syntax:
while [ condition ]; do
# commands to execute as long as the condition is true
Done
Example:
count=0
while [ $count -lt 5 ]; do
echo "Count: $count"
((count++))
done
for loop:
The for loop operates on lists of items. It repeats a set of commands for every item in a list.
Syntax:
for variable in list; do
# commands to execute for each item in the list
Done
Example:
for fruit in apple banana orange; do
echo "I like $fruit"
done
case statement:
A case statement in bash scripts is used when a decision has to be made against multiple
choices. In other words, it is useful when an expression has the possibility to have multiple
values. This methodology can be seen as a replacement for multiple if-statements in a script.
Case statements have an edge over if-statements because it improves the readability of our
code and they are easier to maintain. Case statements in a Bash script are quite similar to
Case statements in C language. But unlike C, the Bash Case statement stops continuing the
search as soon as the match occurs. In simple words, they don’t require any break statement
that is mandatory to be used in C to stop searching for a pattern further.
Syntax:
case $variable in
pattern1)
# commands to execute if $variable matches pattern1
;;
pattern2)
# commands to execute if $variable matches pattern2
;;
*)
# commands to execute if no pattern matches
;;
esac
Example:
case $day in
Monday)
echo "It's the start of the week."
;;
Friday)
echo "It's almost the weekend!"
;;
*)
echo "It's a regular day."
;;
esac