Faith D asked The objective of this assignment is to give you an opportunity to
ID: 3649043 • Letter: F
Question
Faith D asked
The objective of this assignment is to give you an opportunity to simulate the function of a paging system.
The assignment requires you to write a program in C++ to simulate the following operation.
(1) reference: used to indicate that a virtual address in a program is referenced.
command: v
where v = virtual address. The virtual address space is 0..999.
Note that each page is 100 bytes in length and the process is allocated 3 frames (i.e. f0..f2) in memory. "First-fit" placement strategy is used to place a new page in memory and "FIFO" replacement strategy is used to replace an old page from memory.
You can use any data structures you like to implement the program. But you are required to use only the FIFO replacement algorithm to replace pages.
The following shows a scenario of running the program, where user's inputs are represented in boldface:
{an executable file}
program starts running .......
please enter a reference (9999 to exit):276
please enter a reference (9999 to exit):18
please enter a reference (9999 to exit):281
please enter a reference (9999 to exit):987
please enter a reference (9999 to exit):695
please enter a reference (9999 to exit):409
please enter a reference (9999 to exit):9999
page 4 is in frame 1
page 6 is in frame 0
page 9 is in frame 2
frame 0 contains page 6
frame 1 contains page 4
frame 2 contains page 9
please write a program and explain ...than you in advance
Explanation / Answer
#include #include #define timepiece 200 #define pagesize 10 #define memorysize 50 #define sleeptime 5 struct page{ int pagetime; int virtunume; int memonume; int fage; }; struct proc{ int procsize; struct page*pagelist; int pagefram; int pagenume; int procpid; int procrunnumber; int*sequence; int cs; int ip; struct quickpage*quicklist; struct proc *next; }; struct mem{ int mempid; int flag; }; struct quickpage{ int virtunume; int memonume; }; struct proc*runlist=NULL; struct proc*waitmlist=NULL; struct proc*blocklist=NULL; int pid=0; int waketime=0; struct mem memory[memorysize]; struct proc *creatproc(){ struct proc*newproc; int suiji,i; newproc=(struct proc*)malloc(sizeof(struct proc)); suiji=rand(); while(!(suiji>=1000&&suijiprocsize=suiji; if(newproc->procsize%pagesize==0)newproc->pagenume=newproc->procsize/pagesize; else newproc->pagenume=newproc->procsize/pagesize+1; newproc->pagefram=newproc->pagenume/2; newproc->pagelist=(struct page*)malloc(newproc->pagenume*sizeof(struct page)); for(i=0;ipagenume;i++){ suiji=rand(); while(!(suiji>=1000&&suijipagelist)[i].pagetime=suiji; (newproc->pagelist)[i].virtunume=i; (newproc->pagelist)[i].fage=0; } newproc->procpid=pid; pid++; newproc->next=NULL; while(!((newproc->procrunnumber=rand()%20)>=10)); newproc->sequence=(int *)malloc(newproc->procrunnumber*(sizeof(int))); for(i=0;ipagenume;i++)(newproc->sequence)[i]=i; for(i=newproc->pagenume;iprocrunnumber;i++)(newproc->sequence)[i]=rand()%(newproc->pagenume); newproc->cs=0; newproc->ip=(newproc->pagelist)[(newproc->sequence)[newproc->cs]].pagetime; return newproc; } void installm(struct proc*process){ int i,j=0; struct proc*p; int remain[memorysize]; for(i=0;i=process->pagefram){ for(i=0;ipagefram;i++){ process->quicklist=(struct quickpage*)malloc(process->pagefram*(sizeof(struct quickpage))); memory[remain[i]].flag=1; (process->pagelist)[(process->sequence)[i]].fage=1; (process->pagelist)[(process->sequence)[i]].memonume=remain[i]; (process->quicklist)[i].virtunume=(process->sequence)[i]; (process->quicklist)[i].memonume=remain[i]; } p=runlist; if(p==NULL)runlist=process; else{ while(p->next!=NULL)p=p->next; p->next=process; } } else{ p=waitmlist; if(p==NULL)waitmlist=process; else{ while(p->next!=NULL)p=p->next; p->next=process; } } } void block(struct proc*process){ struct proc*p; p=blocklist; if(p==NULL)blocklist=process; else{ while(p->next!=NULL)p=p->next; p->next=process; } } int inmemory(struct proc*process,int cs){ if((process->pagelist)[(process->sequence)[cs]].fage==1)return 1; else return 0; } void wakeup(){ struct proc*p1,*p2; if(blocklist!=NULL){ p1=blocklist; blocklist=blocklist->next; p1->next=NULL; p2=runlist; if(p2==NULL)runlist=p1; else{ while(p2->next!=NULL)p2=p2->next; p2->next=p1; } } } void delproc(struct proc*process){ int i; struct proc*p; for(i=0;ipagefram;i++)memory[(process->quicklist)[i].memonume].flag=0; free(process->pagelist); free(process->quicklist); free(process->sequence); free(process); p=waitmlist; if(p!=NULL){ waitmlist=waitmlist->next; p->next=NULL; installm(p); } } void FIFO(struct proc*process,int cs){ int i,t; t=(process->quicklist)[0].memonume; for(i=1;ipagefram;i++){ (process->quicklist)[i-1].memonume=(process->quicklist)[i].memonume; (process->quicklist)[i-1].virtunume=(process->quicklist)[i].virtunume; } (process->quicklist)[(process->pagefram)-1].memonume=t; (process->quicklist)[(process->pagefram)-1].virtunume=(process->sequence)[cs]; } int run(struct proc*process){ int restime; struct proc*p; restime=timepiece; while(restime--){ process->ip--; if(process->ip==0){ if(process->cs+1procrunnumber){ process->cs++;process->ip=(process->pagelist)[(process->sequence)[process->cs]].pagetime; } else{ delproc(process);return restime; } if(!inmemory(process,process->cs)){block(process);FIFO(process,process->cs);return restime;} } } p=runlist; if(p==NULL)runlist=process; else{ while(p->next!=NULL)p=p->next; p->next=process; } return restime; } int main(){ int i,restime; char key; struct proc*process; for(i=0;inext; process->next=NULL; restime=run(process); waketime=waketime+timepiece-restime; if(waketime>=sleeptime){ wakeup();waketime=0; } } process=runlist; i=0; while(process!=NULL){process=process->next;i++;} printf("runing process is:%d ",i); process=runlist; while(process!=NULL){ printf("pid:%d procsize:%d ",process->procpid,process->procsize); process=process->next; } process=blocklist; i=0; while(process!=NULL){process=process->next;i++;} printf("blocking process is:%d ",i); process=blocklist; while(process!=NULL){ printf("pid:%d procsize:%d ",process->procpid,process->procsize); process=process->next; } key=getchar(); } }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.