You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

64 lines
1.7 KiB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <limits.h>
  4. typedef struct {
  5. int id, at, bt, et, tat, wt;
  6. } Process;
  7. int proc_cmp(const void *a, const void *b) {
  8. return ((Process*)a)->bt - ((Process*)b)->bt;
  9. }
  10. int proc_cmp_id(const void *a, const void *b) {
  11. return ((Process*)a)->id - ((Process*)b)->id;
  12. }
  13. int main() {
  14. int n, mn = INT_MAX, curt = 0, jd = 0;
  15. printf("Number of processes: ");
  16. scanf("%d", &n);
  17. Process p[n];
  18. printf("Arrival time and Burst time:\n");
  19. for (int i = 0; i < n; ++i) {
  20. scanf("%d %d", &p[i].at, &p[i].bt);
  21. p[i].id = i;
  22. p[i].et = -1;
  23. mn = (mn < p[i].at ? mn : p[i].at);
  24. }
  25. qsort(p, n, sizeof(Process), proc_cmp);
  26. printf("ID\tAT\tBT\tET\tTAT\tWT\n");
  27. double avg_tat = 0, avg_wt = 0;
  28. while (jd < n) {
  29. mn = INT_MAX;
  30. for (int i = 0; i < n; ++i) {
  31. if (p[i].et == -1)
  32. mn = (mn < p[i].at ? mn : p[i].at);
  33. if (p[i].at <= (mn > curt ? mn : curt) && p[i].et == -1) {
  34. jd++;
  35. p[i].et = (curt > p[i].at ? curt : p[i].at) + p[i].bt;
  36. curt = p[i].et;
  37. p[i].tat = p[i].et - p[i].at;
  38. p[i].wt = p[i].tat - p[i].bt;
  39. avg_tat += p[i].tat;
  40. avg_wt += p[i].wt;
  41. break;
  42. }
  43. }
  44. curt = (curt > mn ? curt : mn);
  45. }
  46. qsort(p, n, sizeof(Process), proc_cmp_id);
  47. for (int i = 0; i < n; ++i) {
  48. printf("%d\t%d\t%d\t%d\t%d\t%d\n", p[i].id, p[i].at, p[i].bt, p[i].et, p[i].tat, p[i].wt);
  49. }
  50. avg_tat /= n;
  51. avg_wt /= n;
  52. printf("avg:\t\t\t\t%lg\t%lg\n", avg_tat, avg_wt);
  53. }