You are on page 1of 32

CSS 214: Operating Systems

(L-2; T-0; P-3) 3 Units

MODULE 5
Dr. O. S. Adebayo
&
Mr. Peter Anyaora
Course Outline

•Operating System Functionality


•OS Basic Operation
Operating System Functionality

The operating system controls the machine


Views of operating systems
An operating system can be viewed in three
ways:
According to the services it provides to users,
such as
– Time slicing.
– A file system.
 By its programming interface, i.e. its system
calls.
 According to its internal structure, algorithms,
and data structures.
OS using 3-D perspective
• There is one hardware base,
• One operating system, but many applications.

• Important interfaces:
• applications can execute only non-privileged
machine instructions, and they may also call upon
the operating system to perform some service for
them.
• The operating system may use privileged
instructions that are not available to applications.
• Various hardware devices may generate interrupts
that lead to the execution of operating system
code.
Operating System Functionality

application

System calls

operating system

non−privileged machine
privileged instructions interrupts
instructions

Hardware
Sequence of actions in OS
1. The operating system executes, and schedules an application
2. The chosen application runs: the CPU executes its (non-privileged)
instructions, and the operating system is not involved at all.
3. The system clock interrupts the CPU, causing it to switch to the clock’s
interrupt handler, which is an operating system function.
4. The clock interrupt handler updates the operating system’s notion of
time, and calls the scheduler to decide what to do next.
5. The operating system scheduler chooses another application to run in
place of the previous one, thus performing a context switch.
6. The chosen application runs directly on the hardware without OS. After
some time, the application performs a system call to read from a file.
7. The system call causes a trap into the OS. The OS sets things up for the
I/O operation
8. The third application runs.
OS Operation

At any given time, only one program is running

Sometimes this is the operating system, and at


other times it is a user application.

When a user application is running, the


operating system loses its control over the
machine.

OS regains control if the user application


performs a system call, or if there is a hardware
interrupt.
How the operating system guarantee that there will be a system
call or interrupt, so that it will regain control
• The operating system is a reactive program
• It does not get an input, do some processing, and produce an
output. Instead,
• It is an event driven to program.
• Then the operating system schedules another application, and
waits for the next event.
• “Normal” programs, accept an input and compute an output,
have a main function that is the program’s entry point.
• An operating system, in contradistinction, has many different
entry points, one for each event type.
• This events do not terminate — when it finishes handling one
event, it just waits for the next event.
• Events can be classified into two types: interrupts and system
calls.
• The goal of the operating system is to run as little as possible,
handle the events quickly, and let applications run most of the
time
List of applications in everyday activities.

The resources in a computer:

External / Hardware Resources:


• The CPU itself.
• Memory to store programs and their data.
• Disk space for files.
Internal resources needed by the operating
system:
• Disk space for paging memory.
• Entries in system tables, such as the process
table and open files table
OS Functionality
The operating system performs resource
management
One of the main features of operating systems is
support for multiprogramming (executing multiple
programs at the same time).
But given that there is only one processor, this
concurrent execution is actually a fiction.
Operating system juggles the system’s resources
between the competing programs, trying to make it
look as if each one has the computer for itself.
OS using resource management for
multiprogramming — deciding which running
program will get what resources.
OS Services {Two Services}
• Abstraction and Isolation.
Abstraction means that the services provide a more
convenient working environment for applications
by hiding some of the details of the hardware, and allowing
the applications to operate at a higher level of abstraction.
• Example: abstraction of a file system and applications do not handle
raw disk interfaces directly.
Isolation means that many applications can co-exist at the
same time,
using the same hardware devices, without falling over each
other’s feet.
• Example, if several applications send and receive data over a
network, the operating system keeps the data streams separated
from each other.
The features exist in the hardware but are not available
in the abstract machine presented to applications

In Hardware, the abstract machines are


isolated
An important aspect of multiprogrammed
systems is that there are many abstract
machines.
Each running application gets its own
abstract machine.
Features of abstraction
A very important feature of the abstract machines
presented to applications is that they are isolated from
each other.
abstraction hides all those resources that are being used to
support other running applications.
The operating system juggles resources among these
competing abstract machines in order to support this
illusion.
One example of this is scheduling: allocating the CPU to
each application in its turn.
How application can find out that it is actually
sharing the machine with other applications

Virtualization allows for decoupling from physical


restrictions
The abstract machine presented by the operating
system is “better” than the hardware by virtue of
supporting more convenient abstractions.
The abstraction is not limited by the physical
resource limitations of the underlying hardware: it
is a virtual machine. This means that the
application does
CPUs Execution Modes
• The execution mode is indicated by a bit in a
special register called the processor status word
(PSW).
• Two Execution Modes
• User mode and kernel mode.
• User mode
• User applications run in user mode.
• Kernel mode
• The heart of the operating system is called the kernel.
• This is the collection of functions that perform the basic
services such as scheduling applications.
• The kernel runs in kernel mode.
• Kernel mode is also called supervisor mode or
privileged mode.
Hardware Support for the Operating System
The operating system doesn’t need to do everything
itself —
it gets some help from the hardware.
Few hardware features are included specifically for the
operating system, and do not serve user applications
directly.
The operating system enjoys a privileged execution
mode
Privileged instructions
Can only be executed by the operating system, and not by
user applications.
Examples include:
 Instructions to set the interrupt priority level (IPL). This
can be used to block certain classes of interrupts from
occurring, thus guaranteeing undisturbed execution.
 Instructions to set the hardware clock to generate an
interrupt at a certain time in the future.
 Instructions to activate I/O devices. These are used to
implement I/O operations on files.
 Instructions to load and store special CPU registers
such as those used to define the accessible memory
addresses, and the mapping from each application’s virtual
addresses to the appropriate addresses in the physical
memory.
 Instructions to load and store values from memory
