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; }
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; }
RC pinPage (BM_BufferPool *const bm, BM_PageHandle *const page, const PageNumber pageNum) { Data_Structure *ds = (Data_Structure *)bm->mgmtData; if(ds[0].pagenum == -1) { //printf("\nINSIDE ds[0]->pagenum == -1\n"); SM_FileHandle fh; openPageFile (bm->pageFile, &fh); ds[0].data = (SM_PageHandle) malloc(PAGE_SIZE); ensureCapacity(pageNum,&fh); readBlock(pageNum, &fh, ds[0].data); ds[0].pagenum = pageNum; ds[0].fixedcnt++; rear = 0; hit = 0; ds[0].hitnum = hit; ds[0].numRef = 0; page->pageNum = pageNum; page->data = ds[0].data; /* printf("\nPinPage function buffer"); int i; for(i=0;i<bufferSize; i++) { printf("\nPagenum: %d fixedcnt: %d dirty: %d",ds[i].pagenum,ds[i].fixedcnt,ds[i].dirtybit); }*/ return RC_OK; } else { //printf("\nINSIDE front != NULL\n"); int i,check = 0; for(i=0;i<bufferSize;i++) { if(ds[i].pagenum != -1) { if(ds[i].pagenum == pageNum) //if Page already in memory { ds[i].fixedcnt++; check = 1; hit++; if(bm->strategy == RS_LRU) ds[i].hitnum = hit; else if(bm->strategy == RS_CLOCK) //if bm-> strategy is RS_CLOCK storing the USED bit in hitnum. ds[i].hitnum = 1; else if(bm->strategy == RS_LFU) { ds[i].numRef++; //printf("Page %d referenced again \n", pageNum); //rear = rear + 2; //printf(" rear : %d \n", rear); } page->pageNum = pageNum; page->data = ds[i].data; clockptr++; break; } } else //Condition when the buffer has space to add a page { SM_FileHandle fh; openPageFile (bm->pageFile, &fh); ds[i].data = (SM_PageHandle) malloc(PAGE_SIZE); readBlock(pageNum, &fh, ds[i].data); ds[i].pagenum = pageNum; ds[i].fixedcnt = 1; ds[i].numRef = 0; rear++; hit++; if(bm->strategy == RS_LRU) ds[i].hitnum = hit; else if(bm->strategy == RS_CLOCK) //if bm-> strategy is RS_CLOCK storing the USED bit in hitnum. ds[i].hitnum = 1; page->pageNum = pageNum; page->data = ds[i].data; check = 1; break; } }//end of for if(check == 0)//Condition when the buffer is full and we need to use a replacement strategy. { Data_Structure *temp = (Data_Structure *)malloc(sizeof(Data_Structure)); SM_FileHandle fh; openPageFile (bm->pageFile, &fh); temp->data = (SM_PageHandle) malloc(PAGE_SIZE); readBlock(pageNum, &fh, temp->data); temp->pagenum = pageNum; temp->dirtybit = 0; temp->fixedcnt = 1; temp->numRef = 0; rear++; hit++; //printf("HIT : %d \n", hit); //test by Rakesh if(bm->strategy == RS_LRU ) temp->hitnum = hit; else if(bm->strategy == RS_CLOCK) //if bm-> strategy is RS_CLOCK storing the USED bit in hitnum. temp->hitnum = 1; page->pageNum = pageNum; page->data = temp->data; switch(bm->strategy) { case RS_FIFO: //printf("\n Inside FIFO switch."); fifo(bm,temp); break; case RS_LRU: //printf("\n Inside LRU switch."); lru(bm,temp); break; case RS_CLOCK: //printf("\n Inside CLOCK switch"); clock(bm,temp); break; case RS_LFU: //printf("\n Inside LFU switch"); LFU(bm,temp); break; case RS_LRU_K: printf("\n Inside LRU_K switch"); break; default: printf("\nAlgorithm Not Implemented\n"); break; }//end of switch }//end of if(check = 0) /* printf("\nPinPage function buffer"); for(i=0;i<bufferSize;i++) printf("\nPagenum: %d fixedcnt: %d dirty: %d",ds[i].pagenum,ds[i].fixedcnt,ds[i].dirtybit); */ return RC_OK; }//end of else }
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); } }