szilard-dobai

scheduling algorithm test (PF)

Nov 15th, 2020 (edited)
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. /******************************************************************************
  2.  
  3. Online C Compiler.
  4. Code, Compile, Run and Debug C program online.
  5. Write your code in this editor and press "Run" button to compile and execute it.
  6.  
  7. *******************************************************************************/
  8.  
  9. #include <stdio.h>
  10. #define min(a,b) a<=b ? a : b;
  11.  
  12. typedef struct map
  13. {
  14. int weight;
  15. int index;
  16. } map;
  17.  
  18. int
  19. main ()
  20. {
  21. int queueLength[5] = { 1, 2, 3, 20, 5 };
  22. int radioLinkQuality[5] = { 3, 3, 1, 2, 1 };
  23. int timeSinceLastServed[5] = { 2, 1, 0, 4, 2 };
  24.  
  25. map userList[5], aux;
  26. int freeChannels = 10, servedAmount = 0;
  27.  
  28. int i = 0, j = 0, n = 5;
  29.  
  30. for (i = 0; i < n; i++)
  31. {
  32. userList[i].index = i;
  33. userList[i].weight = 0;
  34. }
  35.  
  36. for (i = 0; i < n; i++)
  37. userList[i].weight = radioLinkQuality[i] * timeSinceLastServed[i];
  38.  
  39. printf ("\n\n");
  40. for (i = 0; i < n; i++)
  41. printf ("Computed weight of %d is %d.\n", i + 1, userList[i].weight);
  42.  
  43. for (i = 0; i < n - 1; i++)
  44. for (j = i; j < n; j++)
  45. if (userList[i].weight <= userList[j].weight)
  46. {
  47. aux = userList[i];
  48. userList[i] = userList[j];
  49. userList[j] = aux;
  50. }
  51.  
  52. printf ("\n\n");
  53. printf ("Current order of users is: %d (%d)", userList[0].index + 1,
  54. userList[0].weight);
  55. for (i = 1; i < n; i++)
  56. printf (", %d (%d)", userList[i].index + 1, userList[i].weight);
  57. printf (".\n");
  58.  
  59. printf ("\n\n");
  60. for (i = 0; i < n; i++)
  61. {
  62. servedAmount = min (userList[i].weight, queueLength[userList[i].index]);
  63. if (freeChannels == 0)
  64. {
  65. timeSinceLastServed[userList[i].index]++;
  66. }
  67. else
  68. {
  69. if (freeChannels >= servedAmount)
  70. {
  71. printf ("Served user %d %d channels.\n", userList[i].index + 1,
  72. servedAmount);
  73. timeSinceLastServed[userList[i].index] = 0;
  74. freeChannels -= servedAmount;
  75. }
  76. else
  77. {
  78. printf ("Served user %d %d channels.\n", userList[i].index + 1,
  79. freeChannels);
  80. timeSinceLastServed[userList[i].index] = 0;
  81. freeChannels -= freeChannels;
  82. }
  83.  
  84. }
  85. }
  86.  
  87. printf ("\n\n");
  88. for (i = 0; i < n; i++)
  89. printf ("Time since last served of %d is %d.\n", i + 1,
  90. timeSinceLastServed[i]);
  91.  
  92. return 0;
  93. }
  94.  
Add Comment
Please, Sign In to add comment