Hello I am trying to make this code to have something similar to this after run:
**************************************************** ************
FIFO simulation with 3 control cycles.
3 concurrent processes have been simulated.
There have been 1 context switches.
**************************************************** ************
RR - Round Robin simulation with 3 control cycles.
3 concurrent processes have been simulated.
There have been 3 context switches.
**************************************************** ************
HRN simulation with 3 control cycles.
3 concurrent processes have been simulated.
There have been 2 context switches.
**************************************************** ************
NMR simulation with 3 control cycles.
3 concurrent processes have been simulated.
There have been 3 context switches.
But for some reason I am having this:
**************************************** ********************
FIFO simulation with32control loops.
What I have tried:
#include <cstdio>
#include <cstdlib>
#include <conio.h>
#include <iostream>
#include <io.h>
#include<windows.h>
using namespace std;
FILE *textfile;
char regis[70];
class Proc{
public:
char identif;
int end;
int count;
int pri;
int level;
Proc()
{ identif = 0; end = 0; count = 0; pri = 0; level = 0;}; };
class processes {
public:
int cc; int np; char auxiden; int auxend; int auxcount; int auxpri; int auxlevel; int change; Proc p[50]; void start(); void enter_data(); void show(); void fifo(); void rrobin(); void hrn(); void rnm(); void end();
};
void processes::start(){
cout << "*******************************************************";
cout << "\n";
cout << "*Processor Allocation Strategies*" ;
cout << "\n";
cout << "*in concurrent execution. *";
cout << "\n";
cout << "*******************************************************";
cout << "\n";
cout << "\n";
cout << "Enter the number of control cycles for the simulation.";
cout << "\n";
cout << "(Suggested 30-40 per 10 processes):";
cin >> cc;
cout << "\n";
cout << "Enter the number of processes in the simulation.";
cout << "\n";
cout << "(The maximum allowed is 50):";
cin >> np;
cout << "\n";
if (np > 50)
{np=50;
cout << "The number of processes has been limited to 50. \n";
cout << "\n";
}
cout << "Open summary file 'PROCESOS.TXT' for recording. \n";
cout << "(BIN folder of TC folder).\n";
cout << "\n";
if ((textfile = fopen("processes.txt" , "a" )) == NULL)
{printf("Error opening for recording in file 'processes.txt' \n ");
exit(0);
}
}
void processes::enter_data()
{ char opc;
cout << "\nIf you want to manually enter the process id \n";
cout << "type: 's' , else: 'n' and the system will assume the ids. \n";
cin >> opc;
if (opc == 's' ) {
for ( int i=0; i<np; i++)
{cout <<"Enter the process ID p["<> p[i].identif;
}
}
else {
p[0].identif = 'a';
p[1].identif = 'b';
p[2].identif = 'c';
p[3].identif = 'd';
p[4].identif = 'e';
p[5].identif = 'f';
p[6].identif = 'g';
p[7].identif = 'h';
p[8].identif = 'i';
p[9].identif = 'j';
p[10].identif = 'k';
p[11].identif = 'l';
p[12].identif = 'm';
p[13].identif = 'n';
p[14].identif = 'o';
p[15].identif = 'p';
p[16].identif = 'q';
p[17].identif = 'r';
p[18].identif = 's';
p[19].identif = 't';
p[20].identif = 'u';
p[21].identif = 'v';
p[22].identif = 'w';
p[23].identif = 'x';
p[24].identif ='y';
p[25].identif = 'z';
p[26].identif ='1';
p[27].identif = '2';
p[28].identif ='3';
p[29].identif ='4';
p[30].identif ='5';
p[31].identif ='6';
p[32].identif = '7';
p[33].identif = '8';
p[34].identif = '9';
p[35].identif = 'A';
p[36].identif = 'B';
p[37].identif = 'C';
p[38].identif = 'D';
p[39].identif = 'E';
p[40].identif = 'F';
p[41].identif = 'G';
p[42].identif = 'H';
p[43].identif = 'I';
p[44].identif = 'J';
p[45].identif = 'K';
p[46].identif = 'L';
p[47].identif = 'M';
p[48].identif = 'N';
p[49].identif ='O';
}
for ( int i=0; i<np; i++)
{p[i].end = 0;
p[i].count = 0;
p[i].pri = 0;
p[i].level = 0;
}
}
void processes::show(){
printf("\n");
cout << "List of processes:" << "\n";
cout << "********************" << "\n" ;
cout << "Status: 0 -> Ready to run." << "\n";
cout << "State: 1 -> Interrupted waiting for input / output." << "\n";
cout << "Status: 2 -> Process terminated." << "\n";
cout << "Status: 3 -> Interrupted for time (not applicable in FIFO or HRN)."
<< "\n";
for (int i=0; i<np; i++)
{cout << "Process p[" << i << "]:" << p[i].identif << "Status:" << p[i].end <<
"Control cycles used:" << p[i].count << "\n";
cout << "Process p[" << i << "]:" << p[i].identif << "Priority:" << p[i].pri <<
"Level:" << p[i].level;
cout << "\n";
}
}
void processes::fifo(){
auxiden = 0; auxend = 0; auxcount = 0; change = 0;
fprintf(textfile,"%s\n","**************************************** ********************");
fprintf(textfile,"%s","FIFO simulation with");
fprintf(textfile,"%i",cc);
fprintf(textfile,"%s\n","control loops.");
cout << "\n";
cout << "Process selection sequence for execution according to FIFO:";
cout << "\n";
cout << "************************************************ ****************";
cout << "\n";
cout << "Processes are served according to their order in the list of ready processes:";
cout << "\n" << "\n";
for (int j=0; j<cc; j++) { auxend = 0;
for ( int m=0; m<np; m++)
{auxend = auxend + p[m].end;}
if (auxend == (np * 2))
{
cout << "\n" << "All processes have finished in" << j << "control cycles.\n";
fprintf(textfile,"%s" , "All processes have ended in");
fprintf(textfile,"%i" , j);
fprintf(textfile,"%s\n", "control cycles.");
j=cc;
auxend = getch();
}
for (int i=0; i<np; i++)
{if (p[i].end == 0)
{
cout << "Processor assigned to process:" << p[i].identif;
cout << "\n";
p[i].count = p[i].count + 1;
p[i].end = rand() % 3; cout << "\n" << p[i].identif << p[i].end << "\n" ;
if (p[i].end == 1)
cout << "Process" <<p[i].identif << "interrupted by input / output." <<"\n";
else {if (p[i].end == 2)
cout << "Process" <<p[i].identif << "terminated." <<"\n";
}
if (p[i].end > 0)
{ change = change + 1;
auxiden = p[i].identif;
auxend = p[i].end;
auxcount = p[i].count;
for ( int k=i; k<(np - 1); k++)
{p[k].identif = p[k+1].identif;
p[k].end = p[k+1].end; p[k].count = p[k+1].count;
}
p[(np - 1)].identif = auxiden;
p[(np - 1)].end = auxend;
p[(np - 1)].count = auxcount;
}
i = np;
auxend = getch();
}
for ( int k=0; k<np; k++) {if (p[k].end == 1) p[k].end = rand() % 2;
}
}
}
cout << "\n" << "***Occurred" << change << "context switches.***" <<
"\n";
fprintf(textfile,"%s","Simulated");
fprintf(textfile,"%i",np);
fprintf(textfile,"%s\n","concurrent processes.");
fprintf(textfile,"%s","Occurred");
fprintf(textfile,"%i", change);
fprintf(textfile,"%s\n","context changes.");
auxend = getch();
}
void processes::rrobin(){
auxiden = 0; auxend = 0; auxcount = 0; change = 0;
fprintf(textfile,"%s\n","**************************************** ********************");
fprintf(textfile,"%s","RR Simulation - Round Robin with");
fprintf(textfile,"%i", cc);
fprintf(textfile,"%s\n","control loops.");
cout << "\n";
cout << "Process selection sequence for execution according to RR - Round Robin:";
cout << "\n";
cout << "************************************************ ********************************";
cout << "\n";
cout << "Processes are served according to their order in the ready list,\n";
cout << "but they have a limited time (quantum) of the processor:";
cout << "\n" << "\n";
for ( int j=0; j<cc; j++) { auxend = 0;
for ( int m=0; m<np; m++)
{if (p[m].end != 2)
{ auxend = 1;
m=np;
}
}
if (auxend == 0)
{
cout << "\n" << "All processes have finished in" << j << "control cycles.\n";
fprintf(textfile,"%s","All processes have ended in");
fprintf(textfile,"%i",j);
fprintf(textfile,"%s\n" ,"control cycles.");
j=cc;
auxend = getch();
}
for ( int i=0; i<np; i++)
{if (p[i].end == 0)
{
cout << "Processor assigned to process:" << p[i].identif;
cout << "\n";
p[i].count = p[i].count + 1;
p[i].end = rand() % 4; cout << "\n" <<p[i].identif <<p[i].end << "\n";
if (p[i].end == 0) p[i].end = 3;
if (p[i].end == 1)
cout << "Process" <<p[i].identif << "interrupted by input / output." << "\n";
else {if (p[i].end == 2)
cout << "Process" <<p[i].identif << "terminated." << "\n";
else {if (p[i].end == 3)
cout << "Process" <<p[i].identif << "interrupted for time." << "\n";
}
}
if (p[i].end >= 0)
{ change = change + 1;
auxiden = p[i].identif;
auxend = p[i].end;
auxcount = p[i].count;
for ( int k=i; k<(np - 1); k++)
{p[k].identif = p[k+1].identif;
p[k].end = p[k+1].end;
p[k].count = p[k+1].count;
}
p[(np - 1)].identif = auxiden;
p[(np - 1)].end = auxend;
p[(np - 1)].count = auxcount;
}
i = np;
auxend = getch();
}
for ( int k=0; k<np; k++) {if (p[k].end == 1) p[k].end = rand() % 2;
}
for ( int l=0; l<np; l++) {if (p[l].end == 3)
{ auxend = rand() % 4;
if (auxend == 1) auxend = 0;
else {if (auxend == 2) auxend = 3;}
p[l].end = auxend;
}
}
}
}
cout << "\n" << "***Occurred" << change << "context switches.***" << "\n";
fprintf(textfile,"%s","Simulated");
fprintf(textfile,"%i",np);
fprintf(textfile,"%s\n","concurrent processes.");
fprintf(textfile,"%s","Occurred");
fprintf(textfile,"%i", change);
fprintf(textfile,"%s\n","context changes.");
auxend = getch();
}
void processes::hrn(){
auxiden = 0; auxend = 0; auxcount = 0; auxpri = 0; change = 0;
fprintf(textfile,"%s\n", "**************************************** ********************");
fprintf(textfile,"%s","HRN simulation with");
fprintf(textfile,"%i", cc);
fprintf(textfile,"%s\n","control loops.");
cout << "\n";
cout << "Process selection sequence for execution according to HRN:";
cout << "\n";
cout << "************************************************ ****************";
cout << "\n";
cout << "Processes are served according to their priority in the ready list;\n";
cout << "priority depends on response relation: (TE + TS) / TS, where\n";
cout << "TE = Waiting Time and TS = Service Time:";
cout << "\n" << "\n";
for (int j=0; j<cc; j++) { auxend = 0;
for ( int m=0; m<np; m++)
{auxend = auxend + p[m].end;}
if (auxend == (np * 2))
{
cout << "\n" << "All processes have finished in" << j << "control cycles.\n";
fprintf(textfile,"%s", "All processes have ended in");
fprintf(textfile,"%i" , j);
fprintf(textfile,"%s\n", "control cycles.");
j=cc;
auxend = getch();
}
if (j == 0)
{for ( int z=0; z<np; z++)
{p[z].count = 1;}
}
if (j < cc)
{
for ( int l=0; l<np; l++)
{p[l].pri = (j / p[l].count);
}
}
if (auxpri == 1)
{
for ( int s=0; s<np; s++)
{for ( int t=s; t<(np - 1); t++)
{if (p[t+1].pri > p[t].pri)
{auxiden = p[t].identif;
auxend = p[t].end;
auxcount = p[t].count;
auxpri = p[t].pri;
p[t].identif = p[t+1].identif;
p[t].end = p[t+1].end;
p[t].count = p[t+1].count;
p[t].pri = p[t+1].pri;
p[t+1].identif = auxiden;
p[t+1].end = auxend;
p[t+1].count = auxcount;
p[t+1].pri = auxpri;
}
}
}
}
for ( int i=0; i<np; i++)
{if (p[i].end == 0)
{ auxpri = 0;
cout << "Processor assigned to process:" << p[i].identif;
cout << "\n";
p[i].count = p[i].count + 1;
p[i].end = rand() % 3; cout << "\n" <<p[i].identif <<p[i].end << "\n";
if (p[i].end == 1)
cout << "Process" <<p[i].identif << "interrupted by input / output." << "\n";
else {if (p[i].end == 2)
cout << "Process" <<p[i].identif << "terminated." << "\n";
}
if (p[i].end > 0)
{ change = change + 1;
auxiden = p[i].identif;
auxend = p[i].end;
auxcount = p[i].count;
auxpri = p[i].pri;
for ( int k=i; k<(np - 1); k++)
{p[k].identif = p[k+1].identif;
p[k].end = p[k+1].end;
p[k].count = p[k+1].count;
p[k].pri = p[k+1].pri;
}
p[(np - 1)].identif = auxiden;
p[(np - 1)].end = auxend;
p[(np - 1)].count = auxcount;
p[(np - 1)].pri = auxpri;
auxpri = 1; }
i = np;
auxend = getch();
}
for ( int k=0; k<np; k++) {if (p[k].end == 1) p[k].end = rand() % 2;
}
}
}
for ( int y=0; y<np; y++)
{p[y].count = p[y].count - 1;}
cout << "\n" <<"***<< change<< context changes occurred.***" << "\n";
fprintf(textfile,"%s","Simulated");
fprintf(textfile,"%i",np);
fprintf(textfile,"%s\n","concurrent processes.");
fprintf(textfile,"%s","Occurred");
fprintf(textfile,"%i", change);
fprintf(textfile,"%s\n","context changes.");
auxend = getch();
}
void processes::rnm(){
auxiden = 0; auxend = 0; auxcount = 0; auxpri = 0; auxlevel = 0; change = 0;
fprintf(textfile,"%s\n", "**************************************** ********************");
fprintf(textfile,"%s","RNM simulation with");
fprintf(textfile,"%i", cc);
fprintf(textfile,"%s\n" ,"control cycles.");
cout << "\n";
cout << "Process selection sequence for execution according to RNM:";
cout << "\n";
cout << "************************************************ ****************";
cout << "\n";
cout << "Processes are served according to their level in the ready list;\n";
cout << "but they have a limited time (quantum) of the processor;\n";
cout << "if they are interrupted by input / output they remain in the subqueue\n";
cout << "of the level where they are, but if they are interrupted by time they go to\n";
cout << "a higher level, which is served when there are no more ready processes\n";
cout << "in the lower levels; hence the name of\n";
cout << "Multi-Level Feedback:";
cout << "\n" << "\n";
for (int j=0; j<cc; j++) { auxend = 0;
for ( int m=0; m<np; m++)
{if (p[m].end != 2)
{ auxend = 1;
m=np;
}
}
if (auxend == 0)
{
cout << "\n" << "All processes have finished in" << j << "control cycles.\n";
fprintf(textfile,"%s","All processes have ended in");
fprintf(textfile,"%i",j);
fprintf(textfile,"%s\n", "control cycles."); j=cc;
auxend = getch();
}
if (auxpri == 1)
{
for ( int s=0; s<np; s++)
{for ( int t=s; t<(np - 1); t++)
{if (p[t+1].level < p[t].level)
{auxiden = p[t].identif;
auxend = p[t].end;
auxcount = p[t].count;
auxpri = p[t].pri;
auxlevel = p[t].level;
p[t].identif = p[t+1].identif;
p[t].end = p[t+1].end;
p[t].count = p[t+1].count;
p[t].pri = p[t+1].pri;
p[t].level = p[t+1].level;
p[t+1].identif = auxiden;
p[t+1].end = auxend;
p[t+1].count = auxcount;
p[t+1].pri = auxpri;
p[t+1].level = auxlevel;
}
}
}
}
for ( int i=0; i<np; i++)
{if (p[i].end == 0)
{ auxpri = 0;
cout << "Processor assigned to process:" << p[i].identif;
cout << "\n";
p[i].count = p[i].count + 1;
p[i].end = rand() % 4; cout << "\n" <<p[i].identif <<p[i].end <<p[i].level << "\n";
if (p[i].end == 0) p[i].end = 3;
if (p[i].end == 1)
cout << "Process" <<p[i].identif << "interrupted by input / output." << "\n";
else {if (p[i].end == 2)
cout << "Process" <<p[i].identif << "terminated." << "\n";
else {if (p[i].end == 3)
cout << "Process" <<p[i].identif << "interrupted for time." << "\n";
p[i].level = p[i].level + 1;
}
}
if (p[i].end > 0)
{ change = change + 1;
auxiden = p[i].identif;
auxend = p[i].end;
auxcount = p[i].count;
auxpri = p[i].pri;
auxlevel = p[i].level;
for ( int k=i; k<(np - 1); k++)
{p[k].identif = p[k+1].identif;
p[k].end = p[k+1].end;
p[k].count = p[k+1].count;
p[k].pri = p[k+1].pri;
p[k].level = p[k+1].level;
}
p[(np - 1)].identif = auxiden;
p[(np - 1)].end = auxend;
p[(np - 1)].count = auxcount;
p[(np - 1)].pri = auxpri;
p[(np - 1)].level = auxlevel;
auxpri = 1; }
i = np;
auxend = getch();
}
for ( int k=0; k<np; k++) {if (p[k].end == 1) p[k].end = rand() % 2;
}
for ( int l=0; l<np; l++) {if (p[l].end == 3)
{ auxend = rand() % 4;
if (auxend == 1) auxend = 0;
else {if (auxend == 2) auxend = 3;}
p[l].end = auxend;
}
}
}
}
cout << "\n" << "***Occurred" << change << "context switches.***" << "\n";
fprintf(textfile,"%s", "Simulated");
fprintf(textfile,"%i",np);
fprintf(textfile,"%s\n","concurrent processes.");
fprintf(textfile,"%s","Occurred");
fprintf(textfile,"%i", change);
fprintf(textfile,"%s\n","context changes.");
auxend = getch();
}
void processes::end(){
cout << "\nClose summary file 'PROCESSES.TXT'.\n";
cout << "It is suggested to view its content with Windows Notepad.exe.\n" ;
fclose(textfile);
}
int main(){
processes p1;
system("cls");
p1.start();
p1.enter_data();
p1.show();
p1.fifo();
p1.show();
p1.enter_data();
p1.show();
p1.rrobin();
p1.show();
p1.enter_data();
p1.show();
p1.hrn();
p1.show();
p1.enter_data();
p1.show();
p1.rnm();
p1.show();
p1.end();
getch();
}