0% found this document useful (0 votes)
3 views

21CSE47A

Uploaded by

luciferyoume
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

21CSE47A

Uploaded by

luciferyoume
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 274

UNIT- Introduction to Operating System

1 Services

Introduction and Operating System Services:


Introduction to Operating System: Basics of Operating Systems:
Definition, Operations – Dual-Mode and Multi-Mode, Services, System
Calls – Types. Operating System Structure: Layered Structure,
Microkernel’s, Modules, Hybrid Systems – Mac OS X, iOS, Android

1. INTRODUCTION

● An operating system acts as an intermediary between the


user of a computer and the computer hardware.
● The purpose of an operating system is to provide an
environment in which a user can execute programs in a
convenient and efficient manner.
● An operating system is software that manages the computer
hardware. The hardware must provide appropriate mechanisms to
ensure the correct operation of the computer system and to
prevent user programs from interfering with the proper
operation of the system.
● The operating system is a control program which acts as a
resource manager and provides a convenient,secure,and

PREPARED BY
UNIT- Introduction to Operating System
1 Services

efficient environment for the execution of user programs.


● The operating system is a program that runs all the time-
right from boot up till shut down.

1.1 What Operating Systems Do:Basic


● A computer system can be divided roughly into four
components: the hardware, the operating system, the
application programs,and the users .

● The hardware—the central processing unit ( CPU ), the memory,


and the input/output ( I/O ) devices—provides the basic
computing resources for the system.
● The application programs—such as word processors,
spreadsheets,compilers, and Web browsers—define the ways in
which these resources are used to solve users’ computing
problems.

PREPARED BY
UNIT- Introduction to Operating System
1 Services

● The operating system controls the hardware and coordinates


its use among the various application programs for the
various users.
● The operating system provides the means for proper use of
these resources in the operation of the computer system.

1.2 Challenges in the Operating System Design

● An operating system is large and complex, it must be created


piece by piece. Each of these pieces should be a well-
delineated portion of the system, with carefully defined
inputs, outputs, and functions.

● Since the operating system should provide an efficient and


secure environment for the execution of user programs,it must
protect itself as well as other programs.

● The Operating system design varies considerably with


respect to various environments.For example,Mainframe
operating systems are designed primarily to optimize
utilization of hardware whereas Personal computer ( PC )
operating systems is mainly designed for convenience with
support for complex games, business applications, and
everything in between. Operating systems for mobile computers
is designed to provide an environment in which a user can
easily interface with the computer to execute programs.
● Hence while designing the operating system the goals have to

PREPARED BY
UNIT- Introduction to Operating System
1 Services

be listed with respect to the user and system point of view.


● There are two views to be considered while designing an
operating system:User View and the System view.

USER VIEW

● The user’s view of the computer varies according to the


interface being used.
● In the case of Personal Computers,the operating system is
designed mostly for ease of use, with some attention paid to
performance and none paid to resource utilization ie how
various hardware and software resources are shared.Such
systems are optimized for the single-user experience rather
than the requirements of multiple users.
● In case of mainframe computers,wherein a user sits at a
terminal connected to a mainframe or a minicomputer,the
operating system is designed to maximize resource
utilization ie to assure that all available CPU time,
memory, and I/O are used efficiently and that no individual
user takes more than her fair share.

● In the case of Workstations,wherein users have dedicated


resources at their disposal, but they also share resources
such as networking and servers,including file, compute, and
print servers. The operating system designed is to compromise
between individual usability and resource utilization.

PREPARED BY
UNIT- Introduction to Operating System
1 Services

● In the case of mobile computing wherein we discuss many


varieties of mobile computers, such as smartphones and
tablets,Which are basically standalone units for individual
users,but connected to networks through cellular or other
wireless technologies,the users providing the inputs via a
touch screen,the operating system is mainly designed for user
convenience,efficient resource management and also power
management stratergies.

● In case of embedded systems,which has very little User


interface,except for some numeric keypads or indicator lights
and start/stop button,the operating systems are designed
primarily to run without user intervention with very
critical time constraints.

SYSTEM VIEW

● From the computer’s point of view, the operating system is


the program most intimately involved with the hardware. In
this context, we can view an operating system as a resource
allocator.
● A computer system has many resources that may be required to
solve a problem: CPU time, memory space, file-storage space,
I/O devices, and so on. The operating system acts as the
manager of these resources.

PREPARED BY
UNIT- Introduction to Operating System
1 Services

● The OS should decide how to allocate the resources to


specific programs and users so that it can operate the
computer system efficiently and fairly and resolve various
conflicting requests.
● Since the Operating system should be a control program,it
should prevent errors and improper use of the computer .
It is especially concerned with the operation and control of
I/O devices.

1.3 IMPORTANCE OF OPERATING SYSTEM

● The operating system is the one program running at all


times on the computer—usually called the kernel. (Along with
the kernel, there are two other types of programs: system
programs, which are associated with the operating system but
are not necessarily part of the kernel, and application
programs, which include all programs not associated with the
operation of the system.)
● The Operating system has so much growing importance due to
the fact that computing is now pervasive in nature ie With
the widespread use of Personal Computers and Hand held
devices,Computing is not restricted to rather one place.
● Mobile operating systems often include not only a core
kernel but also middleware—a set of software frameworks that
provide additional services to application developers. For
example, each of the two most prominent mobile operating
systems—Apple’s i OS and Google’s Android —features a core

PREPARED BY
UNIT- Introduction to Operating System
1 Services

kernel along with middleware that supports databases,


multimedia, and graphics.

1.4 COMPUTER SYSTEM OPERATION


● A modern general-purpose computer system consists of one or
more CPU s and a number of device controllers connected
through a common bus that provides access to shared memory.
● Each device controller is in charge of a specific type of
device (for example, disk drives, audio devices, or video
displays).
● The CPU and the device controllers can execute in parallel,
competing for memory cycles. To ensure orderly access to the
shared memory, a memory controller synchronizes access to the
memory.
● For a computer to start running—for instance, when it is
powered up or rebooted —it needs to have an initial program
to run. This initial program,or bootstrap program, tends to
be simple. Typically, it is stored within the computer
hardware in read-only memory ( ROM ) or electrically
erasable programmable read-only memory ( EEPROM ), known
by the general term firmware.

1.5 FUNCTIONS OF BOOTSTRAP PROGRAM

PREPARED BY
UNIT- Introduction to Operating System
1 Services

● It initializes all aspects of the system, from CPU


registers to device controllers to memory contents.
● The bootstrap program must know how to load the operating
system and how to start executing that system . To
accomplish this goal, the bootstrap program must locate the
operating-system kernel and load it into memory.
● Once the kernel is loaded and executing, it can start
providing services to the system and its users. Some
services are provided outside of the kernel, by system
programs that are loaded into memory at boot time to
become system processes, or system daemons that run the
entire time the kernel is running.
● Once this phase is complete, the system is fully booted, and
the system waits for some event to occur.
● The occurrence of an event is usually signaled by an
interrupt from either the hardware or the software.
● Hardware may trigger an interrupt at any time by sending a
signal to the CPU , usually by way of the system bus.
● Software may trigger an interrupt by executing a special
operation called a system call (also called a monitor call).

1.6 HANDLING INTERRUPTS


● When the CPU is interrupted, it stops what it is doing and
immediately transfers execution to a fixed location.

PREPARED BY
UNIT- Introduction to Operating System
1 Services

● The fixed location usually contains the starting address


where the service routine for the interrupt is located.
● The interrupt service routine executes; on completion, the
CPU resumes the interrupted computation.
● The interrupt must transfer control to the appropriate
interrupt service routine.
● The straightforward method for handling this transfer would
be to invoke a generic routine to examine the interrupt
information. The routine, in turn, would call the interrupt-
specific handler.
● However, interrupts must be handled quickly.
● Generally implemented by maintaining a table of pointers to
interrupt routines to improve speed. (INTERRUPT VECTOR
TABLE)
● The interrupt routine is called indirectly through the table,
with no intermediate routine needed.
● Generally, the table of pointers is stored in low memory (the
first hundred or so locations). These locations hold the
addresses of the interrupt service routines for the various
devices. On encountering any interrupt,it performs a look up
in the table regarding which ISR to be invoked.
● More recent architectures store the return address on the
system stack.
● If the interrupt routine needs to modify the processor state—
for instance, by modifying register values—it must explicitly
save the current state and then restore that state before
returning. After the interrupt is serviced, the saved return

PREPARED BY
UNIT- Introduction to Operating System
1 Services

address is loaded into the program counter, and the


interrupted computation resumes as though the interrupt had
not occurred.

2. OPERATING SYSTEM STRUCTURE


