Question: #linux #operation system #c The program memory ( also called virtual memory ) is divided into pages which are brought to the main memory as
#linux #operation system #c The program memory also called virtual memory is divided into pages which are brought to the main memory as needed.
We will implement virtual memory of a computer with one program. The memory mapping system in the exercise is demonstrated in the figure below:
The main function of the exercise will consist of a sequence of load and store commands random according to the example at the end of the exercise. The heart of the exercise will deal with
In the implementation a page table that maps the logical to physical pages
The load function:
Receives a logical address that must be accessed for reading. The function ensures that the relevant page of the address is in memory. if the address
that the load function received is invalid, print an error message to stderr and continue the simulation.
The store function:
Receives an address to which one must access for writing. Here too, as in load, you must make sure that the page of the address in question is in memory. in any case of
Error An error message should be printed to stderr and return from the function and continue the simulation.
The flow chart of the program:
Explain the diagram
For each address we receive, first check in the page table if the page to which the address belongs is in the main memory.
If the page is already in the main memory, a conversion from a virtual address to a physical address must be made: it must be done only with the help of actions
binaries
o A virtual address consists of the page number the offset within the page, so given a virtual address we would like to identify it first
All to which page the address belongs and what is the offset.
o The page table will return to us for the page we found what the appropriate frame is and thus we can access the appropriate frame in memory
the main one and advance in it according to the offset for reading or writing.
If the page is not in the main memory, it must be fetched from the appropriate place. There are several options here:
rightAccess: check if this is a page with readonly permissions text or if it can be written heapstackbssdata
o In case there are no write permissions, then the page is in the executable file. Why
o In case there are write permissions, ie it is a page of type stackheap, bss data then check:
o If the page is "dirty", that means things have been written on it and changes have been made to it in other words, there was already a call with store to this page
If so then the page is in the swap file.
If not, check if it is a stackheap, bss or data page
If stackheap, bss we will allocate a new and empty page. here a situation of malloc or a call to a function is simulated
Otherwise, address data is read from the file
The data structures of the system
databasesim struct: the main data structure of the program. Will be defined in the c file of your program. Contains the following fields:
tablepage the page table of the system. A pointer to an array of
struct pagedescriptor is defined later
fdswap the descriptor file of the swap file, to which we will refer pages from the main memory received by open of
the file
fdprogram the descriptor file of the program file that we "run" in the system and where the data resides received on
by opening the file
memorymain we will implement it as an array of characters of size
struct simdatabase
pagedescriptor pagetableNUMOFPAGES; pointer to page table
int swapfilefd; swap file fd
int programfd; executable file fd
char mainmemoryMEMORYSIZE;
int textsize;
int datasize;
int bssheapstacksize;
;
The table of pages tablepage will be realized as an array of type descriptorpage with size PAGESOFNUM as follows:
typedef struct pagedescriptor
unsigned int V; valid
unsigned int D; dirty
unsigned int P; permission
unsigned int frameswap; the number of a frameswap if in case it is pagemapped
pagedescriptor;
We initialize the sizes of the arrays like this:
#define PAGESIZE
#define NUMOFPAGES
#define MEMORYSIZE
# define SWAPSIZE
The functions you are required to write
systeminit initializes the work environment
simdatabase simdb initsystemchar exefilename char swapfilename int textsize, int datasize, int
bssheapstacksize
The function accepts
Executable file name
swap file name
The size of the sizetext in bytes
The size of the size data in bytes
In bytes bssheapstacksize e size void printswap struct simdatabase memsim;
Printing the tablepage
void printpagetablestruct simdatabase memsim;
clearsystem
void clearsystemstruct simdatabase memsim;
Closes the open files and frees dynamic memory
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
