Sie sind auf Seite 1von 19

Unit – 3

CPU Scheduling and Memory Management


3.1 CPU Scheduling:
The management system that controls the switching of the CPU between/among processes in
such a way as to the computer system effective/productive. Also called process scheduling.
Short-term scheduler (CPU scheduler) selects next job to run.
When are scheduling decisions made?
1 - When a process temporarily releases the CPU (I/O, yield, wait, sleep).
2 - When an interrupt occurs.
3 - When a process becomes ready (& wasn’t).
4 - When a process terminates.
If the decisions are made at 2 or 3 then the system uses preemption.
Preemption can interfere with access to shared data if the preemption occurs at an inopportune
time. => coordination. The shared data may be in the kernel which leads to the need to accommodate
interrupts and non preemption within the kernel.
The dispatcher is the module that actually transfers control of the CPU to the next process. Time to do
this (context switch, etc) is called the dispatch latency.
3.2 Basic Concepts
Maximum CPU utilization obtained with multiprogramming
CPU–I/O Burst Cycle – Process execution consists of a cycle of CPU execution and I/O wait.
CPU burst distribution
Alternating Sequence of CPU And I/O Bursts

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

First-Come, First-Served (FCFS) Scheduling


In this scheme, the process that requests the CPU first is allocated the CPU first.

Example: Process Burst Time


P1 24
P2 3
P3 3
Suppose that the processes arrive in the order: P1 , P2 , P3
The Gantt Chart for the schedule is:

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.

Example of Non-Preemptive SJF


Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
SJF (non-preemptive)
P P P P
1 3 2 4
1 1
0 3 7 8
2 6
Average waiting time = (0 + 6 + 3 + 7)/4 - 4
Example of Preemptive SJF
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4

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.

Example: RR with Time Quantum = 20


Process Burst Time
P1 53
P2 17
P3 68
P4 24
The Gantt chart is:

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

Turnaround Time Varies With The Time Quantum

3.5 Multilevel Queue Scheduling


Ready queue is partitioned into separate queues:

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

Multilevel Queue Scheduling

Multilevel Feedback Queue

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

Multilevel Feedback Queues

Example of Multilevel Feedback Queue


Three queues:
Q0 – time quantum 8 milliseconds
Q1 – 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.

3.6 Multiple-Processor Scheduling


CPU scheduling more complex when multiple CPUs are available.

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();
}

This Yields Control to Another Thread of Equal Priority.

3.8 Algorithm Evaluation

10
Deterministic modeling – takes a particular predetermined workload and defines the performance of
each algorithm for that workload.
Queuing models
Implementation

Evaluation of CPU Schedulers by Simulation

3.9 Memory Management


Current computer architectures arrange the computer's memory in a hierarchical manner, starting

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

Data read or write


(untranslated)

Base-and-Bound Translation

For the base-and-bound translation, each process


Base is loaded into a contiguous region of physical
memory, but with protection among processes.

+ 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:

$ dd if=/dev/zero of=/extra-swap bs=1024


count=1024
1024+0 records in
1024+0 records out
$

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:

$ mkswap /extra-swap 1024


Setting up swapspace, size = 1044480
bytes
$

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.

Contiguous Memory Allocation


Contiguous Allocation

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.

File header 4 Entry for block 2

0 Entry for block 3

5 Entry for block 4

EOF Entry for block 5

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

Begin block End block

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

Virtual page number Offset Page table size


Paging-based translation reduces the complexity of memory allocation by having fixed-size chunks of
memory, or pages. The memory manager under paging can use a stream of 0s and 1s, or a bitmap, to
track the allocation status of memory pages. Each bit represents one page of physical memory—1
means a page is allocated; 0 means unallocated. Memory mapping is> done at the granularity of a page,
so a virtual page is mapped to a physical page of memory.
Physical page number

Physical page number

Physical page number

Physical page number Offset Error 16

Physical address
The following is an example of a page table with a page size of 4 Kbytes.

Virtual page number Physical page number


0 4
1 0
3 2

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 segment base Segment bound

Physical segment base Segment bound

Physical segment base Segment bound

+ Physical address

Virtual segment bits Offset

Virtual address >


The following is an example of a segmentation table, with 2 bits to identifyError
virtual segments, and a 12-
bit segment offset.

Virtual segment number Physical segment base Segment bound


Code 0x4000 0x700
Data 0 0x500
- 0 0
Stack 0x2000 0x1000

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.

3.15 Segmentation with paging


To handle the sparse address space allocation, segmented-paging translation can break the page table
into segments that are allocated as necessary—a significant reduction of page table size. The virtual
address is now decomposed into three components: virtual segment number, virtual page number, and
the offset.

Virtual segment number Virtual page number Offset

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.

Paged Page Tables

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.

Inverted Page Tables

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

Das könnte Ihnen auch gefallen