0% found this document useful (0 votes)
19 views

Assignment 2

The document describes a C program to implement the Round Robin CPU scheduling algorithm. It begins with an explanation of how Round Robin scheduling works, allocating each process a time quantum to use the CPU before switching to the next process. It then provides pseudocode for the Round Robin algorithm and includes the full C code implementation with explanations of how it calculates waiting time, turnaround time, and averages.

Uploaded by

Kshitij Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Assignment 2

The document describes a C program to implement the Round Robin CPU scheduling algorithm. It begins with an explanation of how Round Robin scheduling works, allocating each process a time quantum to use the CPU before switching to the next process. It then provides pseudocode for the Round Robin algorithm and includes the full C code implementation with explanations of how it calculates waiting time, turnaround time, and averages.

Uploaded by

Kshitij Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Assignment 2

By Kshitij Sharma – 102103374

1)Aim: To write a C program to implement the CPU


scheduling algorithm for Priority Scheduling.

Solution:-

Priority Scheduling is a CPU scheduling algorithm in which the CPU


performs the task having higher priority at first. If two processes
have the same priority then scheduling is done on FCFS basis (first
come first serve). Priority Scheduling is of two
types : Preemptive and Non-Preemptive.

Preemptive: In this case, resources can be voluntarily snatched.

Non-Preemptive: In this type, if a process is once started, it will


execute completely i.e resources cannot be snatched.

CODE:-
1. #include <stdio.h>
2.  
3. //Function to swap two variables
4. void swap(int *a,int *b)
5. {
6. int temp=*a;
7. *a=*b;
8. *b=temp;
9. }
10. int main()
11. {
12. int n;
13. printf("Enter Number of Processes: ");
14. scanf("%d",&n);
15.  
16. // b is array for burst time, p for priority and index for process id
17. int b[n],p[n],index[n];
18. for(int i=0;i<n;i++)
19. {
20. printf("Enter Burst Time and Priority Value for Process %d: ",i+1);
21. scanf("%d %d",&b[i],&p[i]);
22. index[i]=i+1;
23. }
24. for(int i=0;i<n;i++)
25. {
26. int a=p[i],m=i;
27.  
28. //Finding out highest priority element and placing it at its desired
position
29. for(int j=i;j<n;j++)
30. {
31. if(p[j] > a)
32. {
33. a=p[j];
34. m=j;
35. }
36. }
37.  
38. //Swapping processes
39. swap(&p[i], &p[m]);
40. swap(&b[i], &b[m]);
41. swap(&index[i],&index[m]);
42. }
43.  
44. // T stores the starting time of process
45. int t=0;
46.  
47. //Printing scheduled process
48. printf("Order of process Execution is\n");
49. for(int i=0;i<n;i++)
50. {
51. printf("P%d is executed from %d to %d\n",index[i],t,t+b[i]);
52. t+=b[i];
53. }
54. printf("\n");
55. printf("Process Id Burst Time Wait Time TurnAround Time\n");
56. int wait_time=0;
57. for(int i=0;i<n;i++)
58. {
59. printf("P%d %d %d %d\
n",index[i],b[i],wait_time,wait_time + b[i]);
60. wait_time += b[i];
61. }
62. return 0;
63. }
Time Complexity: O(n*n)
Sorting takes time of the order of O(n*n), So time complexity is of the order
of O(n*n).
Space Complexity: O(n)
Space is required to store burst time, arrival time and index, So space
complexity is O(n).

2) Aim: To write a C program to implement the CPU


scheduling algorithm for Round Robin.

Solution:-

Round Robin Scheduling is a scheduling algorithm used by the system


to schedule CPU utilization. This is a preemptive algorithm. There exist
a fixed time slice associated with each request called the quantum. The
job scheduler saves the progress of the job that is being executed
currently and moves to the next job present in the queue when a
particular process is executed for a given time quantum.

ROUND ROBIN SCHEDULING ALGORITHM

 We first have a queue where the processes are arranged in first


come first serve order.
 A quantum value is allocated to execute each process.

 The first process is executed until the end of the quantum value.
After this, an interrupt is generated and the state is saved.

 The CPU then moves to the next process and the same method is
followed.

 Same steps are repeated till all the processes are over.

Code for RR Scheduling:-


#include<stdio.h>
 
int main()
{
      int i, limit, total = 0, x, counter = 0, time_quantum;
      int wait_time = 0, turnaround_time = 0, arrival_time[10], burst_time[10],
temp[10];
      float average_wait_time, average_turnaround_time;
      printf("nEnter Total Number of Processes:t");
      scanf("%d", &limit);
      x = limit;
      for(i = 0; i < limit; i++)
      {
            printf("nEnter Details of Process[%d]n", i + 1);
            printf("Arrival Time:t");
            scanf("%d", &arrival_time[i]);
            printf("Burst Time:t");
            scanf("%d", &burst_time[i]);
            temp[i] = burst_time[i]; }
      printf("nEnter Time Quantum:t");
      scanf("%d", &time_quantum);
      printf("nProcess IDttBurst Timet Turnaround Timet Waiting Timen");
      for(total = 0, i = 0; x != 0;)
      {
            if(temp[i] <= time_quantum && temp[i] > 0)
            {
                  total = total + temp[i];
                  temp[i] = 0;
                  counter = 1; }
            else if(temp[i] > 0)
            {
                  temp[i] = temp[i] - time_quantum;
                  total = total + time_quantum;
            }
            if(temp[i] == 0 && counter == 1)
            {
                  x--;
                  printf("nProcess[%d]tt%dtt %dttt %d", i + 1, burst_time[i],
total - arrival_time[i], total - arrival_time[i] - burst_time[i]);
                  wait_time = wait_time + total - arrival_time[i] - burst_time[i];
                  turnaround_time = turnaround_time + total - arrival_time[i];
                  counter = 0;
            }
            if(i == limit - 1)
            {
                  i = 0;
            }
            else if(arrival_time[i + 1] <= total)
            {
                  i++;
            }
            else
            {
                  i = 0; }}
      average_wait_time = wait_time * 1.0 / limit;
      average_turnaround_time = turnaround_time * 1.0 / limit;
      printf("nnAverage Waiting Time:t%f", average_wait_time);
      printf("nAvg Turnaround Time:t%fn", average_turnaround_time);
      return 0;}

You might also like