Skip to main content

Assignment : 3

 Assignment : 3


Set A:

i) Write the program to simulate FCFS CPU-scheduling. The arrival time and first CPU-

Burst for different n number of processes should be input to the algorithm. Assume that

the fixed IO waiting time (2 units). The next CPU-burst should be generated randomly.

The output should give Gantt chart, turnaround time and waiting time for each process.

Also find the average waiting time and turnaround time.

CODE:

👇

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct proc_info
  4. {
  5. int atime;
  6. int cpub;
  7. int pid;  
  8. } proc[20], temp;

  9. struct ganttchart
  10. {
  11. int stime;
  12. int pid;
  13. int etime;
  14. } gchart[20];

  15. int i,j,ctime,n,gi=0,wtime[20],ttime[20];

  16. void getprocs();
  17. void start();
  18. void dispgchart();
  19. void addgchart(int);
  20. void disptimes();

  21. int main()
  22. {
  23. getprocs();
  24. ctime=0;
  25. start();
  26. dispgchart();
  27. disptimes();
  28. }

  29. void getprocs()
  30. {
  31. printf("\nHow many processes:");
  32. scanf("%d",&n);
  33. printf("\nPID\tATIME\tCPUB\n");
  34. for(i=1; i<=n; i++)
  35. {
  36. scanf("%d%d%d",&proc[i].pid, &proc[i].atime, &proc[i].cpub);
  37. }
  38. for(i=1; i<=n; i++)
  39. {
  40. for(j=i+1; j<=n; j++)
  41. {
  42. if(proc[i].atime > proc[j].atime)
  43. {
  44. temp = proc[i];
  45. proc[i] = proc[j];
  46. proc[j] = temp;
  47. }
  48. }
  49. }
  50. for(i=1; i<=n; i++)
  51. {
  52. printf("\n%d\t%d\t%d",proc[i].pid, proc[i].atime, proc[i].cpub);
  53. }
  54. }//getprocs


  55. void start()
  56. {
  57. int pid;
  58. for(i=1; i<=n; i++)
  59. {
  60. wtime[i] = ctime-proc[i].atime;
  61. addgchart(proc[i].pid);
  62. while(proc[i].cpub != 0)
  63. {
  64. ctime++; 
  65. proc[i].cpub--;
  66. if(proc[i].cpub==0)//proc finished
  67. {
  68. printf("\nProc %d completed at time %d", proc[i].pid,ctime);
  69. ttime[i] = ctime-proc[i].atime;
  70. }//if
  71. }
  72. }
  73. gchart[gi].etime = ctime;
  74. }//start

  75. void addgchart(int pid)
  76. {
  77. gchart[++gi].pid = pid;
  78. gchart[gi].stime = ctime;
  79. gchart[gi-i].etime = gchart[gi].stime;
  80. }


  81. void dispgchart()
  82. {
  83. printf("\n");
  84. for(i=1; i<=n; i++)
  85. printf("|--");
  86. printf("|\n");
  87. for(i=1; i<=n; i++)
  88. printf("| %d", gchart[i].pid);
  89. printf("|\n");

  90. for(i=1; i<=gi; i++)
  91. printf("|--");
  92. printf("|\n");

  93. for(i=1; i<=gi; i++)
  94. printf("%d  ",gchart[i].stime);

  95. printf("%d\t",gchart[gi].etime);
  96. }

  97. void disptimes()
  98. {
  99. int sumwt=0, sumtt=0,pid;
  100. printf("\n\n**Waiting Time**");
  101. printf("\nPID\tWtimes");
  102. for(i=1; i<=n; i++)
  103. {
  104. printf("\n%d\t%d",proc[i].pid, wtime[i]);
  105. sumwt += wtime[i];
  106. }
  107. printf("\nAverage: %.2f", (float)sumwt/n);
  108. printf("\n\n**Turnaround Time**");
  109. printf("\nPID\tttime");
  110. for(i=1; i<=n;i++)
  111. {
  112. printf("\n%d\t%d",i,ttime[i]);
  113. sumtt+=ttime[i];
  114. }
  115. printf("\nAverage: %.2f", (float)sumtt/n);
  116. printf("\n");
  117. }


ii) Write the program to simulate Non-preemptive Shortest Job First (SJF) -scheduling. The

arrival time and first CPU-burst for different n number of processes should be input to the

algorithm. Assume the fixed IO waiting time (2 units). The next CPU-burst should be

generated randomly. The output should give Gantt chart, turnaround time and waiting

time for each process. Also find the average waiting time and turnaround time.

CODE:

👇

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct proc_info
  4. {
  5. int atime;
  6. int cpub;
  7. int pid; 
  8. } proc[20], temp;

  9. struct ganttchart
  10. {
  11. int stime;
  12. int pid;
  13. int etime;
  14. } gchart[20];

  15. int i,j,ctime,n,gi=0,wtime[20],ttime[20];

  16. void getprocs();
  17. void start();
  18. void dispgchart();
  19. void addgchart(int);
  20. void disptimes();

  21. int main()
  22. {
  23. getprocs();
  24. ctime=0;
  25. start();
  26. dispgchart();
  27. disptimes();
  28. }

  29. void getprocs()
  30. {
  31. printf("\nHow many processes:");
  32. scanf("%d",&n);
  33. printf("\nPID\tATIME\tCPUB\n");
  34. for(i=1; i<=n; i++)
  35. {
  36. scanf("%d%d%d",&proc[i].pid, &proc[i].atime, &proc[i].cpub);
  37. }
  38. for(i=1; i<=n; i++)
  39. {
  40. for(j=i+1; j<=n; j++)
  41. {
  42. if(proc[i].cpub > proc[j].cpub)
  43. {
  44. temp = proc[i];
  45. proc[i] = proc[j];
  46. proc[j] = temp;
  47. }
  48. }
  49. }
  50. for(i=1; i<=n; i++)
  51. {
  52. printf("\n%d\t%d\t%d",proc[i].pid, proc[i].atime, proc[i].cpub);
  53. }
  54. }//getprocs


  55. void start()
  56. {
  57. int pid;
  58. for(i=1; i<=n; i++)
  59. {
  60. wtime[i] = ctime-proc[i].atime;
  61. addgchart(proc[i].pid);
  62. while(proc[i].cpub != 0)
  63. {
  64. ctime++; 
  65. proc[i].cpub--;
  66. if(proc[i].cpub==0)//proc finished
  67. {
  68. printf("\nProc %d completed at time %d", proc[i].pid,ctime);
  69. ttime[i] = ctime-proc[i].atime;
  70. }//if
  71. }
  72. }
  73. gchart[gi].etime = ctime;
  74. }//start

  75. void addgchart(int pid)
  76. {
  77. gchart[++gi].pid = pid;
  78. gchart[gi].stime = ctime;
  79. gchart[gi-i].etime = gchart[gi].stime;
  80. }


  81. void dispgchart()
  82. {
  83. printf("\n");
  84. for(i=1; i<=n; i++)
  85. printf("|--");
  86. printf("|\n");
  87. for(i=1; i<=n; i++)
  88. printf("| %d", gchart[i].pid);
  89. printf("|\n");

  90. for(i=1; i<=gi; i++)
  91. printf("|--");
  92. printf("|\n");

  93. for(i=1; i<=gi; i++)
  94. printf("%d  ",gchart[i].stime);

  95. printf("%d\t",gchart[gi].etime);
  96. }

  97. void disptimes()
  98. {
  99. int sumwt=0, sumtt=0,pid;
  100. printf("\n\n**Waiting Time**");
  101. printf("\nPID\tWtimes");
  102. for(i=1; i<=n; i++)
  103. {
  104. printf("\n%d\t%d",proc[i].pid, wtime[i]);
  105. sumwt += wtime[i];
  106. }
  107. printf("\nAverage: %.2f", (float)sumwt/n);
  108. printf("\n\n**Turnaround Time**");
  109. printf("\nPID\tttime");
  110. for(i=1; i<=n;i++)
  111. {
  112. printf("\n%d\t%d",i,ttime[i]);
  113. sumtt+=ttime[i];
  114. }
  115. printf("\nAverage: %.2f", (float)sumtt/n);
  116. printf("\n");
  117. }


Set B:

ii) Write the program to simulate Non-preemptive Priority scheduling. The arrival time and

first CPU-burst and priority for different n number of processes should be input to the

algorithm. Assume the fixed IO waiting time (2 units). The next CPU-burst should be

generated randomly. The output should give Gantt chart, turnaround time and waiting

time for each process. Also find the average waiting time and turnaround time.

CODE:

👇

  1. #include<stdio.h>
  2. #include<stdlib.h>

  3. struct proc_info
  4. {
  5. int atime;
  6. int cpub;
  7. int pid;
  8. int priority;
  9. } proc[20],temp;

  10. struct ganttchart
  11. {
  12. int stime;
  13. int pid;
  14. int etime;
  15. }gchart[20];

  16. int i,j,n,ctime,gi=0,wtime[20],ttime[20];

  17. void getprocs();
  18. void start();
  19. void dispgchart();
  20. void addgchart(int);
  21. void disptimes();
  22. int finished();

  23. int main()
  24. {
  25. // randomize();
  26. getprocs();
  27. ctime=0; //set current time to 0
  28. start();
  29. dispgchart();
  30. disptimes();
  31. }

  32. void getprocs()
  33. {
  34. printf("\n How  many processes :"); 
  35. scanf("%d",&n);
  36. printf("\n PID\tATIME \tCPUB \tPRIORITY\n");
  37. for(i=1;i<=n; i++)
  38. {
  39. scanf("%d%d%d%d",&proc[i].pid,&proc[i].atime,&proc[i].cpub,&proc[i].priority);
  40. }
  41. for(i=1;i<=n;i++)
  42. {
  43. for(j=i+1;j<=n;j++)
  44. {
  45. if(proc[i].priority<proc[j].priority)
  46. {
  47. temp=proc[i];
  48. proc[i]=proc[j];
  49. proc[j]=temp;
  50. }
  51. }
  52. }
  53. for(i=1;i<=n;i++)
  54. {
  55. //printf("%d\t",i);
  56. printf("\n %d\t%d\t%d\t%d",proc[i].pid,proc[i].atime,proc[i].cpub,proc[i].priority);
  57. }
  58. }//get proc

  59. void start()
  60. {
  61. int pid;
  62. while(!finished())
  63.       {
  64. for(i=1;i<=n;i++)
  65. {
  66.    if(proc[i].atime<=ctime&&proc[i].cpub!=0)
  67.              {
  68. wtime[i]=ctime-proc[i].atime;
  69. addgchart(proc[i].pid);
  70. while(proc[i].cpub!=0)
  71. {
  72. ctime++; //increament current time
  73. proc[i].cpub--;
  74. if(proc[i].cpub==0) //proc finished
  75. {
  76. printf("\n Proc %d completed at time %d..",proc[i].pid,ctime);
  77. ttime[i]=ctime-proc[i].atime;
  78. break;
  79. }
  80. }
  81. }
  82. }
  83.  }
  84. gchart[gi].etime=ctime;
  85. }//start

  86. void addgchart(int pid)
  87. {
  88. gchart[++gi].pid=pid;
  89. gchart[gi].stime=ctime;
  90. gchart[gi-1].etime=gchart[gi].stime;
  91. }


  92. int finished()
  93.  {
  94.    for(i=1;i<=n; i++)
  95.      {
  96.        if(proc[i].cpub!=0)
  97.         return 0;
  98.      }
  99.   return 1;
  100. }


  101. void dispgchart()
  102. {
  103.  printf("\n");
  104.   for(i=1; i<=gi; i++)
  105.     printf("|-----");
  106.     printf("|\n");
  107.     
  108.     for(i=1;i<=gi;i++)
  109.     printf("| %d ",gchart[i].pid);
  110.     printf("|\n");
  111.     
  112.     for(i=1; i<=gi; i++)
  113.     printf("|-----");
  114.     printf("|\n");
  115.     
  116.     for(i=1;i<=gi;i++)
  117.     printf("| %d ",gchart[i].stime);   
  118.     
  119.     printf("%d\n",gchart[gi].etime);
  120. }

  121.  void disptimes()
  122.  {
  123.  int sumwt=0, sumtt=0,pid;
  124.  printf("\n***Waiting time***");
  125.  printf("\nPID\tWtime");
  126.  for(i=1; i<=n; i++)
  127.   {
  128.     printf("\n%d\t%d",proc[i].pid,wtime[i]);
  129.     sumwt+=wtime[i];
  130.   }
  131.  printf("\nAverage:%.2f",(float)sumwt/n);
  132.  printf("\n***Turnaround Time***");
  133.  printf("\nPID\t ttime");
  134.  for(i=1; i<=n; i++)
  135.   {
  136.      printf("\n%d\t%d",i,ttime[i]);
  137.      sumtt+=ttime[i];
  138.    }
  139.  printf("\nAverage:%.2f",(float)sumtt/n);
  140.  
  141. }     


Set C

