User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 397,758 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,422 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Nov 28th, 2006
Views: 9,339
This code calculates and the average waiting time of the process given acc to their burst time..It is a famous scheduling algorithm for process scheduling in operating sysytem . After calculating waiting time it also generates the gantt chart for the process given .
c Syntax | 5 stars
  1. //////////////////////////////////////////////////////
  2. /////// SHORTEST JOB FIRST ALGORITHM ///////
  3. ////////////////////////////////////////////////////
  4.  
  5. /* Developed BY : Harsh Chandra
  6.   Contact : harshchandra@gmail.com
  7.   Tested & Compiled on Turbo C++ IDE
  8. */
  9.  
  10.  
  11.  
  12. # include<stdio.h>
  13. # include<conio.h>
  14. # include<alloc.h>
  15.  
  16. struct node
  17. { int process_no;
  18. int btime;
  19. struct node *link;
  20. };
  21.  
  22. void append(struct node **,int,int);
  23. void display(struct node *,int);
  24. void chart(struct node **);
  25.  
  26. void main()
  27. { clrscr();
  28. struct node *p;
  29. p = NULL; /* Empty Linked list */
  30. int i=1,time;
  31. char ans = 'y';
  32. while(ans != 'n')
  33. { printf("\nEnter Burst Time For Process No. %d : ",i);
  34. scanf("%d",&time);
  35. append(&p,i,time);
  36. printf("Any More Process (Y/N) ? ");
  37. ans = getche();
  38. i++;
  39. }
  40. getch();
  41. clrscr();
  42. printf("\nThe Order Of Execution Of Process For SJF Algorithm Will be :- ");
  43. display(p,1);
  44. getch();
  45. printf("\nThe Waiting Time Of Process For SJF Algorithm Will be :- ");
  46. display(p,2);
  47. getch();
  48. chart(&p);
  49. getch();
  50.  
  51. }
  52.  
  53. void append(struct node **q,int i,int time)
  54. { struct node *r,*temp = *q;
  55. r = (struct node *)malloc(sizeof(struct node));
  56. r->process_no = i;
  57. r->btime = time;
  58.  
  59. /* If list is empty or new data to be inserted b4 the first node */
  60. if(*q==NULL || (*q)->btime > time)
  61. { *q = r;
  62. (*q)->link = temp;
  63. }
  64. else
  65. { /* traverse the entire linked list
  66.   to search the position the new node to be inserted */
  67. while(temp != NULL)
  68. { if(temp->btime <=time &&(temp->link->btime > time || temp->link ==NULL))
  69. { r->link = temp->link;
  70. temp->link = r;
  71. r->link = NULL;
  72. }
  73. temp = temp->link;
  74. }
  75. }
  76. }
  77.  
  78. void display(struct node *q,int kk)
  79. { struct node *temp;
  80. int wtime,avg=0,total = 0,count=0;
  81. temp = q;
  82. int i;
  83. printf("\n");
  84. printf("\n\t\t%c",218);
  85. for(i=1;i<33;i++)
  86. { if(i==17)
  87. printf("%c",194);
  88. else
  89. printf("%c",196);
  90. }
  91. printf("%c",191);
  92. if(kk==1)
  93. { printf("\n\t\t%c PROCESS NO. %c BURST TIME %c\n",179,179,179);
  94. }
  95. if(kk==2)
  96. { printf("\n\t\t%c PROCESS NO. %c WAIT TIME %c\n",179,179,179);
  97. }
  98. printf("\t\t%c",195);
  99. for(i=1;i<33;i++)
  100. { if(i==17)
  101. printf("%c",197);
  102. else
  103. printf("%c",196);
  104. }
  105. printf("%c",180);
  106.  
  107. while( temp !=NULL)
  108. { count++;
  109. if(kk==1)
  110. { printf("\n\t\t%c %d. %c %3d %c",179,temp->process_no,179,temp->btime,179);
  111. }
  112. if(kk==2)
  113. { if(temp ==q) /* First Process In The Ready Queue */
  114. { wtime = 0;
  115. total+= temp->btime;
  116. avg+=wtime;
  117. }
  118. else
  119. { wtime = total;
  120. total+= temp->btime;
  121. avg +=wtime;
  122. }
  123.  
  124. printf("\n\t\t%c %d. %c %3d %c",179,temp->process_no,179,wtime,179);
  125.  
  126. }
  127. temp=temp->link;
  128. }
  129. printf("\n\t\t%c",192);
  130. for(i=1;i<33;i++)
  131. { if(i==17)
  132. printf("%c",193);
  133. else
  134. printf("%c",196);
  135. }
  136. printf("%c",217);
  137. if(kk==2)
  138. { printf("\nThe Average Waiting Time (%d %c %d) = %.2f",avg,246,count,float(avg/float(count)));
  139. }
  140.  
  141. }
  142.  
  143. void chart(struct node **q)
  144. {
  145. printf("\n");
  146. printf("\nThe Glant Chart Is As Follows :-\n");
  147.  
  148. struct node *temp,*temp1,*temp2,*temp3,*temp4;
  149. temp = *q; temp1 = *q;temp2 = *q;temp3 = *q;temp4=*q;
  150. int sum = 0;float sfactor;
  151. while(temp4 !=NULL)
  152. { sum+=temp4->btime;
  153. temp4 = temp4->link;
  154. }
  155. if(sum<80)
  156. { sfactor = 1.0;
  157. }
  158. else
  159. { sfactor = (sum%80)/float(sum);
  160. }
  161.  
  162. int i,k=0;
  163. printf("%d",k);
  164. while(temp3 != NULL)
  165. { if((sfactor*temp3->btime) == 0)
  166. { goto harsh;
  167. }
  168. for(i=-1;i<=(sfactor*temp3->btime);i++)
  169. { printf(" ");
  170. }
  171. k+=temp3->btime;
  172. printf("%d",k);
  173. harsh:
  174. temp3=temp3->link;
  175. }
  176. printf("\n");
  177. while( temp !=NULL)
  178. { if(temp == *q)
  179. {
  180. printf("%c%c",218,196);
  181. if((sfactor*temp->btime) == 0)
  182. { goto last;
  183. }
  184.  
  185. for(i=-1;i<=(sfactor*temp->btime);i++)
  186. { printf("%c",196);
  187. }
  188. if(temp->link != NULL)
  189. { printf("%c",194);
  190. }
  191. else
  192. { printf("%c",191);
  193. }
  194. }
  195. else
  196. { if((sfactor*temp->btime) == 0)
  197. { goto last;
  198. }
  199. for(i=-1;i<=(sfactor*temp->btime);i++)
  200. { printf("%c",196);
  201. }
  202. if(temp->link != NULL)
  203. { printf("%c",194);
  204. }
  205. else
  206. { printf("%c",191);
  207. }
  208. }
  209. last:
  210. temp = temp->link;
  211.  
  212. }
  213. printf("\n");
  214. printf("%c ",179);
  215.  
  216. while(temp1 != NULL)
  217. { if((sfactor*temp1->btime) == 0)
  218. { goto last1;
  219. }
  220. for(i=0;i<=(sfactor*temp1->btime);i++)
  221. { if(i==int(sfactor*temp1->btime)/2)
  222. { printf("P%d",temp1->process_no);
  223. }
  224. else
  225. printf(" ");
  226. }
  227. printf("%c",179);
  228. last1:
  229. temp1 = temp1->link;
  230.  
  231. }
  232.  
  233. printf("\n");
  234. while(temp2 !=NULL)
  235. { if(temp2 == *q)
  236. { printf("%c%c",192,196);
  237. if((sfactor*temp2->btime) == 0)
  238. { goto last2;
  239. }
  240. for(i=-1;i<=(sfactor*temp2->btime);i++)
  241. { printf("%c",196);
  242. }
  243. if(temp2->link != NULL)
  244. { printf("%c",193);
  245. }
  246. else
  247. { printf("%c",217);
  248. }
  249. }
  250. else
  251. { if((sfactor*temp2->btime) == 0)
  252. { goto last2;
  253. }
  254. for(i=-1;i<=(sfactor*temp2->btime);i++)
  255. { printf("%c",196);
  256. }
  257. if(temp2->link != NULL)
  258. { printf("%c",193);
  259. }
  260. else
  261. { printf("%c",217);
  262. }
  263. }
  264. last2:
  265. temp2=temp2->link;
  266.  
  267. }
  268. }
Comments (Newest First)
manutd | Junior Poster in Training | Nov 29th, 2006
1) This void main()
Should be int main( void )
2)Try to eliminate the nonstandard functions.
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 3:44 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC