?
Solved

Scheduling program in C++

Posted on 2006-03-23
24
Medium Priority
?
23,868 Views
1 Endorsement
Last Modified: 2012-05-05
I am doing FCFS, SJF preemptive, SJF non-preemptive, Priority FCFS, RR scheduling algorithms using c++. Assume that I have 3 jobs stream such as  job stream: (A,0,0,10,2,10,3), (B,0,0,10,2,5,2), (C,0,15,15,10,15,5)
A job will be represented as follows:
     (job id, priority, arrival time, CPU1, IO1, ..., CPUn, IOn)
I know the concept of scheduling algorithms. I have already do working on paper. I have difficulties in converting them into programming.

My programs need to do the following thing.

Input specification
-------------------
User will be prompted for:
  Job stream source:
    if file, prompt for file name
    if random generation,
       jobid's should be A, B, C, ..., , etc.
       prompt for number of jobs,
       prompt for min and max of ranges for:
         priority
         arrival time
         number of CPU/IO burst pairs
         CPU burst size
         IO burst size
  Scheduling algorithm (display choices) -
    prompt for parameters as needed, i.e. quantum size for RR
  Suppress table output? Hit y to suppress, otherwise hit Enter.

I need to calculate

Output specification
--------------------
  Summary of user's input info
  blank line
  Job stream in above format, one job per line
  blank line
  Scheduliing trace table (unless supressed)
  blank line

CPU utilization =
IO utilization  =
Avg. turnaround time =
Waiting time (in ready queue): A:       B:       C:  
Avg. waiting time =

Could you give me some idea how to start with. I will start writing it today and keep posting my post.

Thanks you.
gugu

1
Comment
Question by:gugu_1500
22 Comments
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 16277271
Try and post the code
0
 

Author Comment

by:gugu_1500
ID: 16277620
#include<iostream>
#include <process.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>

using namespace std;

struct processors      //PROCESS ATTRIBUTES//
  {
   float burst;
   float arrival;
   int priority;
   float numofpairs;
   float cpu;
   float io;
   int num;
  }p[20],temp;
 
 
void main(int)
{

      int n,c;

      cout <<"ENTER THE NUMBER OF JOBS:\t";
      cin >> n;
      
      for(int i=0;i<n;i++)
  {
        
        printf("JOB ID:%c\n",__toascii(i+65));//convert integer to ascii character
        //cout<<__toascii(i+65);
        printf("PRIORITY :\t"); //
        scanf("%d",&p[i].priority); // capturing user input
        printf("ARRIVAL TIME :\t");
        scanf("%f",&p[i].arrival);
        printf("NUMBER OF CPU BURST/IO BURST PAIRS :\t");
        scanf("%f",&p[i].numofpairs);
        printf("CPU BURST SIZE :\t");
        scanf("%f",&p[i].cpu);
        printf("IO BURST SIZE :\t");
        scanf("%f",&p[i].io);
        
        p[i].num = i+1;
        _getch();
        }

printf("CPU SCHEDULING\n");
 printf("1.FCFS scheduling\n");
 printf("2.SHORTEST JOB FIRST pre emptve scheduling\n");
 printf("3.SHORTEST JOB FIRST non preemptive scheduling\n");
 printf("4.PRIORITY scheduling\n");
 printf("5.ROUND ROBIN scheduling\n");
 printf("ENTER YOUR CHOICE:\t");
 scanf("%d",&c);
 switch(c)
 {
   case 1:
              {
                fcfs();
                break;
              }
   case 2:
              {
                sjfp();
                break;
              }
   case 3:
              {
                sjfnp();
                break;
              }
   case 4:
              {
                priority();
                break;
              }
   case 5:
              {
                roundrobin();
                break;
              }
 }
  getch();

return 0;
}
0
 
LVL 8

Accepted Solution

by:
deepu chandran earned 600 total points
ID: 16278071

//here u need a queue of process
  //we can create like this
struct queue
  {
    struct processors pcb;//this is not processor it is process control block(pcb)
    int rear;
    int front;
  };
void fcfs()
{
 //then you have to add the pcb's into the queue depends on the time they have created
 //then assign the pcb to the process
}

void sjfp()
{
  //here also you need a queue or u can use array depents on your conveniance
  //first find out the shortest cpu burst and assign the pcb to the cpu
}
   
void sjfnp(int noofprocess)
{
  // here best way is array it declire as follows this should be sorted with cpu burst
  processors pcb[20];
  int noofprocess;// for no.processes now in cpu
  //just assign pcb[0] to the cpu
  //during this time check whether any process is comeing or not,
  //in your program no need for checking this also noneed for nonpreemtive scheduling        
}

