Unit I and Unit II
Unit I and Unit II
1.1 Definition:
• Operating system is the one program running at all times on the computer
(usually called the kernel), with all else being application programs.
Examples of Operating System are –
• Windows (GUI based, PC)
• GNU/Linux (Personal, Workstations, ISP, File and print server, Three-tier
client/Server)
• macOS (Macintosh), used for Apple’s personal computers and workstations
(MacBook, iMac).
• Android (Google’s Operating System for smartphones/tablets/smartwatches)
• iOS (Apple’s OS for iPhone, iPad, and iPod Touch)
• Program Execution:
• The system must be able to load program into memory and to run that
program.
• The program must end its execution either normally or abnormally
(Indicating error).
• I/O Operations: A running program may require I/O, for efficiency and
protection; users usually cannot control I/O devices directly. Hence Operating
system has to perform I/O.
• File System Manipulation: For files
• Files can be created, deleted, searched by name
• File permissions to allow or deny access to file or directory based on
ownership.
• Programs need to read and write files and directories.
• Organizing file system structure
All these services must be provided by Operating System.
• Communications:
• One process needs to exchange information with another process.
• Process communication occurs between two processes that are executing
on same computer or different computers in a network.
• Operating system provides process communication by using pipes, shared
memory and message passing.
• Error Detection: Operating system needs to be constantly aware of possible
errors and it has to take appropriate action to ensure correct and consistent
computing. The errors may occur in
• CPU, Memory (Power failure, Timer error, Memory Insufficient)
• I/O Devices (Parity errors, Printer Errors, Connection failure in network).
• User Program (Arithmetic Overflow, Illegal memory access, use of more
CPU time)
The services those are helpful to for efficient operation of the system can be
• Resource Allocation: Operating system has to manage different types of
resources. When multiple users or multiple jobs running at the same time, the OS
has to allocate the resources. The recourses can be
• CPU clock cycles (CPU Time)
• Main Memory
• File Storage (Disks)
• I/O Devices
• Accounting: The operating system has to record and keep track of which
users howmuch and what kind of resources.
This information is for
• Accounting (Billing)
• Using statistics, the researchers can reconfigure system to improve
computing services.
• Protection & Security:
• When several process executing concurrently they should not interfere
with others or with the operating system. Protection involves ensuring
that all accessto the system resources is controlled.
• In a multi user and networked systems, the file and I/O system access
must be secured from unauthorized access. Security must ensure from
external attacks.
1.4 Operations of Operating-Systems
• Shell is a UNIX term for the interactive user interface with an operating system.
The shell is the layer of programming that understands and executes the
commands a user enters. In some systems, the shell is called a command
interpreter then shell is a set of UNIX commands.
• Bootstrap program is loaded at power-up or reboot
• Typically stored in ROM or EPROM, generally known as firmware
• Initializes all aspects of system
• Loads operating system kernel and starts execution.
• A compiler is a program that accepts a source program in a “high-level
language” and produces a corresponding object program.
• Interpreter: --- An interpreter is a program that appears to execute a source
program as if it was machine language.
• Loader: --- the loader must load, relocate, and link the object program. Loader is a
program that places programs into memory and prepares them for execution.
• High Level Language à Assembly Language à Machine Level Language
• Assembler: ---- is Software that converts an assembly language code to
machine code. It takes basic Computer commands and converts them into Binary
Code that Computer's Processor can use to perform its Basic Operations.
Most of the operating systems are Interrupt Driven. An operating system performs any
servicewith an event. Events are signaled by an interrupt or trap (exception).
• A trap (or an exception) is a software-generated interrupt caused either by an
error (for example, division by zero or invalid memory access) or by a specific
request from a user program that an operating-system service be performed.
The interrupt can be
• Hardware interrupts: By any one of the devices
• Software Interrupts: interrupt or trap
• Software error (e.g., division by zero)
• Request for operating system service
• Process problems include infinite loop, processes modifying each
other orthe operating system
For each type of interrupt, separate segment of code (Interrupt Service Routine-ISR)
in the operating system determine what action is taken. Since the operating system and
users share the hardware and software resources of the computer, Operating system
must ensure that an incorrect program cannot cause other programs to execute
incorrectly.
For this, operating systems uses
1. Dual Mode operation:
To ensure the proper execution of operating systems, we must be able to
distinguishbetween the execution of operating-system code and user defined code.
Main Memory
Two separate modes of operation: user mode and kernel mode (also called supervisor
mode, system mode, or privileged mode).
• User Mode / Non- Privileged Mode: The computer system is operated on
behalf of auser application i.e executes user program.
• System / Privileged / supervisor/ Kernel Mode: The computer system
executed operatingsystem programs to obtain the system services.
When a user program requires the service of an operating system, the CPU switches from
user tokernel mode.
• Kernel: --- “The one program running at all times on the computer”. Everything
else is either a system program (ships with the operating system) or an
application program.
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.
System calls provide the means for a user program to ask the operating system to
perform tasks reserved for the operating system on the user program’s behalf. A system
call is invoked in a variety of ways, depending on the functionality provided by the
underlying processor. In all forms, it is the method used by a process to request action by
the operating system. A system call usually takes the form of a trap to a specific location
in the interrupt vector. This trap can be executed by a generic trap instruction, although
some systems (such as MIPS) have a specific syscall instruction to invoke a system call.
Most of the operating systems Windows, UNIX, LINUX, and Solaris use Dual-Mode
operation to protect the system.
• Timer:
The operating system maintains control over the CPU.
Example: Operating System cannot allow
• A user program to get stuck in infinite loop
• Fail to call system services
• Never return control to the operating systemto avoid this, a timer is used.
• A timer can be set to interrupt the computer after a specified period of time.
• The period may be fixed or variable, set by operating system.
• A variable timer generally implemented by a fixed rate clock and a counter. The OS
sets the counter. Every time the clock ticks, the counter is decremented. When the
counter reaches 0, an interrupt occurs.
• A privileged instruction can only modify the content of timer.
• Process Management
A program does nothing unless its instructions are executed by a CPU. A program in
execution, as mentioned, is a process. A time-shared user program such as a compiler
is a process. A word-processing program being run by an individual user on a PC is a
process. A system task, such as sending output to a printer, can also be a process (or
at least part of one).
A process needs certain resources—including CPU time, memory, files, and I/O
devices—to accomplish its task. These resources are either given to the process when it is
created or allocated to it while it is running. In addition to the various physical and logical
resources that a process obtains when it is created, various initialization data (input) may
be passed along.
A single-threaded process has one program counter specifying the next instruction
to execute. The execution of such a process must be sequential. The CPU executes one
instruction of the process after another, until the process completes. A multithreaded
process has multiple program counters, each pointing to the next instruction to execute
for a given thread.
A process is the unit of work in a system. A system consists of a collection of
processes, some of which are operating-system processes (those that execute system
code) and the rest of which are user processes (those that execute user code).
The operating system is responsible for the following activities in connection with
process management:
• Memory Management
The main memory is central to the operation of a modern computer system. Main
memory is a large array of bytes, ranging in size from hundreds of thousands to
billions. Each byte has its own address. Main memory is a repository of quickly
accessible data shared by the CPU and I/O devices. The central processor reads
instructions from main memory during the instruction-fetch cycle and both reads and
writes data from main memory during the data-fetch cycle.
To improve both the utilization of the CPU and the speed of the computer’s
response to its users, general-purpose computers must keep several programs in
memory, creating a need for memory management.
Memory management activities
• Keeping track of which parts of memory are currently being used and by whom
• Deciding which processes (or parts thereof) and data to move into and out of
memory
• Allocating and deallocating memory space as needed
• Storage Management
To make the computer system convenient for users, the operating system provides a
uniform, logical view of information storage. The operating system abstracts from the
physical properties of its storage devices to define a logical storage unit, the file. The
operating system maps files onto physical media and accesses these files via the
storage devices.
• File-System Management
File management is one of the most visible components of an operating system.
Computers can store information on several different types of physical media. Magnetic
disk, optical disk, and magnetic tape are the most common. Each of these media has its
own characteristics and physical organization. Each medium is controlled by a device,
such as a disk drive or tape drive, that also has its own unique characteristics. These
properties include access speed, capacity, data-transfer rate, and access method
(sequential or random).
Magnetic tape drives and their tapes and CD and DVD drives and platters are typical
tertiary storage devices.
• Caching
When we need a particular piece of information, we first check whether it is in the cache.
If it is, we use the information directly from the cache. If it is not, we use the information
from the source, putting a copy in the cache under the assumption that we will need it
again soon.
Other caches are implemented totally in hardware. For instance, most systems
have an instruction cache to hold the instructions expected to be executed next. Without
this cache, the CPU would have to wait several cycles while an instruction was fetched
from main memory. Because caches have limited size, cache management is an important
design problem. Careful selection of the cache size and of a replacement policy can result
in greatly increased performance.
Main memory can be viewed as a fast cache for secondary storage, since data in
secondary storage must be copied into main memory for use and data must be in main
memory before being moved to secondary storage for safekeeping. The file-system data,
which resides permanently on secondary storage, may appear on several levels in the
storage hierarchy. At the highest level, the operating system may maintain a cache of file-
system data in main memory.
I/O Subsystem
The I/O subsystem consists of several components:
• A memory-management component that includes buffering, caching, and spooling
• A general device-driver interface
• Drivers for specific hardware devices
Protection and security require the system to be able to distinguish among all its
users. Most operating systems maintain a list of user names and associated user
identifiers (user IDs). In Windows parlance, this is a security ID (SID). These numerical
IDs are unique, one per user. When a user logs in to the system, the authentication stage
determines the appropriate user ID for the user. That user ID is associated with all of the
user’s processes and threads. When an ID needs to be readable by a user, it is translated
back to the user name via the user name list.
In some circumstances, we wish to distinguish among sets of users rather than individual
users. For example, the owner of a file on a UNIX system may be allowed to issue all
operations on that file, whereas a selected set of users may be allowed only to read the
file. To accomplish this, we need to define a group name and the set of users belonging to
that group. Group functionality can be implemented as a system-wide list of group names
and group identifiers. A user can be in one or more groups, depending on operating-
system design decisions. The user’s group IDs are also included in every associated
process and thread.
• Systems generally first distinguish among users, to determine who can do what
• User identities (user IDs, security IDs) include name and associated number, one
per user
• User ID then associated with all files, processes of that user to determine access
control
• Group identifier (group ID) allows set of users to be defined and controls
managed, then also associated with each process, file
• Privilege escalation allows user to change to effective ID with more rights
The functions that make up an API typically invoke the actual system calls on
behalf of the application programmer. For example, the Windows function
CreateProcess () (which unsurprisingly is used to create a new process) actually
invokes the NT CreateProcess () system call in the Windows kernel.
For most programming languages, the run-time support system (a set of functions built
into libraries included with a compiler) provides a system call interface that serves as
the link to system calls made available by the operating system. The system-call interface
intercepts function calls in the API and invokes the necessary system calls within the
operating system.
To start a new process, the shell executes a fork() system call. Then, the selected program
is loaded into memory via an exec() system call, and the program is executed. Depending
on the way the command was issued, the shell then either waits for the process to finish
or runs the process “in the background.”
File Management
We first need to be able to create () and delete () files. Either system call requires the
name of the file and perhaps some of the file’s attributes. Once the file is created, we need
to open () it and to use it. We may also read (), write (), or reposition () (rewind or skip to
the end of the file, for example). Finally, we need to close () the file, indicating that we are
no longer using it.
Device Management
Process may need several resources to execute—main memory, disk drives, access to
files, and so on. If the resources are available, they can be granted, and control can be
returned to the user process. Otherwise, the process will have to wait until sufficient
resources are available. The various resources controlled by the operating system can be
thought of as devices. Some of these devices are physical devices (for example, disk
drives), while others can be thought of as abstract or virtual devices (for example, files). A
system with multiple users may require us to first request () a device, to ensure exclusive
use of it. After we are finished with the device, were lease () it. Once the device has been
requested (and allocated to us), we can read (), write (), and (possibly) reposition () the
device, just as we can with files.
Information Maintenance
Many system calls exist simply for the purpose of transferring information between the
user program and the operating system. For example, most systems have a system call to
return the current time () and date (). Other system calls may return information about
the system, such as the number of current users, the version number of the operating
system, the amount of free memory or disk space, and so on. Another set of system calls is
helpful in debugging a program. Many systems provide system calls to dump () memory.
This provision is useful for debugging.
Communication
There are two common models of inter process communication: the message passing
model and the shared-memory model. In the message-passing model, the communicating
processes exchange messages with one another to transfer information. Messages can be
exchanged between the processes either directly or indirectly through a common
mailbox. Before communication can take place, a connection must be opened.
In the shared-memory model, processes use shared memory create()and shared memory
attach() system calls to create and gain access to regions of memory owned by other
processes.
Protection
Protection provides a mechanism for controlling access to the resources provided by a
computer system. Typically, system calls providing protection include set permission
()and get permission (), which manipulate the permission settings of resources such as
files and disks. The allow user () and deny user() system calls specify whether particular
users can—or cannot—be allowed access to certain resources.
1.7 SYSTEM PROGRAMS
System programs, also known as system utilities, provide a convenient environment for
program development and execution. Some of them are simply user interfaces to system
calls. Others are considerably more complex. They can be divided into these categories:
• File management: These programs create, delete, copy, rename, print, dump, list,
and generally manipulate files and directories.
• Status information: Some programs simply ask the system for the date, time,
amount of available memory or disk space, number of users, or similar status
information. Others are more complex, providing detailed performance, logging,
and debugging information.
• Some ask the system for info - date, time, amount of available memory, disk
space, number of users
• Others provide detailed performance, logging, and debugging information
• Typically, these programs format and print the output to the terminal or
otheroutput devices
• Some systems implement a registry - used to store and retrieve
configurationinformation
• File modification: Several text editors may be available to create and modify the
content of files stored on disk or other storage devices. There may also be special
commands to search contents of files or perform transformations of the text.
• Programming-language support: Compilers, assemblers, debuggers, and
interpreters for common programming languages (such as C, C++, Java, and PERL)
are often provided with the operating system or available as a separate download.
• Program loading and execution: Once a program is assembled or compiled, it
must be loaded into memory to be executed. The system may provide absolute
loaders, relocatable loaders, linkage editors, and overlay loaders. Debugging
systems for either higher-level languages or machine language are needed as well.
• Communications: Provide the mechanism for creating virtual connections among
processes, users, and computer systems.
• Allow users to send messages to one another’s screens, browse web pages, send
electronic-mail messages, and log in remotely, transfer files from one machine to
another etc.
• Application Programs: Operating system provides programs like web browsers,
word processors, text formatters, database systems, compilers, games and
statistical packages.
• Background services: All general-purpose systems have methods for launching
certain system-program processes at boot time. Some of these processes
terminate after completing their tasks, while others continue to run until the
system is halted. Constantly running system-program processes are known as
services, subsystems, or daemons.
Along with system programs, most operating systems are supplied with programs that
are useful in solving common problems or performing common operations. Such
application programs include Web browsers, word processors and text formatters,
spreadsheets, database systems, compilers, plotting and statistical-analysis packages, and
games.
1 Stack
The process Stack contains the temporary data such as method/function
parameters, return address and local variables.
2 Heap
This is dynamically allocated memory to a process during its run time.
3 Text
This includes the current activity represented by the value of Program Counter
and the contents of the processor's registers.
4 Data
This section contains the global and static variables.
Program
A program is a passive entity, such as a file containing a list of instructions stored on
disk (often called an executable file). In contrast, 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. A program is a piece of code which may be a single line or millions of lines.
A computer program is usually written by a computer programmer in a programming
language. For example, here is a simple program written in C programming language
−
#include <stdio.h>
int main ()
{
printf("Hello, World! \n");
return 0;
}
A computer program is a collection of instructions that performs a specific task when
executed by a computer. When we compare a program with a process, we can conclude
that a process is a dynamic instance of a computer program.
A part of a computer program that performs a well-defined task is known as
an algorithm. A collection of computer programs, libraries and related data are referred
to as software.
1 Start
This is the initial state when a process is first started/created.
2 Ready
The process is waiting to be assigned to a processor. Ready processes are waiting
to have the processor allocated to them by the operating system so that they can
run. Process may come into this state after Start state or while running it by but
interrupted by the scheduler to assign CPU to some other process.
3 Running
Once the process has been assigned to a processor by the OS scheduler, the
process state is set to running and the processor executes its instructions.
4 Waiting
Process moves into the waiting state if it needs to wait for a resource, such as
waiting for user input, or waiting for a file to become available.
5 Terminated or Exit
Once the process finishes its execution, or it is terminated by the operating
system, it is moved to the terminated state where it waits to be removed from
main memory.
• 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 (Figure 3.4).
• 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 items as the
value of the base and limit registers and 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.
In brief, the PCB simply serves as the repository for any information that may vary from
process to process.
Process Termination
A process terminates when it finishes executing its final statement and asks the operating
system to delete it by using the exit () system call. At that point, the process may return a
status value (typically an integer) to its parent process (via the wait () system call). All
the resources of the process—including physical and virtual memory, open files, and I/O
buffers—are deallocated by the operating system.
A parent may terminate the execution of one of its children for a variety of
reasons, such as these:
• The child has exceeded its usage of some of the resources that it has been
allocated. (To determine whether this has occurred, the parent must have a
mechanism to inspect the state of its children.)
• The task assigned to the child is no longer required.
• The parent is exiting, and the operating system does not allow a child to continue if
its parent terminates.
Some systems do not allow a child to exist if its parent has terminated. In such systems, if
a process terminates (either normally or abnormally), then all its children must also be
terminated. This phenomenon, referred to as cascading termination, is normally
initiated by the operating system. A process that has terminated, but whose parent has
not yet called wait (), is known as a zombie process. All processes transition to this state
when they terminate, but generally they exist as zombies only briefly.
Zombie is unable to terminate itself because it is treated as a dead process. So, parent
process needs to execute to terminate the command to terminate the child.is
2.OrphanProcess:
A child process that remains running even after its parent process is terminated or
completed without waiting for the child process execution is called an orphan. A
process becomes an orphan unintentionally. Sometime intentionally becomes orphans
due to long-running time to complete the assigned task without user attention. The
orphan process has controlling terminals.
3.Daemonprocess:
Daemon processes are started working when the system will be bootstrapped and
terminate only when the system is shutdown. It does not have a controlling terminal. It
always runs in the background.
A new process is initially put in the ready queue. It waits there until it is selected
for execution, or dispatched. Once the process is allocated the CPU and is executing, one
of several events could occur:
• The process could issue an I/O request and then be placed in an I/O queue.
• The process could create a new child process and wait for the child’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.
Schedulers
A process migrates among the various scheduling queues throughout its lifetime. The
operating system must select, for scheduling purposes, processes from these queues in
some fashion. The selection process is carried out by the appropriate scheduler.
Often, in a batch system, more processes are submitted than can be executed
immediately. These processes are spooled to a mass-storage device (typically a disk),
where they are kept for later execution. The long-term scheduler, or job scheduler,
selects processes from this pool and loads them into memory for execution. The short-
term scheduler, or CPU scheduler, selects from among the processes that are ready to
execute and allocates the CPU to one of them. The long-term scheduler executes much
less frequently; minutes may separate the creation of one new process and the next. The
long-term scheduler controls the degree of multiprogramming (the number of
processes in memory).
Process Synchronization: IPC- Pipes, Message Passing and Shared Memory. Concept
of Synchronization, Critical section problem, Peterson’s solution, Semaphores,
Classic problems of Synchronization-The Bounded Buffer Problem, The Readers–
Writers Problem, Dining - Philosophers Problem.