● Operating systems vary greatly in their makeup, since they
are organized along many different lines.
● One of the most important aspects of operating systems is the
ability to multiprogram. Since a single program cannot, in
general, keep either the CPU or the I/O devices busy at all
times,modern operating systems are designed to do
Multiprogramming to increase CPU Utilization.
● Single users frequently have multiple programs running.
● Multiprogramming is the ability of the system to organize
jobs in the main memory which are ready to be executed so
that the CPU always has one to execute.
● The idea is as follows:The jobs are intially kept in hard
disk(job pool).
● From the job pool,the operating system picks up those jobs
which are ready to be executed into the main memory.
● The operating system picks up job from the main memory(CPU
scheduling) and allocates CPU so that it begins to execute .
● Eventually, the job may have to wait for some task, such as
an I/O operation, to complete,or wait for an event etc,the
operating system simply switches to, and executes, another
job. When that job needs to wait, the CPU switches to another
job, and so on.

PREPARED BY
UNIT- Introduction to Operating System
1 Services

● In a non-multiprogrammed system, the CPU would sit idle. But


here,the CPU is given some task to be executed so that it is
never idle.

FIG:MEMORY LAYOUT FOR MULTIPROGRAMMING

ADVANTAGES OF MULTIPROGRAMMING
● Multiprogrammed systems provide an environment in which the
various system resources (for example, CPU , memory, and
peripheral devices) are utilized effectively, but they do
not provide for user interaction with the computer system.

MULTITASKING
● Time sharing (or multitasking) is a logical extension of
multiprogramming. In time-sharing systems, the CPU executes
multiple jobs by switching among them, but the switches occur
so frequently that the users can interact with each program
while it is running.

PREPARED BY
UNIT- Introduction to Operating System
1 Services

● 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 a 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. As the system switches rapidly
from one user to the next, each user is given the impression
that the entire computer system is dedicated to his use, even
though it is being shared among many users.
● A time-shared operating system uses CPU scheduling and
multiprogramming to provide each user with a small portion
of a time-shared computer.
● Time sharing and multiprogramming require that several jobs
be kept simultaneously in memory. If several jobs are ready
to be brought into memory,job scheduling need to be performed
and CPU scheduling need to be done among them for the
allocation of CPU.
MULTIPROCESSOR SYSTEMS
Multiprocessor systems are those systems which has more than one
CPU.
ADVANTAGES

PREPARED BY
UNIT- Introduction to Operating System
1 Services

● Increased Throughput due to parallel execution


● Increased Reliability
● Economy of Sale

3. OPERATING SYSTEM OPERATIONS


● Modern operating systems are interrupt driven. Ie The
Operating system will be waiting for any event to happen.
● Events are almost always signaled by the occurrence of an
interrupt or a trap.
● 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-driven nature of an operating system defines
that system’s general structure. Ie For each type of
interrupt, separate segments of code in the operating system
determine what action should be taken.
● An interrupt service routine(ISR) is provided to deal with
the interrupt.
● When many user programs and Operating system code need to
share the hardware and software resources of the computer
system, we need to ensure that an error in a user program
could cause problems only for the one program running.

PREPARED BY
UNIT- Introduction to Operating System
1 Services

● With sharing enabled,the Operating System should not only


protect itself from corruption due to malicious programs,but
also protect other processes from getting corrupted.
● For example, if a process gets stuck in an infinite loop,
this loop could prevent the correct operation of many other
processes.
● A properly designed operating system must ensure that an
incorrect (or malicious) program cannot cause other programs
to execute incorrectly.

3.1 IMPLEMENTATION OF SECURE EXECUTION ENVIRONMENT


Dual-Mode and Multimode Operation

● In a system,the two different programs in execution are


1)User Process 2)System Process
● In order to ensure the proper execution of the operating
system, we must be able to distinguish between the execution
of operating-system code and user defined code.
● The set of instructions which should be executed only by the
kernel are called as Privileged Instructions.Thus there is a
need to differentiate between the user and system processes.
● The approach taken by most computer systems is to provide
hardware support that allows us to differentiate among
various modes of execution.
● At the very least, we need two separate modes of operation:
user mode and kernel mode (also called supervisor mode,
system mode, or privileged mode).

PREPARED BY
UNIT- Introduction to Operating System
1 Services

● A bit, called the mode bit, is added to the hardware of the


computer to indicate the current mode:if modebit=0 then it
implies kernel mode and if modebit=1,it implies user mode.
● With the mode bit, we can 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.
● when a user application requests a service from the operating
system (via a system call), the system must transition from
user to kernel mode to fulfill 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.

PREPARED BY
UNIT- Introduction to Operating System
1 Services

● The dual mode of operation provides us with the means for


protecting the operating system from errant users—and errant
users from one another.
● The hardware allows privileged instructions to be executed
only in kernel mode. If an attempt is made to execute a
privileged instruction in user mode, the hardware does
not execute the instruction but rather treats it as
illegal and traps it to the operating system.
● The instruction to switch to kernel mode is an example of a
privileged instruction. Some other examples include I/O
control, timer management, and interrupt management.
● The concept of modes can be extended beyond two modes.
(Multimode)
● In this case the CPU uses more than one bit to set and test
the mode. Eg:CPU s that support virtualization frequently
have a separate mode to indicate when the virtual machine
manager ( VMM )—and the virtualization management software—is
in control of the system. In this mode, the VMM has more
privileges than user processes but fewer than the kernel. It
needs that level of privilege so it can create and manage
virtual machines, changing the CPU state to do so. Sometimes,
too, different modes are used by various kernel.

PREPARED BY
UNIT- Introduction to Operating System
1 Services

● Alternatively instead of having different mode for


virtualization,The CPU designer can use other methods to
differentiate operational privileges. Eg:The Intel 64 family
of CPUs supports four privilege levels.

4. Operating-System Operations

The life cycle of instruction execution in a computer system.


● Initial control resides in the operating system, where
instructions are executed in kernel mode.
● When control is given to a user application, the mode is
set to user mode. Eventually, control is switched back to the
operating system via an interrupt, a trap, or a system
call.
SYSTEM CALLS
● 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.

PREPARED BY
UNIT- Introduction to Operating System
1 Services

● When a system call is executed, it is typically treated by


the hardware as a software interrupt. Control passes through
the interrupt vector to a service routine in the operating
system, and the mode bit is set to kernel mode. The system-
call service routine is a part of the operating system.
● The kernel examines the interrupting instruction to determine
what system call has occurred; a parameter indicates what
type of service the user program is requesting.
● Additional information needed for the request may be passed
through any of the following methods
● Registers:Simple and fast,but cannot be used if there
are large number of parameters.
● Stack:Parameters can be stored and retrieved from
stack using push and pop operation.
● Block or table method :In this method,the parameters
are stored in memory (with pointers to the memory
locations passed in registers). The kernel verifies that
the parameters are correct and legal, executes the
request, and returns control to the instruction
following the system call.

PREPARED BY
UNIT- Introduction to Operating System
1 Services

WHY DO WE REQUIRE ATLEAST DUAL MODE OPERATION


● The lack of a hardware-supported dual mode can cause serious
shortcomings in an operating system.
● For instance, MS-DOS was written for the Intel 8088
architecture, which has no mode bit and therefore no dual
mode.
● A user program running awry can wipe out the operating system
by writing over it with data; and multiple programs are able
to write to a device at the same time, with potentially
disastrous results.
● Modern versions of the Intel CPU do provide dual-mode
operation.
● Accordingly, most contemporary operating systems—such as
Microsoft Windows 7, as well as Unix and Linux take advantage
of this dual-mode feature and provide greater protection
for the operating system.
● Once hardware protection is in place, it detects errors that
violate modes.
STEPS TAKEN BY OPERATING SYSTEM ON DETECTION OF ERRORS THAT
VIOLATE MODES

CASE: If a user program fails in some way—such as by making an


attempt either to execute an illegal instruction or to access
memory that is not in the user’s address space

PREPARED BY
UNIT- Introduction to Operating System
1 Services

● STEP 1:Hardware traps to the operating system.


● STEP 2:The trap transfers control through the interrupt
vector to the operating system, just as an interrupt does.
● STEP 3:The OS identifies the type of interrupt,executes the
corresponding ISR(Interrupt Service Routines) and displays
suitable error messages and terminates the program .
● STEP 4: An appropriate error message is given, and the memory
of the program may be dumped. The memory dump is usually
written to a file so that the user or programmer can examine
it and perhaps correct it and restart the program.

IMPORTANCE OF TIMER
● We must ensure that the operating system maintains control
over the CPU .
● We cannot allow a user program to get stuck in an infinite
loop or to fail to call system services and never return
control to the operating system.
● To ensure that the control will be returned to kernel
mode ,we require the use of timers.
● A timer can be set to interrupt the computer after a
specified period. The period may be fixed (for example, 1/60
second) or variable (for example, from 1 millisecond to 1
second).
● A variable timer is generally implemented by a fixed-rate
clock and a counter.
IMPLEMENTATION

