//PROGRAM NUMBER :7
/* Design, develop and implement a C/C++/Java program to simulate the working of 
Shortest remaining time first and Round Robin (RR) scheduling algorithms. Experiment 
with different quantum sizes for RR algorithm. */


#include<stdio.h>
 
int processCount, timeQuantum;
int processIds[10], arrivalTimes[10], burstTimes[10], turnAroundTimes[10], waitTimes[10], residualBurstTimes[10], availableProcesses[10], completedProcesses[10];
int totalBurstTime = 0;
 
//Find the process having the minimum residual burst time among the available and non-completed process at that time
//Returns the processId of that process.
int findmin() {
  int minResidualTime = 99, processWithMinResidualTime = -1;
 
  for (int i = 0; i < processCount; i++) {
    if (residualBurstTimes[i] < minResidualTime && availableProcesses[i] == 1 && completedProcesses[i] != 1 && residualBurstTimes[i] > 0)
       {
             minResidualTime = residualBurstTimes[i];
             processWithMinResidualTime = i;
    }
  }
 
  return processWithMinResidualTime;
}
 
int srtf() {
  int i, j, processInExecution = 0;
 
  //This loop initializes some array members.
  //Set all processes as not available.
  //Set all processes as non-completed.
  //Set residual burst times to be same as actual burst times for all processes.
  // Set totalBurstTime as the sum of the burst times of all processes.
  for (i = 0; i < processCount; i++) {
    availableProcesses[i] = 0;
    completedProcesses[i] = 0;
    residualBurstTimes[i] = burstTimes[i];
       totalBurstTime += burstTimes[i];
  }
 
  printf("\ntotal burst time is:%d\n", totalBurstTime);
 
  //Iterate from time 0 till the total burst time.
  for (i = 0; i < 99; i++)
  {
         bool atLeastOneProcessNotCompleted  = false;
    // At the given time, iterate through all processes and find which all processes are available at this time .
    for (j = 0; j < processCount; j++)
    {
      if (arrivalTimes[j] <= i)
               availableProcesses[j] = 1;
         if(completedProcesses[j] == 0)
               atLeastOneProcessNotCompleted = true;
    }
 
       if(atLeastOneProcessNotCompleted == false)
       {
             break;
       }
 
       //find the process having minimum residual burst time at this time.
    processInExecution = findmin();
 
       if(processInExecution == -1)
       {
             printf("%d", i);
             printf("(-) - ");
             continue;
       }
 
    printf("%d", i);
    printf("(%d) - ", processIds[processInExecution]);
 
       //Reduce the residual burst time by 1.
    if (residualBurstTimes[processInExecution] > 0)
               residualBurstTimes[processInExecution]--;
   
 
       //Check if the process completed.
       //If residualBurstTime <= 0, process is completed, so calculate turnAroundTime for this process.
    if (residualBurstTimes[processInExecution] <= 0) {
      turnAroundTimes[processInExecution] = i - arrivalTimes[processInExecution] + 1;
      completedProcesses[processInExecution] = 1;
    }
  }
 
  return 0;
}
 
 
 
void rr() {
  int completedProcessCount = 0, timeIncrement, time = 0;
 
  //Set residualBurstTimes to burstTimes for all processes.
  for (int i = 0; i < processCount; i++) {
    availableProcesses[i] = 0;
    completedProcesses[i] = 0;
    residualBurstTimes[i] = burstTimes[i];
       totalBurstTime += burstTimes[i];
  }
 
  int activeProcess = 0;
  while (completedProcessCount < processCount)
  {
         bool atLeastOneProcessNotCompleted = false;
         for (int k = 0; k < processCount; k++)
         {
               if(completedProcesses[k] == 0)
               {
                      atLeastOneProcessNotCompleted = true;
                      break;
               }
 
         }
         if(atLeastOneProcessNotCompleted == false)
               break;
 
         //Iterate over the processes.
    for (int i = 0; i < processCount; i++)
       {
      
             if(completedProcesses[i] == 1)
             {
                    
                    continue;
             }
 
                    bool canContinueWithLastExecutedProcess = false;
               //If a process is not aailable at this time, move to next process.
               if(arrivalTimes[i] > time)
               {
                    
 
                                 if(i == processCount - 1)
                                  {
                                        //Check the just executed process.
                                        if(arrivalTimes[i-1] < time && completedProcesses[i-1] == 0)
                                        {
                                               canContinueWithLastExecutedProcess = true;
                                               i--;
                                        }
                                        else
                                        {
                                               //No process available at this time. Increment the count and break.
                                               time ++;
                                               break;
                                        }
                                 }
                                 if(!canContinueWithLastExecutedProcess)
                      continue;
               }
              
 
               //If residual burst time for the process is more than the time quantum, reduce the residual burst time by the time quantum.
              //If residual burst time is greater than 0 , but less than the time quantum, dont execute for the whole timeQuantum duration.
               if (residualBurstTimes[i] > timeQuantum)
               {
                      timeIncrement = timeQuantum;
                      residualBurstTimes[i] = residualBurstTimes[i] - timeQuantum;
               //printf("\tProcess %d After execution, residual burst time = %d\n", i, residualBurstTimes[i]);
      
               }
               else if (residualBurstTimes[i] >= 0)
               {
                    timeIncrement = residualBurstTimes[i];
                    residualBurstTimes[i] = 0;
                    turnAroundTimes[i] = time + timeIncrement - arrivalTimes[i];
                    completedProcesses[i] = 1;
                    completedProcessCount++;
             
 
               }
               time += timeIncrement;
    }
  }
 
}
 
