Beruflich Dokumente
Kultur Dokumente
1
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.
Dispatcher
Dispatcher module gives control of the CPU to the process selected by the short-term scheduler; this
involves:
switching context
switching to user mode
jumping to the proper location in the user program to restart that program
Dispatch latency – time it takes for the dispatcher to stop one process and start another running.
3.3 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 wiating 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)
2
Optimization Criteria
Max CPU utilization
Max throughput
Min turnaround time
Min waiting time
Min response time
3.4 Scheduling Algorithms
The various scheduling algorithms are :
• First-Come, First-Served(FCFS) Scheduling
• Shortest-Job First Scheduling
• Priority Scheduling
• Round-Robin Scheduling
P P P
1 2 3
2 2 3
0
Waiting time for P1 = 0; P2 = 24; P3 = 27 4 7 0
Average waiting time: (0 + 24 + 27)/3 = 17
FCFS Scheduling (Cont.)
Suppose that the processes arrive in the order
P2 , P3 , P1 .
The Gantt chart for the schedule is:
P P P
2 3 1
3
0 3 6
0
3
Waiting time for P1 = 6; P2 = 0; P3 = 3
Average waiting time: (6 + 0 + 3)/3 = 3
Much better than previous case.
Convoy effect short process behind long process
Shortest-Job-First (SJR) Scheduling
Associate with each process the length of its next CPU burst. Use these lengths to schedule the process
with the shortest time.
Two schemes:
nonpreemptive – once CPU given to the process it cannot be preempted until completes its CPU
burst.
Preemptive – if a new process arrives with CPU burst length less than remaining time of current
executing process, preempt. This scheme is know as the
Shortest-Remaining-Time-First (SRTF).
SJF is optimal – gives minimum average waiting time for a given set of processes.
SJF (preemptive)
4
P P P P P P
1 2 3 2 4 1
1 1
0 2 4 5 7 1 6
Average waiting time = (9 + 1 + 0 +2)/4 - 3
Priority Scheduling
A priority number (integer) is associated with each process
The CPU is allocated to the process with the highest priority (smallest integer ≡ highest priority).
Preemptive
nonpreemptive
SJF is a priority scheduling where priority is the predicted next CPU burst time.
Problem ≡ Starvation – low priority processes may never execute.
Solution ≡ Aging – as time progresses increase the priority of the process.
Round Robin (RR)
Each process gets a small unit of CPU time (time quantum), usually 10-100 milliseconds. After this
time has elapsed, the process is preempted and added to the end of the ready queue.
If there are n processes in the ready queue and the time quantum is q, then each process gets 1/n of the
CPU time in chunks of at most q time units at once. No process waits more than (n-1)q time units.
Performance
q large ⇒ FIFO
q small ⇒ q must be large with respect to context switch, otherwise overhead is too high.
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
2 3 5 7 9 11 12 13 15
0
0 7 7 7 7 7 1 4 4
Typically, higher average turnaround than SJF, but better response.
5
How a Smaller Time Quantum Increases Context Switches
6
foreground (interactive)
background (batch)
Each queue has its own scheduling algorithm,
foreground – RR
background – FCFS
Scheduling must be done between the queues.
Fixed priority scheduling; i.e., serve all from foreground then from background. Possibility of
starvation.
Time slice – each queue gets a certain amount of CPU time which it can schedule amongst its
processes; i.e.,
80% to foreground in RR
20% to background in FCFS
7
A process can move between the various queues; aging can be implemented this way.
Multilevel-feedback-queue scheduler defined by the following parameters:
number of queues
scheduling algorithms for each queue
method used to determine when to upgrade a process
method used to determine when to demote a process
method used to determine which queue a process will enter when that process needs service
8
Homogeneous processors within a multiprocessor.
Load sharing
Symmetric Multiprocessing (SMP) – each processor makes its own scheduling decisions.
Asymmetric multiprocessing – only one processor accesses the system data structures, alleviating the
need for data sharing.
3.7 Real-Time Scheduling
Hard real-time systems – required to complete a critical task within a guaranteed amount of time.
Soft real-time computing – requires that critical processes receive priority over less fortunate ones.
Dispatch Latency
Thread Scheduling
Local Scheduling – How the threads library decides which thread to put onto an available LWP.
9
Global Scheduling – How the kernel decides which kernel thread to run next.
Solaris 2 Scheduling
Time-Slicing
Since the JVM Doesn’t Ensure Time-Slicing, the yield() Method May Be Used:
while (true) {
// perform CPU-intensive task
...
Thread.yield();
}
10
Deterministic modeling – takes a particular predetermined workload and defines the performance of
each algorithm for that workload.
Queuing models
Implementation
11
from the fastest registers, CPU cache, random access memory and disk storage. An operating system's
memory manager coordinates the use of these various types of memory by tracking which one is
available, which is to be allocated or deallocated and how to move data between them. This activity,
usually referred to as virtual memory management, increases the amount of memory available for each
process by making the disk storage seem like main memory. There is a speed penalty associated with
using disks or other slower storage as memory – if running processes require significantly more RAM
than is available, the system may start thrashing. This can happen either because one process requires a
large amount of RAM or because two or more processes compete for a larger amount of memory than is
available. This then leads to constant transfer of each process's data to slower storage.
Another important part of memory management is managing virtual addresses. If multiple
processes are in memory at once, they must be prevented from interfering with each other's memory
(unless there is an explicit request to utilise shared memory). This is achieved by having separate
address spaces. Each process sees the whole virtual address space, typically from address 0 up to the
maximum size of virtual memory, as uniquely assigned to it. The operating system maintains a page
table that match virtual addresses to physical addresses. These memory allocations are tracked so that
when a process terminates, all memory used by that process can be made available for other processes.
The operating system can also write inactive memory pages to secondary storage. This process is called
"paging" or "swapping" – the terminology varies between operating systems
3.10 BackGround
. Translation tables are implemented in hardware, but they are controlled by software. This handout
steps through various translation schemes, starting from the simplest.
Translation
Virtual tables Physical
addresses addresses
Base-and-Bound Translation
+ Physical address
Virtual address
> Error
12
Bound
Each process has the illusion that it is running on its own dedicated machine, with memory starting from
0 and going up to the size equal to bound. Like linker-loader, a process gets contiguous regions of
memory. Unlike linker-loader, a process can only touch locations in physical memory between base and
base + bound.
Virtual Physical
addresses addresses
0
code
data
base = 6250
stack code
data
bound
stack
6250 + bound
The translation step provides a level of indirection. An operating system can now move a process
around by copying bits and changing the base and bound registers.
The main advantages of the base-and-bound translation are its simplicity and speed. However, there are
a number of drawbacks.
1. It is difficult to share programs. Each instance of a program needs to have a copy of the code
segment.
2. Memory allocation is complex. If a process needs to allocate an address space, the process
needs to find a contiguous chunk of memory that is big enough. In the worst case, the
memory manager has to shuffle large chunks of memory to fit a new process.
3. Base-and-bound translation does not work so well if address spaces need to grow and shrink
dynamically.
Swapping
13
In computer operating systems that have their main memory divided into pages, paging
(sometimes called swapping) is a transfer of pages between main memory and an auxiliary store, such
as hard disk drive.[1] Paging is an important part of virtual memory implementation in most
contemporary general-purpose operating systems, allowing to easily use disk storage for data that do not
fit into physical RAM. Paging is usually implemented as a task built into the kernel of the operating
system.
A swap file is an ordinary file; it is in no way special to the kernel. The only thing that matters to
the kernel is that it has no holes, and that it is prepared for use with mkswap. It must reside on a local
disk, however; it can't reside in a filesystem that has been mounted over NFS due to implementation
reasons.
The bit about holes is important. The swap file reserves the disk space so that the kernel can
quickly swap out a page without having to go through all the things that are necessary when allocating a
disk sector to a file. The kernel merely uses any sectors that have already been allocated to the file.
Because a hole in a file means that there are no disk sectors allocated (for that place in the file), it is not
good for the kernel to try to use them.
One good way to create the swap file without holes is through the following command:
where /extra-swap is the name of the swap file and the size of is given after the count=. It is best for
the size to be a multiple of 4, because the kernel writes out memory pages, which are 4 kilobytes in size.
If the size is not a multiple of 4, the last couple of kilobytes may be unused.
A swap partition is also not special in any way. You create it just like any other partition; the only
difference is that it is used as a raw partition, that is, it will not contain any filesystem at all. It is a good
idea to mark swap partitions as type 82 (Linux swap); this will the make partition listings clearer, even
though it is not strictly necessary to the kernel.
After you have created a swap file or a swap partition, you need to write a signature to its beginning;
this contains some administrative information and is used by the kernel. The command to do this is
mkswap, used like this:
Note that the swap space is still not in use yet: it exists, but the kernel does not use it to provide virtual
memory.
You should be very careful when using mkswap, since it does not check that the file or partition isn't
used for anything else. You can easily overwrite important files and partitions with mkswap!
Fortunately, you should only need to use mkswap when you install your system.
14
The Linux memory manager limits the size of each swap space to about 127 MB (for various technical
reasons, the actual limit is (4096-10) * 8 * 4096 = 133890048$ bytes, or 127.6875 megabytes). You can,
however, use up to 8 swap spaces simultaneously, for a total of almost 1 GB. [1]
This is actually no longer true, this section is slated for a rewrite Real Soon Now (tm). With newer
kernels and versions of the mkswap command the actual limit depends on architecture. For i386 and
compatibles it is 2Gigabytes, other architectures vary. Consult the mkswap(8) manual page for more
details.
For contiguous allocation, file blocks are stored contiguously on disk. A user specifies the file size in
advance, and the file system will search the disk allocation bitmap according to various allocation
policies (e.g., first-fit and best-fit policies) to locate the space for the file. The file header contains the
first block of the file on disk, and the number of blocks in the file.
Contiguous allocation provides fast sequential access. A random disk location in a file can be trivially
computed by adding an offset to the first disk block location of the file. The disadvantages are external
fragmentation and difficulties to grow files.
Linked-List Allocation
For linked-list allocation, each file block on disk is associated with a pointer to the next block. Perhaps,
the most popular example is the MS-DOS file system, which uses the file attribute table (FAT) to
implement linked-list files. A file header points to the table entry of the first file block, and the content
of the file block contains the table entry number of the next block. A special marker is used to indicate
the end of the file.
One advantage of the linked-list approach is that files can grow dynamically with incremental allocation
of blocks. However, sequential accesses may suffer since blocks may not be contiguous. Random
accesses are horrible and involve multiple sequential searches. Finally, linked-list allocation is
unreliable, since a missing pointer can lead to loss of the remaining file.
Segment-Based Allocation
Segment-based allocation uses a segment table to allocate multiple regions of contiguous blocks. The
file header points to a table of begin-block and end-block pairs.
15
File header Begin block End block
Segment-based allocation provides the flexibility to break the allocation into a number of contiguous
disk regions, while it still permits contiguous allocation to reduce disk seek time. However, as the disk
becomes increasingly fragmented, in the extreme Datacase,
blocks
segment-based allocation needs a bigger and
bigger table to locate piece-wise contiguous blocks. As an extreme case, segment-based allocation can
potentially need one table entry per disk block. In addition, under this scheme, random accesses are not
as fast as the contiguous allocation, since the file system needs to locate the pair of begin block and end
block that contains the target byte before making the disk accesses.
Indexed Allocation
Indexed allocation uses an index to directly track the file block locations. A user declares the
maximum file size, and the file system allocates a file header with an array of pointers big enough to
point to all file blocks.
Although indexed allocation provides fast disk location lookups for random accesses, file blocks may be
scattered all over the disk. A file system needs to provide additional mechanisms to ensure that disk
blocks are grouped together for good performance (e.g., disk defragmentor). Also, as a file increases in
size, the file system needs to reallocate the index array and copy old entries. Ideally, the index can grow
incrementally.
File header
Block 0
Data blocks
Block 1
Block 2
3.13 Paging
Virtual address
Physical address
The following is an example of a page table with a page size of 4 Kbytes.
Virtual Physical
addresses addresses
0x0
0x0
0x1000 0x1000
0x2000 0x2000
0x3000 0x3000
0x3fff 0x4000
0x5000
Although it allows code sharing and easier memory allocation, paging has its own drawbacks. First, if a
process sparsely uses its address space, the size of the page table is prohibitive, in particular, if a process
has the starting virtual address of 0 for the code and 231 – 1 for stack (assuming a 32-bit architecture).
With 1-Kbyte pages, a process will need 2 million table entries. Second, if the page size is too big,
paging suffers from internal fragmentation, where allocated pages are not fully used. On the other
hand, the base-and-bound approach suffers from external fragmentation, where memory is wasted
because the available memory is not contiguous for allocation.
17
3.14 Segmentation
A segment is a region of logically contiguous memory, and the idea of segmentation-based translation
is to generalize the base-and-bound approach by allowing a table of base-and-bound pairs.
+ Physical address
Virtual Physical
addresses addresses
0x0
0x0
0x6ff 0x4ff
0x1000
0x14ff
0x2000
0x3000 0x2fff
0x3fff 0x4000
Each segment gets mapped to a contiguous location in physical memory, but there may be gaps between
segments. These gaps allow heap and stack segments of a process 0x46ffto grow by changing the segment
bound in the table entry. Also, by adding a protection mode to each segment, we can have a finer
control of segment accesses. For example, the code segment should be set to read-only (only execution
and loads are allowed). Data and stack segments are set to read-write (stores allowed).
18
Compare to the base-and-bound approach, segmentation translation is more efficient for processes that
do not use the entire address space. Also, segmentation allows multiple instances (processes) of a
program to share the same code segment. However, segmentation still require contiguous chunks of
memory; therefore, memory allocation is still complex.
At the lowest level, memory allocation can still be done with a bitmap due to paging. Sharing can be
performed at either the segment or the page level.
However, segmented paging also has a few drawbacks: (1) this approach still requires a certain
overhead for storing additional pointers; (2) page tables still need to be contiguous; and (3) each
memory reference now takes two or more memory table lookups.
To further reduce the overhead of contiguous page tables, another solution uses paged page tables, or a
two-level tree of page tables. This approach reduces unwanted allocation of page table entries and can
be generalized into multi-level paging.
Multiple levels of page tables also mean multiple memory references before getting to the actual data.
One way to speed up the lookup is to use translation lookaside buffers (TLBs), which stores recent
translated memory addresses for short-term reuses.
A simple and power approach to speed up lookups is to use a big hash table (inverted page table), where
each virtual page number is hashed to a physical page number. The size of the table is independent of
the size of address spaces, and proportional to the number of pages being used. However, managing
hash collision can be complex and inefficient.
19