PREPARED BY
UNIT- Introduction to Operating System
1 Services

● The operating system sets the counter. Every time the clock
ticks, the counter is decremented. When the counter reaches
0, an interrupt occurs.
● Before turning over control to the user, the operating system
ensures that the timer is set to interrupt. If the timer
interrupts, control transfers automatically to the
operating system, which may treat the interrupt as a fatal
error or may give the program more time.

Instructions that modify the content of the timer are


privileged instructions

● Timer can be used to prevent a user program from running too


long.
● A simple technique is to initialize a counter with the
amount of time that a program is allowed to run.
● A program with a 7-minute time limit, for example,would have
its counter initialized to 420. Every second, the timer
interrupts,and the counter is decremented by 1. As long as
the counter is positive, control is returned to the user
program.
● When the counter becomes negative, the operating system
terminates the program for exceeding the assigned time limit.

Operating-System Services
● An operating system provides an environment for the
execution of programs.It provides certain services to programs
and to the users of those programs.

PREPARED BY
UNIT- Introduction to Operating System
1 Services
● The services provided by the OS can be broadly classified
from two view points:USER AND SYSTEM

PREPARED BY
UNIT- Introduction to Operating System
1 Services

USER VIEW POINT SERVICES


1. User interface. Almost all operating systems have a user
interface ( UI ). This interface can take several forms like
CLI,GUI,Batch interface etc.
● command-line interface ( CLI ), which uses text commands
and a method for entering them (say, a keyboard for typing in
commands in a specific format with specifications).
● Batch interface, in which commands and directives to control
those commands are entered into files, and those files are
executed.
● Graphical user interface ( GUI ) is a window system with a
pointing device to direct I/O , choose from menus, and make
selections and a keyboard to enter text.

2. Program execution. The system must be able to load a program


into memory and to run that program. The program must be able to
end its execution, either normally or abnormally (indicating
error).

3. I/O operations. A running program may require I/O , which may


involve a file or an I/O device. For specific devices, special
functions may be desired (such as recording to a CD or DVD drive
or blanking a display screen). For efficiency and protection,
users usually cannot control I/O devices directly. Therefore,
the operating system must provide a means to do I/O .

PREPARED BY
UNIT- Introduction to Operating System
1 Services

4. File-system manipulation. The programs need to read and write


files and directories. They also need to create and delete them
by name, search for a given file, and list file information.
Finally, some operating systems include permissions management to
allow or deny access to files or directories based on file
ownership. Many operating systems provide a variety of file
systems, sometimes to allow personal choice and sometimes to
provide specific features or performance characteristics.

5. Communications: For the working of the system,One process needs


to exchange information with another process. Such communication
may occur between processes that are executing on the same
computer or between processes that are executing on different
computer systems tied together by a computer network.
● Communications may be implemented via shared memory, in
which two or more processes read and write to a shared section of
memory, or message passing, in which packets of information in
predefined formats are moved between processes by the operating
system.

6. Error detection:The operating system needs to be detecting and


correcting errors constantly. Errors may occur in the CPU and
memory hardware (such as a memory error or a power failure), in
I/O devices (such as a parity error on disk, a connection failure
on a network, or lack of paper in the printer), and in the user

PREPARED BY
UNIT- Introduction to Operating System
1 Services

program (such as an arithmetic overflow, an attempt to access an


illegal memory location, or a too-great use of CPU time).
● For each type of error, the operating system should take the
appropriate action.
● To ensure correct and consistent computing the OS does one
of the following
-halt the system
-terminate an error-causing process or return an error
code to a process for the process to detect and possibly
correct.

SYSTEM VIEWPOINT SERVICES

● These services exists not for helping the user but rather for
ensuring the efficient operation of the system itself.

1. Resource allocation:For the multiple jobs running at the same


time, limited resources must be allocated in most efficient
manner.
● The operating system manages many different types of
resources.
● Resources such as CPU cycles, main memory, and file storage
may have special allocation code, whereas others (such as I/O
devices) may have much more general request and release code.
● For instance, in determining how best to use the CPU ,
operating systems have CPU -scheduling routines that take into

PREPARED BY
UNIT- Introduction to Operating System
1 Services

account the speed of the CPU , the jobs that must be executed,
the number of registers available, and other factors.
● For the efficient use of I/O devices also routines exist to
allocate printers, USB storage drives, and other peripheral
devices.

2. Accounting:For efficient allocation of resources,the operating


system must keep track of which users use how much and what
kinds of computer resources. This record keeping may be used for
accounting (so that users can be billed) or simply for
accumulating usage statistics. Usage statistics may be a
valuable tool for researchers who wish to reconfigure the
system to improve computing services.

3. Protection and security. The owners of information stored in a


multiuser or networked computer system should be given the control
of decision to use of that information.
● When several separate processes execute concurrently, one
process should not interfere with the others or with the
operating system itself.
● Protection involves ensuring that all access to system
resources is controlled. Protection is all about what to
implement.
● Security is all about how to implement
● Such security starts with requiring each user to authenticate
himself or herself to the system, usually by means of a password,
to gain access to system resources. It extends to defending

PREPARED BY
UNIT- Introduction to Operating System
1 Services

external I/O devices, including network adapters, from invalid


access attempts and to recording all such connections for
detection of break-ins.

SYSTEM CALLS
● System Calls provide an interface to the services made
available by an operating system. These calls are generally
available as routines written in C and C++, although certain low-
level tasks (for example, tasks where hardware must be accessed
directly) may have to be written using assembly-language
instructions.
● EG:To write a simple program to read data from one file
and copy them to another file.
● STEP 1:GET NAMES OF FILES-These names can be specified in
many ways, depending on the operating-system design. One approach
is for the program to ask the user for the names. In an
interactive system, this approach will require a sequence of
system calls, first to write a prompting message on the screen
and then to read from the keyboard the characters that define
the two files. On mouse-based and icon-based systems, a menu of
file names is usually displayed in a window.
● The user can then use the mouse to select the source name,
and a window can be opened for the destination name to be
specified. This sequence requires many I/O system calls.

PREPARED BY
UNI Introduction to Operating System

● STEP 2:open the input file and create the output file
. Each of these operations requires another system call.
Possible error conditions for each operation can require
additionalsystem calls.
● ERROR CONDITIONS: No input file ,NO access permission
for the input file,Output file already exists,
● In these cases, the program should print a message on
the console (another sequence of system calls) and
then terminate abnormally (another sequence of system
call).

● STEP3:When both files are set up, we enter a loop that reads
from the input file (a system call) and writes to the
output file (another system call).
● STEP 4:Each read and write must return status information
regarding various possible error conditions.
● POSSIBLE ERRORS: the end of the file ,a hardware
failure in the read (such as a parity error).The write
operation may encounter various errors, depending on the output device
STEP 5:Close both the files(system call),write success(system
call) or error prompt(system call)

PREPARED BY Ms.UMA.N,ASST.PROF,NHCE
UNI Introduction to Operating System

● The application developers design programs according to an


