Ejemplo n.º 1
0
/*				Overflow				*/
int checkOverflow(FILE* yyout, int reg, int type){

	if (reg == -1)
	{
		int i = 0;
		switch(type)
		{
			case TYPE_INTEGER:
			case TYPE_CHAR:
			case TYPE_BOOLEAN:
				reg = extraInfoPerRegister[LRURegs[0]]->nRegister;					
				//reg = extraInfoPerRegister[*nextRegisterOverflow]->nRegister;
				fprintf(yyout,"\tR7 = R7-4;\t//Register overflow\n\tI(R7) = R%d;\t//Saving to stack\n",reg);
				extraInfoPerRegister[LRURegs[0]]->nRegister = 7;
				//extraInfoPerRegister[*nextRegisterOverflow]->nRegister = 7;
				LRU(reg);	
			break;
			case TYPE_FLOAT:
				reg = extraInfoPerDoubleRegister[LRUDoubleRegs[0]]->nRegister;
				//reg = extraInfoPerRegister[*nextRegisterOverflow]->nRegister;
				fprintf(yyout,"\tR7 = R7-4;\t//Register overflow\n\tF(R7) = RR%d;\t//Saving to stack\n",reg);
				extraInfoPerDoubleRegister[LRUDoubleRegs[0]]->nRegister = 77;
				//extraInfoPerRegister[*nextRegisterOverflow]->nRegister = 77;
			break;
			default:
				reg = -1;
			break;
		}
	}
	return reg;
	
}
Ejemplo n.º 2
0
void* ThreadFunction ()
{
	int wsLimit =4; //4 page frames
	int virtualPages =8; //Numero de paginas que os threads usarao
 	int i,id,r;
 	id=search(threadNames,NUM_THREADS,(int)0); //Ache um Zero
 	if(id<0)
 		printf("Erro!\n");
 	threadNames[id]=(int)pthread_self();

	for(i=0;i<DeathTime;i++)
	{	
		printf("Thread %d (%d) Executando pela %da vez\n",id+1,threadNames[id],i+1);
		/*Execute Thread and Call LRU */
		printf("Call for LRU\n");
		r=LRU(8*id+i); //Acesse a página de numero 8 * id + i
		if(!r) //LRU Está ocupada
		{
			i--; //Nao conte esta iteracao
			usleep(5000); //tente logo após
			

		}
		else //Funcionou! Espere 5 segundos para a proxima requisicao
			sleep(5);
	}
	pthread_exit(NULL);
}
Ejemplo n.º 3
0
Archivo: page.c Proyecto: kingfree/haut
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;
}
/**************************************************************************************
 * 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
Ejemplo n.º 5
0
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;
}
Ejemplo n.º 6
0
Archivo: cache.c Proyecto: nikmash/C
int searchor(linkedListPtr foundlist, linkedListPtr searchlist, data *dataptr){
	int y;
	int comp;
	int boolean;
	boolean = 1;
	int lru;

	/* use this loop for however many words are input by the user*/
	llnode *currsearch = searchlist->head;
	
	while(currsearch != NULL){
		

		for (y = 0; y < lwsize; y++){
			comp = strcmp(currsearch->filename, listwords[y]->string);

			if (comp == 0){
				listwords[y]->counter++;
				llnode *curr = listwords[y]->filelist->head;
				llnode *prev = NULL;

				while(curr != NULL){
						LLInsert(foundlist, curr->filename);
						prev = curr;
						curr = curr->next;
				}
				break;
			}
		}

		if(comp != 0){
			printf("Could not find %s in cache \n", currsearch->filename);
			lru = LRU(currsearch->filename, dataptr, searchlist);

			if(lru == -1){
				printf("Could not find %s in specified text file \n", currsearch->filename);
				return 0;
			}
			continue;
		}
		currsearch = currsearch->next;	
	}

	if(foundlist->head == NULL){
		fprintf(stderr, "The words you entered are not in the index file you specified \n");
		return 0;
	}
	


	llnode *currfoundit = foundlist->head;
	while(currfoundit != NULL){

		printf("%s \n", currfoundit->filename);
		currfoundit = currfoundit->next;
	}
	return 1;
}
Ejemplo n.º 7
0
/* Reading block blockNum from file opening implyed from finfo in 
 * memory and making pointer blockBuf, pointing to the beginning of it.
 * If block is already in memory from current file opening set
 * errno but do not return error code. If there is no space in 
 * memory LRU decides which block to replace, if possible, otherwise
 * error code is returned since block cannot be loaded.
 */   