void priority()
{
   //in this algorithm u just need an array it should be sorted using priority
   //the rest are just like others
}
void roundrobin()
{
  //here you can use the struct queue structure
  //also you have to specify the timestamp value
  int timestamp=2;//like this
   
}
             
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:gugu_1500
ID: 16286337
I have not started algorithm parts yet. Here is my codes with some updates.

#include<iostream>
#include <process.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>
#include<dos.h>
using namespace std;


struct processors      //PROCESS ATTRIBUTES//
  {
   float burst;
   float arrival;
   int priority;
   float numofpairs;
   float cpu;
   float io;
   int num;
  }p[20],temp;

  void fcfs()
{
      cout<<"FCFS";
}
 void sjfp()
{
      cout<<"SJFP";
}
  void sjfnp()
{
      cout<<"SJFNP";
}

  void priority()
{
      cout<<"priority";
}
  void rr()
{
      cout<<"rr";
}

void main()
{
      int n,ch;
      
      cout <<"ENTER THE NUMBER OF JOBS:\t";
      cin >> n;
      
      for(int i=0;i<n;i++)
  {
        
        printf("JOB ID:%c\n",__toascii(i+65));//convert integer to ascii character
        printf("PRIORITY :\t"); //
        scanf("%d",&p[i].priority); // capturing user input
        printf("ARRIVAL TIME :\t");
        scanf("%f",&p[i].arrival);
        printf("NUMBER OF CPU BURST/IO BURST PAIRS :\t");
        scanf("%f",&p[i].numofpairs);
        //cout<< p[i].numofpairs;
        
        for(int j=0;j<p[i].numofpairs;j++)
      
        {
            printf("CPU BURST SIZE :\t");
            scanf("%f",&p[i].cpu);
            printf("IO BURST SIZE :\t");
            scanf("%f",&p[i].io);
            cout<<p[i].cpu;
            cout<<p[i].io;
        }
        

        p[i].num = i+1;
        _getch();
        }

      printf("CPU SCHEDULING\n");
      printf("----------------\n");
      printf("1.FCFS \n");
      printf("2.SHORTEST JOB FIRST (preemptive)\n");
      printf("3.SHORTEST JOB FIRST (non-preemptive)\n");
      printf("4.PRIORITY\n");
      printf("5.ROUND ROBIN\n");
      printf("ENTER YOUR CHOICE:\t");
      scanf("%d",&ch);
      switch(ch) // Select a scheduling from the list
      {
      case 1: {      fcfs();
                        break;
                  }

      case 2:      {      sjfp();
                        break;
                  }

      case 3:      {      sjfnp();
                        break;
                  }
      
      case 4:      {      priority();
                        break;
                  }

      case 5:      {      rr();
                        break;}
            }
            _getch();
          _putch('\r');
      
}
Thank u
gugu
0
 

Author Comment

by:gugu_1500
ID: 16290237
struct processors      //PROCESS ATTRIBUTES//
  {
   float burst;
   float arrival;
   int priority;
   float cpu;
   float io;
   int num;
   char jobid;
  }p[20],temp;

struct ssss
 {
  int no;
  int xx;
  float arr;
  float burst;
  float ww;
  int pri;
  float wait;
 }process[N];


void fcfs(int n)
{
      int i,w,y=0,pr[30],l=0;
      float awt = 0,wt[10],s=0,iot,q=0,g[100],fwt,s1;
      s = p[0].arrival;
      s1= p[0].arrival;
   //cout<< p[0].arrival;
   
   
   //cout<<p[0].burst;

   s = s + p[0].burst;// cpu burst finished
   s1= s1 + p[0].burst;

   s1= s1 + p[0].io; // io finished
   //first process is completed
  // cout<<s;
  for(i=1;i<n;i++)
  {
  if(p[i].arrival>s)              //Checks for idle time
               {
             wt[i] = 0;
            
             s = p[i].arrival;
      
             s = s + p[i].burst;
             s1= s+ p[i].io;      

  }
              else
             {
                        //completes the i th process
             s = s + p[i].burst;//cpu2 finished
         s1 = s+ p[i].io;// io2 finished
         cout << s1;
             wt[i] = s - p[i].arrival - p[i].burst;

             //cout<<wt[i];
                                
  }
   }

 
  for(i=1;i<n;i++)
   {
             q = q + wt[i];              //Calculates the sum of all the process'waiting time
   }
     awt = q/n;  
      
}

 
 void sjfp()
{
      cout<<"SJFP";
}
  void sjfnp()
{
      cout<<"SJFNP";
}

  void priority()
{
      cout<<"priority";
}
  void rr()
{
      cout<<"rr";
}