application programming interface ( API ). The API
specifies a set of functions that are available to an
application programmer, including the parameters that are
passed to each function and the return values the programmer
can expect.
● Three of the most common API s available to application
programmers are
● the Windows API for Windows systems
● the POSIX API for POSIX -based systems(which include
virtually all versions of UNIX , Linux, and Mac OS X

● the Java API-API for programs that run on the Java


virtual machine.
● A programmer accesses an API via a library of code
provided by the operating system.
● In the case of UNIX and Linux for programs written in
the C language, the library is called libc. N
● Behind the scenes, 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.

● Why would an application programmer prefer programming


according to an API rather than invoking actual system calls?
● One benefit concerns program portability. An application
programmer designing a program using an API can expect her
program to compile and run on any system that supports the
same API (although, in reality, architectural differences

PREPARED BY
UNI Introduction to Operating System

often make this more difficult than it may appear).


● Actual system calls can often be more detailed and
difficult to work with than the API available to an
application programmer. Nevertheless, there often exists a
strong correlation between a function in the API and its
associated system call within the kernel. In fact, many of
the POSIX and Windows API s are similar to the native system

calls provided by the UNIX , Linux, and Windows operating


systems.
● ABSTRACTION:The caller need know nothing about how the system
call is implemented or what it does during execution. Rather,
the caller need only obey the API and understand what the
operating system will do as a result of the execution of that
system call. Thus, most of the details of the operating-
system interface are hidden from the programmer by the API
and are managed by the run-time support library.
● 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.
● IMPLEMENTATION:

● A number is associated with each system call, and the system-


call interface maintains a table indexed according to these
numbers.
● The system call interface then invokes the intended system
call in the operating-system kernel and returns the
status of the system call and any return values.

PREPARED BY
UNI Introduction to Operating System

● operating system handles a user application invoking the


open() system call.

SYSTEM CALL -API-OS RELATIONSHIP

● System calls occur in different ways, depending on the


computer in use.
● Often, more information is required than simply the identity
of the desired system call.ie parameters need t be passed in
system calls like the input file or device to use as the
source, as well as the address and length of the memory
buffer into which the input should be read.
● Three general methods are used to pass parameters to the
operating system.
● Registers:The simplest approach is to pass the
parameters in but not suitable if the parameters to be
passed exceeds the no of registers.
● Block, or table, in memory, and the address of the
block is passed as a parameter in a register. This is
the approach taken by Linux and Solaris.

PREPARED BY
UNI Introduction to Operating System

● Stack: Parameters also can be placed, or pushed, onto


the stack by the program and popped off the stack by the
operating system.
● Some operating systems prefer the block or stack method
because those approaches do not limit the number or length of
parameters being passed.
TYPES OF SYSTEM CALLS
● System calls can be grouped roughly into six major
categories:

● process control
● file manipulation
● device manipulation
● information maintenance,

PREPARED BY
UNI Introduction to Operating System

● communications
● protection.

Process Control
When a process is in execution various scenarios happen.
● For a program to be executed it has to be first loaded into
the main memory.This is done via load() system call.
● A running program needs to be able to halt its execution
either normally ( end() ) or abnormally ( abort() ).
● Under either normal or abnormal circumstances, the operating
system must transfer control to the invoking command
interpreter.
● In an interactive system, the command interpreter
simply continues with the next command assuming user
will issue appropriate command.
● In a GUI system, a pop-up window might alert the user
to the error and ask for guidance.
● In a batch system, the command interpreter usually
terminates the entire job and continues with the next
job.
● A process or job executing one program may want to load() and
execute() another program. Eg:keeping track of mouse clicks to
execute the necessary commands.
● If control returns to the existing program when the new
program terminates, we must save the memory image of the
existing program; thus, we have effectively created a
mechanism for one program to call another program. If both

PREPARED BY
UNI Introduction to Operating System

programs continue concurrently, we have created a new job or


process to be multiprogrammed. Often, there is a system call
specifically for this purpose ( create process() or submit
job() ).
● The process has to be controlled while in the system,like the
the ability to determine and reset the attributes of a job or
process, including the job’s priority, its maximum allowable
execution time, and so on.For this we can use the get process
attributes() and set process attributes() system calls.
● To terminate a job or process that we created we use
( terminate process() ) system call if it is incorrect or
is no longer needed.
● Having created new jobs or processes, we may need to wait for
them to finish their execution. We may want to wait for a
certain amount of time to pass ( wait time() ). or wait for
a specific event to occur ( wait event() ). The processes
should signal when that event has occurred ( signal
event() ).
● Quite often, two or more processes may share data. To ensure
the integrity of the data being shared, operating systems
often provide system calls allowing a process to lock
shared data. Then, no other process can access the data until
the lock is released. Typically, such system calls include acquire
lock() and release lock() .

SUMMARY OF SYSTEM CALLS UNDER PROCESS CONTROL


● end, abort

PREPARED BY
UNI Introduction to Operating System

● load, execute
● create process, terminate process
● get process attributes, set process attributes
● wait for time
● wait event, signal event
● allocate and free memory

File Management
● There are various system calls which comes under file
management.
● Ability to create or delete files via 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.
● We may need these same sets of operations for directories if
we have a directory structure for organizing files in the
file system.
● Attributes are those properties that define a file.Eg:name,
file type, protection codes, accounting information, and
so onwith respect to attributes,the operating system should
be able to get file attributes()or In addition, for either

PREPARED BY
UNI Introduction to Operating System

files orsometimes set some of the file


attributes(setfileattributes())
● Some operating systems provide many more calls, such as
calls for file move() and copy() .

SUMMARY OF SYSTEM CALLS UNDER FILE MANAGEMENT

● File management
● create file, delete file
● open, close
● read, write, reposition
● get file attributes, set file attributes
Device Management
● A 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 devices controlled by the operating system can be
physical devices (for example, disk drives), or abstract
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, we release() it.These functions
are similar to the open() and close() system calls for files.

PREPARED BY
UNI Introduction to Operating System

● Once proper synchronization is not present,it can result in a


situation called deadlock.
● 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.
● Since,the files and device system calls are very similar many
Operating systems like Linux,Uniix etc use a file-device
system call where I/O devices are identified by special file
names, directory placement, or file attributes.
● The user interface can also make files and devices appear to
be similar, even though the underlying system calls are
dissimilar.
SUMMARY OF DEVICE MANAGEMENT SYSTEM CALLS
● request device, release device
● read, write, reposition
● get device attributes, set device attributes
● logically attach or detach devices

Information Maintenance
● For the purpose of transferring information between the
user program and the operating system there are many system
calls.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

PREPARED BY
UNI Introduction to Operating System

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.EG: dump() system call which is useful for debugging.
A program trace lists each system call as it is executed.
● Microprocessors provide a CPU mode known as single step, in
which a trap is executed by the CPU after every
instruction. The trap is usually caught by a debugger.
● Many operating systems provide a time profile of a program
to indicate the amount of time that the program executes at a
particular location or set of locations.
● A time profile requires either a tracing facility or regular
timer interrupts.
● In addition, the operating system keeps information about
all its processes, and system calls are used to access this
information.
● Generally, calls are also used to reset the process
information ( get process attributes() and set process
attributes() ).

SUMMARY OF SYSTEM CALLS UNDER INFORMATION MAINTANANCE


● get time or date, set time or date
● get system data, set system data
● get process, file, or device attributes
● set process, file, or device attributes

PREPARED BY
UNI Introduction to Operating System

communication

● There are two common models of interprocess 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. The name of the other communicator must be known.
● Each computer in a network has a host name by which it is
commonly known. A host also has a network identifier, such
as an IP address. Similarly, each process has a process
name, and this name is translated into an identifier by which
the operating system can refer to the process. The get
hostid() and get processid() system calls do this
translation. The identifiers are then passed to the general
purpose open() and close() calls provided by the file system
or to specific open connection() and close connection()
system calls.

● The recipient process usually must give its permission for


communication to take place with an accept connection()
call.
● The deamon program(system programs which will be running in
the background (execute a wait for connection() call and are
awakened when a connection is made.
● The client(source) and the server(daemon) then exchange
messages by using read message() and write message() system
calls. The close connection() call terminates the
communication.
● In the shared-memory model, processes use shared memory

PREPARED BY
UNI Introduction to Operating System

create() and shared memory attach() system calls to create


and gain access to regions of memory owned by other
processes.
● Normally, the operating system tries to prevent one process
from accessing another process’s memory,but in a Shared
memory model it requires that two or more processes will be
sharing the same shared region in memory.
● The operating system should ensure that they are not writing
to the same location simultaneously.

COMPARISON OF SHARED MODEL WITH MESSAGE PASSING


Shared Memory model Message Passing
Can be used for large volume Useful for small amount of data
of data
A little difficulty in Easier to implement
implementation
Fast ,as transfer happens within Slow
memory system
Requires kernel intervention Does not require kernel
intervention

PREPARED BY
UNI Introduction to Operating System

SUMMARY OF SYSTEM CALLS UNDER COMMUNICATION


● create, delete communication connection
● send, receive messages
● transfer status information
● attach or detach remote devices

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.
SUMMARY ABOUT SYSTEM CALLS UNDER PROTECTION
● Set permission()
● Get permission()
● Deny permission()
● Allow user()
● Deny user()

PREPARED BY
UNI Introduction to Operating System

Operating-System Structure

● A system as large and complex as a modern operating


system must be engineered carefully if it is to
function properly and be modified easily.
● In monolithic systems,every functionality is
written in the single module and hence it is very
difficult to debug or to do modifications.
● The common approach is to partition the task into
small components, or modules.
● Each of these modules should be a well-defined
portion of the system, with carefully defined inputs,
outputs, and functions.

Simple Structure

● MS-DOS is an example of a system which do not have


well-defined structures.It was originally designed
and implemented by a few people who had no idea that
it would become so popular. It was written to provide
the most functionality in the least space, so it
was not carefully divided into modules.
● In MS-DOS , the interfaces and levels of
functionality are not well separated.
● For instance, application programs are able to
access the basic I/O routines to write directly to

PREPARED BY
UNI Introduction to Operating System

the display and disk drives. Such freedom leaves MS-


DOS vulnerable to errant (or malicious) programs,
causing entire system crashes when user programs
fail.
● MS-DOS was also limited by the hardware,No dual
mode or hardware protection.

● The original UNIX operating system also had a limited


structure -but maintained two separable parts: the
kernel and the system programs.

PREPARED BY
UNI Introduction to Operating System

● Everything below the system-call interface and above


the physical hardware is the kernel. The kernel
provides the file system, CPU scheduling, memory
management, and other operating-system functions
through system calls. Taken in sum, that is an
enormous amount of functionality to be combined into
one level. This monolithic structure was difficult
to implement and maintain. It had a distinct
performance advantage, however: there is very
little overhead in the system call interface or
in communication within the kernel.

PREPARED BY
UNI Introduction to Operating System

Traditional Unix Structure

Layered Approach
● With proper hardware support, operating systems can
be broken into pieces that are smaller and more
appropriate .
● The operating system can then retain much greater
control over the computer and over the applications
that make use of that computer.
● Implementers have more freedom in changing the
inner workings of the system and in creating
modular operating systems by providing data
abstraction or information hiding.
● In a layered system , the operating system is broken
into a number of layers (levels).
● The bottom layer (layer 0) is the hardware;
● The highest (layer N) is the user interface.

PREPARED BY Ms.UMA.N,ASST.PROF,NHCE
UNI Introduction to Operating System

● A typical operating-system layer—say,layer M


-consists of data structures and a set of
routines that can be invoked by higher-level
layers.
● Layer M, in turn, can invoke operations on lower-
level layers.
ADVANTAGES :
● simplicity of construction.The layers are selected
so that each uses functions (operations) and services
of only lower-level layers.
● Simplifies debugging and system verification. Each
layer can be debugged without affecting the other
layers. Thus, the design and implementation of the
system are simplified.
● Each layer is implemented only with operations
provided by lower-level layers.

● A layer does not need to know how these operations


are implemented; it needs to know only what these
operations do. Hence, each layer hides the
existence of certain data structures, operations,
and hardware from higher-level layers.
CHALLENGES/DRAWBACKS
● Appropriately defining the various layers are
difficult.
● Careful planning is necessary for the design Eg:For
example, the device driver for the backing store

PREPARED BY
UNI Introduction to Operating System

(disk space used by virtual-memory algorithms) must


be at a lower level than the memory-management
routines, because memory management requires the
ability to use the backing store.
● tend to be less efficient than other types.For
instance, when a user program executes an I/O
operation, it executes a system call that is trapped
to the I/O layer, which calls the memory-management
layer, which in turn calls the CPU -scheduling layer,
which is then passed to the hardware. At each layer,
the parameters may be modified, data may need to be
passed, and so on. Each layer adds overhead to the
system call. The net result is a system call that

takes longer than does one on a nonlayered


system.
● The backing-store driver would normally be above
the CPU scheduler, because the driver may need to
wait for I/O and the CPU can be rescheduled during
this time. However, on a large system, the CPU
scheduler may have more information about all the
active processes than can fit in memory.
Therefore, this information may need to be
swapped in and out of memory, requiring the
backing-store driver routine to be below the CPU
scheduler.

SOLUTION ADOPTED IN MODERN OS

PREPARED BY
UNI Introduction to Operating System

Fewer layers with more functionality are being designed,


providing most of the advantages of modularized code
while avoiding the problems of layer definition and
interaction.

Microkernels
● Researchers at Carnegie Mellon University developed
an operating system called Mach that modularized
the kernel using the microkernel approach.

● In this approach,the operating system is structured


by removing all nonessential components from the
kernel and implementing them as system and user-
level programs. The result is a smaller kernel.
● Microkernels provide minimal process and memory
management, in addition to a communication
facility.

Application Program File System Device Driver

messages messages
user
mod
Interprocess Communication memory managment CPU e
scheduling

microkernel

hardware kerne
l
mode

PREPARED BY
UNI Introduction to Operating System

● The main function of the microkernel is to provide


communication between the client program and the
various services that are also running in user

space.
● Communication is provided through message passing.

ADVANTAGES
● Extensions of the operating system is easier . All
new services are added to user space and consequently
do not require modification of the kernel.
● When the kernel have to be modified, the changes
tend to be fewer, because the microkernel is a
smaller kernel and it is easier to compile the
kernel.
● The resulting operating system is easier to port
from one hardware design to another.
● The microkernel also provides more security and
reliability, since most services are running as user
rather than kernel processes. If a service fails,
the rest of the operating system remains
untouched.
● Examples of operating systems which uses microkernel
approach :
● Tru64 UNIX (formerly Digital UNIX ) provides a

PREPARED BY
UNI Introduction to Operating System

UNIX interface to the user, but it is


implemented with a Mach kernel.

● The Mac OS X kernel (also known as Darwin) is


also partly based on the Mach microkernel.
● QNX , a real-time operating system for
embedded systems.

DISADVANTAGES

● The performance of microkernels can suffer due to


increased system-function overhead.
● CASE:Consider the history of Windows NT . The
first release had a layered microkernel
organization. This version’s performance was low
compared with that of Windows 95.

Modular Approach
● This is the best current methodology adopted in
various operating-system design. This type of
design is common in modern implementations of UNIX
, such as Solaris, Linux, and Mac OS X , as well
as Windows.
● It involves designing kernel modules as loadable
kernel modules. Here, the kernel has a set of
core components and links in additional services

PREPARED BY
UNI Introduction to Operating System

via modules, either at boot time or during run


time.
● The idea of the design is for the kernel to
provide core services while other services are
implemented dynamically, as the kernel is
running.
ADVANTAGES
● Addition of new features to kernel is very easy
● The overall result resembles a layered system in
that each kernel section has defined, protected
interfaces; but it is more flexible than a
layered system, because any module can call any
other module.
● The approach is also similar to the microkernel
approach in that the primary module has only
core functions and knowledge of how to load and
communicate with other modules.
● It is more efficient, because modules do not need
to invoke message passing in order to communicate.
● The Solaris operating system structure,is
organized around a core kernel with seven types
of loadable kernel modules:

PREPARED BY Ms.UMA.N,ASST.PROF,NHCE
UNI Introduction to Operating System

1. Scheduling classes
2. File systems
3. Loadable system calls
4. Executable formats
5. STREAMS modules
6. Miscellaneous
7. Device and bus drivers

● Linux also uses loadable kernel modules,


primarily for supporting device drivers and file
systems.

Hybrid Systems

● Modern Operating systems combine different


structures, resulting in hybrid systems that
address performance, security, and usability
issues.
UNI Introduction to Operating System

● For example, both Linux and Solaris are


monolithic, because having the operating system in
a single address space provides very efficient
performance. However, they are also modular, so
that new functionality can be dynamically added to
the kernel.

● Windows is largely monolithic as well (again


primarily for performance reasons), but it retains
some behavior typical of microkernel systems,
including providing support for separate
subsystems (known as operating-system
personalities) that run as user-mode processes.
Windows systems also provide support for
dynamically loadable kernel modules.

● Three powerful hybrid systems: the Apple Mac OS


X operating system and the two most prominent
mobile operating systems—i OS and Android.

Mac OS X
● The Apple Mac OS X operating system uses a
hybrid structure.

PREPARED BY
UNI Introduction to Operating System

graphical user interface


Aqua

application environments and services

Java Cocoa Quicktime BSD

BSD

I/O kit kernel extensions

PREPARED BY
UNI Introduction to Operating System

● It is a layered system.
● The top layers include the Aqua user interface
and a set of application environments and
services.
● The Cocoa environment specifies an API for the
Objective-C programming language, which is used
for writing Mac OS X applications.
● Below these layers is the kernel environment,
which consists primarily of the Mach
microkernel and the BSD UNIX kernel.
● Mach provides memory management;support for
remote procedure calls ( RPC s) and
interprocess communication( IPC ) facilities,
including message passing; and thread
scheduling.
● The BSD component provides a BSD command-line
interface, support for networking and file

systems, and an implementation of POSIX API s,


including Pthreads.
● In addition to Mach and BSD , the kernel
environment provides an I/O kit for development of
device drivers and dynamically loadable modules
(which Mac OS X refers to as kernel extensions).

SUMMARY OF MAC Osx

PREPARED BY
UNI Introduction to Operating System

Aqua GUI
Cocoa API For Objective C Programming Language
Mach Memory management,RPC,IPC,message
passing,thread scheduling
BSD CLI,Support for network and file
systems,POSIX APIs,
I/O Kit Device Drivers

iOS
● iOS is a mobile operating system designed by
Apple to run its smartphone, the iPhone, as well
as its tablet computer, the iPad.
● iOS is structured on the Mac OS X operating
system, with added functionality pertinent to
mobile devices,
● It does not directly run Mac OS X applications.
The structure of i OS is as shown below

PREPARED BY
UNI Introduction to Operating System

● Cocoa Touch is an API for Objective-C that


provides several frameworks for developing
applications that run on i OS devices.
● The fundamental difference between Cocoa,
mentioned earlier, and Cocoa Touch is that the
latter provides support for hardware features
unique to mobile devices, such as touch
screens.
● The media services layer provides services for
graphics, audio, and video.

Android

● The Android operating system was designed by the


Open Handset Alliance (led primarily by Google)

PREPARED BY
UNI Introduction to Operating System

and was developed for Android smartphones and


tablet computers.
● Android runs on a variety of mobile platforms
and is open-sourced, partly explaining its rapid
rise in popularity.
● Android is similar to iOS in that it is a layered
stack of software that provides a rich set of
frameworks for developing mobile applications .

Applications

Application Framework

Libraries Android runtime


SQLlite OpenGL
Core Libraries
Surface Manager Media Framework

Dalvik Virtual Machine


Webkit libc
Linux Kernel

PREPARED BY
UNIT-1 Introduction to Operating System Services

● In the bottom of this software stack is the


Linux kernel, although it has been modified by
Google and is currently outside the normal
distribution of Linux releases.
NOTE:
● iOS is designed to run on Apple mobile devices and
is close-sourced, Android runs on a variety of
mobile platforms and is open-sourced,

END OF UNIT -1

PREPARED BY Ms.UMA.N,Sr.ASST.PROF,NHCE
MODULE 2
PROCESS
What it is ???
•Program in execution state

How it executes …
- Using resources CPU, Memory, I/O devices
When Resources are allocated…
-Process creation time, Execution time
• System has many processes
• How process executes
- Sequentially or Parallely
- “””Thread”””” concept
• How many Thread a process can have??
• OS responsible for ‘’’’P ‘’R management….
• Process scheduling..
• Communication…
PROCESS…
• How many Thread a process can have??
- Single threaded , Multithreaded

-OS responsible for ‘’’’P ‘’R management….


-Process scheduling..
-Communication…
PROCESS IN MEMORY..
• Process State

As a process executes, it changes state. The state of a process is defined in part by the current activity of that
process.
A process may be in one of the following states:

• NEW

• RUNNING

• WAITING

• READY

• TERMINATED

•Only one P is running in Processor/CPU


Process states
Process Control Block (PCB)

• Each process is represented in the operating system by a process control block (PCB)

• TCB

• It contains many pieces of information associated with a specific process


1. Process state.

The state may be new, ready, running, waiting, halted, and so on.

2. Program counter.

3. CPU registers.

4. CPU-scheduling information.

-Process priority

5. Memory-management information.

- page table

- segment table

- register values
6. Accounting information.

-amount of CPU and real time used,


-time limits, account numbers
- job or process numbers

7. I/O status information.

-list of I/O devices allocated to the process


-list of open files
Thread

-Single thread of execution

Eg: Process running MS word application

- Allow only one Task at a time

PCB..
P

Th3
Th1 Th2
PROCESS SCHEDULING

- Multiprogramming

- to maximize CPU utilization.

-Time sharing is to switch the CPU among processes

- Process Scheduler

Current
P1
CPU

Waiting Switches
P2

Pn
PCB..
P

Th3
Th1 Th2
Scheduling queues:
Job queue, Ready queue

PCB pointers

I/O request
Device queue

3 events occurs

QUEUE diagram for Process Scheduling


• A common representation of process scheduling is a queueing diagram

• Each rectangular box represents a queue.

• Two types of queues are present: the ready queue and a set of device queues.

• The circles represent the resources that serve the queues,

- arrows indicate the flow of processes in the system.

- 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:

- .
•1. The process could issue an I/O request and then be placed in an I/O queue.

• 2.The process could create a new child process and wait for the child’s termination.

•3. The process could be removed forcibly from the CPU, as a result of an interrupt, and be put back in the
ready queue.

In the first two cases, the process eventually switches from the waiting state to the ready state and is then put
back in the ready queue.

-A process continues this cycle until it terminates, at which time it is removed


from all queues and has its PCB and resources deallocated
Schedulers

-Short term Scheduler (CPU scheduler)


selects from among the processes that are ready to execute and allocates the CPU to one of them.

More frequent

-Long tern scheduler (Job scheduler)


selects processes from this pool and loads them into memory for execution.

Less frequent

Control Multiprogramming
• I/O bound process

• CPU bound process

medium-term scheduler (SWAPPING)

The key idea behind a medium-term scheduler is that sometimes it can be advantageous to remove a process
from memory (and from active contention for the CPU) and thus reduce the degree of multiprogramming.
Later, the process can be reintroduced into memory, and its execution can be continued where it left off.

This scheme is called swapping.

The process is swapped out, and is later swapped in, by the medium-term scheduler.

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.
CONTEXT SWITCH

• Switching the CPU

• state save

• the kernel saves the context of the old process

• rescheduled to run.

- Context-switch time is pure overhead

- Switching speed varies from machine to machine, depending on the memory speed, the number of registers

- A typical speed is a few milliseconds.


Operation on Process:

• Creation

• Deletion

• execution

• Kill ----Normal------Abnormal

• Pid
•Init : The init process (which always has a pid of 1) serves as the root parent process for all user processes.

“we can obtain a listing of processes by using the ps command. For example, the command

ps –el
When a process creates a new process, two possibilities for execution exist:

1. Pp parallel with Cp

2. Pp waits for Cp

There are also two address-space possibilities for the new process:

1. Cp is copy of Pp

2. Cp has program loaded into it

Directory---folder1 ---- open close append

folder2 --- rename exit

Fork() exec() wait()


CreateProcess(); // WINDOWS
Process termination
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.

• task assigned to the child is no longer required.

• Once Pp exits Cp will terminates

-”Cascading Termination”

TerminateProcess() in Windows

exit(); system call by process to OS to terminate


wait system call
Pp Cp

return Pid of Cp
• The wait() system call is passed a parameter

• This system call also returns the process identifier of the terminated child so that the parent can tell
which of its children has terminated:

• status of Cp can be obtained

init() in Linux
pid is 1
create various user P
Eg: web server, print server , ssh server

child process of init is


- kthread // kernel thread
- sshd // secure shell
INTER PROCESS COMMUNICATION (IPC)

- OS supports all Process in


parallel execution

- Independent Process
P1
- Cooperative Process
P2

P3
Pn
Why IPC ????
-Information Sharing

-Computation Speed up

-Modularity

- Convenience
SHARED MEMORY MODEL
Message pass model

P1
n/w P2

S1
S2

send(msg) receive(msg)
indirect mode
• use mail box

• it is data structure

M
p AI
L p
BO
X
• BUFFERS

• ZERO CAPACITY

• BOUNDED CAPACITY

• UN BOUNDED CAPACITY
CPU SCHEDULING::
Cpu scheduler
Ready queue

P4 P3 P2 P1

Cpu scheduler C
P
U
LTS

P1 P2 P3 P4
DISK / SECONDARY P5
STORAGE High Priority Task

JOB QUEUE PROGRAM


Cpu scheduler
Ready queue

P4 P3 P2 P1
P1
STS

Cpu scheduler C
P
U
LTS
SWAP OUT

P1
DISK / SECONDARY P5
STORAGE High Priority Task

JOB QUEUE PROGRAM


Cpu scheduler
Ready queue

P4 P3 P2 P5 Terminates
P5
STS

Cpu scheduler C
P
U
LTS
SWAP OUT

P1
DISK / SECONDARY P5
STORAGE High Priority Task

JOB QUEUE PROGRAM


Cpu scheduler
Ready queue

P4 P3 P2 P1 Terminates
P1
STS

Cpu scheduler C
P
U
LTS

P1
DISK / SECONDARY
STORAGE

JOB QUEUE PROGRAM


SCHEDULING CRITERIA

1. CPU UTILIZATION

2. THROUGHPUT

3. TURN AROUND TIME

4. WAITING TIME

5. RESPONSE TIME
PREEMPTIVE & NON – PREEMPTIVE SCHEDULING

NON-PREEMPTIVE

P1 ONCE ALLOCATE CPU


CANNOT TAKE BACK
W C
P2 A P
I U ALL REMAINING PROCESS
T HAS TO WAIT
I
N
P3 G ONCE COMPLETE ….

THEN CPU ALLOCATED


PROCESS VOLUNTARILY RELESE CPU
AFTER EXECUTION….

EG: FCFS
PREEMPTIVE SCHEDULING

P1 CPU CONTROL CAN BE


SWITCH ANYTIME
C
P2 P
U PROCESS NEED NOT WAIT
LONG TIME

P3

PROCESS FORCIBLY RELESE CPU WHILE


EXECUTING….

EG:: SJF
ROUND ROBIN
PRIOTIRY
• FCFS TECHNIQUE:::

GANTT CHART….
process arriving in the order P1, P2, P3….
AT(P3)
AT(P2)

AT(P1) COMPLETION TIME


OF P1 CT(P3)
CT(P2)
WT(P1) ==0 msec

WT(p2) ==24

WT(p3)==27

AWT= (0+24+27)/3== 17msec

EXAMPLE-2

wt(p1) =0
wt(p2)=6
wt(p3)=14
wt(p4)=21

AWT=10.25 msecc

p1 p3 p4
p2

0 6 14 21 24
• SHORTEST JOB FIRST SCHEDULING (SJF)
Process with Minimum CPU time / Burst Time is allocated to CPU

Process with more CPU time is scheduled Last

More efficient technique

It gives minimum waiting time for processes

Eg: Consider a process having 4 process P1 P2 P3 P4

Cpu time / Burst time of P1=6msec P2=8msec P3=2msec P4=3msec

P3
P4 P1 P2

0 2 5 11 19
Above diagram is called as GANTT CHART

• Waiting time of P3 =WT(P3)=0msec

• P4 =WT(P4)=2msec

• P1 =WT(P1)=5msec

• P2 =WT(P2)=11msec

• Average waiting time of all process in system

• AWT=(sum of all waiting time of all processes)/Number of Processes

== (p1+p2+p3+p4)/4

== (5+11+0+2) / 4

== 18 /4

==4.5 msec
SJF(shortest job firs) Example -2

Process Burst time(msec)


P1 6
P2 8
P3 2
P4 1

total BT= 17 msec


Gantt Chart ::

p4 p3 p1 p2

0 1 3 9 17

WT(P4)=0 WT(P3)=1 WT(P1)=3 WT(P2)=9

AWT== WT(P4+P3+P1+P2)/4
== 0+1+3+9 / 4
== 3.25 msecc
SJF SCHEDULING
• Shortest Remaining time First Schedule Algorithm (SRTF)
• Process P1 is started at time 0, since it is the only process in the queue.

• Process P2 arrives at time 1.

• The remaining time for process P1 (7 milliseconds) is larger than the time required by
process P2 (4 milliseconds)
• process P1 is preempted, and process P2 is scheduled.

•The average waiting time for this example is

[(10 − 1) + (1 − 1) + (17 − 2) + (5 − 3)]/4 =

= 26/4 = 6.5 milliseconds.


PRIORITY SCHEDULING

Priority….???

Preemptive or Non pre emptive

Indefinite Blocking time/ STARVATION

“”Aging “”

Low Number ---- High

High Number------ Low


PRIORITY SCHEDULING

Process Burst time(msec) PRIORITY


P1 4 2
P2 6 0
P3 8 3
P4 10 1

total BT= 28 msec


Gantt Chart ::
P2 P4 P1 P3

6 16 20 28
0

WT OF P2=0, P4=6 P1=16 P3=20

AWT== WT(P4+P3+P1+P2)/4
== 0+6+16+20/ 4
== 10.5 msec
• ROUND ROBIN SCHEDULING (RRS)

• For time sharing systems

• Pre-emption of process

• Time slice/ time quantum

• 10-100 msec

Time quantum= 4msec


Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
MODULE 5
File System Interface and
Implementation
FILE CONCEPT
• Provides a mode of permanent storage of information.
• Information in a file is related in some way.
• File
– A file is a named collection of related information that is recorded in a
secondary storage
• Types
– Data
• numeric
• character
• binary
• Files can hold any kind of information
– text, binary, application-defined structures, etc.
FILE CONCEPTS
FILE STRUCTURE
• None - sequence of words, bytes
• Simple record structure
– Lines
– Fixed length
– Variable length
• Complex Structures
– Formatted document
– Relocatable load file
• Can simulate last two with first method by inserting
appropriate control characters
• Who decides:
– Operating system
– Program
FILES AND DIRECTORIES
FILE ATTRIBUTES
• Name – only information kept in human-readable form
• Identifier – unique tag (number) identifies file within file
system
• Type – needed for systems that support different types
• Location – pointer to file location on device
• Size – current file size
• Protection – controls who can do reading, writing, executing
• Time, date, and user identification – data for protection,
security, and usage monitoring
• Information about files are kept in the directory structure,
which is maintained on the disk
FILE OPERATIONS
• File is an abstract data type
• Create
• Write
• Read
BASIC OPERATIONS
• Reposition within file
• Delete
• Truncate
• Open(Fi) – search the directory structure on disk for entry Fi,
and move the content of entry to memory
• Close(Fi) – move the content of entry Fi in memory to
directory structure on disk
• COMMON OPERATIONS
• Appending – adding new information to the end of an existing file
• Renaming – an existing file
FILE OPEN

Most of the file operations involves


searching in the directories for the entry
associated with the file name

To avoid constant searching, OS keeps a


small table called Open-file table

Open-File Table
- Contains information about all open
files
• When a file operation is requested, the file is specified via an
index into this table, so no searching is required
• When a file is no longer being actively used, it is closed by the
process and the OS removes its entry from the open-file table
• Create and delete are system calls that work with closed rather
than with open files
Open(), Close()

Implicitly done Explicitly done (using system call)


Open() Done implicitly Open() takes a file name & search
when the first reference to it is the directory, copying the directory
entry into the open-file table
made Can also accept access mode
Close() Closed automatically information – create, readonly,
when job or program that writeonly etc.
opened the file terminates
• Multi-process Environment
– Several processes may open the
file simultaneously
– Several different applications
open the same file at same time
• So, OS uses 2 levels of internal table
i) Per-Process Table
- Traces all files the process
has opened
- Store information
regarding the use of the
file by that process
- Access rights, accounting
information can also be
included
ii) System-wide Table
- Contains process -
independent information
such as, location of file on
disk, access dates, file-
size
OPEN FILE
• Several pieces of data are needed to manage open
files:
– File Pointer: pointer to last read/write location,
per process that has the file open
– File-Open Count: counter of number of times a
file is open – to allow removal of data from open-
file table when last processes closes it
– Disk Location of the File: cache of data access
information
– Access Rights: per-process access mode
information
FILE LOCKING

• Some OS provide facilities for locking an open


file (or section of a file)
• File lock allow
– One process to lock a file and prevent other
processes from gaining access to it
• Useful for files that are shared by several
processes
– Ex: System log file
• Types of File Locks
• Shared Lock (akin reader lock)
– Several process can acquire the lock concurrently
• Exclusive Lock (akin writer lock)
– Only one process at a time can acquire such a lock
• Some OS provides only exclusive lock
• File-locking Mechanism
• OS may provide either mandatory or advisory file-locking mechanism
• Mandatory (locking scheme is mandatory)
– If a lock is mandatory, then once a process acquires an exclusive lock,
the OS will prevent other process from accessing the locked file
• Advisory Lock (Optional)
– The OS will not prevent other process from accessing the locked file
• Windows adopts mandatory locking, UNIX adopts advisory locking
• Use of file locks:
– Enforces precautions
– Ensures that two or more processes do not become involved in deadlock while trying to
acquire file locks
FILE TYPES – NAMES, EXTENSIONS
FILE STRUCTURE
• Indicates the internal structure of the file.
• Files must confirm to the required structure, that is
understood by the OS
• Ex:
– exe file  with the specific structure, OS will determine
whether in memory to load the file and at what location the
first instruction is
• Disadvantage
– Cumbersome to maintain the different structures, if the OS
maintains multiple file structure
ACCESS METHOD
1. Sequential Access
2. Direct Access
3. Other Access Methods
• Sequential Access
– Simplest access method
– Information in the file is processed in order, one record after
the other
– Ex: Editors, Compilers
– Program may be able to skip forward or backward n records
for some integer n
read next
write next
reset (to the beginning)
no read after last write
(rewrite)

SEQUENTIAL ACCESS FILE


• Direct Access
– Another method is direct access or relative access
– A file is made up of fixed-length logical records
that allow programs to read and write records
rapidly in no particular order
– For direct access, the file is viewed as a numbered
sequence of blocks or records.
– Thus we may read block 14, then read block 53,
then write block 7
– There are no restrictions on the order of reading or
writing for a direct-access file
• For direct-access method, the file operations
must be modified to include the block number
as a parameter.
read n (rather than read next)
write n (rather than write next)
position to n
read next
write next
rewrite n
n = relative block number

essential for database systems


SIMULATION OF SEQUENTIAL ACCESS ON A DIRECT-
ACCESS FILE
Example of Index and Relative Files
Index  contains pointers to the
various blocks
- To find a record in the file, first search
the index and then use the pointer to
access the file directly and to find the
desired record
-Binary Search is made on the index
Disadvantage:
For large files, the index file
itself may become too large to be kept
in memory
Solution
Create index for the index file
Primary index file contains the
pointers to the secondary index files,
which would point to the actual data
items
PROTECTION
PROTECTION

• File owner/creator should be able to control:


– what can be done
– by whom

• Types of access
– Read
– Write
– Execute
– Append
– Delete
– List
ACCESS LIST AND GROUPS
ACCESS CONTROL
• Scheme Implemented
• Identity dependent access is associated with each file and
directory called as Access-Control List (ACL)
• Mode of Access
– read, write, execute
• Three classes of users
RWX
a) owner access (creator) 7  111
RWX
b) group access (Shared user) 6  110
RWX
c) public access (all other users) 1  001
Windows XP Access-control List Management
A Sample UNIX Directory Listing
Partition and mounting
• In-memory information
– Used for both file-system management and performance improvement
via caching
– Data are loaded at mount time, updated during file-system operations
and discarded at dismount
• Types of Structures used
– In-Memory Mount Table
• Contains information about each mounted volume
– In-Memory Directory-Structure
• Cache, holds the directory information of recently accessed directories
– System-wide Open-File Table
• Contains a copy of the FCB of each open file, as well as other information
– Per-Process Open-File Table
• Contains a pointer to the appropriate entry in the system-wide open-file table
– Buffers
• Hold file-system blocks when they are being read from disk or written to disk
A Typical File Control Block (FCB)

