Chapter 2 (Slides)
Chapter 2 (Slides)
Semester : 04 Directed by
2024/2025 Dr. DABBA ALI
➢ Contact us
alidabba@[Link]
[Link]@[Link]
Basic Mechanisms of
Operating System
Plan
I. Introduction
II. Computer Architecture and Technology
III. VON-NEUMANN Machine
IV. Instruction Cycle (Fetch-Decode-Execute Cycle)
V. Boot Process
VI. Program Execution Steps in an Operating System
VII. Process
VIII. Interruption Systems 4
I. Introduction
➢ Human has used computers to solve their problems
automatically.
➢ These problems are formulated into programs, then
executed by the hardware machine.
➢ For a problem to be interpreted, executed and produce
the results expected by the machine's processor, it
must be formulated in a language that the
machine can understand, and the operating system
must control its execution.
I. Introduction
➢ In order to do this, the programmer begins by writing
his problem in a structured way, in the form of an
algorithm
Algorithmic
Problem Algorithm
modeling
Memory CPU
I/O I/O … I/O
BUS
Data Program
Memory Memory
CPU I/O I/O … I/O
DATA BUS
Figure 2.4 : Harvard Architecture
II. Computer Architecture and Technology
➢ The best-known architecture is Von
Neumann (the same architecture as our
PC), while the Harvard architecture is used
in devices more specific to the computing
field (DSP: digital signal processor).
➢ Our studies focus on Von Neumann
architecture.
III. VON-NEUMANN Machine
Definition 2.1
A Von-Neumann machine is a memory-based
electronic computer whose components are :
➢ Main Memory (MM/RAM).
➢ Processor or Central Processing Unit (CPU);
to perform calculations and execute
instructions.
➢ Peripheral or Input/Output (I/O) Units.
III. VON-NEUMANN Machine
Processor
Control Unit
BUS
Main Memory (RAM) Input/Output Units
Figure 2.5 : Architecture of Computing System.
III. VON-NEUMANN Machine
1) Main Memory
➢ An addressable memory is a collection of information
storage units of equal capacity known as Locations.
➢ Each Location within the memory is identified by a
specific piece of information called an address, which
allows it to be designated and distinguished from other
locations.
➢ The interface between a processor and an addressable
memory is illustrated in the figure 2.6.
III. VON-NEUMANN Machine
1) Main Memory
ADDRESS BUS
MAIN
PROCESSOR
MEMORY
R/W
Read/Write signal
DATA BUS
PSW @ Bus
M
Decoder
TRALU
IR A
(opcode) | (operand)
R RAM
ALU sequencer
ACC
Operations Control Lines
to Registers, ALU, Memory, etc MDR
Data Bus
1) Fetch
2) The program counter (PC) stores the address of the next instruction to
be fetched. This value is copied to the MAR via the “address” bus.
3) The control unit then sends a signal over the control bus to read the
memory. In other words, the MAR has triggered the read signal. So, the
address data is transferred the contents of the selected memory cell to
the MDR via the “data” bus. Simultaneously the PC increments by one
(holding the address of next instruction to be executed).
4) The contents of the MDR is copied into the instruction register (IR) via
the instruction bus. This consists of the instruction (opcode) and the
data part (operand).
IV. Instruction Cycle (Fetch-Decode-Execute Cycle)
+1
+1
@PC
0005 R/W
1
PSW @ Bus
M ADD 10; 0005
Decoder
TRALU
IR A
ADD
(opcode) 10;
| (operand) 0005
R RAM
ALU sequencer
ACC 2
Operations Control Lines
to Registers, ALU, Memory, etc MDR
Data Bus
3
Input/Output
Figure 2.8 : Internal (I/O) Unit
Microprocessor Architecture.
IV. Instruction Cycle (Fetch-Decode-Execute Cycle)
2) Decode
The next step for the CPU to interpret the
data just fetched is to decode it. This
instruction in the IR is split into the
opcode and operand.
IV. Instruction Cycle (Fetch-Decode-Execute Cycle)
2) Decode
3) The decode analyses the instruction (opcode) in the IR and decodes it.
The decode has an instruction set that defines the opcodes and so what
commands to carry out. Every CPU will have its own instruction set
defining legitimate commands. The opcode determines what part of
the hardware is needed for execution.
4) A series of micro-signals are sent to different areas to prepare the CPU
for readiness of the next step (by the Control unit).
5) The data to be operated on (operand) may be: passed onto the ALU OR
the address of the data may be used with the operation which is then
copied to the MAR OR the data to be operated on is copied to the
MDR.
IV. Instruction Cycle (Fetch-Decode-Execute Cycle)
+1
PC R/W
PSW @ Bus
3 M
Decoder
TRALU
IR A
ADDADD
(opcode) 10; 10
| (operand)
R RAM
ALU sequencer
ACC 2
Control Lines
Operations
to Registers, ALU, Memory, etc MDR
1 Data Bus
Input/Output
Figure 2.8 : Internal (I/O) Unit
Microprocessor Architecture.
IV. Instruction Cycle (Fetch-Decode-Execute Cycle)
3) Execute
4) The appropriate instruction is carried out upon the data
(operand) in the MDR or accumulator by the execute unit
(containing an arithmetic logic unit that carries out calculations
on the data). This is the executing of the instruction
5) The results of the arithmetic logic unit (ALU) are output into
another register called the accumulator.
6) The control unit assigns new signals to reset the CPU to
prepare for the next cycle
IV. Instruction Cycle (Fetch-Decode-Execute Cycle)
+1
PC R/W
PSW @ Bus
M
Decoder
TRALU
IR A
ADD | (operand)
(opcode) 10
R RAM
ALU sequencer
30
20
ACC
Operations Control Lines
to Registers, ALU, Memory, etc MDR
Data Bus
Input/Output
Figure 2.8 : Internal (I/O) Unit
Microprocessor Architecture.
V. Boot Process
➢ The main memory, cache memory, and internal registers of the
CPU are supposed to be volatile. The information that is stored
in these devices is lost when the computer’s power is turned
off.
➢ From instruction cycle, we learned that a computer is a device that
fetches the instruction from main memory that the PC
register points to. The computer then executes the instruction.
➢ This cycle is repeated over and over again until the computer
is turned off. However, if we have only volatile memories, there
is no instruction in the main memory when the computer is
turned on.
V. Boot Process
➢ To overcome this problem, a program is designed and stored in a special
memory called the Read Only Memory (ROM) and it is installed into
the computer’s motherboard (sometimes called the main-board).
➢ This program is called the Basic Input Output System (BIOS).
➢ When a computer is turned on, an address is automatically loaded
into the PC register. This is done by hardware circuitry.
➢ The address given is the location of the first executable instruction
of the BIOS.
➢ The journey starts from there. BIOSes are produced by many factories,
but perform the same basic functions.
V. Boot Process
1) Loading of Operating System
➢ The process by which the OS gets loaded into the
memory so that it can start executing is known as the
System Bootup.
➢ During the boot up sequence, a series of instructions
need to be executed so that at the end of this sequence
the OS is running and can in turn start executing user
programs.
V. Boot Process
1) Loading of Operating System
The boot sequence begins with the following:
1) The RESET pin of the CPU is set to logical high.
2) The code which is found at some specific starting address (0xfffffff0 in
case of an Intel processor) is executed.
3) 0xfffffff0 maps to the persistent memory chip of the computer known as
the Read Only Memory (ROM).
4) The series of instructions stored in ROM is called the Basic Input
/Output System (BIOS).
V. Boot Process
2) BIOS Actions
➢ Although the BIOS performs a number of functions e.g. making sure
that all the different chips, hard drives, and the CPU function
together as an entity, its most important function is loading the
Operating System.
➢ When the computer is first powered on, the microprocessor attempts to
execute the first instruction. For this purpose, as mentioned earlier the
processor needs to fetch this instruction.
➢ The processor cannot fetch it from the Operating System because
the OS has not been loaded yet into the memory; it is still residing on
the disk. It is the BIOS which provides the processor with the first
instructions to be executed in order to load the OS.
V. Boot Process
2) BIOS Actions
The other functions performed by the BIOS during the system
bootup apart from loading the OS are summarized as:
➢ A power-on self-test (POST) for the different hardware
components in the system to make sure everything are
functioning properly.
➢ Activating other BIOS entities e.g. graphics cards.
➢ Providing a set of low-level routines to enable the OS to
interface different hardware devices keyboard, screen, and
the ports (serial as well parallel).
V. Boot Process
3) Fetch Execute Cycle During System Bootup
➢ When the computer is powered on, the PC as usual points to some
specific address. This pointer holds the address of the instruction to be
executed. Thus, the Program Counter is initialized to some specific
address like 0xfffffff0 in the case of Intel CPUs.
➢ This address would be different for different Operating Systems. This is
the address of Read Only Memory (ROM).
➢ ROM contains a special program called Boot Loader. Thus
immediately, after we turn on the computer, the instructions comprising
the Boot loader start executing because the PC was initialized with its
address.
V. Boot Process
3) Fetch Execute Cycle During System Bootup
➢ The boot loader loads the OS from disk to
the memory and then executes a JUMP
instruction to that section of memory.
➢ The boot loader only loads a part of the OS and
the instructions subsequent to the jump
instruction read the remaining part of the OS
into memory.
V. Boot Process
3) Fetch Execute Cycle During System Bootup
The above process can be summarized as follows:
➢ The ROM contains the Basic Input Output System (BIOS) which makes
a call to the boot loader program.
➢ The boot loader program loads the OS from disk to memory. It reads
from the first sector of the disk which is termed as the Master Boot
Record (MBR).
➢ The boot loader copies the code from the MBR to memory, starting
at some specified location depending on the processor.
➢ The boot loader then performs a jump to that address, and
subsequently the code for the OS starts executing.
V. Boot Process
3) Fetch Execute Cycle During System Bootup
➢ The Operating system when completely loaded
in the memory starts accepting and executing so
that it can now in turn start executing the user
instructions.
➢ It does this by continuously performing the
Instruction Fetch Execute Cycle as mentioned
before.
VI. Program Execution Steps in an Operating System
➢ Developing a program, from problem analysis to fine-tuning,
requires a number of software tools that make up a programming
environment.
➢ To function, these tools use the services of the operating system.
➢ The program production chain transforms a program written in
a high-level language (Pascal, C, VB, Java, etc.) into a so-called
executable program, written in machine language. This
transformation is carried out in several steps (see Figure 2.9):
➢ These translate the source code into machine code, while allowing the
programmer to correct any errors that may have arisen at each step.
Problem
VI. Program Execution Steps in an Operating System
Process
High-level Language (*.pas,
Source
*.java, *.c, ...)
Program
Loading Errors
Translation Loader
Translation Errors
(Compiler / Interpreter)
Object Machine language (Binary /Assembly) Executable
Program (*.obj, *.class, ...) Program
2) Translator
➢ Translator is a program that takes as input a program
written in a programming language (the source
language) and produces as output a program in
machine language (the Object or Binary language).
➢ There are two types of translators:
❑ Compiler
❑ Interpreter
VI. Program Execution Steps in an Operating System
2) Translator
➢ Compiler is a complex program, which translates a source program
into an equivalent machine language (object program).
➢ The compilation process typically involves several stages such as
lexical analysis, syntax analysis, semantic analysis, code generation, and
code optimization.
➢ During these steps, the compiler signals errors to be corrected,
which may appear in each step.
➢ The result of this phase is a file called object code, with a specific
extension depending on the programming language (Example: *.obj
Pascal, *.class Java, ...).
VI. Program Execution Steps in an Operating System
2) Translator
➢ Interpreter is a program that translates the source program
into machine language and executes it directly, without,
producing any object code (e.g. PHP, Python, ...).
➢ It does this by fetching the source program instructions
one by one, analyzing them one by one, and then
"executing" them one by one.
➢ Smaller ( advantage) and
➢ Slower (disadvantage)
VI. Program Execution Steps in an Operating System
[EXTDEF]
[EXTREF]
Procedure
3) Linker (Example)
Consider the following two C-language programs: Prog1.c and Prog2.c
Prog1.c Prog2.c
int v1 ;
extern float v2 ;
extern int v1 ; int proc1(int x) ;
extern int proc1(int x) ; main()
float v2 ; {
float b ;
main()
b = v2 *2;
{
}
int a, x ; int proc1(int x)
x = v1 + proc1(a) ; {
} int y ;
return (x * y /2) ;
}
VI. Program Execution Steps in an Operating System
3) Linker (Example)
➢ In this example, we see that for the program Prog1.c, the external references
are the v1 variable and the proc1 procedure, while the external definition is the
variable v2.
➢ On the other hand, for Prog2.c, the external reference is the v2 variable, while
the external definitions are the v1 variable and the proc1 procedure.
Module External References External Definition
Variable v1
Prog1.c Variable v2
Procedure proc1
Variable v1
Prog2.c Variable v2
Procedure proc1
VI. Program Execution Steps in an Operating System
4) Loader
➢ Once the linking operation has been completed, the
program is ready to run.
➢ This phase is performed by a system tool called the
loader.
➢ This step consists in reading the instructions in
secondary memory, and transferring them to main
memory, while reading the loading address provided
by the linker at the beginning.
VI. Program Execution Steps in an Operating System
4) Loader
➢ A loader is a program used by an operating system to load
programs from a secondary to main memory so as to be
executed. In this case, the program becomes a process.
@ Loading
4) Loader
There are two types of loading:
➢ Absolute loading: the loaded code must not be placed in
another data block other than the one indicated in the
object code ( in other words, the code must be copied).
➢ Translatable loading: loading can take place at any
address in main memory. The way addresses are translated
is based on information provided by the linker.
VI. Program Execution Steps in an Operating System
Exercise :
The translation of a module consists in modifying its
contents so that it can be executed in a different location
from the one for which it was originally intended.
❑ Give the result of translating the following module in
68000 assembler. If the linker decides to set the code
section to hexadecimal address 032340 and the data
section to hexadecimal address 043320, the addresses
generated are on 4 bytes.
Section Code :
VI. Program Execution Steps in an Operating System
ADD10 : move.w #10, D0 0 : 30 3C 00 0A
Exercise
jmp C: 4 : 4E F9 00 00 00 0E
ADD16 : move.w #16, D0 A : 30 3C 00 10
C: add.w D1, D0 E : D1 01
Move.w D0, MEMO 10 : 33 C0 00 00 00 02
rts 16 : 2E 75
Section Data :
LOC : ds.w 1 0 : 00 00
MEMO : ds.w 1 2 : 00 00
VII. Process
➢ In this part, we discuss one of the most fundamental
abstractions that the OS provides to users: the process.
➢ The definition of a process, informally, is quite simple: it is a
running program.
➢ The program itself is a lifeless thing: it just sits there on the
disk, a bunch of instructions (and maybe some static data),
waiting to spring into action.
➢ It is the operating system that takes these bytes and gets them
running, transforming the program into something useful.
VII. Process
Definition 2.2
A process is a running program executed by a
processor. Several processes can be associated
with a program. Each process has its own
memory workspace, program counter and
registers.
VII. Process
Definition 2.3
A process is a dynamic entity corresponding to
the execution of a sequence of instructions: an
executing program, together with its data, stack,
program counter, stack pointer, resources and
other register contents required for execution.
VII. Process
Definition 2.4
A process can be defined as a running
program. In other words, a program by itself is
not a process. A program is a passive entity, like
the contents of a file stored on disk, whereas a
process is an active entity, with an instruction
counter specifying the next instruction to be
executed and a set of associated resources.
VII. Process
1) What Happens After a Program Start-Up
➢ A program is a piece of code which may be a single
line or millions of lines.
➢ A computer program is usually written by a computer
programmer in a programming language.
➢ When the user runs a program, and the machine uses
the principle of virtual memory and multitasking, the
program goes through the following stages:
VII. Process
1) What Happens After a Program Start-Up
➢ The system creates a job in virtual memory while loading the
program.
➢ Afterwards, according to some criterion or other, this job is
admitted into the system and loaded into main memory in its
entirety or in part, creating what is known as a process.
➢ The process is then directed to execution in the processor
according to specific conditions, where it can change its state
several times during execution, until it terminates.
CPU Memory
VII. Process
code static
data heap
stack
Process
code static
data Loading:
Program Takes on-disk program and
reads it into the address space of process
Disk
Figure 2.13 : Loading From Program To Process
VII. Process
2) Components of a Process
The memory space allocated to a process; known as the process memory
map. It is divided into the following four sections:
1. Stack: Temporary data like method or function parameters, return
address, and local variables are stored in the process stack.
2. Text (Code): This comprises the contents present in the processor's
registers as well as the current activity reflected by the value of the
program counter. Access to this area is read-only.
3. Heap: This is the memory that is dynamically allocated to a process
during its execution.
4. Data: The global as well as static variables are included in this section.
VII. Process
2) Components of a Process Stack
Heap
Data
Text (Code)
Ready Running
Interrupt
I/O or event completion I/O or event wait
Waiting
Figure 2.15 : Process States and Their Transitions
VII. Process
5) Process States and State Transition Model
➢ New → Ready : OS creates process and prepares the process
to be executed, then OS moved the process into ready queue.
➢ Ready → Running : OS selects one of the Jobs from ready
Queue and move them from ready to Running.
➢ Running → Terminated : When the Execution of a process
has Completed, OS terminates that process from running
state. Sometimes, OS terminates the process for some other
reasons including Time exceeded, memory unavailable, access
violation, protection Error, I/O failure and soon.
VII. Process
5) Process States and State Transition Model
➢ Running → Waiting : A process is put into the waiting
state, if the process need an event occur (or) an I/O
Device require.
➢ Waiting → Ready : A process in the waiting state is
moved to ready state when the event for which it has been
Completed.
➢ Running → Ready : When the time slot of the processor
expired (or) If the processor received any interrupt signal,
the OS shifted Running → Ready State.
VII. Process
6) Context Switching
➢ In a multi-programmed system, the processor executes several processes
in parallel (pseudo-parallelism).
➢ When the CPU is changed from one process to another, the context
of the first process is saved and that of the second process is
loaded into appropriate registers and other data structures. We call this
context switching or process switching.
➢ Mind that process switching happens from one process to another in
relation to a CPU allotment and is managed by the OS.
➢ But mode switching (user mode to kernel mode or vice versa) is
essentially a processor mode activity - that happens within the context of
a running process.
VII. Process
6) Context Switching (Who Causes The Context Switch and
When?)
⋮ Idle
Reload state from PCB1
⋮ Idle
Reload state from PCB0
executing