int BMM_GetBlock(fileInfo_t finfo, int blockNum, char** blockBuf)
{
	int	i;
	int 	RetVal, LRURetVal;
	
	Time++;										/* increase Time */
	for ( i=0; i<BF_BUFFER_SIZE; i++ )						
		if ( Mem_info[i].Empty == FALSE 					/* Block already in Memory */
		&& strcmp(finfo.filename,Mem_info[i].FileName) == 0  
		&& blockNum == Mem_info[i].BlockNum ){
			if ( Mem_info[i].PinedBy[finfo.fd] == TRUE ){			/* block already pinned by current file opening */
				*blockBuf=Mem_info[i].Block;				/* Inform pointer */
				BF_Errno=BFE_BLOCKFIXED ;				/* inform errno */
				return BFE_OK;						/* DO NOT return error code */
			}
			*blockBuf=Mem_info[i].Block;				/* Inform pointer */
			Mem_info[i].PinCounter++;
			Mem_info[i].PinedBy[finfo.fd]=TRUE;
			Mem_info[i].TimeStamp=Time;	
			return BFE_OK;
		}		
	for ( i=0; i< BF_BUFFER_SIZE; i++) 						/* Block not in memory */
		if ( Mem_info[i].Empty == TRUE ){					/* Available space in memory */
			if ( (RetVal=HDF_ReadBlock(blockNum+1, Mem_info[i].Block, finfo.fp)) != BFE_OK ) 
				return RetVal;
			bzero(Mem_info[i].FileName,MAX_FILE_NAME);
			strcpy(Mem_info[i].FileName,finfo.filename);
			Mem_info[i].Empty=FALSE;
			Mem_info[i].PinCounter=1;					/* PinCounter=1 since we just  brought block in memory.*/ 
			Mem_info[i].BlockNum=blockNum;					
			Mem_info[i].PinedBy[finfo.fd]=TRUE;
                        Mem_info[i].TimeStamp=Time;
 			*blockBuf= Mem_info[i].Block;
			return BFE_OK;
		}									
	if ( (LRURetVal=LRU()) < 0 ){ 							/* No Available space in memory */
		BF_Errno=RetVal;							/* LRU for Unpinned Blocks 	*/
		return RetVal;
	}												

	if ( (RetVal=HDF_ReadBlock(blockNum+1, Mem_info[LRURetVal].Block, finfo.fp))!= BFE_OK ) 
				return RetVal;
	bzero(Mem_info[LRURetVal].FileName,MAX_FILE_NAME);
	strcpy(Mem_info[LRURetVal].FileName,finfo.filename);
	Mem_info[LRURetVal].PinCounter=1;						/* PinCounter=1 since we just  brought block in memory.*/
	Mem_info[LRURetVal].BlockNum=blockNum;
	Mem_info[LRURetVal].PinedBy[finfo.fd]=TRUE;
        Mem_info[LRURetVal].TimeStamp=Time;
	*blockBuf= Mem_info[LRURetVal].Block;
	return BFE_OK;
}
/**************************************************************************************
 * 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;
}
Ejemplo n.º 9
0
int main() 
{
  int refString[100];
  srand(time(NULL));

  refString[1] = rand()%15;
  for(int i=1; i<100; i++) {
    refString[i] = rand()%15;
    while(refString[i] == refString[i-1]) {
      refString[i] = rand()%15;
    }
  }

  int t1[16];
  zeroTable(t1,16);

  printf("FIFO:\n");
  int one;

  for(int i=1; i<17; i++) {
    zeroTable(t1,16);
    one = fifo(t1, i, refString, 100);
    printf("%d\n", one);
  }

  printf("\nLRU:\n");
  zeroTable(t1,16);
  one = LRU(t1, 1, refString, 100);

  for(int i=1; i<17; i++) {
    zeroTable(t1,16);
    one = LRU(t1, i, refString, 100);
    printf("%d\n", one);
  }

}
bool assignPage(Memory* mem,JobInMemory* job,int index,int clock){
	Page* thispage = list_Page_pop(mem->freepages);
        if(thispage==NULL) {
        	debug_print_string("Memory full, will perform LRU\n");
		if(!LRU(mem,1))		
                	return false;
        	thispage = list_Page_pop(mem->freepages);
	}
        debug_print("Available empty page: %d\n",PAGELOC(thispage,mem->pages[0]));
        thispage->job = job->job;
        thispage->last_accessed_at = clock;

        job->pages[index]=thispage;
	
	list_Page_append(mem->LRU_list, thispage);
	return true;
}
Ejemplo n.º 11
0
/* Creating an empty block corresponing to block with blockNum 
 * from file opening implyed from finfo and making pointer blockBuf, 
 * pointing to the beginning of it. If there is no space in 
 * memory LRU decides which block to replace, if possible, 
 * otherwise error code is returned since block cannot be loaded.
 */   
int BMM_AllocBlock(fileInfo_t finfo, int blockNum,  char** blockBuf)
{
	int	i;
	int 	RetVal;
	
	Time++;
	for ( i=0; i<BF_BUFFER_SIZE; i++ )						
		if ( Mem_info[i].Empty == FALSE 					
		&& strcmp(finfo.filename,Mem_info[i].FileName) == 0  			/* Block already in Memory */
		&& blockNum == Mem_info[i].BlockNum ){					/* It shouldn't be... */
				*blockBuf=Mem_info[i].Block;				/* Inform pointer */
				BF_Errno=BFE_BLOCKFIXED ;				/* inform errno */
				return BFE_OK;						/* DO NOT return error code */
		}

	for ( i=0; i< BF_BUFFER_SIZE; i++) 						/* Block not in memory */
		if ( Mem_info[i].Empty == TRUE ){					/* Available space in memory */
			bzero(Mem_info[i].Block,BF_BLOCK_SIZE);
			bzero(Mem_info[i].FileName,MAX_FILE_NAME);
			strcpy(Mem_info[i].FileName,finfo.filename);
			Mem_info[i].Empty=FALSE;
			Mem_info[i].Dirty=TRUE;
			Mem_info[i].PinCounter=1;					/* PinCounter=1 since we just  brought block in memory.*/
			Mem_info[i].BlockNum=blockNum;
			Mem_info[i].PinedBy[finfo.fd]=TRUE;
                        Mem_info[i].TimeStamp=Time;
 			*blockBuf= Mem_info[i].Block;
			return BFE_OK;
		}									/* No Available space in memory */			
	if ( (RetVal=LRU()) < 0 ){ 							/* LRU for Unpinned Blocks 	*/
		BF_Errno=RetVal;
		return RetVal;
	}
	bzero(Mem_info[RetVal].Block,BF_BLOCK_SIZE);
	bzero(Mem_info[RetVal].FileName,MAX_FILE_NAME);
	strcpy(Mem_info[RetVal].FileName,finfo.filename);
	Mem_info[RetVal].PinCounter=1;							/* PinCounter=1 since we just  brought block in memory.*/
	Mem_info[RetVal].BlockNum=blockNum;
	Mem_info[RetVal].PinedBy[finfo.fd]=TRUE;
        Mem_info[RetVal].TimeStamp=Time;
 	Mem_info[RetVal].Dirty=TRUE;	
	*blockBuf= Mem_info[RetVal].Block;
	return BFE_OK;
}
Ejemplo n.º 12
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;
}
Ejemplo n.º 13
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;
}
Ejemplo n.º 14
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");
     }
 }