• Creating a new file:


– Application Program
calls the logical file
system
– Logical file system
knows the format of the
directory structure
– It allocates a new FCB.
• File Control Block (or
inode) – Storage structure
consisting of information
about a file
• Open()  Call passes a file name to the logical file system
– Searches the system-wide open-file table to see if the
file is already in use by another process
– If so, a per-process open-file table entry is created
pointing to the existing system-wide open-file table
– If file not already open, the directory structure is
searched for the given file name
• Once file is found, FCB is copied into a system-wide
open-file table in memory
• Next, an entry is made in per-process open-file
table, with a pointer to the entry in the system-wide
open-file table
DIRECTORY IMPLEMENTATION
Two Methods
Linear List
Hash Table
• Linear List of file names with pointer to the data
blocks.
• Creating a new file
– First search the directory, ensure no existing file has the
same name
– Then add the new entry at the end of the directory
• Deleting an existing file
– Search the directory for the existence of the named file
– Release the space allocated to it
• Advantage
– simple to program
• Disadvantage
– time-consuming to execute as it uses linear search to
search a file for its existence
Chained-overflow hash table
• Hash Table – linear list with hash data structure
– Advantage: decreases directory search time
– Collisions – situations where two file names hash
to the same location
– Disadvantage
• Fixed size
• Depends on hash function
– Alternative Approach
• Chained-overflow hash table can be used
• Each hash entry can be a linked list instead of
individual value
ALLOCATION METHODS
QUESTIONS
Explain different disk space allocation method with an example. (8 Marks)
Name the different file allocation methods. Explain the linked allocation of
file implementation with merits and demerits. (8 Marks)
With supporting diagrams, explain linked and indexed method of allocating
disk space. (8 Marks)
What is a file? Explain the different allocation methods. (10 Marks)
What are the three methods for allocating disk space? Explain with
suitable example. (12 Marks)
Compare contiguous and linked allocation methods for disk space. (05
Marks)
• Allocation Method
– refers to how disk blocks are allocated for files
• Methods of allocating disk space
ALLOCATION METHOD