void main()
{
      int numofpairs,n,ch;
      
      printf("ENTER THE NUMBER OF JOBS:\t");
      scanf("%d",&n);
      for(int i=0;i<n;i++)
  {
        
        printf("JOB ID:%c\n",__toascii(i+65));//convert integer to ascii character
        //scanf("%c%",&p[i].jobid);
        printf("PRIORITY :\t"); //
        scanf("%d",&p[i].priority); // capturing user input
        printf("ARRIVAL TIME :\t");
        scanf("%f",&p[i].arrival);
        printf("NUMBER OF CPU BURST/IO BURST PAIRS :\t");
        //cout<<p[i].arrival;
        scanf("%d",&numofpairs);
        //cout<< p[i].numofpairs;
        
        for(int a=0;a<numofpairs;a++)
      
        {
            printf("CPU BURST SIZE :\t");
            scanf("%f",&p[i].burst);
            printf("IO BURST SIZE :\t");
            scanf("%f",&p[i].io);
            //cout<<p[i].cpu;
            //cout<<p[i].io;
        }
        

        p[i].num = i+1;
       // cout<<p[i].num;
        _getch();
        }
      
      for(int i=0;i<n;i++)
   {
    for(int j=i;j<n;j++)
      {     if(p[i].arrival > p[j].arrival)
      {
                 temp = p[i];
                 p[i] = p[j];
                 p[j] = temp;
                  
              }
     }
   }
 
   
      printf("CPU SCHEDULING\n");
      printf("----------------\n");
      printf("1.FCFS \n");
      printf("2.SHORTEST JOB FIRST (preemptive)\n");
      printf("3.SHORTEST JOB FIRST (non-preemptive)\n");
      printf("4.PRIORITY\n");
      printf("5.ROUND ROBIN\n");
      printf("ENTER YOUR CHOICE:\t");
      scanf("%d",&ch);
      
      switch(ch) // Select a scheduling from the list
      {
      case 1: {      fcfs(n);
                        break;
                  }

      case 2:      {      sjfp();
                        break;
                  }

      case 3:      {      sjfnp();
                        break;
                  }
      
      case 4:      {      priority();
                        break;
                  }

      case 5:      {      rr();
                        break;}
            }
            _getch();
          
      
}



THis code works for one CPU and I/O pair for FCFS . But it does not work If CPU and I/O burst is more than one pair.

Please help !!!!!
0
 

Author Comment

by:gugu_1500
ID: 16297033
#include<iostream>
#include <process.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>
#include <dos.h>
#define N  100
#define NULL 0

using namespace std;


struct processors      //PROCESS ATTRIBUTES//
  {
   float burst[20];
   float arrival;
   int priority;
   float cpu;
   float io[20];
   int num;
   char jobid;
   int numofpairs;
  }p[20],temp;

struct ssss
 {
  int no;
  int xx;
  float arr;
  float burst;
  float ww;
  int pri;
  float wait;
 }process[N];

struct queue1
{
  char front;
  char rear;
}job[20];


struct queue2
{
  char front;
  char rear;
}io[20];

struct queue3
{
  char space;
}cpu[20];
struct queue4
{
  char space1;
}ready[20];


 void fcfs(int n)
 {  
       float t,t1;
       for(int i=0;i<n;i++)
    {
    for(int j=i;j<n;j++)
       {     if(p[i].arrival > p[j].arrival)
           {
                 temp = p[i];
                 p[i] = p[j];
                 p[j] = temp;
                  
              }
     }
   }
 
for(int y=0;y<n;y++)
{
      job[y].front=p[y].jobid;
      cout<<job[y].front;
}


t=p[0].arrival;

for(int j=0;j<2;j++)
{
for(int x=0;x<=n;x++)
 {  
        // add 'A' arrival time to t

       job[x].front=NULL; //add Null to jobid[1] ,clear job A from job queue

          

       if( t <= p[x].arrival & cpu[x].space == NULL )
       {  
             cpu[x].space=p[x].jobid;
             t= t + p[x].burst[j];
             t1= t + p[x].io[j];
       }
       else if( t <= p[x].arrival & cpu[x].space != NULL )
       {
             ready[x].space1=p[x].jobid;
       }

       else
       {
             t= t + p[x].burst[j];          
             t1= t+ p[x].io[j];  
       }
        
 }
}
 cout<<t;
 cout<<t1;

cpu[0].space=NULL;
   
   
 }

 void sjfp()
{
      cout<<"SJFP";
}
  void sjfnp()
{
  }

 void priority()
 {
      

}
  void rr()
{
      cout<<"rr";
}