Ejemplo n.º 15
0
RC pinPage (BM_BufferPool *const bm, BM_PageHandle *const page,const PageNumber pageNum)
{
PageFrames *findpage;
if(bm==NULL){RC_message= "pinPage"; return RC_BUFFER_NOT_INIT;}	

if(pageNum==NO_PAGE){RC_message= "pinPage"; return RC_PAGE_NOT_INIT;}		

RC msg;
switch(bm->strategy)
{
case RS_FIFO:
return fifo(bm,page,pageNum);
break;
case RS_LRU:
return LRU(bm,page,pageNum);
break;
default:
break;
}

return RC_OK;
}
Ejemplo n.º 16
0
int assignRegisters(int type)
{
    int i=0, aux, index;
    int flag = 0;
    int reg = -1;
    
	/* Search a register */
    if ((type == 0) && (nR>0))
    {
        for (i=0;i<nMaxR;i++)
        {
            if (intRegs[i]==0)
            {
                intRegs[i] = 1;
                nR--;
                reg = i;
                break;
            }
        }
        LRU(reg);
    }
    else if ((type == 1) && (nRR>0)){
        for (i=0;i<nMaxRR;i++)
        {
            if (floatRegs[i]==0)
            {
                floatRegs[i] = 1;
                nRR--;
                reg = i;
                break;
            }
        }
        LRUDouble(reg);
    }
    
    return reg;   
}
Ejemplo n.º 17
0
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;
}
Ejemplo n.º 18
0
//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;
    }	
  }
Ejemplo n.º 19
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);

    }

}
Ejemplo n.º 20
0
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;
}
Ejemplo n.º 21
0
Archivo: cache.c Proyecto: nikmash/C
int searchand(linkedListPtr foundlist, linkedListPtr searchlist, data *dataptr){
	int y;
	int w;
	int boolean;
	boolean = 1;
	int on = 0;
	int lru;


	/*THIS FIRST FOR LOOP HAS TO PUT THE FILES OF THE FIRST WORD INTO THE FOUND ARRAY */

	llnode *currsearch = searchlist->head;

	while(on == 0){
		for (y = 0; y < lwsize; y++){
			w = strcmp(currsearch->filename, listwords[y]->string);
			if (w == 0){
				listwords[y]->counter++;
				llnode *curr = listwords[y]->filelist->head;
				llnode *prev = NULL;
				while(curr != NULL){
					LLInsert(foundlist, curr->filename);
					prev = curr;
					curr = curr->next;	
				}
				on = 1;
				break;
			}
		}
		if(w != 0){
			printf("Word was not in cache %s \n", currsearch->filename);
			lru = LRU(currsearch->filename, dataptr, searchlist);

			if(lru == -1){
				printf("Could not find %s in specified text file \n", currsearch->filename);
				return 0;
			}
		}
	}

	currsearch = currsearch->next;
	
	/* THIS SECOND FOR LOOP GETS ALL THE OTHER WORDS AND THEIR FILES AND COMPARES THEM TO WHATEVER IS ALREADY IN THE FOUND ARRAY.
	EACH FILE IN THE FOUND ARRAY IS COMPARED TO THE OTHER WORDS' FILES, AND IF IT DOESNT EXIST THEN THAT FILE IS DELETED 
	FROM THE FOUND ARRAY */
	int found = 0;
	
	while(currsearch != NULL){
		for (y = 0; y < lwsize; y++){
			w = strcmp(currsearch->filename, listwords[y]->string);

			if (w == 0){
				found = 1;
				listwords[y]->counter++;
				llnode *curr = listwords[y]->filelist->head;
				llnode *prev = NULL;
				llnode *currfound = foundlist->head;


				while(currfound != NULL){
						curr = listwords[y]->filelist->head;

					while(curr != NULL){
							boolean = strcmp(currfound->filename, curr->filename);
							if (boolean == 0){
								break;
							}
							prev = curr;
							curr = curr->next;
					}
					
					if(boolean != 0){
						LLRemove(foundlist, currfound->filename);
					}

					currfound = currfound->next;
					boolean = 1;
				}
				
			}
		}
		if(found == 0){
			printf("%s was not in cache \n", currsearch->filename);
			lru = LRU(currsearch->filename, dataptr, searchlist);
				if(lru == -1){
					printf("%s is not in the specified file", currsearch->filename);
					return 0;
				}
			continue;
		}

		currsearch = currsearch->next;
	}

	if(foundlist->head == NULL){
		fprintf(stderr, "There were no files with all input words \n");
		return 0;
	}

	llnode *currfoundit = foundlist->head;
	while(currfoundit != NULL){

		printf("%s \n", currfoundit->filename);
		currfoundit = currfoundit->next;
	}

	
	return 0;
}