System Programing and Operating System
System Programing and Operating System
Systems Programming
and Operating System
Iresh A. Dhotre
M.E. (Information Technology)
Ex-Faculty, Sinhgad College of Engineering,
Pune
Anuradha A. Puntambekar
M.E. (Computer)
Formerly Assistant Professor in
P.E.S. Modern College of Engineering,
Pune
Rupesh G. Mahajan
M.E. (Computer)
Assistant Professor,
Dr. D. Y. Patil Institute of Technology, Pimpri,
Pune
® ®
TECHNICAL
PUBLICATIONS
SINCE 1993 An Up-Thrust for Knowledge
(i)
Systems Programming and Operating System
Published by :
® ®
Amit Residency, Office No.1, 412, Shaniwar Peth,
TECHNICAL Pune - 411030, M.S. INDIA, Ph.: +91-020-24495496/97
PUBLICATIONS
SINCE 1993 An Up-Thrust for Knowledge Email : sales@technicalpublications.org Website : www.technicalpublications.org
Printer :
Yogiraj Printers & Binders
Sr.No. 10/1A,
Ghule Industrial Estate, Nanded Village Road,
Tal. - Haveli, Dist. - Pune - 411041.
ISBN 978-93-91567-04-0
SPPU 19
9 789391 567040
Operating systems
Assembly language
I/O Processor - 1
Disk
I/O Processor - 2
Memory
Printer - 1
CPU - 1
CPU - 2
Other I/O 0
channel
Memory
controller 1
Input/Output
channel 2
Memory address
register
Memory buffer
register
Other
CPU
Location counter
Working
registers
Data
Instruction register Instruction
General
registers
Instruction
interpreter
Arg Index
Opcode (8 bits) register register Base (4 bits) Offset (12 bits)
(4 bits) (4 bits)
Sign
bit
0 1 15
Sign
Integer
bit
0 1 31
0 1 7 8 31
0 1 7 8 63
RX
Operation code R1 X2 B2 D2
0 7 8 11 12 15 16 19 20 31
(R1 = Register operand 1, X2, B2 and D2 = Storage operand 2)
RS
Operation code R1 R3 B2 D2
0 7 8 11 12 15 16 19 20 31
(R1 = Register operand 1, R3 = Register operand 3, B2 and D2 = Storage operand 2)
SI
Operation code I2 B1 D1
0 7 8 15 16 19 20 31
(I2 = Immediate operand 2, B1 and D1 = Storage operand 1)
SS
Operation code Length B1 D1 B2 D2
0 7 8 15 16 19 20 31 32 35 36 47
(B1, D1 = Storage operand 1, B2 and D2 = Storage operand 2)
Computer users
System software
Computer hardware
Users
System
software
Operating system
Hardware interface
Hardware
Source
program
Assembler
Object
code
Linker
Executable
code
Loader
Source Target
Compiler
program program
Interpreter
Source Intermediate
code code
Preprocessing Processing
User application 1 User application 2 User application 3
System System
calls calls
Device Device
calls calls
Device controller
Kernel
Device driver
Computer hardware
User process
/dev/xxx
User space
Kernel space
Device driver
Physical device
Assembler Object Linker
Source code
program
Executable
code
Loader
Sign Opcode Register Main memory operand
operand
Label Opcode Operands ; Comments
Optional Mandatory
Source
program
Analysis
phase
a
ss
ac e
t
ces cc
s Synthesis Data a
phase
Target
program
Source program
Pass 1
Control transfer
Data Intermediate
structure code
ta
Da ess
c
ac
Pass II
Target program
Pass 2 Pass 1 Pass 2
Pass 1
Wastage of
memory Data structures Data structures
Source
program
(user)
Pass I
Opcode
table
Source
program
Symbol
table
Intermediate
Literal code
table
Pass II
Program Required
listing target
program
Flowchart for Pass 1
Start
Pass 1
Initialize LC
END
Read card DS EQU USING
DC
DROP
Adjust LC Calculate operand
field
Search
Yes
Pseudo-op table Assign storage
Assign value to locations to
No L = Length of symbol in label literals
data field field
Search
Machine-op table Reset
copy file
L = Length
Go to
Pass 2
Process literals
Symbol Yes
in label Assign current value
field of LC to symbol
?
N
LC = LC+L
Pass 2
Initialize LC
Read card
form file copy
Search Yes
pseudo-op table DS EQU DROP END
DC START USING
No Adjust LC Evaluate
to proper operand
Search
machine-op table alignment
Shows base
reg. number
Get op-code byte Form constant Enter base unavailable
and format code, DC DC and insert in
or reg. no. and
L = Length assembled value into
DS program base table
DS
Type of
instruction L = length
RR of data field
Printing listing
Evaluate both
register expressions
A
and insert into RX
2nd byte
Evaluate register and
index expressions
and insert into
''Punch''
2nd byte
assembled
instruction Generates literals
for entries in
Calculate effective Literal Table
Display address of operand
A assembly
listing line STOP
D+C (B) = EA
LC = LC+L
Put B&D into
bytes 3 and 4
Program
Source Load-and-go
loaded
program assembler
in memory
Assembler
Object
Source Translator deck Object program
program (Assembler) or object Loader ready for
program execution
Memory
Symbol Value
LENGTH 100C
RDREC 2013
THREE 1003
ZERO 1006
WRREC 201F
EOF 100D
ENDFIL 201C
RETADR 1009
BUFFER 100F
CLOOP 2012
FIRST 200F
Symbol Value
LENGTH 100C
RDREC 203D
THREE 1003
ZERO 1006
EOF 1000
ENDFIL 2024
RETADIR 1009
BUFFER 100F
CLOOP 2012
FIRST 200F
MAXLEN 203A
NPUT 2039
EXIT 2050
RLOOP 2043
TM
MACRO
ADDS & arg1, & arg2, arg3
ADD1 & arg1
ADD1 & arg2
ADD1 & arg3
MEND
ARGTAB
1 F1
2 BUFFER
3 LENGTH
Program
with macro Macro Target
preprocessor Assembler program
definitions
and calls
Program
without
macros
Pass 1
R Read
End pseudo-op
Search R R
Machine-op Table
Found, macro call
R R
Pass 1
MDTC = 1
MNTC = 1
Read next
source card
Yes End
No
pseudo-op
?
Read next
source card Yes
Go to
PASS 2
Enter macro name
and current value
of MDTC in MNT
Read next
source card
MNTC = MNTC+1
Substitute index
Prepare argument notation for
list array arguments
MDTC = MDTC+1
MDTC = MDTC+1
Yes MEND No
pseudo-op
?
Pass 2
Macro
No Write into expanded
name found
source card file
?
Yes End
No
pseudo-op
MDTP = MDT index ?
from MNT entry
Yes
Supply expanded
Set up argument source file to
list array assembler
MDTP = MDTP +1
Substitute arguments
from macro call
Compiler
Source Target
Analysis Synthesis
program program
Lexical Analysis
Syntactical Analysis
:=
id1 +
id2 *
id3 2
Symantic Analysis
:=
id1 +
id2 *
int to float
t1 : = id3 * 2
t2 : = id2 + t1
id1 : = t2
Code generation
MUL #2.0, R0
ADD R0, R1
MOV R1, id1
total +
count
rate 10
Source
program
Syntax analyzer
Semantic
analyzer
Synthesis
Code optimizer phase
Code generator
a = b+c 60
Lexical analyzer
Syntax analyzer
id1 +
Syntax tree
id2
id3 60
Semantic analyzer
id1 +
Semantic tree
id2
id3 int to float
60
a
Intermediate code
b generator
c t1 : = inttofloat (60)
. t2 : = id3 t1
. Intermediate code
. t3 : = id2 + t2
id1: = t3
Symbol table
Code optimizer
t1 := id3 60.0
Optimized code
id1 : = id2 + t1
Code generator
MOVF id3, R2
MULF #60.0, R2
MOVF id2,R1 Machine code
ADDF R2, R1
MOVF R1, id1
Data
Source Output
program
1. Symbol table 2. Data store 3. Data manipulation routine
Source program Result
Interpreter
Data
Source program Result
Preprocessor Interpreter
Intermediate Data
code
TM
Memory
User Compile
Program for
program and go
execution
loader
Assembler
Main memory
Source Object
program Translator program 1
Executable object
Loader
code
Source Object
program Translator program 2
Loader
program
Main memory
Object program
for MAIN
1000
MAIN
Absolute 2000
loader
5000
SUM
Object program 7000
for SUM
Memory
Object
Source Relocating program
Assembler
program loader + Inter segment
references
32 bit
OP R1 X2 A2
8 4 4 16
relative Address of
Location TABLE is at 28 th byte
from beginning .
ESD
RLD
END
S1 (16K)
S2 (14k) S3 (8k)
S7 (16k)
S8 (12k) S9 (18k)
S1
16
S2 S3
24 S4 S5 S6
30
36
40 Free
44 Space
S7
60
S8 S9
72
78
80
Start
What
0 is the type 1
of
card?
MAIN START
Maximum 255
entries are allowed.
Copy of
object program
Program
Assembler / Object Pass I Pass II gets loaded
compiler deck / program of loader of loader here for
execution.
Global external
symbol table
Core memory
Local
external
symbol
array (LESA)
Copy of
object deck
A
S
S
Program
E
Object deck loaded in
M Pass 1 Pass 2
memory
B
for execution
L
E
R Program
Printed load address
(PLA) Memory
load
map
Initial program
load address
Global
(IPLA)
external
symbol
Table
(GEST)
Loader
PLA IPLA
Write copy of
card for pass 2
TXT / RLD
What
is the type LDT / EOF
Go to pass 2
END of card ?
PLA PLA + SLENGTH
ESD
What
ER is the LD
type of
ESD
SD
SLENGTH LENGTH
Is
No symbol entry Yes
in GEST ?
PLA IPLA
EXADDR IPLA
ER Is
LD ESD ADDR Yes
type Search GEST specified?
? for symbol
EXADDR
SD PLA + ADDR
No
SLENGTH LENGTH
PLA PLA + SLENGTH
Is
Set
symbol
LESA (ID) = PLA Get VALUE from
present ?
LESA (ID)
No
Yes
What –
+ is
ERROR flag ?
LINKER
time
run
Finished
program
MEMORY
TM
System
software
Operating system
Hardware interface
Hardware
User 1
Job
User 2
Job Batch 1
Jobs
Computer
User 3 Job Jobs
Operator Batch 3 Computer
Job
User 4
Jobs Batch 2
b
Jo
User 5
Job
User 6
b
Jo
User 7
b
Jo
User 8
Operating system
kernel
Program 1
Program 2
Program 3
Free space
Program 4
Program 5
Free space
Operating system
kernel
CPU
Program 1
ALU Performing I/O
Program 2 operation
(Blocking state)
Cache
Program 3
(Executing by CPU)
Waiting for CPU
Program 4 Hard disk
(Secondary storage device)
Un-allocated space
Host
computer
LAN
Internet
LAN
Request Request
Client Client
Response Response
Server
Request
Client
Request
Response Client
Response
Peer Peer
Peer Peer
Higher
Environment variables
address
Stack region 1
Stack
Start
pointer
End
Dynamic / heap region
Priority number
Program counter
Memory allocation
Accounting information
Number of registers
Process state
PID PCB
1 PCB
2 PCB
3 PCB
N–1 PCB
N PCB
Process table
New
state Interrupted Terminated
Admitted Exit state
Ready Running
state state
Scheduler dispatch
activated
Heavy weight process
Thread
Register
Stack
Mask
Data
Address space and
Local global information
information
letes
I/O comp
ing Blocked
Dispatch lock
I/O b
Start Run completes
New Ready Running Dead
Yield Sleep
Sleeping
Wa
it
Noti
fy
Tim
e slice Waiting
exp
ires
Thread library
User
space
Kernel
space Processor
User
space
User threads
Kernel
space
Processor
User thread 1
User thread 2
User thread 3
Task 1
Task 2
Task 3
Thread
library
User space
Kernel space
Kernel Kernel Kernel
level level level
thread 1 thread 2 thread 3
User thread 1
User thread 2
User thread 3
Process A
Task 1
Task 3
Task 2
User space
Thread library
with scheduler
Kernel space
Kernel
thread
User thread 1
User thread 2
User thread 3
Task 2
Ta
k3
sk
s
Thread library
Ta
1
with scheduler
User space
Kernel space
Kernel level Kernel level Kernel level Kernel level Kernel level
thread 1 thread 2 thread 3 thread 4 thread 5
Header
First
Priority
Long term scheduling
Suspended
process
Time out
s
ur
Interrupt from
cc
an interrupt wait
en
process
Ev
P2 P3 P1
0 2 6 20
Sum of P1 , P2 and P3 waiting time
3
6 0 2
3
P1 P2 P3 P4 P5
0 4 11 14 17 22
0 4 11 14 17 46
5 5
4 11 14 17 22 68
5 5
P2 P4 P1 P3
0 2 6 11 17
6 0 11 2 19
4 4
11 2 17 6 36
4 4
P3 P4 P1 P2
0 6 10 15 17
10 15 0 6 31
4 4
15 17 6 10 48
4 4
P1 P2 P3 P4 P1 P3 P4 P1 P3
0 2 4 6 8 10 12 14 15 17
10 2 11 10 33
4 4
15 4 17 14 50
4 4
P1 P2 P4 P1 P3
0 1 3 7 12 20
6 0 10 0 16
4 4
12 2 18 4 36
4 4
P1 P4 P2 P3
0 8 10 16 17
P2 P3 P4 P2 P1
0 2 3 5 9 17
P1
P2
P3
P4
P1 P4 P2 P3
0 6 9 13 21
P1 P2 P1 P4 P3 P2 P1 P4 P3 P3 P3
0 2 4 6 8 10 12 14 15 17 19 21
P1 P1
P2 P2
P3 P3
P4 P4
A A B A B C B C D B C E D B C E D B D D
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
4 16 11 14 8
5
TM
Exit section
Entry section
Critical section
Exit section
Remainder section
Monitor entry queue
Shared data
Condition variable
Local to
X
monitor
Y
Entry methods
Only one active
at any moment
called from
outside monitor
Local methods
Code
initialization
Monitor entry queue
Thread waiting to enter the monitor
Shared data
Condition variable
Local to
monitor
Entry method
Only one active
at any moment
called from
outside monitor
Local methods
Code
initialization
reader writer
1 1
Shared buffer
Producer Consumer
(Waiting)
Buffer full
Producer Consumer
(Waiting)
Buffer empty
Producer Consumer
Food
P2
P5
P4 P3
Thinks a Lock left Lock Eat for a
while fork first right fork while
Philosopher
Request Held by
P P
P1 P2
R1 R2
P1 P3
R3
R1 R2
P2 P4
R1
R2
R3
P1 P1
R1
R1 R2
R2
P2 P2
(P1 , P2 , P3 , P4 ) (R 1 , R 2 , R 3 , R 4 )
R1
R2
R3
R4
R5
R2 R3
P1 P2 P3 P4
R1
R4
P1 Pk D1 Dk
H1 Hk
P1 P2 Pk P1
M1 Mn P1 Pn
H 1 ... H k Di
Mi Hi + Di
M 1 ... M k
Pk+ 1 Pn
Mi M k+ 1 ... M n
Unsafe
state Safe
state
Deadlock
Rj Pi
n
Allocation (Pi )
i 1
(P1 , P2 , P3 , P4 , P5 )
(R 1 , R 2 , R 3 ) R1 R2
R3
P1
need i available i
P3
P2 P4 P5 P1 P3
R1 R2 R3 R4 R1 R2 R3 R4 R1 R2 R3 R4
P0
P1
P2
P3
P4
P1
P0 P2 P3 P4 P1
P1
R1 R2 R3 R1 R2 R3
P1
P2
P3
P1
P3 , P2 , P1
(1 1 0)
R1 R2 R1 R2 R1 R2
P1
P2
P3
P2 , P4 , P1 , P3
P0
P1
P2
P3
P0
P1
P2
P3
P1
P4
P0
P1
P2
P3
P4
P0
P1
P2
P3
P4
P1
P2
P0 P3 P4 P1 P2
P1
P0
P1
P2
P3
P4
P0
P1
P2
P3
P4
P4
P0
P1
P2
P3
P4
P3 P2
P3 P2
P1
P1
Pi Pj
Pi Pj
TM
Free space
Free space
User
Limit process 1
+
User Allocated space
base
process 4 for process
Base Free space
Operating
system
kernel
0
Primary
memory
Memory
Source Linkage
Compiler Loader image
program editor of program
Load
Compile time Execution
time time
Relocation Main memory
register
Kernel
(y)
free
Logical Physical
+
Processor address address
(x) Relocation (x + y) free
Memory Data
management
unit
Process control
information Process control
block
Entry point
to program Jump
Program instruction
Reference
Data to data
Current top
of stack
Stack
Primary memory
Kernel
16 m
16 m
16 m
16 m
Kernel Kernel Kernel
16 M 16 M 16 M
Program 1 Program 1
16 M 16 M
Free Program 2
space 10 M
48 M Free
space Free space
32 M 22 M
(a) (b) (c)
Logical
address(x)
Limit N
x<y Error
register
generates
(y)
Y
Limit
register (x + z)
(z)
Physical
address
OS
Free
Primary
memory
Operating Operating Operating
system system system
Program 2 Program 2
(15 MB) (15 MB)
Free space
Program 3
Free space (12 MB)
Free space
Operating Operating
system system
Program 1 Program 1
(10 MB) (10 MB)
Free space
Free space
Operating Operating
system system
Program 1 Program 1
24 MB 24 MB
Operating
system
Program 1 Program 91
24 MB (52 MB)
Free space
20 MB
Operating system Operating system
Program 1 Program 1
Program 2 Program 4
Program 4
Free space
Free space
6 kB 6 kB 6 kB 6 kB
14 kB 14 kB 14 kB 12 kB
2 kB
12 kB
19 kB 19 kB 9 kB
7 kB
11 kB 11 kB 11 kB
13 kB 12 kB 13 kB 13 kB
512 kB 512 kB
256 kB 25 kB 512 kB
2N
2k
2k
2k 2 k+ 1
2k
Page number (P) Displacement or offset (D)
Address
translation
Page table
Page
number
X
Page frame
number
Process P1
0 0 13
1 1 7
2 2 5
3 3 6
4 4 2
5 5 8
6 6 9
7 7 0
Page table
for process
P1
Physical Page
address frame
000000
P1
100000 1
Virtual Virtual Page
address page number Page frame 101000 100
0000 0 0 FFD
102000 101
01000 1 100 P2
1
2 103 103000 102
02000
LDA 003200 2
3 FFF 104000 103
03000
3
200
FFF200
C FFE000 FFE
P0
FFF000 FFF
P3
Memory
Frame Protection
Index
number bit
0 4 Valid
1 2 Invalid
Main
2 5 Valid memory
3 7 Valid
4 1 Valid
5 3 Invalid
CPU Offset Offset
Page Primary
table memory
TLB
2 12
2 32 2 12
2 20
Virtual address
Page directory + P t d
entries 10 bits 10 bits 12 bits
Page table
a
X
Y
W
X +
X+W Y'
Page Displacement
Outer frame
page table
Physical address
CPU
Byte
Virtual
address To primary
D memory
Displacement
Hash table
Hash (f)
function
Main
Heap
program Library
function
Thread
stack
Thread
stack
Symbol
table
Library
Add ( ) function
Segment number Offset
Segment table
Limit Base
address
Segment Displacement
CPU
number (s) (d)
Logical address
Memory
Yes Physical
d limit +
address
No
Error
Process P1
OS
23
28
2 29
2 29 2 8 2 29 8 2 21 21
28
Main
memory
Address
space
Virtual
memory Hard disk
Primary
memory
Virtual address
space OS
Memory
Management
unit Secondary
storage
device
Phys l
Virtual ical sica
CPU MMU Phy
addre ess
address ss addr
Cache Primary
memory memory Disk
DMA
Swap P2 out
Image for
P2
P2 P2
P2
P2
P1
P1
pi w i pi wi
pi
pi w i
pi
pi
wi
Primary
memory
OS
Process
removed
from memory
Secondary
storage
Process device
loaded into
memory
Frame Valid-invalid bit
0 1
1 0
2 1 Primary
memory
3 1
4 0
5 1
Page table
Add R1
Page table
Reference
Error Operating Search Secondary
Restart 0 system storage
kernel device
Primary memory
to
in
M t
g
od ab
n ry
di mo
ify le
a
Lo me
pa
ge
Free
space
Hit rate Hit time Miss rate Miss time
Hit time Miss rate Miss penalty
Memory access time * (1 – p) p * page fault service time
si wi
si wi
Processor
utilization Thrashing begins
Degree of
multiprogramming
t
CPU time
Page
referenced
2 2 3 4 7 6 7 7 6 4 5 5 5 3 3 4 4 4 4 4
t3
t1 t2
ti
0 1 2 3 0 1 2 3 0 1 2 3 4 5 6 7
t3
t1 t2
WS(t1) = { 0, 1, 2, 3 }
WS(t2) = { 3, 0, 1, 2, 4, 5, 6, 7 }
WS(t3) = { 1, 2, 3, 0, 4, 5 }
R1 R2 R3 R4 R1 R2 R3 R4 R1 R2 R3 R4
P0
P1
P2
P3
P4
P1
9 789391 567040
Made in India