Review For OS. Printed Version
Review For OS. Printed Version
Review For OS. Printed Version
Final Exam
XIPING LIU
SCHOOL OF COMPUTER SCIENCE,
NANJING UNIVERSITY OF POSTS AND
TELECOMMUNICATIONS
What have we learned?
Process/CPU
Management I/O Device Management
Memory
Management
Basic Information 3
• Summary
• Shell and GUI, Two Roles, CPU modes (Kernel mode and user
mode), Time and space multiplexing
• Multiprogramming and SPOOLing
• Program counter and PSW, Cash hit, RAM, ROM, Interrupt,
BIOS
• Process, interprocess communication, UID/GID, file,
directory, path name, working directory, block/ character
special files
• System Calls, TRAP instruction
1.1 What Is An Operating System5
• Two types of user interface
• Process Hierarchies
– In UNIX
• A process and all of its children and further descendants
together form a process tree
• All the processes in the whole system belong to a single
tree , with process init at the root
2.1 Processes 10
• Process Hierarchies
– In Windows
• Has no concept of a process hierarchy
• All processes are equal
• Parent can use a special token (called a handle) to
control the child
• It is free to pass this token to some other process
2.1 Processes 11
• Process States
– Three basic states
• Running (actually using the CPU at that instant)
• Ready (runnable; temporarily stopped to let another
process run)
• Blocked (unable to run until some external event
happens), also called waiting/sleeping state
– Five basic states
• New, Exit
– Seven basic states
• Suspend
2.1 Processes 12
• Process States
– Process State Transitions Transition 1 occurs
when the operating
system discovers that
a process cannot
continue right now.
Processes can
execute a system call
pause to get into
blocked state
• Process States
– Process State Transitions: Transition 2&3 are caused
by process scheduler Transition 2 occurs
when the process
scheduler decides
that the running
process has run long
enough, and it is time
to let another process
have some CPU time
Transition 3 occurs when process scheduler choose this
process by certain scheduling algorithm
2.1 Processes 14
• Process States
– Process State Transitions
Transition 4 occurs
when the external
event for which a
process was waiting
(such as the arrival of
some input) happens
• When to schedule
– when a new process is created---run the parent
process or the child process
– When a process exits
– when a process blocks on I/O, on a semaphore, or
for some other reasons
– when an I/O interrupt occurs, a scheduling decision
may be made
• Clock interrupt
2.4 Scheduling
---Scheduling in Batch System 20
CPU A B C D
10 18 24 28
A× B× C× D×
SJF:
DCBA: ((4-0) + (10-0) + (18-0) + (28-0) )/4 = 60/4 = 15
CPU D C B A
4 10 18 28
D× C B× A×
2.4 Scheduling
---Scheduling in Batch System 24
SRTN
ABCDCAE: ((11-0) + (3-1) + (7-3) + (5-4) + (18-5))/5=31/5 =
6.2
CPU A B C D C A E
1 3 4 5 7 11 18
B× D× C× A× E×
A: 4 A: 4 A: 4 A: 4 A: 4 E: 7
B: 2 C: 3 C: 2 C: 2 E: 7
D:1 E: 7
2.4 Scheduling
---Scheduling in Batch System 26
A/5 B/2 C/3 D/1 E/7
Arrival time 0 1 5 7 8
FCFS---ABCDE: ((5-0) + (7-1) + (10-5) + (11-7) + (18-8))/5 =
30/5 =A6
CPU B C D E
5 7 10 11 18
A× B× C× D× E×
SJF---ABDCE: ((5-0) + (7-1) + (11-5) + (8-7) + (18-8))/5 =
28/5 = 5.6
CPU A B D C E
5 7 8 11 18
A× B×D C E×
× + (11-5)
SRTN---ABADCE: ((7-0) + (3-1) × + (8-7) + (18-8))/5
=26/5 = 5.2
CPU A B A D C E
1 3 7 8 11 18
B× A×D C E×
2.4 Scheduling
---Scheduling in Interactive System 27
• Round-Robin Scheduling
– Each process is assigned a time interval, called its quantum,
during which it is allowed to run
– If the process is still running at the end of the quantum, the
CPU is preempted and given to another process
– If the process has blocked or finished before the quantum
has elapsed, the CPU switching is done when the process
blocks
Assignments 28
• Five batch jobs (A through E, all completely CPU bound have
estimated running times of 10, 5, 2, 3, and 7 minutes. Their
arrival time is 0, 2, 3, 5, 6. For each of the following scheduling
algorithms, determine the mean process turnaround time
(Ignore process switching overhead)
– Round robin scheduling (assume that the system is
multiprogrammed, and that each job gets its fair share of
the CPU) (A quantum = 2 minute)
– FCFS (in order A, B, C, D, and E), SJF, SRTN (Assume that only
one job at a time runs, until it finishes)
Chapter 2 Processes and Threads
2.1 Processes 29
Execution time of A~E: 10, 5, 2, 3, and 7
minutes
Arrival Robin:
Round time ofquantum
A~E: 0, 2,=3,2 5,
minand 6
27:
CPU A A B B C C D D A A B B D E EA A B E EA A A A×
EE 10 17 22E 25
A
6: 15: 20: 25:
C× D× B× E×27:
CPU A B B A C B B D D E E A B D E E A AE E A EE×
A 2: A A C A 10 20 A 30
3: A 8: 17: 26:
C4: C C8: ×D E A B18:
× A×
B 5: C B D D×
6: B D E A (5+15+13+26+21)/5=80/5=
16
Chapter 2 Processes and Threads
2.1 Processes 30
Execution time of A~E: 10, 5, 2, 3, and 7 minutes
Arrival time of A~E: 0, 2, 3, 5, and 6
FCFS:
(10+13+14+15+21)/5=73/5=14.6
A B C D E
CPU
5 10 15 20 25 30
10: A×15: 17: 20: 27:
SJF: ACDBE B× C× D× E×
(10+9+10+18+21)/5=68/5=13.6
CPU A C D B E
5 10 15 20 25 30
10: A× 15: 20:B× 27:
12: D× E×
Chapter 2 Processes and Threads
2.1 Processes 31
Execution time of A~E: 10, 5, 2, 3, and 7 minutes
Arrival time of A~E: 0, 2, 3, 5, and 6
SRTN:
(2+3+10+13+27)/5=55/5=11
CPU A B C D B E A
2: 5: C×8: D×12:B× 19: 27:
A-8 5: 6: E× A×
B-5 A-8A-8
3: B-4 B-4
A-8D-3D-2
B-4 E-7
C-2
Important Content in Chapter 3
32
• Summary
• An example
– Suppose the page size is 4KB
– 16-bit address supplies virtual addresses from 0 up to 64K−1
– The virtual address space with 64KB can be divided into 16
virtual pages
– Suppose the computer has only 32 KB of physical memory
(8 page frames). So a 64-KB program cannot be loaded into
memory entirely
– A complete copy of a program’s core image, up to 64 KB,
must be present on the disk, however, so that pieces can be
brought in as needed virtual address space consists of fixed-
size units
3.3 Virtual Memory
---Paging 41
va 32780?
• An example
– Map a virtual address onto a
Byte 12 in
physical memory address
virtual page
– va/4k=n…m 8
– Use n to check page table and
get the page frame number x
– pa=4k*x+m
• va 0 – pa 8192
• va 8192 – pa 24K=24576
• va 20500 – pa
12K+20=12308 The relation between virtual pages
• va 4300? and page frames is given by the
3.3 Virtual Memory
---Paging 42
• Page fault
– Trap to the operating system when a running
program access a virtual page which is not loaded
into main memory
– The operating system picks a page frame (Page
replacement algorithms) and writes its contents
back to the disk
– It then fetches (also from the disk) the page that
was just referenced into the page frame just freed,
changes the map, and restarts the trapped
instruction
3.3 Virtual Memory
---Paging 43
C
FIFO D
Second-chance C
LRU B
3.4 Page Replacement
Algorithms 52
---Summary of PRAs
OPT
F=6
54
Page reference order: 2 3 2 1 5 2 4 5 3 2
52
Page frames: 3, all empty
Local page replacement
OPT: F=6
FIFO
F=9
55
Page reference order: 2 3 2 1 5 2 4 5 3 2
52
Page frames: 3, all empty
Local page replacement
Page sequence: 0 3 0 1 5 2 4 5 0 3 0 5
FIFO 0 3 3 1 5 2 4 4 0 3 3 5
0 0 3 1 5 2 2 4 0 0 3
0 3 1 5 5 2 4 4 0
0 3 1 1 5 2 2 4
Page fault: * * * * 0 3 1 5 2
F=9;R=9/12=75%
Chapter 3 Memory Management
2.1 Processes 59
Page sequence: 0 3 0 1 5 2 4 5 0 3 0 5
LRU 0 3 3 1 5 5 5 5 5 5 5 5
0 0 3 1 1 1 1 0 0 0 0
0 3 2 2 2 2 3 3 3
0 0 4 4 4 4 4 4
Page fault: * * * * 3 0 1 2
F=8;R=8/12=67%
Important Content in Chapter 4
• Summary 60
• Directories
– To keep track of files, file systems normally have
directories or folders
– A directory is a special file, which content is the
information about contained files
– In this section we will discuss directories, their
organization, their properties, and the operations
that can be performed on them
4.2 Directories
---3. Path Names 64
• When the file system is organized as a directory tree,
two ways are used for specifying a file name
– An absolute path name
• Consisting of the path from the root directory to the file
– E.g. /usr/ast/
• Unique and always start at the root directory (start with
a separator)
– Windows: \usr\ast\mailbox
– UNIX: /usr/ast/mailbox
– MULTICS: >usr>ast>mailbox
– A relative path name
4.2 Directories
---3. Path Names 65
• When the file system is organized as a directory tree,
two ways are used for specifying a file name
– A relative path name
• Used in conjunction with the concept of the working
directory (also called the current directory)
• A user can designate one directory as the current
working director
– All path names not beginning at the root directory
are taken relative to the working directory
• E.g. if the current working directory is /usr/ast
– cp /usr/ast/mailbox /usr/ast/mailbox.bak
– cp mailbox mailbox.bak is the same
4.2 Directories
---3. Path Names 66
• Usage of Different Path Name Methods
– The absolute path name will always work
– Some programs need to access a specific file without
regard to what the working directory is
• E.g. a spelling checker might need to read
/usr/lib/dictionary to do its work
– It should use the full, absolute path name in this case
because it does not know what the working directory
will be when it is called
• If the checker needs to read a large number of files from
/usr/lib, a system call can be issued to change the working
directory to /usr/lib. Then relative paths can be used
4.2 Directories
---3. Path Names 67
• Directory Tree
– Most operating systems that support a hierarchical
directory system have two special entries in every
directory, ‘‘.’’ and ‘‘..’’
• ‘‘.’’ refers to the current directory
• ‘‘..’’ refers to its parent
– Use .. to go higher up the tree
4.2 Directories
---3. Path Names 68
• Directory Tree
– E.g. A certain process has
/usr/ast as its working
directory. If it copies the file
/usr/lib/dictionary to its own
directory, following
commands can be used:
• cp ../lib/dictionary .
• cp /usr/lib/dictionary .
• cp /usr/lib/dictionary
dictionary
• cp /usr/lib/dictionary
/usr/ast/dictionary
4.3 File System Implementation
---2. Implementing Files 69
– Linked-List Allocation
– i-Nodes
4.3 File System Implementation
---Implementing Files 70
• Linked-List Allocation Using a Table in Memory
– Keep each file as a linked list of disk blocks
– Put the pointer word to next block in a table in memory
(called FAT)
terminated with a
special marker
(e.g., −1) that is
not a valid block
number
4.3 File System Implementation
---Implementing Files 71
• Linked-List Allocation Using a Table in Memory
– Take the pointer word from each disk block and put it in a
table in memory. This table is called a FAT (File-Allocation
Table)
– Advantages
• The entire block is available for data
• Random access is much easier
• The directory keeps a single integer (the starting block
number) to locate all the blocks
– Disadvantages
• The entire table must be in memory all the times
– FAT is used in MS-DOS and supported by Windows
4.3 File System Implementation
---2. Implementing Files
• i-nodes
– Each file is
associated with
an i-node (index-
node)
– The i-node lists
the attributes
and disk
addresses of the
file’s blocks
4.3 File System Implementation
---2. Implementing Files
• i-nodes
– Each file is associated with an i-node which lists the
attributes and disk addresses of the file’s blocks
– Advantages
• The i-node need to be in memory only when the
corresponding file is open
– If each i-node occupies n bytes and a maximum of k
files may be open at once, only kn bytes need be
reserved in advance
• The array of i-nodes is usually far smaller than FAT’s
– The FAT table for holding the linked list of all disk
blocks is proportional in size to the disk itself
4.3 File System Implementation
---3. Implementing Directories
• How to Locate File Data
– When a file is opened, the operating system uses the path
name supplied by the user to locate the directory entry on the
disk
– The main function of the directory system is to map the ASCII
name of the file onto the information needed to locate the
data
– The directory entry provides the information needed to find
the disk blocks
• The number of the first block and size(Contiguous allocation)
• The number of the first block (Both linked-list schemes)
• Or the number of the i-node
4.3 File System Implementation
---3. Implementing Directories
• Attributes Storage
– Every file system maintains various file attribute
– One solution is to store them directly in the directory entry
• A directory consists of a list of fixed-size entries, one per
file, containing a file name, a structure of the file
attributes, and one or more disk addresses telling where
the disk blocks are
• Character devices
– A character device delivers or accepts a stream of characters,
without regard to any block structure
– It is not addressable and does not have any seek operation
– Printers, network interfaces, mice (for pointing), and most
other devices that are not disk-like can be seen as character
devices
• Some devices are neither block nor character devices
– Clock: Cause interrupts at well-defined intervals
5.1 Principles of I/O Hardware
---Device Controllers 83
P1 P1
P2 P2
P3 P3
6.4 Deadlock Detection and Recovery
---Deadlock Detection with Multiple Resource of
99
Each Type
• An example to apply the deadlock detection algorithm
– Look for a process whose resource request can be satisfied
• A=(2100)+(0120)=(2220) (P3 returns all its resources)
• P2: (1010) ≤ A, so P2 runs and eventually returns all its
resources (A=4221) and P1 can runs
(2 2 2 0)
P1 P1
P2 P2
P3 P3
6.4 Deadlock Detection and Recovery
10
---Deadlock Detection with Multiple Resource of
0
Each Type
• Another example to apply the deadlock detection algorithm
– Look for a process whose resource request can be satisfied
• No process can be satisfied
P1 P1
P2 P2
P3 2 1 0 1 P3
6.5 Deadlock Avoidance 10
---The Banker’s Algorithm for Multiple 1
Resources
• The algorithm for checking to see if a state is safe is stated as
follows (find a safe sequence for all processes):
– Step 1: Look for a row, R, whose unmet resource needs are
all smaller than or equal to A. If no such row exists, the
system will eventually deadlock since no process can run to
completion (assuming processes keep all resources until
they exit)
• If several processes are eligible to be chosen, it does not
matter which one is selected
– Step 2: Assume the process of the chosen row requests all
the resources it needs (which is guaranteed to be possible)
and finishes. Mark that process as terminated and add all of
its resources to the A vector
6.5 Deadlock Avoidance 10
---The Banker’s Algorithm for Multiple 2
Resources
• The algorithm is stated as follows:
– Step 3: Repeat steps 1 and 2 until either all processes are
marked terminated (in which case the initial state was safe)
or no process is left whose resource needs can be met (in
which case the system was not safe)
– E.g. Check to see if this state is safe: Yes! DA/E….
6.5 Deadlock Avoidance 10
---The Banker’s Algorithm for Multiple 3
Resources
– E.g., suppose B makes a request for one printer
• Check to see if granting this request leads to a safe state
– A=(1010), DA/E…, it’s safe
– The request is granted
0 1 1 0 0 1 0 2
(1010)
6.5 Deadlock Avoidance 10
---The Banker’s Algorithm for Multiple 4
Resources
– E.g., suppose E wants the last printer after B gets one
• Check to see if granting this request leads to a safe state
– A=(1000), no process can finish, unsafe
– The request is deferred for a while
0 1 1 0 0 1 0 2
(1000)
0 0 1 0 2 1 0 0
10
Exercises 5
• Consider the following state of a system with four processes, P1,
P2, P3, andP4, and five types of resources, RS1, RS2, RS3, RS4,
andRS5. Using the deadlock detection algorithm described in
Section 6.4.2, show that there is no deadlock in the system
– E = (24144), A = (01021) P2 (02031)
– C=01112 R= 1 1 0 2 1
P3 (12032)
01010 01021
P1 (13144)
10001 02031
11000 02110 P4 (24144)
• Suppose that P1 requests (0 0 0 0 1) A=(01020), no process
can be satisfied
– Is the state still safe? (Is there any safe sequence?) No
– Should this request be granted? No
QUESTIONS?