You are on page 1of 44

Operating Systems 1

CS 241
Spring 2021

By
Marwa M. A. Elfattah

Main Reference
Operating System Concepts, Abraham Silbrschatz,
10th Edition
Fundamentals
When you power up a computer, what happens?
For a computer to start it needs to have an initial simple
program to run – bootstrap - bootloader.
▪ It is stored within the computer hardware in firmware (BIOS
– Basic I/O System).
When you power up a computer, what happens?
For a computer to start it needs to have an initial simple
program to run – bootstrap - bootloader.
▪ It is stored within the computer hardware in firmware (BIOS
– Basic I/O System).

▪ BIOS initializes all


aspects of the
system and builds
an inventory of the
devices.
• This process is
called Power On
Self Test
(POST)
When you power up a computer, what happens?
For a computer to start it needs to have an initial simple
program to run – bootstrap - bootloader.
▪ It is stored within the computer hardware in firmware (BIOS
– Basic I/O System).

▪ Also, It provides
a user interface
to configure the
hardware and to
select storage
device from
which OS is
loaded.
When you power up a computer, what happens?
For a computer to start it needs to have an initial simple
program to run – bootstrap - bootloader.
▪ It is stored within the computer hardware in firmware (BIOS
– Basic I/O System).

▪ BIOS then locates the OS kernel and load it into memory.


• BIOS loads the OS boot loader from Master Boot
Record (MBR) in the storage first sector, then the boot
loader loads the actual OS.
▪ Once the kernel is loaded and executing, it can start
providing services, then, the system is fully booted, and the
system waits for some event to occur.
When you power up a computer, what happens?

What about storing the


OS Kernel in ROM?
When you power up a computer, what happens?

What
What about is the the
storing
OS Kernel
Kernel?in ROM?
Operating System Kernel
“The one program running at all times on the computer”
is the kernel. It is the Operating System Core
Operating System Kernel
“The one program running at all times on the computer”
is the kernel. It is the Operating System Core
• Everything else is either:
➔ A system services – system
utility (ships with the operating
system, but not part of the kernel),
such as: file manipulation, status
information, background services,…

➔ An application program, all


programs not associated with the
operating system
Operating System Kernel
▪ Kernel is a programs which is close to the hardware
of the system and so,
• It performs all the tasks of the operating systems which
involve a link between both the hardware and the user
level applications.
• Read/write to any memory, access any I/O device,
read/write any disk sector, …
Operating System Kernel
In fact, “the kernel is the lowest level of software
running on the system”.
• Both the operating system kernel and application
processes are sharing the same machine — the same
processor, the same memory, and the same disk.

• When we are running the operating system kernel, it can


do anything; when we are running an application process
on behalf of a user, the process’s behavior is restricted.
Operating System Kernel
In fact, “the kernel is the lowest level of software
running on the system”.
• Both the operating system kernel and application
processes are sharing the same machine — the same
processor, the same memory, and the same disk.

• When we are running the operating system kernel, it can


do anything; when we are running an application process
on behalf of a user, the process’s behavior is restricted.
Think 🤔 !!
What’s to keep a user process from giving
the CPU an instruction to overwrite some
other process running at the same time,
and allow the kernel to do so?
Operating System Kernel
In fact, “the kernel is the lowest level of software
running on the system”.
• Both the operating system kernel and application
processes are sharing the same machine — the same
processor, the same memory, and the same disk.

• When we are running the operating system kernel, it can


do anything; when we are running an application process
on behalf of a user, the process’s behavior is restricted.

A computer operates in two modes (Dual Modes),


systems frequently switch between them.
Dual Mode

A computer operates in two modes (Dual Modes),


systems frequently switch between them.
Dual Mode
▪ When the computer is running application software,
it is in user mode.
• It is also known as less privileged mode, slave
mode or restricted mode.
Dual Mode
▪ After the application software request for hardware,
the computer enters kernel mode.
• Kernel mode is also called as system
mode or privileged mode.
• If a process in kernel mode fails, the entire
operating system might fail.
Dual Mode
▪ Mode bit - provided by hardware - provides ability
to distinguish when system is running user code or
kernel code
▪ System call, exceptions and interrupts changes
mode to kernel, return from call resets it to user
Dual Mode
▪ Mode bit - provided by hardware - provides ability
System call?
to distinguish when system is running user code or
kernel code Exceptions?
Interrupt?
▪ System call, exceptions and interrupts changes
mode to kernel, return from call resets it to user
System Calls
▪ System calls provide the means for a user program
to ask the operating system to perform tasks
Interface(procedure) to the services provided
by the OS.
System Calls
▪ EX: How to read data from one file and copy them to
another file?
• Users can use:
command like: cp in.txt out.txt
mouse-based and icon-based systems
…
• To do so
system call
sequence can be:
System Calls
▪ EX: How to read data from one file and copy them to
another file? Write prompt to screen
Accept input //input file name
• Users can Write
use: prompt to screen
command Accept input
like: cp in.txt //output file name
out.txt
Open the input file
mouse-based and icon-based systems
if file doesn't exist, abort
… Create output file
• To do so if file exists, abort
system callLoop
sequence can Read
be: from input file
Write to output file
Until read fails
Close output file
Write completion message to screen
Terminate normally
System Calls
▪ Most developers design programs according to an
application programming interface (API).
need know nothing about how the system call is
implemented or what it does during execution.
only obey the API and understand what will be a result.
The API specifies a set of functions that are
available to an application programmer

