/* Name: ShutDown BufferPool
 * Behavior:Would shutdown an existing buffer pool.
 * Version: 1.0.0 */
RC shutdownBufferPool(BM_BufferPool *const bm)
{
    RC ret=RC_WRITE_FAILED;
    int i ,*pgnums, *fixounts;
    bool ispinned=FALSE;
    Buffer_page_Dtl *pg_dtl;
    BufferPool_Node *bufnode;
    char *frame;
    int currentaccessed;
//    pthread_mutex_lock(&work_mutex_shutdown);//1st Incoming thread hold a lock.
    fixounts=getFixCounts(bm);
    pgnums=getFrameContents(bm);
    for(i=0 ; i < bm->numPages ; i++)
    {
        if(fixounts[i] > 0)
        {
            ispinned=TRUE;//Condition to check for pinned pages before shut down.
        }
    }
    free(fixounts);
    if(ispinned==FALSE)
    {
        ret=RC_OK;
        bufnode=search_data(BP_StNode_ptr,bm);//Get the active buffer pool from collection of pools.
        currentaccessed=getfilebeingused(BP_StNode_ptr,bm->pageFile);//Check if the same pool is being shared.
        if(bufnode!=NULL)
        {
            pg_dtl=bufnode->buffer_page_dtl;
            if(pg_dtl!=NULL)
            for(i=0;i<bm->numPages;i++)
            {
                frame=pg_dtl[i].pageframes;
                if(pg_dtl[i].isdirty==TRUE)
                {
                   ret=writeBlock(pg_dtl[i].pagenums,bm->mgmtData,frame)==RC_OK?RC_OK:RC_WRITE_FAILED;//Write the content with dirty to disk.
                }
                currentaccessed == 1 ?free(frame):' ';
            }
            currentaccessed == 1 ?free(pg_dtl):' ';
            pg_dtl=NULL;
            delete_node(&BP_StNode_ptr,bm);
        }
    currentaccessed == 1 ?closePageFile(bm->mgmtData):' ';
    currentaccessed == 1 ?free(bm->mgmtData):' ';

  }
//  pthread_mutex_unlock(&work_mutex_shutdown);//1st Incoming thread release a lock.
    free(pgnums);
    return ret;
}
char *sprintPoolContent (BM_BufferPool *const bm)
{
  PageNumber *frameContent;
  bool *dirty;
  int *fixCount;
  int i;
  char *message;
  int pos = 0;

  message = (char *) malloc(256 + (22 * bm->numPages));
  frameContent = getFrameContents(bm);
  dirty = getDirtyFlags(bm);
  fixCount = getFixCounts(bm);

  for (i = 0; i < bm->numPages; i++)
    pos += sprintf(message + pos, "%s[%i%s%i]", ((i == 0) ? "" : ",") , frameContent[i], (dirty[i] ? "x": " "), fixCount[i]);
  
  return message;
}
// external functions
void  printPoolContent (BM_BufferPool *const bm)
{
  PageNumber *frameContent;
  bool *dirty;
  int *fixCount;
  int i;

  frameContent = getFrameContents(bm);
  dirty = getDirtyFlags(bm);
  fixCount = getFixCounts(bm);

  printf("{");
  printStrat(bm);
  printf(" %i}: ", bm->numPages); 
  
  for (i = 0; i < bm->numPages; i++)
      printf("%s[%i%s%i]", ((i == 0) ? "" : ",") , frameContent[i], (dirty[i] ? "x": " "), fixCount[i]);
  printf("\n");
}
Beispiel #4
0
/* Purpose:updates frame position considering if the page is pinned */
Frames_List *Update_Frame_Position(Frames_List *ReplaceNode,int PageNum,int FoundFlag,BM_BufferPool *const bm)
{
Frames_List *Temp,*LastNode,*Temp2;
int FrameNumberValue,PageNumberValue,i;
bool PinedPageFound=false;
Temp=ReplaceNode->head;
Frames_List *NewNode=CreateFrameNodes();
LastNode=ReplaceNode->last;
Temp=ReplaceNode->head;
int *fixCount = getFixCounts(bm);
if(FoundFlag==0)
{
	while(Temp!=NULL)
    {
		if(fixCount[Temp->Frame_Number]==1){
		Temp=Temp->next;
        PinedPageFound=true;
        FoundFlag=1;
        }
        else
            break;
    }
}

if(FoundFlag==1 && 	ReplaceNode->head->Page_Number!=PageNum)
{
        if(LastNode->Page_Number==PageNum) return ReplaceNode;
        while(Temp!=NULL && !(PinedPageFound))
        {
				if(Temp->Page_Number==PageNum)
                    break;
				Temp=Temp->next;
        }
		FrameNumberValue=Temp->Frame_Number;
		PageNumberValue=Temp->Page_Number;
		Temp->previous->next=Temp->next;
		Temp->next->previous=Temp->previous;
		LastNode->next=NewNode;
		NewNode->next=NULL;
		NewNode->Frame_Number=FrameNumberValue;
		NewNode->Page_Number=PageNumberValue;
		NewNode->previous=LastNode;
		free(Temp);
		ReplaceNode->last=NewNode;
        return ReplaceNode;
}
        FrameNumberValue=Temp->Frame_Number;
		ReplaceNode->head=Temp->next;
		Temp->next->previous=NULL;
		Temp->previous=NULL;
		free(Temp);
		NewNode->next=NULL;
		NewNode->Frame_Number=FrameNumberValue;
		NewNode->Page_Number=PageNum;
		LastNode->next=NewNode;
		NewNode->next=NULL;
		NewNode->previous=LastNode;
		ReplaceNode->last=NewNode;
		Temp=ReplaceNode->head;
		return ReplaceNode;
}