int main() { int S,i; srand((int)getpid()); S=(int)rand()%390; for(i=0; i<total_instruction; i+=1) /*产生指令队列*/ { a[i]=S; /*任选一指令访问点*/ a[i+1]=a[i]+1; /*顺序执行一条指令*/ a[i+2]=(int)rand()%390; /*执行前地址指令m’*/ a[i+3]=a[i+2]+1; /*执行后地址指令*/ S=(int)rand()%390; } for(i=0; i<total_instruction; i++) /*将指令序列变换成页地址流*/ { page[i]=a[i]/10; offset[i]=a[i]%10; } for(i=4; i<=32; i++) /*用户内存工作区从4个页面到32个页面*/ { printf("%2d page frames",i); FIFO(i); LRU(i); OPT(i); LFU(i); NUR(i); printf("\n"); } return 0; }
/************************************************************************************** * Function Name: pinPage * * Description: * Pin the page with the requested pageNum in the BUffer Pool * If the page is not in the Buffer Pool, load it from the file to the Buffer Pool * * Parameters: * BM_BufferPool * const bm: Buffer Pool Handler * BM_PageHandle * const page: Buffer Page Handler * PageNumber pageNum: the page number of the requested page * * Return: * RC: return code * * Author: * Jie Zhou <*****@*****.**> * * History: * Date Name Content * ---------- ---------------------------------- ---------------------------- * 2015-03-17 Jie Zhou <*****@*****.**> Initialization * 2015-03-20 Xin Su <*****@*****.**> Modify the logic of pinning the requested page * Add comments **************************************************************************************/ RC pinPage(BM_BufferPool * const bm, BM_PageHandle * const page, const PageNumber pageNum) { RC rc = -99; // init the return code if (bm->strategy == RS_FIFO) { rc = FIFO(bm, page, pageNum); // Because the searchPage() in the FIFO() returns a different return code (RC_PAGE_FOUND) // when it completes without errors from the return code (RC_OK) returned by appendPage() and replacePage(), // so it should be reset to RC_OK when searchPage() is executed and returned successfully if (rc == RC_PAGE_FOUND) { rc = RC_OK; } } else if (bm->strategy == RS_LRU) { rc = LRU(bm, page, pageNum); } else if (bm->strategy == RS_CLOCK) { rc = CLOCK(bm, page, pageNum); } else if (bm->strategy == RS_LFU) { // Replacement strategy is not implemented yet return RC_RS_NOT_IMPLEMENTED; } else if (bm->strategy == RS_LRU_K) { // Replacement strategy is not implemented yet return RC_RS_NOT_IMPLEMENTED; } return rc; } // pinPage
int main(int argc, char* argv[]) { int i, m; /* 生成指令序列 */ srand((int)getpid()); for (i = 0; i < INSTRUCTION_NUM;) { /* 在$[0,319]$的指令地址之间随机选取一起点$m$ */ m = (int)rand() % INSTRUCTION_NUM; /* 顺序执行一条指令,即执行地址为$m+1$的指令 */ instructions[i++] = m + 1; /* 在前地址$[0,m+1]$中随机选取一条指令并执行,该指令的地址为$m'$ */ instructions[i++] = m = (int)rand() % (m + 2); /* 顺序执行一条指令,其地址为$m'+1$的指令 */ instructions[i++] = m + 1; /* 在后地址$[m'+2,319]$中随机选取一条指令并执行 */ instructions[i++] = ((int)rand() + (m + 2)) % INSTRUCTION_NUM; } /* 转换为页地址流 */ for (i = 0; i < INSTRUCTION_NUM; i++) { /* 按每K存放PER_K_INSTS条指令排列虚存地址 */ addrs[i].p = instructions[i] / PER_K_INSTS; addrs[i].n = instructions[i] % PER_K_INSTS; } /* 分配内存容量从4K循环到32K */ for (i = 4; i <= 32; i++) { printf("%2dK\tFIFO: %4.2lf%%\tLRU: %4.2lf%%\n", i, FIFO(i), LRU(i)); } return 0; }
int main(void) { // open input file. printf("Enter input file name :\n"); scanf("%s", gStrBuf); printf("\n"); gInOutFile = fopen(gStrBuf, "r"); if(gInOutFile != NULL) { printf("File : %s opened success.\n", gStrBuf); } // end if else { printf("File : %s could not be opened, or not exists.\n", gStrBuf); printf("Program terminated.\n"); return 0; } // end else // Get page frame value getPageFrame(); printf("Page frame : %d\n", gPageFrame); // Get page request getPageRequest(); // print out page request printf("Page request : "); for(int CNT = 0; gPageRequest[CNT] != END_SIGN; CNT++) printf("%d ", gPageRequest[CNT]); printf("\n"); // Close input file fclose(gInOutFile); // Declaration memory page reference MEM_REF* memoryPage = (MEM_REF*) malloc(sizeof(MEM_REF) * gPageFrame); // Initial memory page initMemPage(memoryPage); // Output memory page and information outMemPage(0, memoryPage); printf("\n"); outMemInf(); // Reset memory information and memory page clearMemInf(); initMemPage(memoryPage); // Method First In First Out printf("----- First In First Out -------------\n"); FIFO(memoryPage); outMemInf(); return 0; } // END main
/************************************************************************************** * Function Name: LRU * * Description: * LRU replacement strategy * * Parameters: * BM_BufferPool * const bm: Buffer Pool Handler * BM_PageHandle * const page: Buffer Page Handler * PageNumber pageNum: the page number of the requested page * * Return: * RC: return code * * Author: * Xin Su <*****@*****.**> * * History: * Date Name Content * ---------- ---------------------------------- ------------------------ * 2015-03-15 Xin Su <*****@*****.**> Initialization **************************************************************************************/ RC LRU(BM_BufferPool * const bm, BM_PageHandle * const page, PageNumber pageNum) { PageList *queue = (PageList *) bm->mgmtData; RC rc; // init return code // Run FIFO first rc = -99; rc = FIFO(bm, page, pageNum); // if FIFO meets error, then return the error code // else if return RC_PAGE_FOUND, then FIFO completes with searchPage, // also if the requested page is not in the tail spot, move it to the tail of the PageList if (rc != RC_OK && rc != RC_PAGE_FOUND) { return rc; } else if (rc == RC_PAGE_FOUND && queue->current != queue->tail) { // Now the current pointer points to the requested page // All we need to do is move the requested page to the tail of the PageList // Remove the current PageFrame // The steps of the remove are different depending on if the requested page is the head or not if (queue->current == queue->head) { queue->head = queue->head->next; queue->current->next->previous = NULL; } else { queue->current->previous->next = queue->current->next; queue->current->next->previous = queue->current->previous; } // Add the current PageFrame to the tail // step 1 - connect tail and current queue->current->previous = queue->tail; queue->tail->next = queue->current; // step 2 - set current's next // Be careful that maybe the PageList is not full // if the PageList is not full, then we should also set the previous pointer of the next PageFrame to the tail to the requested page if (queue->size < bm->numPages) { queue->current->next = queue->tail->next; queue->tail->next->previous = queue->current; } else { queue->current->next = NULL; } // step 3 - set tail queue->tail = queue->tail->next; // Now the current pointer still points to the requested page } /* * If the code comes here, then LRU complete * Now the current pointer points to the requested page */ return RC_OK; } // LRU
/************************************************************************************** * Function Name: pinPage * * Description: * Pin the page with page number pageNum * * Parameters: * BM_BufferPool * const bm: Buffer Pool Handler * BM_PageHandle * const page: Buffer Page Handler * PageNumber pageNum: the page number of the requested page * * Return: * RC: return code * * Author: * Jie Zhou <*****@*****.**> * * History: * Date Name Content * ---------- ---------------------------------- ---------------------------- * 2015-03-17 Jie Zhou <*****@*****.**> Initialization * 2015-03-20 Xin Su <*****@*****.**> Modify the logic of pinning the requested page * Add comments **************************************************************************************/ RC pinPage(BM_BufferPool * const bm, BM_PageHandle * const page, const PageNumber pageNum) { RC rc = -99; // init the return code if (bm->strategy == 0) { rc = FIFO(bm, page, pageNum); } else if (bm->strategy == 1) { rc = LRU(bm, page, pageNum); } else if (bm->strategy == 2) { rc = CLOCK(bm, page, pageNum); } return rc; }
int main() { init_Ref_String(); page_frames = malloc(sizeof(int)); for (number_of_frames = 1; number_of_frames <= MAX_PAGE_FRAMES; number_of_frames++) { page_frames = realloc(page_frames, number_of_frames * sizeof(int)); resetFrames(); printf("#Of Page Frames %d \n", number_of_frames); printf("FIFO: %d page faults \n", FIFO()); printf("OPTMIN: %d page faults \n\n", OPTMIN()); } return 0; }
int main() { int choice; //以菜单的形式显示出来 while(1) { system("cls");//清屏 manu(); scanf("%d",&choice); switch(choice) { case 1:FIFO();break; case 2:LRU();break; case 3:Optimal();break; case 0:exit(0); default:printf("您的输入有误,请重新输入\n"); } system("pause"); } return 0; }
int main() { FILE* fp=fopen("datafile.txt","r"); if(!fp) { perror("fopen"); return -1; } int n,d; fscanf(fp,"%d%d",&n,&d); fclose(fp); int size=n*n*(2*n+1); int iter=3*n*n/d+2; int ref[size]; generate_reference_string(n,d,ref); int input; while(1) { printf("Enter Option (0:exit, 1:FIFO, 2:LRU, 3:LFU, 4:2nd Chance) : "); scanf("%d",&input); if(!input) break; switch(input) { case 1: FIFO(size,ref,iter); break; case 2: LRU(size,ref,iter); break; case 3: LFU(size,ref,iter); break; case 4: IICHANCE(size,ref,iter); break; } } return 0; }
void main() { int s, i, j; srand(10*getpid()); s=(float)319*rand()/32767/32767/2+1; // 前面两位是页号,后面是偏移地址 for (i=0; i<total_instruction; i+=4) { if (s<0||s>319) { printf("When i==%d, Error, s==%d\n",i,s); exit(0); } a[i] = s; a[i+1] = a[i]+1; a[i+2] = (float)a[i]*rand()/32767/32767/2; a[i+3] = a[i+2]+1; s=(float)(318-a[i+2])*rand()/32767/32767/2+a[i+2]+2; if ((a[i+2]>318)||(s>319)) printf("a[%d+2], a number which is: %d and s==%d\n", i, a[i+2], s); } for (i=-1; i<total_instruction; i++) { page[i] = a[i]/10; offset[i] = a[i]%10; } for (i=4; i<=32; i++) { printf("%2d page frames", i); FIFO(i); LRU(i); OPT(i); LFU(i); NUR(i); printf("\n"); } }
int main() { int s, i, j; srand(10 * getpid()); /*由于每次运行时进程号不同,故可用来作为初始化随机数队列的“种子”*/ s = (float)319 * rand() / 32767 / 32767 / 2 + 1; // for (i = 0; i < total_instruction; i += 4) /*产生指令队列*/ { if (s < 0 || s > 319) { printf("When i==%d,Error,s==%d\n", i, s); exit(0); } a[i] = s; /*任选一指令访问点m*/ a[i + 1] = a[i] + 1; /*顺序执行一条指令*/ a[i + 2] = (float)a[i] * rand() / 32767 / 32767 / 2; /*执行前地址指令m' */ a[i + 3] = a[i + 2] + 1; /*顺序执行一条指令*/ s = (float)(318 - a[i + 2]) * rand() / 32767 / 32767 / 2 + a[i + 2] + 2; if ((a[i + 2] > 318) || (s > 319)) printf("a[%d+2],a number which is :%d and s==%d\n", i, a[i + 2], s); } for (i = 0; i < total_instruction; i++) /*将指令序列变换成页地址流*/ { page[i] = a[i] / 10; offset[i] = a[i] % 10; } for (i = 4; i <= 32; i++) /*用户内存工作区从4个页面到32个页面*/ { printf("---%2d page frames---\n", i); FIFO(i); LRU(i); LFU(i); NUR(i); OPT(i); } return 0; }
/************************************************************************************** * Function Name: LRU * * Description: * LRU replacement strategy * * Parameters: * BM_BufferPool * const bm: Buffer Pool Handler * BM_PageHandle * const page: Buffer Page Handler * PageNumber pageNum: the page number of the requested page * * Return: * RC: return code * * Author: * Xin Su <*****@*****.**> * * History: * Date Name Content * ---------- ---------------------------------- ------------------------ * 2015-03-15 Xin Su <*****@*****.**> Initialization. **************************************************************************************/ RC LRU(BM_BufferPool * const bm, BM_PageHandle * const page, PageNumber pageNum) { // Run FIFO first RC rc = -99; // init return code rc = FIFO(bm, page, pageNum); // If FIFO meets error, then return the error code if (rc != RC_OK) { return rc; } /* * If the code comes here, then FIFO complete. * Now the current pointer points to the requested page * All wee need to do is to move the current page to the tail */ PageList *queue = (PageList *) bm->mgmtData; // If the requested page is not in the tail spot, then move it to the tail if (queue->current != queue->tail) { // Remove the current PageFrame. queue->current->previous->next = queue->current->next; queue->current->next->previous = queue->current->previous; // Add the current PageFrame to the tail queue->current->previous = queue->tail; queue->current->next = NULL; queue->tail->next = queue->current; queue->tail = queue->tail->next; // Now the current pointer still points to the requested page } return RC_OK; }
//pinPage RC pinPage (BM_BufferPool *const bm, BM_PageHandle *const page, const PageNumber pageNum) { sd = (Structure_Details *)bm->mgmtData; if(sd[0].pagenum == -1) { openPageFile (bm->pageFile, &fh); sd[0].data = (SM_PageHandle) malloc(PAGE_SIZE); ensureCapacity(pageNum,&fh); readBlock(pageNum, &fh, sd[0].data); sd[0].pagenum = pageNum; sd[0].fixedcount++; n = 0; strike = 0; sd[0].freq_used = strike; sd[0].nr = 0; page->pageNum = pageNum; page->data = sd[0].data; return RC_OK; } else { int i=0; int buffer_check = 0; while(i<buffpg_size) { if(sd[i].pagenum != -1) { if(sd[i].pagenum == pageNum) { sd[i].fixedcount++; buffer_check = 1; strike++; if(bm->strategy == RS_LRU) sd[i].freq_used = strike; page->pageNum = pageNum; page->data = sd[i].data; break; } } else { openPageFile (bm->pageFile, &fh); sd[i].data = (SM_PageHandle) malloc(PAGE_SIZE); readBlock(pageNum, &fh, sd[i].data); sd[i].pagenum = pageNum; sd[i].fixedcount = 1; sd[i].nr = 0; n++; strike++; if(bm->strategy == RS_LRU) sd[i].freq_used = strike; page->pageNum = pageNum; page->data = sd[i].data; buffer_check = 1; break; } i++; } if(buffer_check == 0) { pin = (Structure_Details *)malloc(sizeof(Structure_Details)); openPageFile (bm->pageFile, &fh); pin->data = (SM_PageHandle) malloc(PAGE_SIZE); readBlock(pageNum, &fh, pin->data); pin->pagenum = pageNum; pin->dirtyPage = 0; pin->fixedcount = 1; pin->nr = 0; n++; strike++; if(bm->strategy == RS_LRU ) pin->freq_used = strike; page->pageNum = pageNum; page->data = pin->data; if(bm->strategy == RS_FIFO) { FIFO(bm,pin); } else if (bm->strategy == RS_LRU) { LRU(bm,pin); } else { return RC_ALGORITHM_NOT_IMPLEMENTED; } } return RC_OK; } }
int main(){ //Read workload to array 'workLoad' FILE *pRead; char baseDir[] ="WorkLoad/workload"; int wl=2; char newDir[10]; sprintf(newDir,"%d",wl); strcat(baseDir,newDir); printf("Reading = %s\n", baseDir); FILE *fin,*fout; fin=fopen(baseDir,"rb"); if(NULL==fin){ printf("File Read Fail"); } while(fscanf(fin,"%d",&workLoad[RW])!=EOF){ RW++; } fclose(fin); printf("Total Read = %d\n",RW); //Initialize the page array. int it1=0; P=pageNumArr[1]; initializePage(); FIFO(); initializePage(); CLOCK(); initializePage(); LRU(); // for(it1=0;it1<2;it1++){ // P=pageNumArr[it1]; // initializePage(); // FIFO(); // } // for(it1=0;it1<2;it1++){ // P=pageNumArr[it1]; // initializePage(); // CLOCK(); // } // for(it1=0;it1<2;it1++){ // P=pageNumArr[it1]; // initializePage(); // LRU(); // } return 0; }
int main(int argc, char *argv[]){ if (argc == 3){ // Nome do arquivo (argv[0]) mais os dois parâmetros char entrada[40] = "entrada/"; char saida[40] = "saida/"; int k; // instâncias a serem simuladas int tam_mem_fis, tam_pagina, n_acessos; // tamanho (em bytes) da memória física, de cada página e o número n de acessos int num_paginas; // Quantas páginas a memória primária terá int posicao_acessada; TipoCelula pagina_atual; strcat(entrada,argv[1]); strcat(saida,argv[2]); FILE * inp = abreArquivoLeitura(entrada); FILE * out = abreArquivoEscrita(saida); fscanf(inp, "%d ", &k); // Lê as k instâncias de problemas for (int l=0; l<k; l++){ fscanf(inp, "%d %d %d\n", &tam_mem_fis, &tam_pagina, &n_acessos); num_paginas = tam_mem_fis / tam_pagina; // Num de páginas é a razão do tam da memória com o tamanho de cada página TipoLista memoria_fifo, memoria_lru, memoria_lfu; Cria(&memoria_fifo); Cria(&memoria_lru); Cria(&memoria_lfu); memoria_fifo.paginas_livres = num_paginas; memoria_lru.paginas_livres = num_paginas; memoria_lfu.paginas_livres = num_paginas; for (int a=0; a<n_acessos; a++){ fscanf(inp, "%d", &posicao_acessada); pagina_atual.pagina = posicao_acessada / tam_pagina; pagina_atual.num_acessos = 1; FIFO(&memoria_fifo, pagina_atual); LRU(&memoria_lru, pagina_atual); LFU(&memoria_lfu, pagina_atual); } fprintf(out,"%d ",memoria_fifo.misses); fprintf(out,"%d ",memoria_lru.misses); fprintf(out,"%d\n",memoria_lfu.misses); LiberaLista(&memoria_fifo); LiberaLista(&memoria_lru); LiberaLista(&memoria_lfu); } fechaArquivo(inp); fechaArquivo(out); } }