algorithm uses in scheduling between RAM and CPU

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Mostafaib Mostafaib is offline Offline Sep 20th, 2004, 8:58 am |
1
hi
this is a program for simulation the scheduling in processes between RAM and CPU the program in c language
and i hope that you will engoy it and i will be ready for more help
Quick reply to this message  
C Syntax
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define MAX 5
  4. #define TRUE 1
  5. #define FALSE 0
  6. struct QType
  7. {
  8. int time;
  9. int a_time;
  10. int priority;
  11. int executions_no;
  12. int p_id;
  13. struct QType * next;
  14. };
  15. struct QType *front;
  16. struct QType *rear;
  17. ///////////////////////////////////////////////////////////////////////
  18. int awt,sum_time,wt,p_no,Quantum; //external variables
  19. int arr[30];
  20. ///////////////////////////////////////////////////////////////////////
  21. int empty()
  22. {
  23. if (front == NULL && rear == NULL)
  24. return TRUE;
  25. else return FALSE;
  26. }
  27. ///////////////////////////////////////////////////////////////////////
  28. void addQ(int t_time,int t_a_time,int t_priority)
  29. {
  30. struct QType * ptr = (struct QType *) malloc(sizeof(struct QType));
  31. ptr->time=t_time;
  32. ptr->a_time=t_a_time;
  33. ptr->priority=t_priority;
  34. ptr->executions_no=0;
  35. ptr->next=NULL;
  36. if (empty())
  37. {
  38. rear = ptr;
  39. front = ptr;
  40. }
  41. else
  42. {
  43. rear->next=ptr;
  44. rear = ptr;
  45. }
  46. }
  47. /////////////////////////////////////////////////////////////////////
  48. int delQ()
  49. {
  50. int item;
  51. if (empty())
  52. {
  53. printf("Empty\n");
  54. return(-1);
  55. }
  56. else
  57. {
  58. struct QType * ptr;
  59. ptr = front;
  60. front=front->next;
  61. item=ptr->time;
  62. free(ptr);
  63. return(item) ;
  64. }
  65. }
  66. ////////////////////////////////////////////////////////////////////////
  67. void showQ()
  68. {
  69. struct QType * sFront = front ;
  70. printf("\nQ = [") ;
  71. while( sFront != NULL )
  72. {
  73. printf("%d,",sFront->time);
  74. sFront = sFront->next;
  75. }
  76. printf("\b]\n");
  77. }
  78. //////////////////////////////////////////////////////////////////////
  79. void sortQ()
  80. {
  81. int p,t,a,id;
  82. struct QType *i,*j=front;
  83. for(i=front;i!=NULL;i=i->next)
  84. for(j=i;j!=NULL;j=j->next)
  85. {
  86. if(i->time>j->time)
  87. {
  88. p=i->priority;
  89. t=i->time;
  90. a=i->a_time;
  91. id=i->p_id;
  92. i->priority=j->priority;
  93. i->time=j->time;
  94. i->a_time=j->a_time;
  95. i->p_id=j->p_id;
  96. j->priority=p;
  97. j->time=t;
  98. j->a_time=a;
  99. j->p_id=id;
  100. }
  101. }
  102. }
  103. /////////////////////////////////////////////////////////////////////////
  104. void priority_sortQ()
  105. {
  106. int p,t,a,id;
  107. struct QType *i,*j=front;
  108. for(i=front;i!=NULL;i=i->next)
  109. for(j=i;j!=NULL;j=j->next)
  110. {
  111. if(i->priority<j->priority)
  112. {
  113. p=i->priority;
  114. t=i->time;
  115. a=i->a_time;
  116. id=i->p_id;
  117. i->priority=j->priority;
  118. i->time=j->time;
  119. i->a_time=j->a_time;
  120. i->p_id=j->p_id;
  121. j->priority=p;
  122. j->time=t;
  123. j->a_time=a;
  124. j->p_id=id;
  125. }
  126. }
  127.  
  128. }
  129. /////////////////////////////////////////////////////////////////////
  130. void choose()
  131. {
  132. int i,time,a_time,priority;
  133. clrscr();
  134. printf("\nWhat's the number of the processes you want to execute? ");
  135. scanf("%d",&p_no);
  136. printf("\nEnter the time Quantum for the Round Robin Scheduling ");
  137. scanf("%d",&Quantum);
  138. for(i=1;i<=p_no;i++)
  139. {
  140. printf("\n///////////////////////////////////////////////////////////");
  141. printf("\nEnter the time needed for process No. %d ",i);
  142. scanf("%d",&time);
  143. printf("\nEnter the process arrival time ");
  144. scanf("%d",&a_time);
  145. printf("\nEnter the priority of this process ");
  146. scanf("%d",&priority);
  147. addQ(time,a_time,priority);
  148. rear->p_id=i;
  149. arr[i]=time; //for the Round Robin sch.
  150. }
  151. }
  152. /////////////////////////////////////////////////////////////////////////
  153. void FCFS()
  154. {
  155. int x=p_no;
  156. struct QType * F;
  157. awt=wt=0;
  158. F=front;
  159. x=p_no-1;
  160. while(F->next!=NULL)
  161. {
  162. wt+=F->time;
  163. awt+=x*F->time;
  164. printf("\nwt for process %d= %d ",++F->p_id,wt);
  165. x--;
  166. F=F->next;
  167. }
  168. showQ();
  169. printf("\nawt= %0.2f Ms",(float)awt/p_no,"\n\n");
  170. }
  171. /////////////////////////////////////////////////////////////////////////
  172. void SJF()
  173. {
  174. int i;
  175. struct QType * F;
  176. awt=wt=0;
  177. front=rear=NULL;
  178. for(i=1;i<=p_no;i++)
  179. {
  180. addQ(arr[i],0,0);
  181. rear->p_id=i;
  182. }
  183. F=front;
  184. sortQ();
  185. while(F->next!=NULL)
  186. {
  187. wt+=F->time;
  188. awt+=wt;
  189. F=F->next;
  190. printf("\n the waiting time for %d process is %d",F->p_id,wt);
  191. }
  192. printf("\nawt= %0.2f Ms",(float)awt/p_no,"\n\n");
  193. }
  194. /////////////////////////////////////////////////////////////////////////
  195. void PRIORITY()
  196. {
  197. int x,i;
  198. struct QType * front1=front;
  199. sum_time=awt=0;
  200. x=p_no-1;
  201. printf("\nPRIORITY\nThe procecess will execute in this order ");
  202. while(front1->next!=NULL)
  203. {
  204. priority_sortQ();
  205. ex: front1=front;
  206. if(can_exe(front1->a_time))
  207. {
  208. sum_time+=front1->time;
  209. awt+=x*front1->time;
  210. awt=awt-front1->a_time;
  211. x--;
  212. printf("%d ",delQ());
  213. }
  214. else
  215. {
  216. addQ(front->time,front->a_time,front->priority);
  217. delQ();
  218. goto ex;
  219. }
  220. }
  221. // delQ();
  222. printf("\nawt= %0.2f Ms",(float)awt/p_no,"\n\n");
  223. }
  224. /////////////////////////////////////////////////////////////////////////
  225. int can_exe(int item)
  226. {
  227. struct QType *ptr=front;
  228. int x=0;
  229. for(ptr=front;ptr!=NULL;ptr=ptr->next)
  230. {
  231. if(item<=sum_time)
  232. x=1;
  233. }
  234. return x;
  235. }
  236. /////////////////////////////////////////////////////////////////////////
  237. void ROUND_ROBIN()
  238. {
  239. int temp,i=0;
  240. sum_time=awt=wt=0;
  241. front=rear=NULL;
  242. for(i=1;i<=p_no;i++)
  243. {
  244. addQ(arr[i],0,0);
  245. rear->p_id=i;
  246. }
  247. printf("\nROUND ROBIN\n");
  248. while(front!=NULL)
  249. {
  250. if(front->time>Quantum)
  251. {
  252. temp=front->time;
  253. front->executions_no++;
  254. front->time=Quantum;
  255. addQ(temp-front->time,0,0);
  256. rear->executions_no=front->executions_no++;
  257. rear->p_id=front->p_id;
  258. sum_time+=front->time;
  259. delQ();
  260. }
  261. else
  262. {
  263. wt=sum_time-(front->executions_no*Quantum);
  264. printf("\nThe waiting time for the process %d = %d",front->p_id,wt);
  265. sum_time+=front->time;
  266. awt+=wt;
  267. delQ();
  268. }
  269. }
  270. printf("\nThe avg. waiting time is %0.2f",(float)awt/p_no);
  271. }
  272. /////////////////////////////////////////////////////////////////////////
  273. void main()
  274. {
  275. int ch;
  276. char stop;
  277. choose();
  278. printf("\n1- PRESS TO VIEW THE REPORT ");
  279. printf("\n2- Enter new other data ");
  280. printf("\n3- Exit");
  281. scanf("%d",&ch);
  282. switch(ch)
  283. {
  284. case 1:
  285. printf("\nFCFS//////////////////////////////////////\n");
  286. FCFS();
  287. printf("\n\n\n\nPRIORITY//////////////////////////////////\n");
  288. PRIORITY();
  289. printf("\n\n\n\nSJF///////////////////////////////////////\n");
  290. SJF();
  291. printf("\n\n\n\nROUND_ROBIN////////////////////////////////\n");
  292. ROUND_ROBIN();
  293. printf("\n\n\n\n\n\n\n\n\n\nENTER ANY NO. TO EXIT ");
  294. scanf("%d",wt);//working as getche() function//
  295. break;
  296. case 2:
  297. choose();
  298. break;
  299. case 3:
  300. break;
  301. }
  302. }
  303.  
  304.  
0
infamous infamous is offline Offline | Sep 20th, 2004
forgot to check malloc() for error return, looks cool other than that :-D
 
0
jerry@83 jerry@83 is offline Offline | Sep 20th, 2004
hey, Mostafaib thankz for the code you got there,..eh quite useful for my c++ reference, thankz alot really
 
0
vegaseat vegaseat is offline Offline | Dec 28th, 2004
Great code Mostafaib, just what I was looking for!
 
 

Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC