Process Scheduling Simulation Program

Source: Internet
Author: User

Process Scheduling Simulation Program

In our course design, I chose a process scheduling simulation. I hope you can give me more comments. I haven't posted a post for a long time.

# Include <iostream. h>

# Include <stdlib. h>

# Include <time. h>

# Include <stdio. h>

# Include <string. h> const int MAXCOMMANDLEN = 50; //////////////////////////////////////// //////////////////////////////////////// /////

//

// PROCESS

//

//////////////////////////////////////// //////////////////////////////////////// /////

Class Process // Process class

{

Friend class CPU;

Protected:

Static int init_ID; // random process ID

Int ID; // process ID

Char runText [MAXCOMMANDLEN]; // process command Array

Int IP address; // process instruction pointer, which stores the specific location where the process instruction is executed

Bool ISuseSource; // whether the process uses resources; true: in use; false: not in use

Bool ISblocked; // whether the process is blocked. True: blocked. false: not blocked.

Int unitTime; // The cpu execution time of the Process Unit. The default value is 1.

Int blockTime; // process blocking time

Public:

Static void RandID (); // randomly generated process ID

Process ();

Int getID ();

Int getIP ();

Void setIP (int );

Void Runed (); // The process is executed by the cpu.

Int getUnittime (); // obtain the execution time of the Process Unit.

Int getBlcoktime (); // get the process blocking time

Void setBlocktime (int); // sets the process blocking time.

Void setUnittime (int); // sets the execution time of the Process Unit.

Char getResult (int); // get the process execution result

Char * getRuntext (); // get the command for executing the process

Void setBlockstate (bool); // sets the blocking status

Bool getBlockstate ();

Bool getISusesource (); // you can call this operation to obtain the resource status.

Void setISusesource (bool); // you can specify the resource usage status.

}; Int Process: init_ID; void Process: RandID ()

{

Srand (unsigned) time (NULL ));

Init_ID = rand ();

}

Process: Process ()

{

ID = init_ID ++;

Int commandLen;

IP = 0; cout <"Please in put the text which process runed by CPU [# command #]:> ";

Cin> runText;

If (commandLen = strlen (runText)> MAXCOMMANDLEN)

Exit (0 );

RunText [commandLen] = '#'; // command end flag '#'

RunText [commandLen + 1] = '';

ISuseSource = false;

ISblocked = false;

UnitTime = 1;

BlockTime = 0;

}

Void Process: Runed ()

Int Process: getID ()

{

Return ID;

} Int Process: getIP ()

{

Return IP;

} Void Process: setIP (int ip)

Bool Process: getISusesource ()

{

Return ISuseSource;

} Void Process: setISusesource (bool s)

Char * Process: getRuntext ()

{

Return runText;

} Int Process: getUnittime ()

{

Return unitTime;

}

Int Process: getBlcoktime ()

{

Return blockTime;

} Void Process: setBlocktime (int BT)

Void Process: setUnittime (int UT)

Void Process: setBlockstate (bool state)

Bool Process: getBlockstate ()

{

Return ISblocked;

} Char Process: getResult (int k)

{

Return runText [k];

}/////////////////////////////////////// //////////////////////////////////////// //////

//

// SOURCE

//

//////////////////////////////////////// //////////////////////////////////////// //// Class Source // Resource class

{

Protected:

Int ID; // resource ID

Bool state; // Resource Status true: not occupied by the process false: occupied

Int pro_ID; // the id of the process that uses the resource

Process * pro; // Process pointer using resources

Int time; // The time when the process uses resources.

Public:

Source (int );

Bool getState (); // get the Process status

Void setState (bool); // sets the Process status

Void setTime (int); // sets the time when the process uses resources.

Void setPro (Process *); // sets the Process that uses the resource.

Int getID (); // obtain the resource id.

Int getPorID (); // process id of the resource to be used

Void setProID (int); // sets the process id of the Resource

Void runned (); // The resource is called by the cpu.

}; Source: Source (int id)