void main()
{
      int a,numofpairs,n,ch,t[20];
      char jobid;
      printf("ENTER THE NUMBER OF JOBS:\t");
      scanf("%d",&n);
      for(int i=0;i<n;i++)
  {
        p[i].jobid=__toascii(i+65);
        printf("JOB ID:%c\n",__toascii(i+65));//convert integer to ascii character
        
        //scanf("%c%",&p[i].jobid);
        printf("PRIORITY :\t"); //
        scanf("%d",&p[i].priority); // capturing user input
        printf("ARRIVAL TIME :\t");
        scanf("%f",&p[i].arrival);
        printf("NUMBER OF CPU BURST/IO BURST PAIRS :\t");
        //cout<<p[i].arrival;
        scanf("%d",&p[i].numofpairs);
        //cout<< p[i].numofpairs;
        
        for(a=0;a<p[i].numofpairs;a++)
      
        {
            printf("CPU BURST SIZE :\t");
            scanf("%f",&p[i].burst[a]);
            //t[a]=p[i].burst;
            printf("IO BURST SIZE :\t");
            scanf("%f",&p[i].io[a]);
            //t[a]=p[i].io;

            //cout<<p[i].cpu;
            //cout<<p[i].io;
        }
        
     
        p[i].num = i+1;
       // cout<<p[i].num;
        _getch();
        }
      
      
   
      printf("CPU SCHEDULING\n");
      printf("----------------\n");
      printf("1.FCFS \n");
      printf("2.SHORTEST JOB FIRST (preemptive)\n");
      printf("3.SHORTEST JOB FIRST (non-preemptive)\n");
      printf("4.PRIORITY\n");
      printf("5.ROUND ROBIN\n");
      printf("ENTER YOUR CHOICE:\t");
      scanf("%d",&ch);
      
      switch(ch) // Select a scheduling from the list
      {
      case 1: {      fcfs(n);
                        break;
                  }

      case 2:      {      sjfp();
                        break;
                  }

      case 3:      {      sjfnp();
                        break;
                  }
      
      case 4:      {      priority();
                        break;
                  }

      case 5:      {      rr();
                        break;
                  }
            
          
      
}


}
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 16309407
It is not easy to understand your problem cause you posted a lot of code without pointing out where you think it is wrong. Furthermore, the code turned to pure C code what doesn't help to make it more understandable.

I'll try to point out the sequences that should be changed. If you want to go that way we will solve your problem.

1. Includes

In C++ you only need

#include <iostream> // for cout/cin
#include <string>      // not string.h
#include <vector>     // for dynamic arrays
using namespace std;

All other includes were for C runtime functions only which you don't need.

2. Global variables

struct processors      //PROCESS ATTRIBUTES//
  {
   float burst[20];
   float arrival;
   int priority;
   float cpu;
   float io[20];
   int num;
   char jobid;
   int numofpairs;
  }p[20],temp;

Here you define a structure (which is equivalent to a class in C++) *and* a global array and a global temporary.Generally, you should separate class definition from variable definition and avoid defining global variables if you don't have to.

struct Processor    // use singular as class name
{
   enum { MAX_PAIRS = 20 };   // use enum for constant integers

   float burst[MAX_PAIRS ];
   float arrival;
   int priority;
   float cpu;
   float io[MAX_PAIRS];
   int num;
   char jobid;
   int numofpairs;
};

Note, you also could make a helper

struct BurstIO
{
    float burst;
    float io;
    BurstIO() : burst(0.0), io(0) {}
};

and change struct Processor to

struct Processor    // use singular as class name
{
   vector<BurstIO> burstIO;
   float arrival;
   int priority;
   float cpu;
   int num;
   char jobid;
   Processor() : arrival(0.0), priority(0), cpu(0.0), num(0), jobid(' ') {}
};

Here you can get rid of 'numofpairs' as well cause the vector already manages its current array size. Note, I added a constructor to get a proper initialization