int main()
{
  int i, choice, tempNumber;
 
  int totalWaitTime = 0, totalTurnAroundTime = 0;
  float averageWaitTime = 0.0, averageTurnAroundTime = 0.0;
 
  printf("1: Shortest Remaining Time First\n");
  printf("2: Round Robin\n");
  printf("3: EXIT\n");
  printf("Enter the choice\n");
  scanf("%d", & choice);
 
  if(choice != 1 && choice != 2) return 0;
 
  printf("\nEnter the no of processes:");
  scanf("%d", & processCount);
 
  for (i = 0; i < processCount; i++)
  {
         printf("Enter arrival time for process %d:\n",i);
      scanf("%d", & arrivalTimes[i]);
         printf("Enter burst time for process %d:\n", i);
         scanf("%d", & burstTimes[i]);
 
             processIds[i] = i;
  }
 
  if (choice == 2) {
    printf("Enter time quantum:");
    scanf("%d", &timeQuantum);
 
       for(int i = 0; i< processCount;i++)
       {
             for (int j = 0;j < processCount-i-1; j++)
             {
                    if(arrivalTimes[j] > arrivalTimes[j+1])
                    {
                           //Switch the arrival times.
                           tempNumber = arrivalTimes[j];
                           arrivalTimes[j] = arrivalTimes[j+1];
                           arrivalTimes[j+1] = tempNumber;
 
                           //Switch the burst times
                           tempNumber = burstTimes[j];
                           burstTimes[j] = burstTimes[j+1];
                           burstTimes[j+1] = tempNumber;
 
                    }
             }
       }
 
       printf("After sorting, the arrival and burst times are : \n");
       for(int i = 0; i< processCount;i++)
       {
             printf("%d  %d\n",arrivalTimes[i], burstTimes[i]);
       }
 
  }
 
  switch (choice) {
  case 1:
    srtf();
    break;
  case 2:
    rr();
    break;
  }
 
 
  //Calculate the average wait time and average turnaround time.
  printf("\n Process_ID Burst time Wait time Turn around time\n");
  for (i = 0; i < processCount; i++)
  {
    waitTimes[i] = turnAroundTimes[i] - burstTimes[i];
    printf("%d\t\t %d\t %d\t %d", i + 1, burstTimes[i], waitTimes[i], turnAroundTimes[i]);
    printf("\n");
    totalWaitTime = totalWaitTime + waitTimes[i];
    totalTurnAroundTime = totalTurnAroundTime + turnAroundTimes[i];
  }
  averageWaitTime = (float) totalWaitTime / processCount;
  averageTurnAroundTime = (float) totalTurnAroundTime / processCount;
  printf("\n\n Average waiting time is %f \n Average turnaround time is %f", averageWaitTime, averageTurnAroundTime);
  return 1;
}
 
 
	


	

 

C++ Online Compiler

Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++ and start coding!

Read inputs from stdin

OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.

#include <iostream>
#include <string>
using namespace std;

int main() 
{
    string name;
    cout << "Enter name:";
    getline (cin, name);
    cout << "Hello " << name;
    return 0;
}

About C++

C++ is a widely used middle-level programming language.

  • Supports different platforms like Windows, various Linux flavours, MacOS etc
  • C++ supports OOPS concepts like Inheritance, Polymorphism, Encapsulation and Abstraction.
  • Case-sensitive
  • C++ is a compiler based language
  • C++ supports structured programming language
  • C++ provides alot of inbuilt functions and also supports dynamic memory allocation.
  • Like C, C++ also allows you to play with memory using Pointers.

Syntax help

Loops

1. If-Else:

When ever you want to perform a set of operations based on a condition If-Else is used.

if(conditional-expression) {
   //code
}
else {
   //code
}

You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.

2. Switch:

Switch is an alternative to If-Else-If ladder.

switch(conditional-expression){    
case value1:    
 // code    
 break;  // optional  
case value2:    
 // code    
 break;  // optional  
......    
    
default:     
 code to be executed when all the above cases are not matched;    
} 

3. For:

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
  //code  
} 

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while (condition) {  
// code 
}  

5. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {  
 // code 
} while (condition); 

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.

How to declare a Function:

return_type function_name(parameters);

How to call a Function:

function_name (parameters)

How to define a Function:

return_type function_name(parameters) {  
 // code
}