Void Source: setProID (int id)

Void Source: setTime (int t)

Void Source: setState (bool s)

Bool Source: getState ()

{

Return state;

} Void Source: setPro (Process * p)

Void Source: runned ()

{

If (time> 0)

{

Cout <"(Source:" <ID <")";

Time --;

}

If (time <= 0) // The process releases the resource after the resource is used.

{

Pro-> setISusesource (false );

Int ip = pro-> getIP ();

Pro-> setIP (++ ip );

Source: setState (true );

Cout <endl <"The process" <pro-> getID () <"relase the source! "<Endl;

Pro = NULL;

}

}/////////////////////////////////////// //////////////////////////////////////// //////

//

// CPU

//

//////////////////////////////////////// //////////////////////////////////////// //// Typedef strUCt Block // Block the queue Structure

{

Process * p_BlockProcess; // blocked Process queue

Int index; // The index (location) of the blocked process in the ready queue)

} Block; class CPU

{

Protected:

Process * p_Process; // Process queue

Process ** pp_Process; // Process ready queue

Block * blockQueue; // process blocking queue

Source * p_Source; // resource pointer

Int numOfprocess; // number of processes

Int numOfblock; // Number of blocked Processes

Int PC; // program counter

Int allTime; // total cpu running time

Public:

CPU (int );

Void Run (); // cpu running process

Bool _ IC (Process &); // virtual IC for Process instruction Translation

Void useSource (Process &); // The Process requests resources.

Void blockProcess (Process &); // block the Process

Void releaseBlockPro (); // release the blocking Process

Int getAlltime (); // obtain the total time of running the process.

Void displayPro (); // displays the basic information, id, command, and running time of a process.

Void blockTimeADD (); // The blocking time is increased by 1.

}; CPU: CPU (int num)

{

P_Source = new Source (379857 );

NumOfprocess = num;

NumOfblock = 0;

AllTime = 0;

P_Process = new Process [numOfprocess];

Pp_Process = new Process * [numOfprocess];

BlockQueue = new Block [numOfprocess];

For (int I = 0; I <numOfprocess; I ++)

} Int CPU: getAlltime ()

{

Return allTime;

} Void CPU: displayPro ()

{

For (int I = 0; I <numOfprocess; I ++)

{

Cout <"Process ID:" <p_Process [I]. getID () <endl;

Cout <"text of runned:" <p_Process [I]. getRuntext () <endl;

}

} Void CPU: Run ()

{

Int numPro = numOfprocess; do

{

For (int num = 0; num <numOfprocess; num ++)

{

If (! Pp_Process [num]) // if the pointer is null, the process is not in the ready queue.

Continue; for (int t = 0; t <p_Process [num]. getUnittime (); t ++)

{

PC = p_Process [num]. getIP ();

If (_ IC (p_Process [num])

{

If (t = 0)

Cout <"the process [" <p_Process [num]. getID () <"] runed :";

If (! P_Process [num]. getISusesource ())

Else

{

P_Source-> runned ();

If (p_Source-> getState () & numOfblock> 0) // release the blocking Process

}

}

Else

{

If (! P_Process [num]. getBlockstate ())

{

NumPro --;

Pp_Process [num] = NULL;

Continue;

}

Break;

}

AllTime ++;

If (numOfblock> 0)

BlockTimeADD ();

} // End for t...

If (p_Process [num]. getUnittime ())

P_Process [num]. setUnittime (1 );

Cout <endl;

} // End for num...} while (numPro );

} Bool CPU: _ IC (Process & p)

{

// Translate commands in the process

Char resultRunned;

ResultRunned = p. getResult (PC );

If (resultRunned = '#')

Return false;

Else

{

If (resultRunned = ') // request resource instruction

{

PC ++;

P. setIP (PC );

ResultRunned = p. getResult (PC );

If (resultRunned> = '1' & resultRunned <= '9 ')

{

If (p_Source-> getState ())

{

// Use the resource if the resource is not used

UseSource (p );

Cout <"The process" <p. getID () <"take up the source! "<Endl;

}

Else

{

// If the resource is used, the process is blocked.

BlockProcess (p );

Cout <"The process" <p. getID () <"is blocked! "<Endl;

Return false ;}

}

Else

{

// 'Is not followed by a number. The syntax is incorrect.

Cout <"The process [" <p. getID () <"] runned fail! It has been stopped! "<Endl;

Return false;

}

}}

Return true;

} Void CPU: blockTimeADD ()

{

For (int I = 0; I <numOfblock; I ++)

{

Int BT = blockQueue [I]. p_BlockProcess-> getBlcoktime ();

BlockQueue [I]. p_BlockProcess-> setBlocktime (++ BT );

}

} Void CPU: useSource (Process & p)

{

P. setISusesource (true );

P_Source-> setState (false );

P_Source-> setProID (p. getID ());

P_Source-> setTime (p. getResult (PC)-'0 ');

P_Source-> setPro (& p);} void CPU: blockProcess (Process & p)

{

Int tempIndex = numOfprocess-(Process: init_ID-p.getID ());

BlockQueue [numOfblock]. p_BlockProcess = & p;

BlockQueue [numOfblock]. index = tempIndex;

NumOfblock ++;

Int ip = p. getIP ();

P. setIP (-- ip );

P. setBlockstate (true );

P. setBlocktime (1 );

P. setUnittime (0 );

Pp_Process [tempIndex] = NULL;} void CPU: releaseBlockPro ()

{

// Release the first process that blocks the queue because it has the longest blocking time

Pp_Process [blockQueue [0]. index] = blockQueue [0]. p_BlockProcess;

BlockQueue [0]. index =-1;

BlockQueue [0]. p_BlockProcess-> setBlockstate (false );

BlockQueue [0]. p_BlockProcess-> setUnittime (blockQueue [0]. p_BlockProcess-> getBlcoktime ());

BlockQueue [0]. p_BlockProcess-> setBlockstate (0 );

BlockQueue [0]. p_BlockProcess = NULL;

NumOfblock --;

// Block other processes in the queue from moving forward to one position

For (int I = 0; I <numOfblock; I ++)

}

//////////////////////////////////////// //////////////////////////////////////// /////

//

// The main progress

//

//////////////////////////////////////// //////////////////////////////////////// /////

Void main ()

{

Int num;

Cout <"************************************ * ******************* "<endl

<Endl;

Cout <"The virtual CPU the process runned" <endl

<Endl;

Cout <"************************************ * ******************* "<endl

<Endl;

Cout <"initialize the information of processes" <endl; cout <"Please in put the number of process [# command #]>"; try

Catch (int)

{

Cout <"You in put the numbers of process is error! "<Endl;

Exit (1 );

} Process: RandID (); // randomly generate the ID of the first Process, and Add 1 to the Process ID generated later

CPU virtualCPU (num );

Cout <"Pocesses runed by CPU" <endl;

VirtualCPU. Run ();

Cout <"Processes runned over! "<Endl;

Cout <"************************************ * ******************* "<endl

<Endl;

Cout <"The time which processes runned by CPU:" <virtualCPU. getAlltime () <endl

<Endl;

VirtualCPU. displayPro ();

Cout <"************************************ * ******************* "<endl

<Endl;

} There is no API call here. It is just a simulation. I have set my own syntax for the process. When I input a general character, the cpu is only output normally, if you encounter ', it indicates that the process must call the system resources and follow a number to indicate the time occupied. if the resources are idle, it occupies the resources; otherwise, it is blocked. when resources are released, it occupies the resources. I used and improved the time slice Rotation Algorithm to call a process. When a process is released from a blocked queue, it will make up the time wasted due to waiting for resources.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.