The global variables should be turned to local variables in your main function (or better to data member in your main class but let's go small steps).

int main()
{
     // if using only plain C types in Processor you might do it like that:
     Processor temp = { 0 };  // init an empty struct
     vector<Processor> pi(20, temp);

     // if using the C++ version with constructor you have
     Processor temp;  // here the constructor does the initialization
     vector<Processor> pi(20); // same as here

    // you also can use C arrays if you don't need dynamic arrays
     Processor temp = { 0 };  // init an empty struct
     Processor pi[20] = { 0 };// init array

    ....

    return 0;
}

3. struct names

>>>> struct ssss
>>>> struct queue1
>>>> struct queue2
>>>> struct queue3
>>>> struct queue4

Actually, that are really poor names. Generally, you shouldn't name a class with a plural name if the class represents one element only. In fact all 'queue' structs are definitively wrongly named.

'ssss' might be renamed to 'Process'.  'queue1' and 'queue2' can be one struct and as you did use only the 'front' member I actually don't know the purpose of that struct.  'queue3' and 'queue4' are same as well and seem to be supposed to store one character for job id only. If that isn't supposed to get enhanced I would use a (unsigned) char array rather than a struct. Otherwise call it 'JobID' and move the global arrays to function main same as all other global variables.

4. Global functions

>>>> void fcfs(int n)

After moving the global variables/arrays to main function you could not access them in the global functions. You could pass them as arguments like

   void fcfs(int n, Processor pi[], Process process[], ...)

Much better is to move all arrays to a class and make fcfs and the other functions member functions:

class Schedule
{
      enum { MAX_PROC = 20 };

      Processor p[MAX_PROC];
      Process    process [MAX_PROC];  
      ...

public:
    ...
    void fcfs(int n);
    void sjfp();
    ...
};

In main you would need:

int main()
{
      Schedule sched;
      ...
                  case 1: {     sched.fcfs(n);  
      ...
}


5. User interface

Your prog made a mix between new iostream classes via cout/cin and C I/O via printf/scanf. Actually, the latter shouldn't be used in C++.

Instead of

>>>>     printf("ENTER THE NUMBER OF JOBS:\t");
>>>>     scanf("%d",&n);

you should use

     cout << "ENTER THE NUMBER OF JOBS:\t";
     cin >> n;
 

6. Other problems

>>>> But it does not work If CPU and I/O burst is more than one pair.

You used a for loop with counter 'j'

>>>>       for(int j=0;j<p[i].numofpairs;j++)

but inside of the loop 'j' isn't used.

Regards, Alex







0
 

Author Comment

by:gugu_1500
ID: 16313544
thank you for pointing me out.
gugu
0
 

Author Comment

by:gugu_1500
ID: 16317043
Hi Alex:

Thank u for pointing me out. I am trying my best to write the scheduling program. It doesnt work yet. I hope you will help me out. As I mention, a job format will be like

A job will be represented as follows:
     (job id, priority, arrival time, CPU1, IO1, ..., CPUn, IOn)


Parentheses are required. Time will start at t=0.
Jobid is a string naming the job.
Other values are integers:
priority is 0-15, with 0 being the highest priority
arrival time is >=0 and jobs can arrive at the same time
CPUk, IOk are the size (>0) of the kth burst in time units. CPU and IO devices execute concurrently when both busy. Assume one CPU and one IO device.

The following scheduling algorithms are to be implemented:
  FCFS, SJF preemptive, SJF non-preemptive, Priority FCFS, RR

The following statistics will be produced:
CPU utilization, IO utilization, Avg. turnaround time, Avg. waiting time,
 Jobs with longest waiting time and turnaround time (show Jobid and wait time)

Input specification
-------------------
User will be prompted for:
  Job stream source:
    if file, prompt for file name. The file will contain a sequence of jobs in
      the above format. Each job is separated by one or more blanks and may
      cross line boundaries.
    if random generation,
       jobid's should be A, B, C, ..., AA, etc.
       prompt for number of jobs,
       prompt for min and max of ranges for:
         priority
         arrival time
         number of CPU/IO burst pairs
         CPU burst size
         IO burst size
  Scheduling algorithm (display choices) -
    prompt for parameters as needed, i.e. quantum size for RR
  Suppress table output? Hit y to suppress, otherwise hit Enter.

WOuld you please give me some advise on this? How do I start on this? Would you please  where I should start with.

I am a kind of lost here.Please.. I have two days to work on this.

THank you.





0
 

Author Comment

by:gugu_1500
ID: 16317287
Hi Alex;

I have start to write again. I have wrote the following things. I am not sure where I should store max and min of ranges in Class or array. I cannot fingure out. Would u please advise me.

#include <iostream> // for cout/cin
#include <string>      // not string.h
#include <vector>     // for dynamic arrays
using namespace std;

void fcfs()
{

}
void sjfp()
{

}
void sjfnp()
{

}
void priority()
{

}
void rr()
{

}


int main()
{  
      int jobsource,Noofjobs,choice,qsize;
      char suppress;
      cout<<"Choose job stream source:";
      cout<<"From File press-1 or Random generation press-2";
      cin>>jobsource;
    if ( jobsource=1)
      {

      }
      else if (jobsource=2)
      {   //job id will be A,B,C...
        cout<<"Enter number of jobs";
            cin>>Noofjobs;
            cout<<"\nPriority:";
            cout<<"Max range\t";
            //cin>>
            cout<<"Min range\n";
            //cin>>
            cout<<"Arrival Time:";
            cout<<"Max range\t";
            //cin>>
        cout<<"Min range\n";
            //cin>>
            cout<<"CPU/IO burst pairs:";
            cout<<"Max range\t";
            //cin>>
        cout<<"Min range\n";
            //cin>>
       
            cout<<"CPU burst size:";
            cout<<"Max range\t";
            //cin>>
        cout<<"Min range\n";
        //cin>>
            cout<<"CPU burst size:";
            cout<<"Max range\t";
            //cin>>
        cout<<"Min range\n";
        //cin>>
      }
      cout<<"1.FCFS \n";
      cout<<"2.SHORTEST JOB FIRST (preemptive)\n";
      cout<<"3.SHORTEST JOB FIRST (non-preemptive)\n";
      cout<<"4.PRIORITY\n";
      cout<<"5.ROUND ROBIN\n";
      cout<<"ENTER YOUR CHOICE:\t";
      cin>>choice;
            
      switch(choice) // Select a scheduling from the list
      {
      case 1: {      fcfs();
                        break;
                  }

      case 2:      {      sjfp();
                        break;
                  }

      case 3:      {      sjfnp();
                        break;
                  }
      
      case 4:      {      priority();
                        break;
                  }

      case 5:      {      cout<<"Enter quantum size for RR";
                    cin>>qsize;
                    rr();
                        break;
              }
     }

      cout<<"Suppress table output? Hit y to suppress,otherwise hit enter";
        cin>>suppress;
        
        if(suppress='y')
        {
         //print out table
        }
      return 0;

}

Thank you
gugu
0
 

Author Comment

by:gugu_1500
ID: 16318444
Please look at this code. Ignore the previous one..I cant fighure out where to continue from here.


int main()
{  
      int jobsource,Noofjobs,choice,qsize;
      int pmin,pmax,pnum;
      int amin,amax,anum;
      int pairmin,pairmax,pairnum;
      int burstmin,burstmax,burstnum;
      int iomin,iomax,ionum;
      char suppress;
      cout<<"Choose job stream source:\n";
      cout<<"From File press-1 or Random generation press-2:\t";
      cin>>jobsource;
      
    if ( jobsource==1)
      {

      }
      else if(jobsource==2)
      {  
                        
            //job id will be A,B,C...
            cout<<"Enter number of jobs:";
            cin>>Noofjobs;
            cout<<"\n";
            cout<<"=========\n";
            cout<<"Priority\n";
            cout<<"=========\n";
            cout<<"Min range:";
            cin>>pmin;
            cout<<"\n";
            cout<<"Max range:";
            cin>>pmax;
            cout<<"\n";
            for (int i = 0;i < Noofjobs; i++ )
         {
             pnum = (((double) rand() /
                         (double) RAND_MAX) * pmax + pmin);

            }
          cout<<"============\n";
            cout<<"Arrival Time\n";
            cout<<"============\n";
            cout<<"Min range:";
            cin>>amin;
            cout<<"\n";
            cout<<"Max range:";
            cin>>amax;
            cout<<"\n";
            for (int i = 0;i < Noofjobs; i++ )
         {
             anum = (((double) rand() /
                         (double) RAND_MAX) * amax + amin);

            }
        cout<<"===========================\n";
            cout<<"Numer of CPU/IO burst pairs\n";
        cout<<"===========================\n";
            cout<<"Min range:";
            cin>>pairmin;
            cout<<"\n";
            cout<<"Max range:";
            cin>>pairmax;
        cout<<"\n";
            for (int i = 0;i <1 ; i++ )
         {
             pairnum = (((double) rand() /
                         (double) RAND_MAX) * pairmax + pairmin);

            }
            
        cout<<"================\n";  
            cout<<"CPU burst size\n";
            cout<<"=================\n";
            cout<<"Min range:";
            cin>>burstmin;
            cout<<"\n";
            cout<<"Max range:";
        cin>>burstmax;
            cout<<"\n";
        for (int i = 0;i < pairnum ; i++ )
         {
             burstnum = (((double) rand() /
                         (double) RAND_MAX) * burstmax + burstmin);
             
            
            }
            cout<<"==============\n";
            cout<<"IO burst size\n";
            cout<<"================\n";
            cout<<"Min range:";
            cin>>iomin;
            cout<<"\n";
            cout<<"Max range:";
            cin>>iomax;
            cout<<"\n";
            
        for (int i = 0;i < pairnum ; i++ )
         {
             ionum = (((double) rand() /
                         (double) RAND_MAX) * iomax + iomin);
           
            }
            
      }
      cout<<"==================================\n";
      cout<<"Choose CPU Scheduling Algorithm\n";
      cout<<"==================================\n";
      cout<<"1. FCFS \n";
      cout<<"2. SHORTEST JOB FIRST (preemptive)\n";
      cout<<"3. SHORTEST JOB FIRST (non-preemptive)\n";
      cout<<"4. PRIORITY\n";
      cout<<"5. ROUND ROBIN\n";
      cout<<"Enter your choice(1-5):";
      cin>>choice;
            
      switch(choice) // Select a scheduling from the list
      {
      case 1: {      fcfs();
                        break;
                  }

      case 2:      {      sjfp();
                        break;
                  }

      case 3:      {      sjfnp();
                        break;
                  }
      
      case 4:      {      priority();
                        break;
                  }

      case 5:      {      cout<<"Enter quantum size for RR";
                    cin>>qsize;
                    rr();
                        break;
              }
     }

        cout<<"Suppress table output? Hit y to suppress,otherwise hit enter:";
        cin>>suppress;
        
     
      if(suppress='y')
        {
          cout<<"Trace Table";
              //print out table
        }
      return 0;

}

Hi there..Please help me advise.

0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 16318825
What is your question?

Does the code above compile? Did you solve the problem regarding pairs of CPU/IO?

I don't think the random job table is a good idea. Better read data from file. If you'll put each value to a new line it is not very difficult to read the values.

struct CpuIO
{
    int cpu;
    int io;
};

struct Job
{
    string jobId;
    int prio;
    int arriv;
    int numpairs;
    CpuIO cpuio[20];
};


   vector<Job> jobs;
   ifstream ifs("jobtable.txt");
   for (int i = 0; i < MAX_JOBS; ++i)
   {
       Job job;
       if (!(ifs >> job.jobId))
            break;
       if (!(ifs >> job.prio))
            break;
       if (!(ifs >> job.arriv))
            break;
       if (!(ifs >> job.numpairs))
            break;
       for (int j = 0; j < job.numpairs; ++j)
       {
            if (!(ifs >> job.cpuio[j].cpu))
                break;
            if (!(ifs >> job.cpuio[j].io))
                break;
       }
       jobs.push_back(job);
   }
   ...


You  would need to add some error handling.

Regards, Alex

0
 

Author Comment

by:gugu_1500
ID: 16324001
Hi Alex.

Random generation is required for my assignment. I have to give two options for user to choose job stream from file or random generated.Above codes, I have generated randomly priority,arrival,no of cpu and io pairs,jobno,io burst size,cpu burst size.
job stream will be like this format(jobid,priority,arrival,cpuburstsize,ioburstsize,cpuburstsize,ioburstsize,...,...)

I have to read those values from either from file or random numberto do scheudling  algorithm.

My question is where I should store random numbers? in structure or classes?

Thank you.
gugu
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 16324257
>>>> where I should store random numbers? in structure or classes?

Actually, in C++ struct and class are equivalent. The only difference is that members default to be public in struct and private in class. However by explicitly stating 'public:' or 'private:' class and struct have identical functionality.

Most C++ developers use structs if the have data members only and don't want to supply get and set methods for any member. They use class if they want to make OOP where the class elements do all stuff by class member functions. You would have classes like

   class JobTable;
   class Job;
   class Schedule;

and your main would look like:

 enum { BY_FILE, BY_RANDOM };

 int main()
 {
      Schedule sched;
      sched.run(BY_FILE);
      sched.run(BY_RANDOM);
      return 0;
 };

Here class Schedule contains all other data objects and member function Schedule::run does the whole job (either from file or randomly).

That would be the way I prefer
0
 

Author Comment

by:gugu_1500
ID: 16324471
I am not very familiar with classes . If i have to start learning today, I cant finish my assignement. I am more fimiliar with struct.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 16324655
>>>> I am more fimiliar with struct.

Yes, the code above uses structs with data member only. That is ok if you are a beginner.

Regards, Alex
0
 

Author Comment

by:gugu_1500
ID: 16325093
yes . I am a beginner. I dont know much about c++.
0
 

Author Comment

by:gugu_1500
ID: 16325161
example
Now I have Cpu burstsize random numbers stored in cpu_io.cpu

struct CpuIO
{
    int cpu;
    int io;
}cpu_io[40];

for (int i = 0;i < pairnum*2 ; i++ )
         {
             cpu_io[i].io = (((double) rand() /
                         (double) RAND_MAX) * iomax + iomin);
           
           }

for (int i = 0;i < pairnum*2 ; i++ )
         {
             cpu_io[i].io = (((double) rand() /
                         (double) RAND_MAX) * iomax + iomin);
           
           }

How can I store a jobstream as(cpu,io,cpu,io)alternating sequence.

Thank you
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 16329980
>>>> }cpu_io[40];

Don't define arrays together struct/class definition. That prevents you from defining a properly designed data model.

  struct CpuIO
  {
    int cpu;
    int io;
 };

The struct is a helper that defines *one* pair of cpu/io. I am assuming you have up to 20 job streams each of them contains two cpu/io pairs. If so, it is bad to define an array of 40 pairs cause there is no direct association to the job stream. Better define a new struct JobStream and add the two pairs as members:

  struct JobStream
  {
       ...
       CpuIO cpuio[2];
  };

For the ... you most likely want to add some properties of a 'job stream', e. g. the job id. Always try to think of 'objects' (and not of arrays or structs).

If you need to manage up to 20 JobStreams, you could define

   JobStream jobs[20];

Note, of course you could add the array definition directly to the struct JobStream definition. But you shouldn't do so. If your prog becomes bigger, struct definitions will be moved to header files but arrays need to be defined in cpp or as struct/class members.

The code to set random numbers to the io member would change to

         for (int i = 0;i < jobnum ; i++ )
         {
             for (int j = 0; j < 2; ++j)
             {
                 jobs[i].cpuio[j].io = (((double) rand() /
                         (double) RAND_MAX) * iomax + iomin);
           
             }

You should use the same loop(s) to set all other properties as well.

Regards, Alex

0
 

Author Comment

by:gugu_1500
ID: 16334133
Hi Alex;
Thank you for your advise. I am struggling to finish it today. I have one more question. You would be mad at me because my programming is lousy.

Now I am trying to read job stream from a text file. In the text file, the job stream will be like
(jobid,priority,arrival,cpu1,io1,cpu2,io2). eg. (A,0,0,10,2,10,3)
(jobid,priority,arrival,cpu1,io1,cpu2,io2). eg.(B,0,0,10,2,5,2)
(jobid,priority,arrival,cpu1,io1,cpu2,io2). eg.(C,0,15,15,10,15,5)

struct Job
{
    char jobid;
    int priority;
    int arrival;
    int numpairs;
      int noofjobs;
    int cpu[20];
    int io[20];
}jobs[20],temp;

How can I read and send to the above structure.
A will stored in jobs[0].jobid
B will stored in jobs[1].jobid
c will stored in jobs[2].jobid

for CPU and IO burst.

I need to store like.
jobs[0].cpu[0]=10
jobs[1].cpu[0]=10
jobs[3].cpu[0]=15
and so on

jobs[0].io[0]=2
jobs[1].io[0]=2
jobs[3].io[0]=10

and so on.

Sorry about that I dont have enough time to change my codes according to ur advise. Assignment is due today. I need to submit something which partially works but not fully work.

thank you Alex.
gugu

0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 600 total points
ID: 16335188
The problem is that it's against EE rules to make homework for you. I may advise and help with some code snippets but can't give you full code where you posted nothing til now.

But some hints how to do:

-   include <fstream>, <sstream> and <string> header at top
-   open the file by using the ifstream constructor "ifstream ifs("input.txt");
-   define a string variable e. g. "string line;
-   read the lines by calling "getline(ifs, line);" (in a loop or by calling it 3 times into different strings)
-  define a istringstream object that takes the input line "istringstream iss(line.substr(1));"
   (we already skipped the '(' by calling substr(1) )
- use iss >> xxx to extract all parts and fill your variables, e. g. iss >> jobs[0].jobid.

Regards, Alex

BTW, I am a few hours offline but will help you if you'll post an attempt of your own.

0
 

Author Comment

by:gugu_1500
ID: 16409598
Hello EE moderator;

Please help me close this question. I solved it by myself.
gugu
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question