Contiguous Allocation Non-Contiguous Allocation

Linked Allocation
Indexed Allocation
CONTIGUOUS ALLOCATION
• Each file occupies a set of contiguous blocks
on the disk
• Example:
File is n blocks long and starts at location b,
then, it occupies block
b, b+1, b+2, … , b+n-1
• Directory entry consists of addresses of :
– Starting block
– Length
• Strategy used to find the free hole:
– First Fit
– Best Fit
• Advantage:
– Simple – only starting location (block #) Disk
and length (number of blocks) are
required
Used in CDROMs, DVDs
• Disadvantage:
– Wasteful of space (dynamic storage-allocation problem)
– Suffers from external fragmentation
• Files allocated and deleted, the free disk space is
broken into little pieces
• Solution:
– Compaction Technique, but expensive
– Complex to determine how much space is needed for a
file
• When creating a file, total amount needed for a file must
be found and allocated
• Allocating too little space : File cannot grow
• Allocating too large space: Space wasted
LINKED ALLOCATION

• Solves the problem of contiguous allocation


• Each file is a linked list of disk blocks:
blocks may be scattered anywhere on the
disk.
• The directory contains a pointer to :
• the first block of the file and
• last block of the file
• Also each block contains pointers to the
next block, which are not made available to
the user.
• Ex: File for five blocks:
• Start at block 9, continues to block 16, then to
block 1, then 3 and 25

block = pointer
• Create a new file
– Create a new entry in the directory, with 2 pointers, 1 to first block,
initialize it to nil
– File is initially empty
• Read a file
– Simply follow the file pointer from block to block
• Write to a file
– Find a free block, written to and linked to the end of the file
• Pros
– No space lost to external fragmentation
– Disk only needs to maintain first block of each file
– Size of the file need not to be declared
• Cons
– Random access is costly
– Overheads of pointers. Ex. Size of a block is 512 bytes, and if pointer
occupies 4 bytes, then user sees block of 508 bytes only
– Loss/damage of file pointer, leads to incorrect access
MS-DOS FILE SYSTEM
• Implement a linked list allocation using a table
– Called File Allocation Table (FAT)
– Take pointer away from blocks, store in this table
-1  special value in the table denotes, end-of-file
0  indicates unused blocks
File-Allocation Table
INDEXED ALLOCATION

• Solves external
fragmentation and
size declaration
problem
• Brings all pointers
together into the
index block.
• Logical view.
Indexed table
• Creating a new file
– Set pointer in the index block to nil
– When ith block is written, a block is obtained from the free-space
manager, and its address is put in the ith index-block entry
• Every file will have an index block, so it must me as small as possible
• If index block is too small, it can’t handle enough pointers
• Mechanisms used for solving this purpose: small file large file
pblm
– Linked Scheme
• For a large file, link several index blocks together.
– Multilevel Index
• First level index block can point to the set of second level index
blocks, which in turn point to the file block
– Combined Scheme
• Direct Blocks 
– Contains addresses of the blocks that contain data of a file
– Can be used for small files
• Indirect Blocks 
– First points to a single indirect block, contains an index block (contains no
data), but the addresses of the block that contains data
– Double indirect block – Contains address of the block containing actual data
– Triple indirect block – Last pointer, containing address of the triple indirect
block
Combined Scheme: UNIX (4K bytes per block)
Direct blocks:
15 pointers of index blocks
FREE SPACE MANAGEMENT
QUESTIONS
• December 2013/Jan 2014
How is free space managed? Explain. (5 Marks)
• June/July 2013
Explain the different approaches to managing free space on disk storage.
(10 Marks)
• June/July 2014
What do you mean by free space list? With suitable example, explain any
two methods of implementation of free space list. (8 Marks)
• June/July 2015
Explain bit vector free-space management technique. (05 Marks)
• December 2015
Write a note on any four different methods for managing free space. (6
Marks)
• Need for Free-Space Management
– Disk space is limited, we need to reuse the space from deleted
files for new files, if possible
• Free-Space List
– Used to keep track of free disk space, system maintains free-
space list
• Free-Disk Blocks
– Free-space list records all free disk blocks – those that are not
allocated to some file or directory
• On Creating a File
– Search the free-space list for the required amount of space and
allocate that space to the new file
– This space is then removed from the free-space list
• On Deleting a File
– The disk-space is added to the free-space list
• Free-Space Management Approaches
– Bit Map or Bit Vector
– Linked List
– Grouping
– Counting
• Bit Map or Bit Vector
– Each block is represented by 1 bit

• Bit vector (n blocks)


0 1 2 n-1


1  block[i] free
… bit[i] =
0  block[i] occupied

• Example
• Consider a disk where blocks 2,3,4,5,8,9,10,11,12,13,17,18,25,26 and
27 are free and rest of the blocks are allocated
• Free-space bit map would be
001111001111110001100000011100000…
• Technique for finding the first free block
– Check the disk space sequentially for checking whether the
value is not 0
– The first non-0 word is scanned for the first 1 bit
• Advantage of this approach
– Simple
– Efficient in finding contiguous free blocks on disk
• Disadvantage
– Requires extra space
• Linked List free-space on disk
– Link all the free disk blocks
– Keep the pointer to the first free
block in a special location of the
disk
• Example
Consider a disk where blocks
2,3,4,5,8,9,10,11,12,13,17,18,
25,26 and 27 are free and rest
of the blocks are allocated
-Block 2 points to block 3, block 3 points
to block 4, so on…
• Advantage
– No waste of space
• Disadvantage
– Scheme is not efficient
– As it needs to traverse and read each block, which
takes too much of I/O time
– Cant get contiguous space easily
• Grouping
– Stores the addresses of n free blocks in the first free
blocks
– The first n-1 of these blocks are actually free
– The last block contains the addresses of another n free
blocks and so on
Advantage
Address of large
number of free blocks
can be found easily
• Counting
– Several contiguous blocks may be allocated or
freed simultaneously
– Keep the list of n free disk addresses in the first
free block and the number (n) of free contiguous
blocks that follow the first block
– Each entry in the disk space consists of a disk
address and count

You might also like