Download Computer-system operation (The OS initialization phase)

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Computer-system operation
(The OS initialization phase)
ROM
1
2
RAM
3
Interrupt Handling
1. Save the CPU status (including the CPU status
register, program counter and some other
registers)
2. Interrupt handling depends on the type of
interrupt
– polling
– vectored interrupt system
– vectored interrupt system + polling
3. Execute the interrupt service routine.
Direct Memory Access Structure
(DMA)
bus
Dual-mode operation
applications
User
mode
AP2
trap
Kernel
mode
ISR
Syscall_handler
OS
CPU
1. Mode=kernel_mode
Push PC
1. Save machine status
2. Jump_to ISR
Exception
:= Trap
:= Software interrupt
System Call Parameter Passing
• Often, more information is required than
simply identity of desired system call
• Three general methods used to pass
parameters to the OS
– Simplest: pass the parameters in registers
– Parameters stored in a block, or table, in memory,
and address of block passed as a parameter in a
register
– Parameters placed, or pushed, onto the stack by
the program and popped off the stack by the
operating system
Operating System Design and Implementation
• Design and Implementation of OS not “solvable”, but
some approaches have proven successful
• Internal structure of different Operating Systems can
vary widely
• Start by defining goals and specifications
• Affected by choice of hardware, type of system
• User goals and System goals
– User goals – operating system should be convenient to use,
easy to learn, reliable, safe, and fast
– System goals – operating system should be easy to design,
implement, and maintain, as well as flexible, reliable,
error-free, and efficient
Operating System Design and Implementation (Cont.)
• Important principle to separate
Policy: What will be done?
Mechanism: How to do it?
• Mechanisms determine how to do something,
policies decide what will be done
• The separation of policy from mechanism is a
very important principle, it allows maximum
flexibility if policy decisions are to be changed
later
Layered Operating System
Modules
• Most modern operating systems implement
kernel modules
– Uses object-oriented approach
– Each core component is separate
– Each talks to the others over known interfaces
– Each is loadable as needed within the kernel
• Overall, similar to layers but with more flexible
Solaris Modular Approach
• Modules & micro kernel
OS kernel
stack
c d e
heap
(int*)malloc(sizeof(int)*1024);
uninitialized data
b
initialized data
a
text
(code)
int a = 2;
int b;
void main() {
int c;
int d;
int* e =
/*…*/
}
Process in Memory
MAX
OS kernel
Kernel space
stack
initialized data
heap
text
(code)
uninitialized
data
header
initialized data
The executable
program file
text
(code)
0
user space
Schedulers (Cont.)
• I/O-bound process – I/O time >> CPU time
– Example: ftp server
i/o
CPU
i/o
• CPU-bound process – I/O time >> CPU time
– Example: image processing
CPU
I/O
CPU
Direct Communication
• Processes must name each other explicitly:
– send (P, message) – send a message to process P
– receive(Q, message) – receive a message from
process Q
• Properties of communication link
– Links are established automatically
– A link is associated with exactly one pair of
communicating processes
– Between each pair there exists exactly one link
– The link may be unidirectional, but is usually bidirectional
Indirect Communication
• Messages are directed and received from mailboxes
(also referred to as ports)
– Each mailbox has a unique id
– Processes can communicate only if they share a mailbox
• Properties of communication link
– Link established only if processes share a common mailbox
– A link may be associated with many processes
– Each pair of processes may share several communication
links
– Link may be unidirectional or bi-directional
Motivation the overhead of using processes
• The overhead of context-switch
– + store/restore the register file (~1kB)
– + TLB miss (~1kB)
CPU
– + CPU cache miss (~1MB) (core)
Reg.
1 cycle
5 cycles
L1$
MMU (+TLB)
L2$
25 cycles
Single and Multithreaded Processes
Threading Issues
• fork() and exec()
– Does fork() duplicate only the calling thread or all
threads?
– exec() will replace the entire process including all
threads
• Cancellation
– Asynchronous cancellation
– Synchronous cancellation
Alternating Sequence of CPU And I/O
Bursts
Histogram of CPU-burst Times
CPU Scheduler
• Selects from among the processes in memory
that are ready to execute, and allocates the CPU
to one of them
• CPU scheduling decisions may take place when a
process:
1. Switches from running to waiting state
2. Switches from running to ready state
3. Switches from waiting to ready
4. Terminates
• Scheduling under 1 and 4 is nonpreemptive
• All other scheduling is preemptive
Scheduling Criteria
• CPU utilization – keep the CPU as busy as possible
• Throughput – # of processes that complete their
execution per time unit
• Turnaround time – amount of time to execute a
particular process
• Waiting time – amount of time a process has been
waiting in the ready queue
• Response time – amount of time it takes from when a
request was submitted until the first response is
produced, not output (for time-sharing environment)
Example of Preemptive SJF
Process Arrival Time
P1
0.0
P2
2.0
P3
4.0
P4
5.0
• SJF (preemptive)
P1
0
P2
2
P3
4
P2
5
Burst Time
7
4
1
4
P4
7
P1
11
• Average waiting time = (9 + 1 + 0 +2)/4 = 3
16
• SJF improves I/O performance!?
Determining Length of Next CPU Burst
• Can be done by using the length of previous
CPU bursts, using exponential averaging
1. t n  actual length of n th CPU burst
2.  n 1  predicted value for the next CPU burst
3.  , 0    1
4. Define :
 n1   tn  1    n
Example of RR with
Time Quantum = 20
Process
P1
P2
P3
P4
• The Gantt chart is:
P1
0
P2
20
P3
37
P4
57
Burst Time
53
17
68
24
P1
77
P3
97
117
P4
P1
121 134
P3
P3
154 162
• Typically, higher average turnaround than SJF, but better
response
思考
• 當time quantum趨近於「無窮大」
• 當Time quantum趨近於「0」
Example of Multilevel Feedback Queue
• Three queues:
– Q0 – RR with time quantum 8 milliseconds
– Q1 – RR time quantum 16 milliseconds
– Q2 – FCFS
• Scheduling
– A new job enters queue Q0 which is served FCFS.
When it gains CPU, job receives 8 milliseconds. If it
does not finish in 8 milliseconds, job is moved to
queue Q1.
– At Q1 job is again served FCFS and receives 16
additional milliseconds. If it still does not complete, it
is preempted and moved to queue Q2.
Multilevel Feedback Queues
Race Condition
Consider this execution interleaving with “count = 5” initially
Producer
Consumer
register1 = count
R
{register1 = 5}
register1 = register1 + 1
{register1 = 6}
R
register2 = count
{register2 = 5}
register2 = register2 - 1
{register2 = 4}
count = register1
{count = 6 }
w
count = register2
{count = 4}
w
Solution to Critical-Section Problem
1. Mutual Exclusion - If a process is executing in its critical
section, then no other processes can be executing in
their critical sections
2. Progress - If no process is executing in its critical
section and there exist some processes that wish to
enter their critical section, then the one of the
processes will enter the critical section
3. Bounded Waiting - A bound must exist on the number
of times that other processes are allowed to enter their
critical sections after a process has made a request to
enter its critical section and before that request is
granted
Algorithm for Process Pi
while (true) {
flag[i] = TRUE;
turn = j;
while ( flag[j] && turn == j);
/*CRITICAL SECTION*/
flag[i] = FALSE;
/*REMAINDER SECTION*/
}
Solution using TestAndSet
• Shared boolean variable lock., initialized to
false.
while (true) {
while ( TestAndSet (&lock ))
; // do nothing
// critical section
lock = FALSE;
// remainder section
}