directly, without going through the usual mapping.
• This allows the operating system to access all the memory.
Privileged vs Non-privilege Instructions

• 1. Change the program counter


• 2. Halt the machine
• 3. Divide by zero
• 4. Change the execution mode

Privilege
• 2 and 3

Non-privilege
• 1 and 4
levels of protection on Intel processors
At the hardware level, Intel processors provide
four levels of protection.
Level 0: The most protected and intended for
use by the kernel.
Level 1: Intended for other, non-kernel parts of
the operating system.
Level 2: Offered for device drivers: needy of
protection from user applications, but not
trusted as much as the operating system proper2
Level 3: The least protected and intended for
use by user applications
Interrupts vs kernel mode
Interrupts cause a switch to kernel mode
Interrupts are special conditions that cause
the CPU not to execute the next instruction.
Instead, it enters kernel mode and executes
an operating system interrupt handler.

Kernel is the portion of OS code that is


resident in Memory and facilitates
interaction between hardware and software
Two main types of interrupts:
1. Asynchronous/External and
2. Synchronous/Internal.

• Asynchronous (external) interrupts are generated


by external devices at unpredictable times.
• Examples include:
Clock interrupt. This tells the operating system that a
certain amount of time has passed. Without clock
interrupts, the application might run forever and
monopolize the computer.

I/O device interrupt. This tells the operating system that


an I/O operation has completed. The operating system
then wakes up the application that requested the I/O
operation.
Types of interrupts:
2. Internal (synchronous) interrupts occur as a result of an
exception condition when executing the current instruction
As this is a result of what the software did (“software
interrupt”).
This means that the processor cannot complete the current
instruction for some reason, so it transfers responsibility to the
operating system.
Two main types of exceptions:
An error condition:
This tells the operating system that the current application did
something illegal (divide by zero, try to issue a privileged instruction,
etc.).
The handler is the operating system function that deals with
misbehaved applications; usually, it kills them.
A temporary problem:
for example, the process tried to access a page of memory that is not
allocated at the moment.
This is an error condition that the operating system can handle, and it
does so by bringing the required page into memory
How the CPU (hardware) know the address of
the appropriate kernel function
When the system is booted, the operating
system stores the addresses of the interrupt
handling functions in the interrupt vector.
When an interrupt occurs, the hardware stores
the current PSW (Program Status Word) and PC
(Program Counter), and loads the appropriate
PSW and PC values for the interrupt handler.
The PSW indicates execution in kernel mode.
The PC is obtained by using the interrupt
number as an index into the interrupt vector,
and using the address found there.
How CPU (hardware) know the address of the
appropriate kernel function
• Depends on what operating system is running, and
• But the operating system might not have been written yet when the
CPU was manufactured!
• The answer to this problem is to use an agreement between the hardware
and the software.
• This agreement is asymmetric, as the hardware was there first.
• Part of the hardware architecture contains the definition of certain features
and how the operating system is expected to use them.
• All operating systems written for this architecture must comply with these
specifications.
• Two particular details of the specification are
• the numbering of interrupts, and
• the designation of a certain physical memory address that will serve as an
interrupt vector.
System calls
System calls explicitly ask for the operating system
An application explicitly transfer control to the
operating system by performing a system call.
This is implemented by issuing the trap instruction.
This instruction causes the CPU to enter kernel mode,
and set the program counter to a special operating
system entry point.
The operating system then performs some service on
behalf of the application.
As an operating system can have more than a
hundred system calls, the hardware cannot be
expected to know about all of them (as opposed to
interrupts, which are a hardware thing to begin with).
System call Sequence of Events
• 1. The application calls a library function that serves as a wrapper
for the system call.
• 2. The library function (still running in user mode) stores the
system call identifier and the provided arguments in a designated
place in memory.
• 3. It then issues the trap instruction.
• 4. The hardware switches to privileged mode and loads the PC
with the address of the operating system function that serves as
an entry point for system calls.
• 5. The entry point function starts running (in kernel mode). It
looks in the designated place to find which system call is
requested.
• 6. The system call identifier is used in a big switch statement to
find and call the appropriate operating system function to actually
perform the desired service. This function starts by retrieving its
arguments from where they were stored by the wrapper library
function.
Typical system calls
Open, close, read, or write to a file.
Create a new process (that is, start running another
application).
Get some information from the system, e.g. the
time of day.
Request to change the status of the application,
e.g. to reduce its priority or to allow it to use more
memory
When the system call finishes, it simply returns to its
caller like any other function while the CPU must
return to normal execution mode.
The hardware special features
• The hardware has special features to help the operating system
• In addition to kernel mode and the interrupt vector, computers
have various features that are specifically designed to help the
operating system.
Examples include:
Hardware to translate each virtual memory address to a
physical address.
This allows the operating system to allocate various scattered
memory pages to an application, rather than having to
allocate one long continuous stretch of memory.
“Used” bits on memory pages, which are set automatically
whenever any address in the page is accessed.
This allows the operating system to see which pages were accessed (bit
is 1) and which were not (bit is 0).
Operating system components
 Process handling. Processes are the agents of
processing. The operating system creates them,
schedules them, and coordinates their interactions.
 Memory management. Memory is allocated to
processes as needed, but there typically is not
enough for all, so paging is used.
 File system. Files are an abstraction providing
named data repositories based on disks that store
individual blocks.
The operating system does the bookkeeping.
Performs Security, protection, accounting, error
handling
References
• Dror G. Feitelson (2011). Notes on Operating Systems. School of
Computer Science and Engineering, The Hebrew University of
Jerusalem 91904 Jerusalem, Israel
Practical Assignments
• Design a unique/specific operating system
using OS functionalities

You might also like