ii. Write the program to simulate Round Robin (RR) scheduling. The arrival time and first

CPU-burst for different n number of processes should be input to the algorithm. Also

give the time quantum as input. Assume the fixed IO waiting time (2 units). The next

CPU-burst should be generated randomly. The output should give Gantt chart, turnaround

time and waiting time for each process. Also find the average waiting time and

turnaround time.

CODE:

👇

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct proc_info
  4. {
  5. int atime;
  6. int cpub;
  7. int pid; 
  8. } proc[20], temp;

  9. struct ganttchart
  10. {
  11. int stime;
  12. int pid;
  13. int etime;
  14. } gchart[20];

  15. int i,j,ctime,n,gi=0,wtime[20],ttime[20],quantum, cnt;

  16. void getprocs();
  17. void start();
  18. void dispgchart();
  19. void addgchart(int);
  20. void disptimes();
  21. int finished();

  22. int main()
  23. {
  24. getprocs();
  25. ctime=0;
  26. start();
  27. dispgchart();
  28. disptimes();
  29. }

  30. void getprocs()
  31. {
  32. printf("\nHow many processes:");
  33. scanf("%d",&n);
  34. printf("\nPID\tATIME\tCPUB\n");
  35. for(i=1; i<=n; i++)
  36. {
  37. scanf("%d%d%d",&proc[i].pid, &proc[i].atime, &proc[i].cpub);
  38. }
  39. printf("\nEnter Time Quantum\t");
  40. scanf("%d",&quantum);
  41. for(i=1; i<=n; i++)
  42. {
  43. printf("\n%d\t%d\t%d",proc[i].pid, proc[i].atime, proc[i].cpub);
  44. }
  45. }//getprocs


  46. void start()
  47. {
  48. int pid;
  49. while(!finished())
  50. {
  51. for(i=1; i<=n; i++)
  52. {
  53. cnt=0;
  54. if(proc[i].atime <= ctime && proc[i].cpub!=0)
  55. {
  56. //wtime[i] = ctime-proc[i].atime;
  57. wtime[i] = wtime[i]+(ctime-proc[i].atime);
  58. addgchart(proc[i].pid);
  59. while(cnt < quantum)
  60. {
  61. ctime++; 
  62. proc[i].cpub--;
  63. cnt++;
  64. if(proc[i].cpub==0)//proc finished
  65. {
  66. printf("\nProc %d completed at time %d", proc[i].pid,ctime);
  67. ttime[i] = ctime-proc[i].atime;
  68. }//if
  69. }//while
  70. }//if
  71. }//for
  72. }//while
  73. gchart[gi].etime = ctime;
  74. }//start

  75. int finished()
  76. {
  77. for(i=1; i<=n; i++)
  78. {
  79. if(proc[i].cpub != 0)
  80. return 0;
  81. }
  82. return 1;
  83. }

  84. void addgchart(int pid)
  85. {
  86. gchart[++gi].pid = pid;
  87. gchart[gi].stime = ctime;
  88. gchart[gi-i].etime = gchart[gi].stime;
  89. }


  90. void dispgchart()
  91. {
  92. printf("\n");
  93. for(i=1; i<=n; i++)
  94. printf("|--");
  95. printf("|\n");
  96. for(i=1; i<=n; i++)
  97. printf("| %d", gchart[i].pid);
  98. printf("|\n");

  99. for(i=1; i<=gi; i++)
  100. printf("|--");
  101. printf("|\n");

  102. for(i=1; i<=gi; i++)
  103. printf("%d  ",gchart[i].stime);

  104. printf("%d\t",gchart[gi].etime);
  105. }

  106. void disptimes()
  107. {
  108. int sumwt=0, sumtt=0,pid;
  109. printf("\n\n**Waiting Time**");
  110. printf("\nPID\tWtimes");
  111. for(i=1; i<=n; i++)
  112. {
  113. printf("\n%d\t%d",proc[i].pid, wtime[i]);
  114. sumwt += wtime[i];
  115. }
  116. printf("\nAverage: %.2f", (float)sumwt/n);
  117. printf("\n\n**Turnaround Time**");
  118. printf("\nPID\tttime");
  119. for(i=1; i<=n;i++)
  120. {
  121. printf("\n%d\t%d",i,ttime[i]);
  122. sumtt+=ttime[i];
  123. }
  124. printf("\nAverage: %.2f", (float)sumtt/n);
  125. printf("\n");
  126. }






Comments