Algorithm for Round Robin
Step 1:Start the process
Step 2. Declare the array size
Step 3. Get the number of elements to be inserted
Step 4. Get the value
Step 5. Set the time sharing system with preemption
Step 6. Define quantum is defined from 10 to 100ms
Step 7. Declare the queue as a circular
Step 8. Make the CPU scheduler goes around the ready queue allocating CPU to each process for the
time interval specified
Step 9. Make the CPU scheduler picks the first process and sets time to interrupt after quantum expired
dispatches the process
Step 9. If the process have burst less than the time quantum than the process release the CPU Step
10. If the process have bust greater then time quantum then time will go off and
cause interrupt to OS and the process put into the tail of ready queue and the schedule select next
process
Step 11. Display the results
Step 12. Stop the process.
Program:
1. #include<stdio.h>
2. #include<conio.h>
3.
4. void main()
5. {
6. // initlialize the variable name
7. int i, NOP, sum=0,count=0, y, quant, wt=0, tat=0, at[10], bt[10], temp[10];
8. float avg_wt, avg_tat;
9. printf(" Total number of process in the system: ");
10. scanf("%d", &NOP);
11. y = NOP; // Assign the number of process to variable y
12.
13. // Use for loop to enter the details of the process like Arrival time and the Burst Time
14. for(i=0; i<NOP; i++)
15. {
16. printf("\n Enter the Arrival and Burst time of the Process[%d]\n", i+1);
17. printf(" Arrival time is: \t"); // Accept arrival time
18. scanf("%d", &at[i]);
19. printf(" \nBurst time is: \t"); // Accept the Burst time
20. scanf("%d", &bt[i]);
21. temp[i] = bt[i]; // store the burst time in temp array
22. }
23. // Accept the Time qunat
24. printf("Enter the Time Quantum for the process: \t");
25. scanf("%d", &quant);
26. // Display the process No, burst time, Turn Around Time and the waiting time
27. printf("\n Process No \t\t Burst Time \t\t TAT \t\t Waiting Time ");
28. for(sum=0, i = 0; y!=0; )
29. {
30. if(temp[i] <= quant && temp[i] > 0) // define the conditions
31. {
32. sum = sum + temp[i];
33. temp[i] = 0;
34. count=1;
35. }
36. else if(temp[i] > 0)
37. {
38. temp[i] = temp[i] - quant;
39. sum = sum + quant;
40. }
41. if(temp[i]==0 && count==1)
42. {
43. y--; //decrement the process no.
44. printf("\nProcess No[%d] \t\t %d\t\t\t\t %d\t\t\t %d", i+1, bt[i], sum-at[i], sum-at[i]-bt[i]);
45. wt = wt+sum-at[i]-bt[i];
46. tat = tat+sum-at[i];
47. count =0;
48. }
49. if(i==NOP-1)
50. {
51. i=0;
52. }
53. else if(at[i+1]<=sum)
54. {
55. i++;
56. }
57. else
58. {
59. i=0;
60. }
61. }
62. // represents the average waiting time and Turn Around time
63. avg_wt = wt * 1.0/NOP;
64. avg_tat = tat * 1.0/NOP;
65. printf("\n Average Turn Around Time: \t%f", avg_wt);
66. printf("\n Average Waiting Time: \t%f", avg_tat);
67. getch();
68. }
Algorithm for priority scheduling
Step 1: Start the process
Step 2: Accept the number of processes in the ready Queue
Step 3: For each process in the ready Q, assign the process id and accept the CPU burst time
Step 4: Sort the ready queue according to the priority number.
Step 5: Set the waiting of the first process as ‘0’ and its burst time as its turnaround time.
Step 6: For each process in the Ready Q calculate
a. Waiting time for process(n)= waiting time of process (n-1) + Burst time of process(n- 1).
b. Turnaround time for Process(n)= waiting time of Process(n)+ Burst time for process(n)
Step 7: Calculate
a. Average waiting time = Total waiting Time / Number of process
b. Average Turnaround time = Total Turnaround Time / Number of process Step 8: Stop the
process
Program:
#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
printf("Enter Total Number of Process:");
scanf("%d",&n);
printf("\nEnter Burst Time and Priority\n");
for(i=0;i<n;i++)
{
printf("\nP[%d]\n",i+1);
printf("Burst Time:");
scanf("%d",&bt[i]);
printf("Priority:");
scanf("%d",&pr[i]);
p[i]=i+1; //contains process number
}
//sorting burst time, priority and process number in ascending order using selection sort for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(pr[j]<pr[pos])
pos=j;
}
temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0; //waiting time for first process is zero
//calculate waiting time
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=total/n; //average waiting time
total=0;
printf("\nProcess\t Burst Time \tWaiting Time \tTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround time
total+=tat[i];
printf("\nP[%d]\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=total/n; //average turnaround time printf("\n\
nAverage Waiting Time=%d",avg_wt); printf("\nAverage
Turnaround Time=%d\n",avg_tat);
return 0;
}