1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Implementation of PCB

Discussion in 'Operating System' started by Gift2011, Feb 15, 2011.

  1. Gift2011

    Gift2011 New Member

    Joined:
    Feb 15, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Write an algorithm for implementing a Process Control Block
     
  2. Gift2011

    Gift2011 New Member

    Joined:
    Feb 15, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    I tried this way:
    Is just Algorithm you can not run it
    Code:
    //Pcb implementation using FCFS
    /***************************************************
    	last ----> ____
    			  |pcb2|
    			  |       |---> _____
    			  |____|    |pcb1 |
    			                |        | <---- first
    			                |____ |
    **************************************************/
    
    struct pcb {
    	int pid;
    	int pc;
    	float burstTime;
    	.
    	.	//Other attibutes of PCB
    	.
    	pcb *next;
    };
    
    class pcbQueue {
    	private:
    		pcb *last,*first;
    	public:
    		pcbQueue() //constractor
    		{first=0;last=0}
    		~pcbQueue() //Destructor
    		void addProcess(pcb process)
    		void executeProcess();
    };
    
    void pcbQueue::addProcess(pcb process){
    	pcb *newpcb = new pcb;
    	if(first == 0 && last== 0) // first process
    		first=last=newpcb;
    	newpcb->pid = process.pid;
    	newpcb->pc  = process.pc;
    	newpcb->burstTime  = process.burstTime;
    	.
    	.
    	.
    	newpcb->next=last;
    	last = newpcb;	
    }
    
    /////////////////////////////////////////////////////////////////////////
    void pcbQueue::executeProcess(){
    	pcb *current = first; float time=0;
    	if(first !=last){
    		while(time < current->burstTime){
    			execute(current); // execute the current process decrement burstTime
                            if(enterrupt_occur)
                               context_swiching(current);
    			//after execution
    			time = time+current->burstTime;
    		}
    	}
    }
    
    void main()
    {
    	pcbQueue fcfsqueue; boolean done = false;
    	while(!done){
    		//create three processes
    		for(int i=0;i<3;i++){
    		pcb newprocess = fork(); //create new process, assume fork return pcb of a new process.
    			fcfsqueue.addProcess(newprocess);
    		}
    		//execute processes
    		fcfsqueue.executeProcess();
    		
    	}	
    }
     
    Last edited by a moderator: Feb 15, 2011

Share This Page