You are on page 1of 18

Faculty of Engineering Technology Page No.

1/6

Department of Electrical Engineering Technology Revision No. 1

Title: Lab 4: Memory Management Effective Date 01/06/2023

FACULTY OF ENGINEERING TECHNOLOGY


DEPARTMENT OF ELECTRICAL ENGINEERING TECHNOLOGY

PRACTICAL ASSIGNMENT

WORKING INSTRUCTION AND REPORT

Course Code & Name /


BNF 32303 ( OPERATING SYSTEM )
Kod & Nama Kursus
Code & Title of Experiment/
Lab 4: Memory Management
Kod & Tajuk Ujikaji
Date of Experiment/ 27/6/2023
Tarikh Ujikaji
Programme/Program BNF
Student Name/ 1. MUHAMMAD SHAHRUL RADZZI BIN AMIRUDIN
Nama Pelajar 2. MUHAMMAD LUQMAN HAKIM BIN ZAMRAN
Matric No./ 1. CN200105
No. Matrik 2. AN200147
Instructor Name / 1. Dr. Aimi Syamimi Binti Ab Ghafar
Nama Instruktor 2.
Result (30%)/
/ %
Keputusan(30%)
Data Analysis and Discussion (30%)/
/ %
Analisis Data dan Perbincangan(30%)
Question (30%)/
/ %
Soalan(30%)
Assessment / Penilaian Conclusion (5%) /
/ %
Kesimpulan(5%)
Peer Assessment (5%) /
/ %
Penilaian Rakan (5%)
/100%
TOTAL / JUMLAH
Faculty of Engineering Technology Page No. 2/6

Department of Electrical Engineering Technology Revision No. 1

Title: Lab 4: Memory Management Effective Date 01/06/2023

1. EXPERIMENT LEARNING OUTCOMES

At the end of the experiment, students should be able to:

1.1 To gain experience working with CPU-OS Simulator.


1.2 Create processes from programs in the OS simulator
1.3 Demonstrate three placement strategies used in memory management in the OS simulator.

2. INTRODUCTION / THEORY

2.1. CPU-OS Simulator

CPU-OS Simulator is a combined CPU simulator and an OS simulator. This educational software is designed
to support computer education through simulations of modern CPU and Operating System for the
learners and teachers of computer organization and architecture. This site is dedicated to this software
which is made freely available. CPU-OS Simulator is the product of many years’ development effort. It has
been successfully integrated in undergraduate computing degree programmes in support of teaching
modules in computer architecture and operating systems at Edge Hill University. It has been the subject
of research work to evaluate and establish the effectiveness of the simulations with the help of funding
from The Higher Education Academy (HEA). The simulator is a complex piece of software and is being
regularly maintained and enhanced.

2.2 OS Simulator

The OS Simulator provides multithreaded processes and supports multiple CPU simulations. It works with
the CPU Simulator and manages multiple processes and virtual memory using different process
scheduling mechanisms.
Faculty of Engineering Technology Page No. 3/6

Department of Electrical Engineering Technology Revision No. 1

Title: Lab 4: Memory Management Effective Date 01/06/2023

The OS Simulator is designed to support two main aspects of a computer system’s resource management:
process management and memory management. Once a compiled code is loaded in CPU Simulator’s
memory, its image is also available to the OS Simulator. It is then possible to create multiple instances of
the program images as separate processes. The OS Simulator displays the running processes, the ready
processes and the waiting processes. Each process is assigned a separate Process Control Block (PCB) that
contains information on the process state. This information is displayed in a separate window. The main
memory display demonstrates the dynamic nature of page allocations according to the currently selected
placement policy. The OS maintains a separate page table for each process which can also be displayed.
The simulator demonstrates how data memory is relocated and the page tables are maintained as the
pages are moved in and out of the main memory illustrating virtual memory activity.

The process scheduler includes various selectable scheduling policies that include priority-based, pre-
emptive and round-robin scheduling with variable time quanta. The OS is able to carry out context-
switching which can be visually enhanced by slowing down or suspending the progress at some key stage
to enable the students to study the states of CPU registers, stack, cache, pipeline and the PCB contents.

The simulator incorporates an input output console device, incorporating a virtual keyboard, and is used
to display text and accept input data.

The OS simulator supports dynamic library simulation which is supported by the appropriate language
constructs in the teaching language. The benefits of sharing code between multiple processes are visually
demonstrated. There is also a facility to link static libraries demonstrating the differences between the
two types of libraries and their benefits and drawbacks.

The simulator allows manual allocation and de-allocation of resources to processes. This facility is used
to create and demonstrate process deadlocks associated with resources and enables experimentation
with deadlock prevention, detection and resolution.
Faculty of Engineering Technology Page No. 4/6

Department of Electrical Engineering Technology Revision No. 1

Title: Lab 4: Memory Management Effective Date 01/06/2023

2.3 Memory Allocation Strategies

When we need to allocate memory, storing the list in segment address order allows us to implement
various strategies.

• First Fit: In the first fit approach is to allocate the first free partition or hole large enough which
can accommodate the process. It finishes after finding the first suitable free partition.
• Best Fit: The best fit deals with allocating the smallest free partition which meets the requirement
of the requesting process. This algorithm first searches the entire list of free partitions and
considers the smallest hole that is adequate. It then tries to find a hole which is close to actual
process size needed.
• Worst fit: In worst fit approach is to locate largest available free portion so that the portion left
will be big enough to be useful. It is the reverse of best fit.

3. EQUIPMENT

3.1. Personal Computer


3.2. CPU-OS Simulator

PROCEDURES

PRELIMINARIES
1) Download and install the CPU-OS Simulator in your computer (https://teach-
sim.com/downloads/). Choose the software Version 7.5.50 executable (current version).
2) When the zip file is opened, rename the extension of the file from .pkg to .exe
3) Double click on the .exe file to start the installation.

Task 1: First Fit Allocation

1) Start the CPU simulator. You now need to create some executable code so that it can be run by the
CPU under the control of the OS. In order to create this code, you need to use the compiler which
is part of the system simulator. To do this, open the compiler window by selecting the COMPILER…
button in the current window.
2) In the compiler window, enter the following simple code in the compiler source editor area (under
PROGRAM SOURCE frame title). Save the code name as ForeverLooping.
program ForeverLooping
while true %Do a forever loop
n = 1 %Does nothing useful but
wend %keeps the CPU busy
end
Faculty of Engineering Technology Page No. 5/6

Department of Electrical Engineering Technology Revision No. 1

Title: Lab 4: Memory Management Effective Date 01/06/2023

3) Now you need to compile this in order to generate the executable code. To do this, click on the
COMPILE… button. You should see the code created on the right PROGRAM CODE view.
4) This code needs to be loaded in memory so that the CPU can execute it. To do this, click on the
LOAD IN MEMORY button in the current window. You should now see the code loaded in memory
ready to be executed.
5) We are now going to use the OS simulator to run this code. To enter the OS simulator, click on the
OS… button in the current window. The OS window opens.
6) You should see an entry, titled ForeverLooping, in the PROGRAM LIST view.
7) In the OS Simulator window, select the Views tab and click on the VIEW MEMORY… button.
8) You’ll see the Main Memory window displayed.
9) Now, make sure the Frames drop‐down list is set to 1.
10) Click on the ADD button repeatedly until all memory is allocated (needs 10 clicks).
11) We will now proceed to make “holes” in the memory. Please follow the instructions below exactly
in the order listed:
Using the Frame No drop‐down list set it to 2 then click the REMOVE button to remove it.
Set the Frame No to 3 and remove it.
Set the Frame No to 5 and remove it.
Set the Frame No to 7 and remove it.
Set the Frame No to 8 and remove it.
Set the Frame No to 9 and remove it.
12) You should now have only frames 0, 1, 4 and 6 allocated with gaps of free memory spaces of
various sizes between them.
13) Now, set the Placement Policy drop‐down list to First Fit.
14) Load the program ForeverLooping if it’s not already loaded. Create a FIVE (5) copy of the processes.
15) Observe which gap in Main Memory its memory is allocated. Screenshot the results.
16) Now remove the process by clicking on the REMOVE button in the READY PROCESSES area (Note:
Do not remove the program, just the process).
17) Next, set the Placement Policy drop‐down list to Best Fit.
18) Repeat step (14).
19) Observe which gap in Main Memory its memory is allocated. Screenshot the results.
20) Once again remove this process.
21) Finally, set the Placement Policy drop‐down list to Worst Fit.
22) Repeat step (14).
23) Observe which gap in Main Memory its memory is allocated. Screenshot the results.
Faculty of Engineering Technology Page No. 6/6

Department of Electrical Engineering Technology Revision No. 1

Title: Lab 4: Memory Management Effective Date 01/06/2023

6. RESULTS AND DISCUSSION (60 Marks)

Screenshot the proof that you have done the instruction above, explain and discuss what have been done
and observed under each of the screenshots.

Figure 1: 10 memory
Figure 2: Frame no set to 2 and remove
Figure 3: Frame no set to 3 and remove
Figure 4: Frame no set to 5 and remove
Figure 5: Frame no set to 7 and remove
Figure 6: Frame no set to 8 and remove
Figure 7: Frame no set to 9 and remove
Figure 8: Main memory gap filled in first fit
Figure 9: Main memory gap filled in best fit
Figure 10: Main memory filled in worst fit

• QUESTION (30 Marks)

[1] How does the First Fit memory allocation algorithm handle situations where multiple
blocks ofsufficient size are available?

The First Fit memory allocation algorithm assigns memory blocks to incoming processes or data
based on the first available block that is large enough to accommodate the requested size. When
multiple blocks of sufficient size are available, the First Fit algorithm typically selects the first one it
encounters during the allocation process.

Here's how the First Fit algorithm handles situations where multiple blocks of sufficient size are
available:
• The algorithm starts searching for a suitable block from the beginning of the memory space.

• When it comes across the first block that has enough space to satisfy the allocation request, it assigns
that block to the process or data.

• The allocated block's size is reduced by the requested amount, and the remaining unused portion (if any)
becomes a new block.

• The algorithm doesn't continue searching for alternative blocks once it finds a suitable one. This means
that if there are subsequent blocks with sufficient size, they are skipped, and the first encountered block
is allocated.

• The process or data is placed in the allocated block, and any metadata or bookkeeping related to that
block is updated accordingly.

[1] Explain how the Best Fit memory allocation algorithm optimizes memory utilization
compared to the First Fit algorithm.

The Best Fit memory allocation algorithm is a dynamic memory allocation technique used in
computer systems to assign memory blocks to incoming processes. It aims to optimize memory
utilization by finding the smallest free block that is still large enough to accommodate the incoming
process.

In contrast, the First Fit memory allocation algorithm assigns the first available memory block that
is large enough to hold the incoming process. It searches the memory from the beginning and
allocates the first suitable block it encounters.

The key difference between the Best Fit and First Fit algorithms lies in their approach to selecting
the appropriate memory block. The Best Fit algorithm scans the entire free memory space and
selects the block that is the closest in size to the process being allocated. It evaluates all available
blocks and picks the one that minimizes internal fragmentation, which is the wastage of memory
within a block due to the allocation of smaller processes.

By selecting the best-fitting block, the Best Fit algorithm optimizes memory utilization because it
reduces both external and internal fragmentation. External fragmentation occurs when there are
enough free memory blocks available, but they are scattered throughout the memory space, making
it difficult to allocate larger processes. The Best Fit algorithm reduces this fragmentation by selecting
smaller, more suitable blocks, potentially making larger blocks available for future allocations.

On the other hand, the First Fit algorithm may lead to higher fragmentation because it allocates the
first suitable block encountered, without considering if there might be a better fit later in the
memory space. This can result in larger blocks becoming unavailable due to smaller processes being
allocated at the beginning of the memory.

the Best Fit algorithm tends to provide better memory utilization compared to the First Fit algorithm
because it aims to minimize both internal and external fragmentation by selecting the smallest block
that can accommodate the incoming process. However, it is worth noting that the Best Fit algorithm
typically requires more time to execute compared to the First Fit algorithm due to the need to scan
the entire memory space for the best fit.
[2] What challenges can arise with the Worst Fit memory allocation algorithm in a dynamic
memoryallocation scenario?

The Worst Fit memory allocation algorithm can encounter several challenges in a dynamic
memory allocation scenario. Here are some of the potential issues:

• Fragmentation: The Worst Fit algorithm tends to allocate the largest available block of
memory to a process. This approach can lead to fragmentation, both external and internal.
External fragmentation occurs when there are small free blocks of memory scattered
throughout the system, making it difficult to allocate larger contiguous blocks. Internal
fragmentation happens when allocated memory blocks are larger than what the process
needs, wasting memory space.

• Inefficient space utilization: Due to its focus on allocating the largest available block, the
Worst Fit algorithm may result in inefficient space utilization. If smaller blocks are ignored
in favor of larger ones, there might be instances where a small process cannot find an
appropriate block, even if the total free memory is sufficient. This inefficiency can limit the
number of concurrent processes that can be accommodated.

• Increased search time: The Worst Fit algorithm requires scanning the entire memory space
to find the largest available block. This search process can be time-consuming, especially in
scenarios with many memory blocks. As a result, the allocation time may increase,
impacting system performance.

• Limited support for small processes: Since the Worst Fit algorithm prioritizes larger blocks,
it may struggle to allocate memory for smaller processes. If the system frequently
encounters small process requests, it can lead to a situation where memory utilization is
low, and many small blocks remain unallocated, causing wastage.

• Difficulty in memory compaction: Worst Fit can make memory compaction more
challenging. Compaction involves relocating allocated blocks to create a larger free block.
However, since Worst Fit scatters small blocks throughout the memory, it becomes harder
to perform compaction efficiently, resulting in higher overheads.

• Increased external fragmentation over time: As the system operates and processes are
allocated and deallocated, the Worst Fit algorithm can contribute to increased external
fragmentation. The allocation of large blocks can leave smaller free spaces between them,
which may not be usable for subsequent processes, leading to further fragmentation.

[3] Which memory allocation algorithm is more suitable for reducing external fragmentation or
wastedmemory space?

There are several memory allocation algorithms that can help reduce external fragmentation and
optimize memory usage. Two commonly used algorithms are the Best Fit and the Worst Fit
algorithms.

• Best Fit Algorithm: The Best Fit algorithm searches for the smallest free partition that can
accommodate a process. By selecting the best-fitting partition, this algorithm aims to
minimize the amount of wasted memory space. It reduces external fragmentation by
allocating the smallest possible partition that can satisfy a process's memory requirements.

• Worst Fit Algorithm: The Worst Fit algorithm, on the other hand, selects the largest free
partition available for a process. By allocating the largest partition, this algorithm tries to
maximize the remaining free memory space, reducing the likelihood of future
fragmentation. It focuses on reducing external fragmentation by keeping larger blocks
available for larger processes.

Between these two algorithms, the Best Fit algorithm is generally considered more suitable for
reducing external fragmentation or wasted memory space. By allocating the smallest possible
partition that satisfies a process's requirements, it helps to minimize the fragmentation caused by
small unused gaps between allocated partitions. This can lead to more efficient memory utilization
overall.

8. CONCLUSION (5 Marks)

Please provide your conclusion.


inally, the OS simulator successfully illustrated three memory allocation techniques that are often
used in memory management. The first method, referred to as first fit, involves finding the first
available block of memory that is big enough to fit a process before allocating memory to it. This
strategy instantly allots memory to processes, making it both easy and speedy. Over time, however,
it can cause fragmentation since there might be smaller intervals between assigned blocks.

The second method, referred to as best-fit, picks the smallest available block of memory that can fit
a process in order to reduce fragmentation. Although it effectively reduces fragmentation, it adds
extra cost because it needs to explore the whole memory to select the best block. The best-fit
strategy can thus be slower than the first-fit strategy.

The third method, referred to as worst-fit, allots memory by looking for the biggest free block and
allocating the process to it. However, because it frequently leads to severe fragmentation, this
method is typically less utilized than first-fit and best-fit. Since the worst-fit technique searches the
whole memory space for the biggest block, it is slower than the other two options.

In reality, the system's unique needs and the trade-offs between performance, fragmentation, and
memory utilization determine the memory placement technique to be used. System designers must
carefully take into account elements including the anticipated workload, the size distribution of
processes, and the memory resources available. One may extensively assess these memory
placement techniques' performance under various circumstances by modelling them in an OS
simulator, and then, based on the needs of the system, decide which approach to use in a real-
world operating system.
Verified by : Verified by :
Signature / Tandatangan : Aimi Syamimi Signature / Tandatangan : Lam
Name / Nama : Dr. Aimi Syamimi Ab Ghafar Name / Nama : Ts. Dr. Lam Hong Yin
Date / Tarikh : 25 May 2023 Date / Tarikh : 1 Jun 2023

You might also like