News: 11 March 2016 - Forum Rules

Author Topic: Simulating the FCFS CPU Scheduling algorithm with multiple CPUs  (Read 2060 times)

Kronus_Arm

  • Hero Member
  • *****
  • Posts: 517
  • Act like a baddass, whine like a sissy.
    • View Profile
Simulating the FCFS CPU Scheduling algorithm with multiple CPUs
« on: October 20, 2015, 08:32:07 am »
After hours of braining I've come to the point of crashing, and come to realize that I cannot implement a way to simulate the FCFS algorithm with the idea of having multiple CPUs handling each jobs/processes in C++, specifically 2 CPUs/cores.

Anyone has an idea, tips or clues where can I start?

Here's my code for simulating the FCFS algorithim in a single CPU:

Code: [Select]
int n, burstTime[99], waitingTime[99], totalAT[99], aveWT = 0, aveTAT = 0, i, j;
cout << "Enter total number of processes: ";
cin >> n;

cout << "\nEnter Process Burst Time\n";
for (i = 0; i<n; i++)
{
    cout << "P[" << i + 1 << "]: ";
    cin >> burstTime[i];
}

waitingTime[0] = 0;    //waiting time for first process is 0

              //calculating waiting time
for (i = 1; i<n; i++)
{
    waitingTime[i] = 0;
    for (j = 0; j<i; j++)
        waitingTime[i] += burstTime[j];
}

cout << "\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time";

//calculating turnaround time
for (i = 0; i<n; i++)
{
    totalAT[i] = burstTime[i] + waitingTime[i];
    aveWT += waitingTime[i];
    aveTAT += totalAT[i];
    cout << "\nP[" << i + 1 << "]" << "\t\t" << burstTime[i] << "\t\t" << waitingTime[i] << "\t\t" << totalAT[i];
}

aveWT /= i;
aveTAT /= i;
cout << "\n\nAverage Waiting Time: " << aveWT;
cout << "\nAverage Turnaround Time: " << aveTAT <<endl;

I know, noob-ish code at best. I have little experience with C++ and programming in general, but I am getting there somehow. Take note of the word "simulating", I'm not really doing anything fancy like multi threading or actually coding something to actually use 2 cores, just a simulation process.

EDIT:

Here's what I envisioned of the output of this code in the commandline:

Code: [Select]
Enter total number of processes: 6

Enter Process Burst Time
P1: [input here]
P2: [input here]
P3: [input here]
p4: [input here]
p5: [input here]
p6: [input here]

Process     Burst Time         Waiting Time                                 Turn Around Time
P1          [burst time here]  [calculated waiting time here]      [calculated turn around time]
P2          [burst time here]  [calculated waiting time here]      [calculated turn around time]
P3          [burst time here]  [calculated waiting time here]      [calculated turn around time]
P4          [burst time here]  [calculated waiting time here]      [calculated turn around time]
P5          [burst time here]  [calculated waiting time here]      [calculated turn around time]
P6          [burst time here]  [calculated waiting time here]      [calculated turn around time]


CPUs handling the processes:
CPU 1: P1, P3, P4
CPU 2: P2, P5, P6

for example: the first process/job (which is P1) having the arrival time of 0 and the burst time of 5 gets processed automatically by the first CPU, the second job which is P2 has an arrival time of 2 and a burst time of 8, as the first CPU is still actively processing the first job, the second job goes to the second CPU (which is CPU 2) for it to be processed.
« Last Edit: October 20, 2015, 08:39:12 am by Kronus_Arm »

henke37

  • Hero Member
  • *****
  • Posts: 643
    • View Profile
Re: Simulating the FCFS CPU Scheduling algorithm with multiple CPUs
« Reply #1 on: October 20, 2015, 11:00:00 am »
Each iteration, find the cpu whose task is closest to completion. Then forward time to that point. Give the cpu a new task and then do a new iteration. Note that if two tasks complete at the same time the second iteration will have a zero length time forwarding.

Kronus_Arm

  • Hero Member
  • *****
  • Posts: 517
  • Act like a baddass, whine like a sissy.
    • View Profile
Re: Simulating the FCFS CPU Scheduling algorithm with multiple CPUs
« Reply #2 on: October 20, 2015, 01:17:16 pm »
By iteration, I assume you mean I should use a for loop and the two CPUs should be declared as arrays?

henke37

  • Hero Member
  • *****
  • Posts: 643
    • View Profile
Re: Simulating the FCFS CPU Scheduling algorithm with multiple CPUs
« Reply #3 on: October 21, 2015, 06:37:34 am »
Code: [Select]
taskQueue:array;
cores:array;
while(taskQueue.length>0 && anyCoreBusy) {
  core=findCoreThatCompletesNext();
  forwardTime(core.task.timeLeft);
  core.task=taskQueue.shift();
}