System calls can often be more detailed and


difficult to work with
System Calls
The standard C
library is an example
of a portion of the
system-call interface.
If printf() is invoked.
The C library intercepts
this call and invokes
the necessary system
call (or calls).
The C library takes the
value returned and
passes it back to the
user program
System Calls Handling
▪ The system-call interface intercepts function calls in
the API and invokes the necessary system calls
within the operating system.
• A number is associated with each system call
• System-call interface maintains a table indexed
according to these numbers
• The system call interface invokes the intended
system call in OS kernel and returns status of the
system call and any return values
Open() System Call
System Calls Handling
▪ System call parameters can be passed to the
operating system in three different ways:
• In registers.
There may be more parameters than registers.
• In a block, or table, in memory, and the address of
the block is passed as a parameter in a register.
Linux uses a combination of these approaches. If there
are five or fewer parameters registers are used,
otherwise memory block used
• Pushed, onto a stack by the program and popped
off the stack by the operating system.
Why Applications are Operating System Specific
▪ Apps compiled on one system usually not
executable on other operating systems
▪ Each operating system provides its own unique
system calls, own file formats, etc
▪ Apps can be multi-operating system
• Written in interpreted language like Python, Ruby, and
interpreter available on multiple operating systems
• App written in language that includes a VM containing the
running app (like Java)
• Use standard language (like C), compile separately on
each operating system to run on each.
Why Applications are Operating System Specific
▪ Apps compiled on one system usually not
executable on other operating systems
▪ Each operating system provides its own unique
system calls, own file is
What formats,
the etc
▪ Apps can be Exeptions and system
multi-operating
Interrupt?
• Written in interpreted language like Python, Ruby, and
interpreter available on multiple operating systems
• App written in language that includes a VM containing the
running app (like Java)
• Use standard language (like C), compile separately on
each operating system to run on each.
Interrupts
▪ Interrupts are a key way in which hardware interacts
with the operating system.
▪ Interrupts occur asynchronously — that is, they are
triggered by an external event.
Processor exceptions and system calls are
synchronous events triggered by process
execution.
▪ A trap or exception is a software interrupt
Software error (e.g., division by zero)
Or processes attempt to write read only
memory, a process attempts to perform a
privileged instruction or accesses memory
outside of its own memory region. …
Interrupts
▪ The CPU hardware has a wire called the interrupt-
request line
▪ A hardware device triggers an interrupt by sending a
signal to the CPU to alert the CPU that some event
requires attention.
▪ As the processor executes instructions, it checks for
whether an interrupt has arrived. If so, instead of
fetching the next instruction, the processor hardware
saves the current execution state and starts
executing at a specially designated interrupt
handler in the kernel.
Interrupts
▪ The interrupt is managed by a separated segments
of code determine what action should be taken -
interrupt handler. Each different type of interrupt
requires its own handler.
• The operating system preserves the state of the
CPU by storing the registers and the program
counter
• Determines which type of interrupt has occurred:
• The interrupt must transfer control to the
appropriate interrupt service routine, through
a generic routine
or interrupt vector, to speed up and reduce searching.
Interrupts
▪ The interrupt is managed by a separated segments
of code determine what action should be taken -
interrupt handler. Each different type of interrupt
requires its own handler.
• The operating system preserves the state of the
CPU by storing the registers and the program
counter
• Determines which type of interrupt has occurred:
• The interrupt must transfer control to the
appropriate interrupt service routine, through:
a generic routine
or interrupt vector, to speed up and reduce searching.
Example
Example
Example
Example
But, How Operating System
is Implemented??
Operating System Implementation
▪ 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.
▪ A common approach is to partition the task into
small components, or modules, rather than have
one single system.
Each of these modules should be a well-defined
portion of the system, with carefully defined
interfaces and functions.
Operating System Implementation
▪ Early operating systems were written in assembly
language.
▪ Now, The lowest levels of the kernel might be written
in assembly language and C. Higher-level routines
might be written in C and C++, and system libraries
might be written in C++ or even higher-level
languages.
▪ Android provides a nice example:
Its kernel is written mostly in C with some assembly
language.
Most Android system libraries are written in C or C++.
Its application frameworks—which provide the developer
interface to the system—are written mostly in Java.
Thank You

You might also like