added more code
This commit is contained in:
BIN
CPE435/Lab7/Lab07.pdf
Normal file
BIN
CPE435/Lab7/Lab07.pdf
Normal file
Binary file not shown.
BIN
CPE435/Lab7/RoundRobin
Executable file
BIN
CPE435/Lab7/RoundRobin
Executable file
Binary file not shown.
85
CPE435/Lab7/RoundRobin.cpp
Normal file
85
CPE435/Lab7/RoundRobin.cpp
Normal file
@ -0,0 +1,85 @@
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct process
|
||||
{
|
||||
int pid; // Process ID
|
||||
int burst_time; // CPU Burst Time
|
||||
int working_time; // Time this process executed, if working_time==burst_time, process is complete
|
||||
int t_round; // turn around time, time needed for the process to complete
|
||||
int arrivalTime; // time for a process to arrive to queue, which is zero in this case
|
||||
int remainTime; // time remaining for a process to complete execution
|
||||
};
|
||||
|
||||
main()
|
||||
{
|
||||
int numProcesses;
|
||||
cout << "Enter the number of processes: ";
|
||||
cin >> numProcesses;
|
||||
struct process pr[numProcesses];
|
||||
|
||||
int i, totalExecutionTime = 0, timeQuantum, flag = 0, n;
|
||||
float totalWaitingTime = 0;
|
||||
|
||||
for (i = 0; i < numProcesses; i++)
|
||||
{
|
||||
cout << "\nEnter PID for process " << i + 1 << " (ms): ";
|
||||
cin >> pr[i].pid;
|
||||
|
||||
cout << "\nEnter Burst Time for process " << i + 1 << " (ms): ";
|
||||
cin >> pr[i].burst_time;
|
||||
}
|
||||
|
||||
// arrival time is zero
|
||||
for (int i = 0; i < numProcesses; i++)
|
||||
{
|
||||
pr[i].arrivalTime = 0;
|
||||
}
|
||||
|
||||
printf("\nEnter Time Quantum: ");
|
||||
scanf("%d", &timeQuantum);
|
||||
|
||||
printf("\n");
|
||||
for (i = 0; numProcesses != 0;)
|
||||
{
|
||||
/**
|
||||
* this below condition check the remain time for any process is less than or equal with the time quantum
|
||||
* or not and also check the remain time is greater than 0 or not. if both condition are true that means
|
||||
* the process can execute fully at one time.
|
||||
*/
|
||||
if (pr[i].remainTime <= timeQuantum && pr[i].remainTime > 0)
|
||||
{
|
||||
totalExecutionTime += pr[i].remainTime;
|
||||
pr[i].remainTime = 0;
|
||||
flag = 1;
|
||||
}
|
||||
|
||||
else if (pr[i].remainTime > 0)
|
||||
{
|
||||
pr[i].remainTime -= timeQuantum;
|
||||
totalExecutionTime += timeQuantum;
|
||||
}
|
||||
|
||||
if (flag == 1 && pr[i].remainTime == 0)
|
||||
{
|
||||
printf("P[%d] | waiting Time : %d\n", i + 1, totalExecutionTime - pr[i].burst_time);
|
||||
totalWaitingTime += totalExecutionTime - pr[i].burst_time;
|
||||
flag = 0;
|
||||
numProcesses--;
|
||||
}
|
||||
|
||||
if (i == n - 1)
|
||||
i = 0;
|
||||
else if (pr[i + 1].arrivalTime <= totalExecutionTime)
|
||||
{
|
||||
i++;
|
||||
}
|
||||
else
|
||||
i = 0;
|
||||
}
|
||||
|
||||
totalWaitingTime = (float)totalWaitingTime / n;
|
||||
printf("\nThe Average Waiting Time : %.2f \n", totalWaitingTime);
|
||||
}
|
BIN
CPE435/Lab7/output
Executable file
BIN
CPE435/Lab7/output
Executable file
Binary file not shown.
16
CPE435/Lab7/output.cpp
Normal file
16
CPE435/Lab7/output.cpp
Normal file
@ -0,0 +1,16 @@
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
int Grades[5] = { 3, 2, 5, 2 };
|
||||
|
||||
cout << "| Time (ms) |";
|
||||
for (int i = 0; i < sizeof(Grades)/sizeof(int); i++) {
|
||||
cout << setw(8) << setfill(' ') << i << setw(4) << setfill(' ')<< "|";
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
BIN
CPE435/Lab7/round_Robin
Executable file
BIN
CPE435/Lab7/round_Robin
Executable file
Binary file not shown.
150
CPE435/Lab7/round_Robin.cpp
Normal file
150
CPE435/Lab7/round_Robin.cpp
Normal file
@ -0,0 +1,150 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
#include <stdbool.h>
|
||||
#include <unistd.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int numProcesses;
|
||||
|
||||
struct process{
|
||||
int pid; //Process ID
|
||||
int burst_time; //CPU Burst Time
|
||||
int working_time; //Time this process executed, if working_time==burst_time, process is complete
|
||||
int t_round; //turn around time, time needed for the process to complete
|
||||
};
|
||||
|
||||
|
||||
|
||||
void findWaitingTime(int processes[], int n,
|
||||
int bt[], int wt[], int quantum);
|
||||
|
||||
|
||||
void findTurnAroundTime(int processes[], int n,
|
||||
int bt[], int wt[], int tat[])
|
||||
{
|
||||
// calculating turnaround time by adding
|
||||
// bt[i] + wt[i]
|
||||
for (int i = 0; i < n ; i++)
|
||||
tat[i] = bt[i] + wt[i];
|
||||
}
|
||||
|
||||
|
||||
void findavgTime(int processes[], int n, int bt[],
|
||||
int quantum)
|
||||
{
|
||||
int wt[n], tat[n], total_wt = 0, total_tat = 0;
|
||||
|
||||
// Function to find waiting time of all processes
|
||||
findWaitingTime(processes, n, bt, wt, quantum);
|
||||
|
||||
// Function to find turn around time for all processes
|
||||
findTurnAroundTime(processes, n, bt, wt, tat);
|
||||
|
||||
// Display processes along with all details
|
||||
cout << "Processes "<< " Burst time "
|
||||
<< " Waiting time " << " Turn around time\n";
|
||||
|
||||
// Calculate total waiting time and total turn
|
||||
// around time
|
||||
for (int i=0; i<n; i++)
|
||||
{
|
||||
total_wt = total_wt + wt[i];
|
||||
total_tat = total_tat + tat[i];
|
||||
cout << " " << i+1 << "\t\t" << bt[i] <<"\t "
|
||||
<< wt[i] <<"\t\t " << tat[i] <<endl;
|
||||
}
|
||||
|
||||
cout << "Average waiting time = "
|
||||
<< (int)total_wt / (int)n;
|
||||
cout << "\nAverage turn around time = "
|
||||
<< (int)total_tat / (int)n;
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
struct process pr[numProcesses];
|
||||
int quantumTime;
|
||||
|
||||
cout << "Enter the number of processes: ";
|
||||
cin >> numProcesses;
|
||||
|
||||
|
||||
cout << "\nEnter Quantum Time (ms): ";
|
||||
cin >> quantumTime;
|
||||
|
||||
for(int i=0; i<numProcesses; i++)
|
||||
{
|
||||
cout << "\nEnter PID for process " << i+1 << " (ms): ";
|
||||
cin >> pr[i].pid;
|
||||
|
||||
cout << "\nEnter Burst Time for process " << i+1 << " (ms): ";
|
||||
cin >> pr[i].burst_time;
|
||||
}
|
||||
// int x = sizeof pr->pid / sizeof pr[0].pid;
|
||||
findavgTime(&pr->pid, numProcesses, &pr->burst_time, quantumTime);
|
||||
}
|
||||
|
||||
void findWaitingTime(int processes[], int n,
|
||||
int bt[], int wt[], int quantum)
|
||||
{
|
||||
// Make a copy of burst times bt[] to store remaining
|
||||
// burst times.
|
||||
int rem_bt[n];
|
||||
for (int i = 0 ; i < n ; i++)
|
||||
rem_bt[i] = bt[i];
|
||||
|
||||
int t = 0; // Current time
|
||||
|
||||
// Keep traversing processes in round robin manner
|
||||
// until all of them are not done.
|
||||
while (1)
|
||||
{
|
||||
bool done = true;
|
||||
|
||||
// Traverse all processes one by one repeatedly
|
||||
for (int i = 0 ; i < n; i++)
|
||||
{
|
||||
// If burst time of a process is greater than 0
|
||||
// then only need to process further
|
||||
if (rem_bt[i] > 0)
|
||||
{
|
||||
done = false; // There is a pending process
|
||||
|
||||
if (rem_bt[i] > quantum)
|
||||
{
|
||||
// Increase the value of t i.e. shows
|
||||
// how much time a process has been processed
|
||||
t += quantum;
|
||||
|
||||
// Decrease the burst_time of current process
|
||||
// by quantum
|
||||
rem_bt[i] -= quantum;
|
||||
}
|
||||
|
||||
// If burst time is smaller than or equal to
|
||||
// quantum. Last cycle for this process
|
||||
else
|
||||
{
|
||||
// Increase the value of t i.e. shows
|
||||
// how much time a process has been processed
|
||||
t = t + rem_bt[i];
|
||||
|
||||
// Waiting time is current time minus time
|
||||
// used by this process
|
||||
wt[i] = t - bt[i];
|
||||
|
||||
// As the process gets fully executed
|
||||
// make its remaining burst time = 0
|
||||
rem_bt[i] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If all processes are done
|
||||
if (done == true)
|
||||
break;
|
||||
}
|
||||
}
|
BIN
CPE435/Lab7/rr
Executable file
BIN
CPE435/Lab7/rr
Executable file
Binary file not shown.
159
CPE435/Lab7/rr.cpp
Normal file
159
CPE435/Lab7/rr.cpp
Normal file
@ -0,0 +1,159 @@
|
||||
#include<stdio.h>
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct process{
|
||||
int pid; //Process ID
|
||||
int burst_time; //CPU Burst Time
|
||||
int working_time; //Time this process executed, if working_time==burst_time, process is complete
|
||||
int t_round; //turn around time, time needed for the process to complete
|
||||
int remainTime;
|
||||
int wait_time;
|
||||
int execTime;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
int timeArray[10000], pidArray[10000];
|
||||
int timeIndex=0;
|
||||
int count,j,n,time,remain,flag=0,time_quantum;
|
||||
int wait_time=0,turnaround_time=0,at[n];
|
||||
printf("Enter Total Process:\t ");
|
||||
scanf("%d",&n);
|
||||
remain=n;
|
||||
struct process pr[n];
|
||||
|
||||
for(count=0;count<n;count++)
|
||||
{
|
||||
cout << "\nEnter PID for process " << count+1 << " (ms): ";
|
||||
cin >> pr[count].pid;
|
||||
cout << "\nEnter Burst Time for process " << count + 1 << " (ms): ";
|
||||
cin >> pr[count].burst_time;
|
||||
pr[count].remainTime=pr[count].burst_time;
|
||||
}
|
||||
|
||||
// arrival time is zero
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
at[i] = 0;
|
||||
}
|
||||
printf("Enter Time Quantum:\t");
|
||||
scanf("%d",&time_quantum);
|
||||
for(time=0,count=0;remain!=0;)
|
||||
{
|
||||
// the process still has some work time left
|
||||
if(pr[count].remainTime<=time_quantum && pr[count].remainTime>0)
|
||||
{
|
||||
pidArray[timeIndex] = pr[count].pid;
|
||||
timeArray[timeIndex] = pr[count].remainTime;
|
||||
time+=pr[count].remainTime;
|
||||
pr[count].remainTime=0;
|
||||
flag=1;
|
||||
// n = timeDuringQuantum - remainingTime
|
||||
}
|
||||
//
|
||||
else if(pr[count].remainTime>0)
|
||||
{
|
||||
pr[count].remainTime-=time_quantum;
|
||||
time+=time_quantum;
|
||||
pidArray[timeIndex]= pr[count].pid;
|
||||
timeArray[timeIndex] = time_quantum;
|
||||
}
|
||||
else{
|
||||
timeIndex--;
|
||||
}
|
||||
|
||||
if(pr[count].remainTime==0 && flag==1)
|
||||
{
|
||||
remain--;
|
||||
pr[count].t_round = time-at[count];
|
||||
pr[count].wait_time = time-at[count]-pr[count].burst_time;
|
||||
// printf("P[%d]\t|\t%d\t|\t%d\n",count+1,time-at[count],time-at[count]-pr[count].burst_time);
|
||||
wait_time+=time-at[count]-pr[count].burst_time;
|
||||
turnaround_time+=time-at[count];
|
||||
flag=0;
|
||||
}
|
||||
if(count==n-1)
|
||||
{
|
||||
count=0;
|
||||
timeIndex++;
|
||||
}
|
||||
else if(at[count+1]<=time)
|
||||
{
|
||||
count++;
|
||||
timeIndex++;
|
||||
}
|
||||
else
|
||||
count=0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
for (int i = 0; i < timeIndex; i++) {
|
||||
cout << setw(9) << setfill('-') << '-' << setw(6) << setfill('-')<< "-";
|
||||
}
|
||||
cout << endl;
|
||||
cout << "| Time (ms) |";
|
||||
for (int i = 0; i < timeIndex; i++) {
|
||||
cout << setw(9) << setfill(' ') << timeArray[i] << setw(4) << setfill(' ')<< "|";
|
||||
}
|
||||
cout << endl;
|
||||
for (int i = 0; i < timeIndex; i++) {
|
||||
cout << setw(9) << setfill('-') << '-' << setw(6) << setfill('-')<< "-";
|
||||
}
|
||||
cout << endl;
|
||||
cout << "| PID |";
|
||||
for (int i = 0; i < timeIndex; i++) {
|
||||
cout << setw(9) << setfill(' ') << pidArray[i] << setw(4) << setfill(' ')<< " |";
|
||||
}
|
||||
cout<<endl;
|
||||
for (int i = 0; i < timeIndex; i++) {
|
||||
cout << setw(9) << setfill('-') << '-' << setw(6) << setfill('-')<< "-";
|
||||
}
|
||||
cout << endl << endl;
|
||||
// Waiting time
|
||||
for (int i = 0; i < n; i++) {
|
||||
cout << setw(9) << setfill('-') << '-' << setw(8) << setfill('-')<< "-";
|
||||
}
|
||||
cout << endl;
|
||||
cout << "| PID |";
|
||||
for (int i = 0; i < n; i++) {
|
||||
cout << setw(6) << setfill(' ') << pidArray[i] << setw(4) << setfill(' ')<< " |";
|
||||
}
|
||||
cout << endl;
|
||||
for (int i = 0; i < n; i++) {
|
||||
cout << setw(9) << setfill('-') << '-' << setw(8) << setfill('-')<< "-";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
cout << "| Wait Time (ms) |";
|
||||
for (int i = 0; i < n; i++) {
|
||||
cout << setw(4) << setfill(' ') << pr[i].wait_time << setw(5) << setfill(' ')<< "|";
|
||||
}
|
||||
cout << endl;
|
||||
for (int i = 0; i < n; i++) {
|
||||
cout << setw(9) << setfill('-') << '-' << setw(8) << setfill('-')<< "-";
|
||||
}
|
||||
cout << endl;
|
||||
cout << "| Turn Around Time (ms) |";
|
||||
for (int i = 0; i < n; i++) {
|
||||
cout << setw(4) << setfill(' ') << pr[i].t_round << setw(4) << setfill(' ')<< " |";
|
||||
}
|
||||
cout << endl;
|
||||
for (int i = 0; i < n; i++) {
|
||||
cout << setw(9) << setfill('-') << '-' << setw(8) << setfill('-')<< "-";
|
||||
|
||||
}
|
||||
|
||||
|
||||
printf("\nAverage Waiting Time= %d\n",wait_time/n);
|
||||
|
||||
return 0;
|
||||
}
|
BIN
CPE435/Lab7/rr.png
Normal file
BIN
CPE435/Lab7/rr.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 51 KiB |
Reference in New Issue
Block a user