944,129 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2181
  • C RSS
Apr 27th, 2006
0

round robin scheduling

Expand Post »
Heloo!

I found codigo well very interesting, but the same it considers that
all the processes arrive in instant zero, as I can make to consider
arrivals in aleaórios instants.



  1. * Scheduling Simulation*/
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. /* Process Data Structure */
  7. struct process {
  8. int pid; /* Process ID */
  9. int burst; /* CPU Burst Time */
  10. int priority; /* Priority */
  11. int working; /* Working time, for round-robin scheduling */
  12. int waiting; /* Waiting time, for round-robin scheduling */
  13. struct process *next;
  14. };
  15.  
  16. /* Function Prototype Declarations */
  17. struct process *init_process (int pid, int burst, int priority);
  18. void fcfs (struct process *proc);
  19. void listprocs (struct process *proc);
  20. void priority (struct process *proc);
  21. void rr (struct process *proc, int quantum);
  22. void sjf (struct process *proc);
  23.  
  24. /* Main Program Segment */
  25. int main (void) {
  26. /* Initialize process list */
  27. struct process *plist, *ptmp;
  28. plist = init_process(1, 10, 3);
  29. plist->next = init_process(2, 1, 1); ptmp = plist->next;
  30. ptmp->next = init_process(3, 2, 3); ptmp = ptmp->next;
  31. ptmp->next = init_process(4, 1, 4); ptmp = ptmp->next;
  32. ptmp->next = init_process(5, 5, 2);
  33.  
  34. /* Perform simulations */
  35. listprocs(plist);
  36. fcfs(plist);
  37. sjf(plist);
  38. priority(plist);
  39. rr(plist, 1);
  40.  
  41. /* Terminate cleanly */
  42. while (plist != NULL) {
  43. ptmp = plist;
  44. plist = plist->next;
  45. free(ptmp);
  46. };
  47. return(0);
  48. };
  49.  
  50.  
  51. /* Process list entry initialization routine */
  52. struct process *init_process (int pid, int burst, int priority) {
  53. struct process *proc;
  54. proc = malloc(sizeof(struct process));
  55. if (proc == NULL) {
  56. printf("Fatal error: memory allocation failure.\nTerminating.\n");
  57. exit(1);
  58. };
  59. proc->pid = pid;
  60. proc->burst = burst;
  61. proc->priority = priority;
  62. proc->working = 0;
  63. proc->waiting = 0;
  64. proc->next = NULL;
  65. return(proc);
  66. };
  67.  
  68.  
  69. /* First-Come-First-Served scheduling simulation */
  70. void fcfs (struct process *proc) {
  71. int time = 0, start, end;
  72. struct process *tmp = proc;
  73.  
  74. printf("BEGIN:\tFirst-Come-First-Served scheduling simulation\n");
  75.  
  76. while (tmp != NULL) {
  77. start = time;
  78. time += tmp->burst;
  79. end = time;
  80. printf("Process: %d\tEnd Time: %d\tWaiting: %d\tTurnaround: %d\n", tmp->pid, time, start, end);
  81. tmp = tmp->next;
  82. };
  83.  
  84. printf("END:\tFirst-Come-First-served scheduling simulation\n\n");
  85. };
  86.  
  87.  
  88. /* Process listing */
  89. void listprocs (struct process *proc) {
  90. struct process *tmp = proc;
  91.  
  92. printf("BEGIN:\tProcess Listing\n");
  93.  
  94. while (tmp != NULL) {
  95. printf("PID: %d\t\tPriority: %d\tBurst: %d\n", tmp->pid, tmp->priority, tmp->burst);
  96. tmp = tmp->next;
  97. };
  98.  
  99. printf("END:\tProcess Listing\n\n");
  100. };
  101.  
  102.  
  103. /* Priority scheduling simulation
  104. * Note: lower priority value gets a higher priority
  105. */
  106. void priority (struct process *proc) {
  107. int time, start, end, highest;
  108. struct process *copy, *tmpsrc, *tmp, *beforehighest;
  109.  
  110. printf("BEGIN:\tPriority scheduling simulation\n");
  111.  
  112. /* Duplicate process list */
  113. tmpsrc = proc;
  114. copy = tmp = NULL;
  115. while (tmpsrc != NULL) {
  116. if (copy == NULL) {
  117. copy = init_process(tmpsrc->pid, tmpsrc->burst, tmpsrc->priority);
  118. tmp = copy;
  119. } else {
  120. tmp->next = init_process(tmpsrc->pid, tmpsrc->burst, tmpsrc->priority);
  121. tmp = tmp->next;
  122. };
  123. tmpsrc = tmpsrc->next;
  124. };
  125.  
  126. /* Main routine */
  127. time = 0;
  128. while (copy != NULL) {
  129. /* Find the next job */
  130. beforehighest = NULL;
  131. highest = copy->priority;
  132. tmp = copy->next;
  133. tmpsrc = copy;
  134. while (tmp != NULL) {
  135. if (tmp->priority < highest) {
  136. highest = tmp->priority;
  137. beforehighest = tmpsrc;
  138. };
  139. tmpsrc = tmp;
  140. tmp = tmp->next;
  141. };
  142.  
  143. /* Process job and remove from copy of process list */
  144. if (beforehighest == NULL) {
  145. /* Handle first job is highest priority case */
  146. start = time;
  147. time += copy->burst;
  148. end = time;
  149. printf("Process: %d\tEnd Time: %d\tWaiting: %d\tTurnaround: %d\n", copy->pid, time, start, end);
  150. tmpsrc = copy->next;
  151. free(copy);
  152. copy = tmpsrc;
  153. } else {
  154. /* Handle first job is not highest priority case */
  155. tmp = beforehighest->next;
  156. start = time;
  157. time += tmp->burst;
  158. end = time;
  159. printf("Process: %d\tEnd Time: %d\tWaiting: %d\tTurnaround: %d\n", tmp->pid, time, start, end);
  160. beforehighest->next = tmp->next;
  161. free(tmp);
  162. };
  163. };
  164.  
  165. printf("END:\tPriority scheduling simulation\n\n");
  166. };
  167.  
  168.  
  169. /* Round-Robin scheduling simulation */
  170. void rr (struct process *proc, int quantum) {
  171. int jobsremain, passes;
  172. struct process *copy, *tmpsrc, *tmp, *slot;
  173.  
  174. printf("BEGIN:\tRound-Robin scheduling simulation (Quantum: %d)\n", quantum);
  175. /* Duplicate process list */
  176. tmpsrc = proc;
  177. copy = tmp = NULL;
  178. while (tmpsrc != NULL) {
  179. if (copy == NULL) {
  180. copy = init_process(tmpsrc->pid, tmpsrc->burst, tmpsrc->priority);
  181. tmp = copy;
  182. } else {
  183. tmp->next = init_process(tmpsrc->pid, tmpsrc->burst, tmpsrc->priority);
  184. tmp = tmp->next;
  185. };
  186. tmpsrc = tmpsrc->next;
  187. };
  188.  
  189. /* Main routine */
  190. jobsremain = 1;
  191. slot = NULL;
  192. while (jobsremain) {
  193. jobsremain = 0;
  194.  
  195. /* Pick next working slot */
  196. if (slot == NULL) {
  197. slot = copy;
  198. jobsremain = 1;
  199. } else {
  200. passes = 0;
  201. do {
  202. if (slot->next == NULL) {
  203. passes++;
  204. slot = copy;
  205. } else {
  206. slot = slot->next;
  207. };
  208. } while (passes <= 2 && slot->burst == slot->working);
  209. if (passes <= 2) {
  210. jobsremain = 1;
  211. };
  212. };
  213.  
  214. /* Perform a cycle */
  215. tmp = copy;
  216. while (tmp != NULL) {
  217. if (tmp->burst > tmp->working) {
  218. if (tmp == slot) {
  219. tmp->working += quantum;
  220. } else {
  221. tmp->waiting += quantum;
  222. };
  223. };
  224. tmp = tmp->next;
  225. };
  226. };
  227.  
  228. /* Display statistics and clean up copy */
  229. tmp = copy;
  230. while (tmp != NULL) {
  231. printf("Process: %d\tWorking: %d\tWaiting: %d\tTurnaround: %d\n", tmp->pid, tmp->working, tmp->waiting, tmp->working + tmp->waiting);
  232. tmpsrc = tmp;
  233. tmp = tmp->next;
  234. free(tmpsrc);
  235. };
  236.  
  237. printf("END:\tRR scheduling simulation\n\n");
  238. };
  239.  
  240.  
  241. /* Shortest Job First scheduling simulation */
  242. void sjf (struct process *proc) {
  243. int time, start, end, shortest;
  244. struct process *copy, *tmpsrc, *tmp, *beforeshortest;
  245.  
  246. printf("BEGIN:\tShortest Job First scheduling simulation\n");
  247.  
  248. /* Duplicate process list */
  249. tmpsrc = proc;
  250. copy = tmp = NULL;
  251. while (tmpsrc != NULL) {
  252. if (copy == NULL) {
  253. copy = init_process(tmpsrc->pid, tmpsrc->burst, tmpsrc->priority);
  254. tmp = copy;
  255. } else {
  256. tmp->next = init_process(tmpsrc->pid, tmpsrc->burst, tmpsrc->priority);
  257. tmp = tmp->next;
  258. };
  259. tmpsrc = tmpsrc->next;
  260. };
  261.  
  262. /* Main routine */
  263. time = 0;
  264. while (copy != NULL) {
  265. /* Find the next job */
  266. beforeshortest = NULL;
  267. shortest = copy->burst;
  268. tmp = copy->next;
  269. tmpsrc = copy;
  270. while (tmp != NULL) {
  271. if (tmp->burst < shortest) {
  272. shortest = tmp->burst;
  273. beforeshortest = tmpsrc;
  274. };
  275. tmpsrc = tmp;
  276. tmp = tmp->next;
  277. };
  278.  
  279. /* Process job and remove from copy of process list */
  280. if (beforeshortest == NULL) {
  281. /* Handle first job is shortest case */
  282. start = time;
  283. time += copy->burst;
  284. end = time;
  285. printf("Process: %d\tEnd Time: %d\tWaiting: %d\tTurnaround: %d\n", copy->pid, time, start, end);
  286. tmpsrc = copy;
  287. copy = copy->next;
  288. free(tmpsrc);
  289. } else {
  290. /* Handle first job is not shortest case */
  291. tmp = beforeshortest->next;
  292. start = time;
  293. time += tmp->burst;
  294. end = time;
  295. printf("Process: %d\tEnd Time: %d\tWaiting: %d\tTurnaround: %d\n", tmp->pid, time, start, end);
  296. beforeshortest->next = tmp->next;
  297. free(tmp);
  298. };
  299. };
  300.  
  301. printf("END:\tShortest Job First scheduling simulation\n\n");
  302. };
Last edited by cscgal; Apr 27th, 2006 at 1:15 pm. Reason: Added code tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
regbrz is offline Offline
3 posts
since Apr 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: MFC problem
Next Thread in C Forum Timeline: MFC Code bugs





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC