Memory Management Simulator

By Enes Kemal Ergin

Operating System Assignment 1

11/02/2015

How to develop a memory Management Simulator?

Task Given:

In general the task is to implement Fixed Memory Partition Memory Management system using C or Java, buttttt since I like Python and more comfortable about using Python. I will go ahead and implement it using Python.

The goal of the simulation is to generate a set of 1000 jobs in different sizes (The size of the job may be between 10K and 90K.) and observe how the memory manager handles them.Our program will use a random number generator to determine the size of job.

Here is our Fixed Memory Manager's table:

Functions

We use functions to break a problem into smaller sub-problems. This reduces program complexity. In this assignment you are supposed to implement 3 distinct functions.

  • main
  • jobs
  • best fit

The main function will be the driver of the program. You should create the loop that controls the time of the simulation. Each iteration of the loop is considered as 1 second.

The function jobs will generate an array of 1000 integers. This array will represent the queue for the jobs. Each job will stay in the memory based on the following integer division

jobProcecessingTime = jobSize/10 (e.g., jobSize=16K, jobProcecessingTime =16/10=1 second)

Sample Output

The program should display:

  • the total time, time , passed for processing all the jobs in the jobQueue ,
  • the number of jobs rejected, numberOfRejectedJobs
  • usage statistics of the partitions, partitionUsage

bestFit(number) Function


In [1]:
def bestFit(number):                       # Function Header
    """
    bestFit function takes a number from the job list and find its best fit
    from pre-defined partitions
    we have 3 different partition sizes:
        16
        32
        64
    Then bestFit returns the integer number of the partition size
    """
    if number <= 16:                       # If number is smaller and equal 16 
        return 16                          # Return 16
    elif number > 16 and number <=32:      # If bigger then 16 and smaller then and equal to 32
        return 32                          # Return 32
    elif number > 32 and number <=64:      # If bigger then 32 and smaller then and equal to 64
        return 64                          # Return 64

jobs() Function:


In [2]:
def jobs():                                  # Function header
    """
    jobs function takes no parameter
    Function also has another sub-function which called by the function
    
    """                                   
    from random import randint               # Import randint from random library
    memory = [16,16,16,32,32,64]             # Memory Partitions 
    i = 1                                    # Initialize the loop counter  
    unprocessed_jobs = []                    # Initialize an empty list to store the random integers
    while i <= 1000:                         # Loop 1000 times
        rand_int = randint(10,90)            # get pseudo-random integers between 10-90
        unprocessed_jobs.append(rand_int)    # Add the random integer to list 
        i += 1                               # Incrementing by 1
    
    # The following line is slicing the jobs which are bigger then the max size in memory partition size
    #  which also stores the sorted list into variable, then we need to randomize it.
    unprocessed_jobs = sorted(unprocessed_jobs)[:sorted(unprocessed_jobs).index(max(memory))]
    def RandomizeArray(myList):              # Randomizing Array Function is sub-function
        max_i = myList[len(myList) - 1]      # Get the max value in list
        i = 0                                # Initialize the counter
        for i in range(len(myList)):         # Loop until the length of the list
            j = randint(i,(len(myList) - 1)) # Get a random integer between i to length of list-1
            temp = myList[i]                 # Swap the i and j
            myList[i] = myList[j]            # Swap the i and j 
            myList[j] = temp                 # Swap the i and j
        return myList                        # Return the randomized list
    
    return RandomizeArray(unprocessed_jobs)  # Return the randomized job list

main() Function:


In [3]:
def main():                                             # Function header without an argument
    """
    This function is the main one which means this functions runs all other functions and 
        prints the outputs. 
    """
    unprocessed_jobs = jobs()                           # jobs() function gets the usable job list
    numberOfRejectedJobs = 0                            # Initialize a variable to store the number of Rejected ones
    numberOfRejectedJobs = 1000 - len(unprocessed_jobs) # Calculate it by substracting the length of usable jobs by 1000 
    memory = [16,16,16,32,32,64]                        # Create the list of partition sizes
    time = 0                                            # Initialize the time elapsed
    for i in unprocessed_jobs:                          # Loop through the usable job list
        if len(memory) != 0:                            # If there is still memory space left
            value = bestFit(i)                          # bestFit(i) and store the returned value in value
            if value in memory:                         # If value is in memory list
                time += i/10                            # Add the time elapsed in time
                memory.remove(value)                    # Then remove that partition
            else:                                       # Else
                continue                                # Continue
        else:                                           # If there is no space left
            memory = [16,16,16,32,32,64]                # Then assign a new memory
    print "The time after all 1000 jobs left: " + str(time)
    print "Number of Rejected Jobs: " + str(numberOfRejectedJobs) # Now we get the numberOfRejectedJobs

In [9]:
main()


The time after all 1000 jobs left: 314
Number of Rejected Jobs: 336

In [ ]: