Virtual Memory System With Multi Programming

Virtual Memory System With Multi Programming multiprocessing import Process

# Problem statement:

# Simulating a virtual memory system with multi programming.

# Machine has 4 bit virtual addresses and 8 bit  physical address with 2 Byte page size.

# It is needed to implement two processes or threads:

# First: Virtual Address Generation: generates a sequence of 4bit random number,

# as virtual addresses, and writes them into an integer buffer of size N.

# Second: Address Translation: reads the next virtual address from the buffer

# and translates it to a physical address, keeping track of page faults.

# System should be tested on the given file ece565hw03.txt

import sys

import os

import string

import random

import time

from threading import Thread

from multiprocessing import Queue

from multiprocessing import Process

input_file = ‘ece565hw03.txt’

fetch      = []   # List that holds page table info

phymem     = []   # List that holds physical memory info

free_list  = []   # List that tracks free spaces in memory

class PageTable(object):    # This class creates pagetable and

# holds two fields- frame number and bit status

def __init__(self):

self.frame      = []

self.status     = []

self.size_table = 8  #Page Table Size

def add(self,frame,status):

self.frame.extend(frame)

self.status.extend(status)

def info(self):

print(“Page table: ” + “(size:” + str(self.size_table) + “)” + “\nFrame no.  Bit Status”)

for i in range(0,self.size_table):

print(”  ” + str(self.frame[i]) + “\t\t” + str(self.status[i]))

multiprocessing import Process

def extract_data(file): # this function crests an object of Page Table

# opens and read data from file and passes the

# extracted data to the object

table = PageTable()

text = open(file)

for line in text:

if line != ‘\n’:

fetch = line.split()

table.add(fetch[0],fetch[1])

text.close()

print(“Following data is extracted from %s\n————————” %file)

table.info()

return table

def initial_phymem(): # Initializinf physical memory

for i in range(16):

phymem.append(0)

phymem[4] = ‘A’; phymem[6] = ‘C’; phymem[9] = ‘F’

print(“Initializing Physical Memory: “)

print(‘[%s]’ % ‘ | ‘.join(map(str, phymem)))

# Initializing free spaces list

free_list = [‘0′,’1′,’2′,’3′,’5′,’7′,’8′,’10’,’11’,’12’,’13’,’14’,’15’,’4′,’6′,’9′]

def fifo_replace(): # FIFO replacement algorithm

global free_list  # FIFO queue

frame = free_list[0]

free_list.append(frame)

free_list.pop(0)

return frame

####### Generating random virtual-address ######

def vagen(buffer):

random.seed(4)   # Random number seed

while True:

ran_num = random.getrandbits(4)

buffer.put(ran_num)     # putting the generated number in buffer

time.sleep(3)

def translator(table,buffer):

while True:

va = buffer.get()    # getting the virtual address from buffer

page   = va / 2      # Finding page number

offset = va % 2      # page offset

status = table.status[page]

print(”   Virtual Address:” + ”  %2d” %va)

print(”          Page No.:” + ”  %2d” %page)

print(”       Page Offset:” + ”  %2d” %offset)

print(”          Validity:   ” + status)

if status == ‘v’:

frame = int(table.frame[page])

phyadd = 2*frame + offset

print(”         Frame No.:” + ”  %2d” %frame)

print(”  Physical Address:” + ”  %2d\n ———————–\n ” %phyadd)

else:

print(”  *** PAGE FAULT!*** “)

frame_str = fifo_replace()

frame = int(frame_str)

phyadd = 2*frame + offset

print(”         Frame No.:” + ”  %2d” %frame)

print(”  Physical Address:” + ”  %2d\n ———————–\n ” %phyadd)

table.status[page] = ‘v’

table.frame[page]  = frame_str

time.sleep(3)

#main function

def main():

global input_file

table = extract_data(input_file)

initial_phymem()

buffer = Queue() # FIFO queue

print(‘———————————-‘)

print(‘Starting virtual memory Simulation’)

print(‘———————————-‘)

#Virtual Address Generator Process

Pvagen = Process(target=vagen,args=(buffer,))

Pvagen.start()  # Process starts

#Address Translation Process

Ptranslator = Process(target=translator,args=(table,buffer,))

Ptranslator.start()  # Process starts

while True:

try:

time.sleep(3)

except KeyboardInterrupt: # pressing ctr+c terminates simulation

print(“**** Simulation is terminating……******”)

break

Pvagen.terminate() # terminate process

Ptranslator.terminate() # terminate process

sys.exit(“\n**********Simulation Terminated !*************\n”)

#Running main routine

if __name__ == “__main__”:

main()