Assignment 2
Assignment 2
Solution:-
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).
Solution:-
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.