944,183 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 14734
  • C++ RSS
Oct 26th, 2006
-1

CPU scheduling algorithm

Expand Post »
Hi all pls help me with the soln to this probelm, the catch is the interface (menu) needs to be Built in C++ as well.
any suggestions would be of great help as well.

thanks

It is a system written using a structured programming language (e.g. C, Turbo C) that simulates the following CPU Scheduling Algorithms:
• First Come First Serve
• Shortest Job First
• Round Robin
• Shortest Remaining Time First
The user must be given the option to choose from any of the algorithms (Menu). Jobs arrive in the queue through manual input with the following required information:

Job No. CPU Burst Time Arrival Time Status
1
2
3
4
5

Status can be Pending, Processing, Finished.

The system should be able to check for invalid inputs and provide appropriate error messages to the user.

The system then shows the movement of each job in the system (from the ready queue up to when it exits the system) plus drawing the GANTT Chart simultaneously.

At the end of the simulation, the system displays the following information:
• Waiting time for each job;
• Turnaround time for each job;
• Average waiting time for the system; and
• Average turnaround time for the system.

The system then asks the user if he wants to simulate the same algorithm, simulate another algorithm, or to quit the system.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shawinke is offline Offline
1 posts
since Oct 2006
Oct 26th, 2006
0

Re: CPU scheduling algorithm

Now that you posted the requirements of the program, what do you want from us? We will not write your program for you.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Sep 17th, 2010
-3
Re: CPU scheduling algorithm
i want the source code of CPU scheduling algorithm ( FCFS, SJF, priority ) in C++ language i need tis for my mini project
Reputation Points: 10
Solved Threads: 0
Newbie Poster
suganya4488 is offline Offline
1 posts
since Sep 2010
Sep 17th, 2010
0
Re: CPU scheduling algorithm
And I'd like to have Bill Gates' $65 Million home (Actually not -- taxes and utilities are too expensive).
Last edited by Ancient Dragon; Sep 17th, 2010 at 4:07 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Sep 19th, 2010
-1
Re: CPU scheduling algorithm
i want the source code for cpu scheduling algorithms in c++
Reputation Points: 10
Solved Threads: 0
Newbie Poster
payal garg is offline Offline
1 posts
since Sep 2010
Sep 19th, 2010
2
Re: CPU scheduling algorithm
  1. while(true)
  2. {
  3. Read previous post;
  4. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Nov 12th, 2010
0

cpu scheduling

ENJOY BUDDY
CODE:
C++ Syntax (Toggle Plain Text)
  1. // Header file for Cpu scheduling
  2.  
  3.  
  4. #include<iostream.h>
  5. #include<conio.h>
  6. #include<stdio.h>
  7.  
  8. class cpuschedule
  9. {
  10. int n,Bu[20];
  11. float Twt,Awt,A[10],Wt[10],w;
  12. public:
  13. //Getting the No of processes & burst time
  14. void Getdata();
  15. //First come First served Algorithm
  16. void Fcfs();
  17. //Shortest job First Algorithm
  18. void Sjf();
  19. //Shortest job First Algorithm with Preemption
  20. void SjfP();
  21. //Shortest job First Algorithm with NonPreemption
  22. void SjfNp();
  23. //Round Robin Algorithm
  24. void RoundRobin();
  25. //Priority Algorithm
  26. void Priority();
  27. };
  28. // Implementation file for Cpu scheduling
  29.  
  30. #include"cpuh.h"
  31. //Getting no of processes and Burst time
  32. void cpuschedule::Getdata()
  33. {
  34. int i;
  35. cout<<"Enter the no of processes:";
  36. cin>>n;
  37. for(i=1;i<=n;i++)
  38. {
  39. cout<<"Enter The BurstTime for Process p"<<i<<"= ";
  40. cin>>Bu[i];
  41. }
  42. }
  43.  
  44. //First come First served Algorithm
  45. void cpuschedule::Fcfs()
  46. {
  47. int i,B[10];
  48. Twt=0.0;
  49. for(i=1;i<=n;i++)
  50. {
  51. B[i]=Bu[i];
  52. cout<<"Burst time for process p"<<i<<"= ";
  53. cout<<B[i];
  54. }
  55. Wt[1]=0;
  56. for(i=2;i<=n;i++)
  57. {
  58. Wt[i]=B[i-1]+Wt[i-1];
  59. }
  60.  
  61. //Calculating Average Weighting Time
  62. for(i=1;i<=n;i++)
  63. Twt=Twt+Wt[i];
  64. Awt=Twt/n;
  65. cout<<"Total Weighting Time="<<Twt;
  66. cout<<"Average Weighting Time="<<Awt<<"";
  67. }
  68.  
  69. //Shortest job First Algorithm
  70. void cpuschedule::Sjf()
  71. {
  72. int i,j,temp,B[10];
  73. Twt=0.0;
  74. for(i=1;i<=n;i++)
  75. {
  76. B[i]=Bu[i];
  77. cout<<"Burst time for process p"<<i<<"= ";
  78. cout<<B[i];
  79. }
  80. for(i=n;i>=1;i--)
  81. {
  82. for(j=1;j<=n;j++)
  83. {
  84. if(B[j-1]>B[j])
  85. {
  86. temp=B[j-1];
  87. B[j-1]=B[j];
  88. B[j]=temp;
  89. }
  90. }
  91. }
  92.  
  93. Wt[1]=0;
  94. for(i=2;i<=n;i++)
  95. {
  96. Wt[i]=B[i-1]+Wt[i-1];
  97. }
  98. //calculating Average Weighting Time
  99. for(i=1;i<=n;i++)
  100. Twt=Twt+Wt[i];
  101. Awt=Twt/n;
  102. cout<<"Total Weighting Time="<<Twt;
  103. cout<<"Average Weighting Time="<<Awt<<"";
  104. }
  105.  
  106. //Shortest job First Algorithm with NonPreemption
  107.  
  108. void cpuschedule::SjfNp()
  109. {
  110. int i,B[10],Tt=0,temp,j;
  111. char S[10];
  112. float A[10],temp1,t;
  113. Twt=0.0;
  114. w=0.0;
  115. for(i=1;i<=n;i++)
  116. {
  117. B[i]=Bu[i];
  118. cout<<"Burst time for process p"<<i<<"= ";
  119. cout<<B[i];
  120. S[i]='T';
  121. Tt=Tt+B[i];
  122. cout<<"Enter the Arrival Time for"<<i<<"th process= ";
  123. cin>>A[i];
  124. }
  125.  
  126. for(i=n;i>=1;i--)
  127. {
  128. for(j=3;j<=n;j++)
  129. {
  130. if(B[j-1]>B[j])
  131. {
  132. temp=B[j-1];
  133. temp1=A[j-1];
  134. B[j-1]=B[j];
  135. A[j-1]=A[j];
  136. B[j]=temp;
  137. A[j]=temp1;
  138. }
  139. }
  140. }
  141.  
  142. for(i=1;i<=n;i++)
  143. {
  144. cout<<"p"<<i<<" "<<B[i]<<" "<<A[i];
  145. }
  146.  
  147. //For the 1st process
  148. Wt[1]=0;
  149. w=w+B[1];
  150. t=w;
  151. S[1]='F';
  152.  
  153. while(w<Tt)
  154. {
  155. i=2;
  156. while(i<=n)
  157. {
  158. if(S[i]=='T'&&A[i]<=t)
  159. {
  160. Wt[i]=w;
  161. cout<<"WT"<<i<<"="<<Wt[i];
  162. S[i]='F';
  163. w=w+B[i];
  164. t=w;
  165. i=2;
  166. }
  167. else
  168. i++;
  169. }
  170. }
  171.  
  172. for(i=1;i<=n;i++)
  173. cout<<"Wt"<<i<<"=="<<Wt[i];
  174.  
  175.  
  176. //calculating average weighting Time
  177. for(i=1;i<=n;i++)
  178. Twt=Twt+(Wt[i]-A[i]);
  179. Awt=Twt/n;
  180. cout<<"Total Weighting Time="<<Twt<<"";
  181. cout<<"Average Weighting Time="<<Awt<<"";
  182. }
  183.  
  184. //Priority Algorithm
  185.  
  186. void cpuschedule::Priority()
  187. {
  188. int i,B[10],P[10],j;
  189. w=0.0;
  190. int max;
  191. Twt=0.0;
  192. max=1;
  193. for(i=1;i<=n;i++)
  194. {
  195. B[i]=Bu[i];
  196. cout<<"Burst time for process p"<<i<<"= ";
  197. cout<<B[i];
  198. cout<<"Enter the priority for process P"<<i<<"= ";
  199. cin>>P[i];
  200. if(max<P[i])
  201. max=P[i];
  202. }
  203. j=1;
  204. while(j<=max)
  205. {
  206. i=1;
  207. while(i<=n)
  208. {
  209. if(P[i]==j)
  210. {
  211. Wt[i]=w;
  212. w=w+B[i];
  213. }
  214. i++;
  215. }
  216. j++;
  217. }
  218.  
  219. //calculating average weighting Time
  220. for(i=1;i<=n;i++)
  221. Twt=Twt+Wt[i];
  222. Awt=Twt/n;
  223. cout<<"Total Weighting Time="<<Twt<<"";
  224. cout<<"Average Weighting Time="<<Awt<<"";
  225. }
  226.  
  227. //Shortest job First Algorithm with Preemption
  228. void cpuschedule::SjfP()
  229. {
  230. int i,j,m,Wt[10],k,B[10],A[10],Tt=0,Wtm[10],temp;
  231. char S[20],start[20];
  232. int max=0,Time=0,min;
  233. float Twt=0.0,Awt;
  234. for(i=1;i<=n;i++)
  235. {
  236. B[i]=Bu[i];
  237. cout<<"Burst time for process P"<<i<<"= "<<B[i];
  238. if(B[i]>max)
  239. max=B[i];
  240. Wt[i]=0;
  241. S[i]='T';
  242. start[i]='F';
  243. Tt=Tt+B[i];
  244. cout<<"Enter the Arrival Time for"<<i<<"th process= ";
  245. cin>>A[i];
  246. if(A[i]>Time)
  247. Time=A[i];
  248. }
  249. //cout<<"Max="<<max;
  250. int w=0,flag=0,t=0;
  251. i=1;
  252. while(t<Time)
  253. {
  254. if(A[i]<=t && B[i]!=0)
  255. {
  256. if(flag==0)
  257. {
  258. Wt[i]=Wt[i]+w;
  259. cout<<"Wt["<<i<<"]="<<Wt[i];
  260. }
  261. B[i]=B[i]-1;
  262. if(B[i]==0)
  263. S[i]='F';
  264. start[i]='T';
  265. t++;
  266. w=w+1;
  267. if(S[i]!='F')
  268. {
  269. j=1;flag=1;
  270. while(j<=n && flag!=0)
  271. {
  272. if(S[j]!='F' && B[i]>B[j] && A[j]<=t && i!=j )
  273. {
  274. flag=0;
  275. Wt[i]=Wt[i]-w;
  276. i=j;
  277. }
  278. else
  279. {
  280. flag=1;
  281. }
  282. j++;
  283. }
  284. }
  285. else
  286. {
  287. i++;
  288. j=1;
  289. while(A[j]<=t &&j<=n)
  290. {
  291. if(B[i]>B[j] && S[j]!='F')
  292. {
  293. flag=0;
  294. i=j;
  295. }
  296. j++;
  297. }
  298. }
  299. }
  300. else
  301. if(flag==0)
  302. i++;
  303. }
  304.  
  305.  
  306. cout<<"Printing remaining burst time";
  307. for(i=1;i<=n;i++)
  308. cout<<"B["<<i<<"]="<<B[i];
  309. cout<<"";
  310.  
  311. while(w<Tt)
  312. {
  313. min=max+1;
  314. i=1;
  315. while(i<=n)
  316. {
  317. if(min>B[i] && S[i]=='T')
  318. {
  319. min=B[i];
  320. j=i;
  321. }
  322. i++;
  323. }
  324. i=j;
  325. if(w==Time && start[i]=='T')
  326. {
  327. w=w+B[i];
  328. S[i]='F';
  329. }
  330. else
  331. {
  332. Wt[i]=Wt[i]+w;
  333. w=w+B[i];
  334. S[i]='F';
  335. }
  336. }
  337.  
  338. cout<<"Weight info";
  339.  
  340. for(i=1;i<=n;i++)
  341. cout<<"WT["<<i<<"]="<<Wt[i];
  342. cout<<"after subtracting arrival time";
  343. for(i=1;i<=n;i++)
  344. {
  345. Wt[i]=Wt[i]-A[i];
  346. cout<<"WT["<<i<<"]="<<Wt[i];
  347. }
  348. //Calculating Average Weighting time
  349. for(i=1;i<=n;i++)
  350. Twt=Twt+Wt[i];
  351. Awt=Twt/n;
  352. cout<<"Average Weighting Time="<<Awt;
  353.  
  354.  
  355.  
  356. }
  357.  
  358. //Round Robin Algorithm
  359. void cpuschedule::RoundRobin()
  360. {
  361.  
  362. int i,j,tq,k,B[10],Rrobin[10][10],count[10];
  363. int max=0;
  364. int m;
  365. Twt=0.0;
  366. for(i=1;i<=n;i++)
  367. {
  368. B[i]=Bu[i];
  369. cout<<"Burst time for process p"<<i<<"= ";
  370. cout<<B[i];
  371. if(max<B[i])
  372. max=B[i];
  373. Wt[i]=0;
  374. }
  375. cout<<"Enter the Time Quantum=";
  376. cin>>tq;
  377. //TO find the dimension of the Rrobin array
  378. m=max/tq+1;
  379.  
  380. //initializing Rrobin array
  381. for(i=1;i<=n;i++)
  382. {
  383. for(j=1;j<=m;j++)
  384. {
  385. Rrobin[i][j]=0;
  386. }
  387. }
  388. //placing value in the Rrobin array
  389. i=1;
  390. while(i<=n)
  391. {
  392. j=1;
  393. while(B[i]>0)
  394. {
  395. if(B[i]>=tq)
  396. {
  397. B[i]=B[i]-tq;
  398. Rrobin[i][j]=tq;
  399. j++;
  400. }
  401. else
  402. {
  403. Rrobin[i][j]=B[i];
  404. B[i]=0;
  405. j++;
  406. }
  407. }
  408. count[i]=j-1;
  409. i++;
  410. }
  411.  
  412. cout<<"Display";
  413. for(i=1;i<=n;i++)
  414. {
  415. for(j=1;j<=m;j++)
  416. {
  417. cout<<"Rr["<<i<<","<<j<<"]="<<Rrobin[i][j];
  418. cout<<" ";
  419. }
  420. cout<<"";
  421. }
  422. //calculating weighting time
  423. int x=1;
  424. i=1;
  425. while(x<=n)
  426. {
  427. for(int a=1;a<x;a++)
  428. {
  429. Wt[x]=Wt[x]+Rrobin[a][i];
  430. }
  431. i=1;
  432. int z=x;
  433. j=count[z];
  434. k=1;
  435. while(k<=j-1)
  436. {
  437. if(i==n+1)
  438. {
  439. i=1;
  440. k++;
  441. }
  442. else
  443. {
  444. if(i!=z)
  445. {
  446. Wt[z]=Wt[z]+Rrobin[i][k];
  447. }
  448. i++;
  449. }
  450. }
  451. x++;
  452. }
  453. for(i=1;i<=n;i++)
  454. cout<<"Weighting Time for process P"<<i<<"="<<Wt[i];
  455.  
  456. //calculating Average Weighting Time
  457. for(i=1;i<=n;i++)
  458. Twt=Twt+Wt[i];
  459. Awt=Twt/n;
  460. cout<<"Total Weighting Time="<<Twt;
  461. cout<<"Average Weighting Time="<<Awt<<"";
  462. }
  463.  
  464. //Application file for cpu Scheduling
  465. #include "cpuh.h"
  466.  
  467. void main()
  468. {
  469. int ch,cho;
  470. cpuschedule c;
  471. do
  472. {
  473. cout<<" MENU";
  474. cout<<"1.Getting BurstTime";
  475. cout<<"2.FirstComeFirstServed";
  476. cout<<"3.ShortestJobFirst";
  477. cout<<"4.RoundRobin";
  478. cout<<"5.Priority";
  479. cout<<"6.EXIT";
  480. cout<<"Enter your choice";
  481. cin>>ch;
  482. switch(ch)
  483. {
  484. case 1:
  485. c.Getdata();
  486. break;
  487. case 2:
  488. cout<<"FIRST COME FIRST SERVED SCHEDULING";
  489. c.Fcfs();
  490. break;
  491. case 3:
  492. cout<<"SHORTEST JOB FIRST SCHEDULING";
  493. do
  494. {
  495. cout<<"1.SJF-Normel";
  496. cout<<"2.SJF-Preemptive";
  497. cout<<"3.SJF-NonPreemptive";
  498. cout<<"Enter your choice";
  499. cin>>cho;
  500. switch(cho)
  501. {
  502. case 1:
  503. c.Sjf();
  504. break;
  505. case 2:
  506. c.SjfP();
  507. break;
  508. case 3:
  509. c.SjfNp();
  510. break;
  511. }
  512. }while(cho<=3);
  513. break;
  514. case 4:
  515. cout<<"ROUND ROBIN SCHEDULING";
  516. c.RoundRobin();
  517. break;
  518. case 5:
  519. cout<<"PRIORITY SCHEDULING";
  520. c.Priority();
  521. break;
  522. case 6:
  523. break;
  524. }
  525. }while(ch<=5);
  526. }
Last edited by Nick Evan; Nov 12th, 2010 at 6:05 am. Reason: Added CODE-tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sunny7 is offline Offline
2 posts
since Nov 2010
Nov 12th, 2010
0
Re: CPU scheduling algorithm
C++ Syntax (Toggle Plain Text)
  1.  
  2. // Header file for Cpu scheduling
  3.  
  4.  
  5. #include<iostream.h>
  6. #include<conio.h>
  7. #include<stdio.h>
  8.  
  9. class cpuschedule
  10. {
  11. int n,Bu[20];
  12. float Twt,Awt,A[10],Wt[10],w;
  13. public:
  14. //Getting the No of processes & burst time
  15. void Getdata();
  16. //First come First served Algorithm
  17. void Fcfs();
  18. //Shortest job First Algorithm
  19. void Sjf();
  20. //Shortest job First Algorithm with Preemption
  21. void SjfP();
  22. //Shortest job First Algorithm with NonPreemption
  23. void SjfNp();
  24. //Round Robin Algorithm
  25. void RoundRobin();
  26. //Priority Algorithm
  27. void Priority();
  28. };
  29. // Implementation file for Cpu scheduling
  30.  
  31. #include"cpuh.h"
  32. //Getting no of processes and Burst time
  33. void cpuschedule::Getdata()
  34. {
  35. int i;
  36. cout<<"Enter the no of processes:";
  37. cin>>n;
  38. for(i=1;i<=n;i++)
  39. {
  40. cout<<"Enter The BurstTime for Process p"<<i<<"= ";
  41. cin>>Bu[i];
  42. }
  43. }
  44.  
  45. //First come First served Algorithm
  46. void cpuschedule::Fcfs()
  47. {
  48. int i,B[10];
  49. Twt=0.0;
  50. for(i=1;i<=n;i++)
  51. {
  52. B[i]=Bu[i];
  53. cout<<"Burst time for process p"<<i<<"= ";
  54. cout<<B[i];
  55. }
  56. Wt[1]=0;
  57. for(i=2;i<=n;i++)
  58. {
  59. Wt[i]=B[i-1]+Wt[i-1];
  60. }
  61.  
  62. //Calculating Average Weighting Time
  63. for(i=1;i<=n;i++)
  64. Twt=Twt+Wt[i];
  65. Awt=Twt/n;
  66. cout<<"Total Weighting Time="<<Twt;
  67. cout<<"Average Weighting Time="<<Awt<<"";
  68. }
  69.  
  70. //Shortest job First Algorithm
  71. void cpuschedule::Sjf()
  72. {
  73. int i,j,temp,B[10];
  74. Twt=0.0;
  75. for(i=1;i<=n;i++)
  76. {
  77. B[i]=Bu[i];
  78. cout<<"Burst time for process p"<<i<<"= ";
  79. cout<<B[i];
  80. }
  81. for(i=n;i>=1;i--)
  82. {
  83. for(j=1;j<=n;j++)
  84. {
  85. if(B[j-1]>B[j])
  86. {
  87. temp=B[j-1];
  88. B[j-1]=B[j];
  89. B[j]=temp;
  90. }
  91. }
  92. }
  93.  
  94. Wt[1]=0;
  95. for(i=2;i<=n;i++)
  96. {
  97. Wt[i]=B[i-1]+Wt[i-1];
  98. }
  99. //calculating Average Weighting Time
  100. for(i=1;i<=n;i++)
  101. Twt=Twt+Wt[i];
  102. Awt=Twt/n;
  103. cout<<"Total Weighting Time="<<Twt;
  104. cout<<"Average Weighting Time="<<Awt<<"";
  105. }
  106.  
  107. //Shortest job First Algorithm with NonPreemption
  108.  
  109. void cpuschedule::SjfNp()
  110. {
  111. int i,B[10],Tt=0,temp,j;
  112. char S[10];
  113. float A[10],temp1,t;
  114. Twt=0.0;
  115. w=0.0;
  116. for(i=1;i<=n;i++)
  117. {
  118. B[i]=Bu[i];
  119. cout<<"Burst time for process p"<<i<<"= ";
  120. cout<<B[i];
  121. S[i]='T';
  122. Tt=Tt+B[i];
  123. cout<<"Enter the Arrival Time for"<<i<<"th process= ";
  124. cin>>A[i];
  125. }
  126.  
  127. for(i=n;i>=1;i--)
  128. {
  129. for(j=3;j<=n;j++)
  130. {
  131. if(B[j-1]>B[j])
  132. {
  133. temp=B[j-1];
  134. temp1=A[j-1];
  135. B[j-1]=B[j];
  136. A[j-1]=A[j];
  137. B[j]=temp;
  138. A[j]=temp1;
  139. }
  140. }
  141. }
  142.  
  143. for(i=1;i<=n;i++)
  144. {
  145. cout<<"p"<<i<<" "<<B[i]<<" "<<A[i];
  146. }
  147.  
  148. //For the 1st process
  149. Wt[1]=0;
  150. w=w+B[1];
  151. t=w;
  152. S[1]='F';
  153.  
  154. while(w<Tt)
  155. {
  156. i=2;
  157. while(i<=n)
  158. {
  159. if(S[i]=='T'&&A[i]<=t)
  160. {
  161. Wt[i]=w;
  162. cout<<"WT"<<i<<"="<<Wt[i];
  163. S[i]='F';
  164. w=w+B[i];
  165. t=w;
  166. i=2;
  167. }
  168. else
  169. i++;
  170. }
  171. }
  172.  
  173. for(i=1;i<=n;i++)
  174. cout<<"Wt"<<i<<"=="<<Wt[i];
  175.  
  176.  
  177. //calculating average weighting Time
  178. for(i=1;i<=n;i++)
  179. Twt=Twt+(Wt[i]-A[i]);
  180. Awt=Twt/n;
  181. cout<<"Total Weighting Time="<<Twt<<"";
  182. cout<<"Average Weighting Time="<<Awt<<"";
  183. }
  184.  
  185. //Priority Algorithm
  186.  
  187. void cpuschedule::Priority()
  188. {
  189. int i,B[10],P[10],j;
  190. w=0.0;
  191. int max;
  192. Twt=0.0;
  193. max=1;
  194. for(i=1;i<=n;i++)
  195. {
  196. B[i]=Bu[i];
  197. cout<<"Burst time for process p"<<i<<"= ";
  198. cout<<B[i];
  199. cout<<"Enter the priority for process P"<<i<<"= ";
  200. cin>>P[i];
  201. if(max<P[i])
  202. max=P[i];
  203. }
  204. j=1;
  205. while(j<=max)
  206. {
  207. i=1;
  208. while(i<=n)
  209. {
  210. if(P[i]==j)
  211. {
  212. Wt[i]=w;
  213. w=w+B[i];
  214. }
  215. i++;
  216. }
  217. j++;
  218. }
  219.  
  220. //calculating average weighting Time
  221. for(i=1;i<=n;i++)
  222. Twt=Twt+Wt[i];
  223. Awt=Twt/n;
  224. cout<<"Total Weighting Time="<<Twt<<"";
  225. cout<<"Average Weighting Time="<<Awt<<"";
  226. }
  227.  
  228. //Shortest job First Algorithm with Preemption
  229. void cpuschedule::SjfP()
  230. {
  231. int i,j,m,Wt[10],k,B[10],A[10],Tt=0,Wtm[10],temp;
  232. char S[20],start[20];
  233. int max=0,Time=0,min;
  234. float Twt=0.0,Awt;
  235. for(i=1;i<=n;i++)
  236. {
  237. B[i]=Bu[i];
  238. cout<<"Burst time for process P"<<i<<"= "<<B[i];
  239. if(B[i]>max)
  240. max=B[i];
  241. Wt[i]=0;
  242. S[i]='T';
  243. start[i]='F';
  244. Tt=Tt+B[i];
  245. cout<<"Enter the Arrival Time for"<<i<<"th process= ";
  246. cin>>A[i];
  247. if(A[i]>Time)
  248. Time=A[i];
  249. }
  250. //cout<<"Max="<<max;
  251. int w=0,flag=0,t=0;
  252. i=1;
  253. while(t<Time)
  254. {
  255. if(A[i]<=t && B[i]!=0)
  256. {
  257. if(flag==0)
  258. {
  259. Wt[i]=Wt[i]+w;
  260. cout<<"Wt["<<i<<"]="<<Wt[i];
  261. }
  262. B[i]=B[i]-1;
  263. if(B[i]==0)
  264. S[i]='F';
  265. start[i]='T';
  266. t++;
  267. w=w+1;
  268. if(S[i]!='F')
  269. {
  270. j=1;flag=1;
  271. while(j<=n && flag!=0)
  272. {
  273. if(S[j]!='F' && B[i]>B[j] && A[j]<=t && i!=j )
  274. {
  275. flag=0;
  276. Wt[i]=Wt[i]-w;
  277. i=j;
  278. }
  279. else
  280. {
  281. flag=1;
  282. }
  283. j++;
  284. }
  285. }
  286. else
  287. {
  288. i++;
  289. j=1;
  290. while(A[j]<=t &&j<=n)
  291. {
  292. if(B[i]>B[j] && S[j]!='F')
  293. {
  294. flag=0;
  295. i=j;
  296. }
  297. j++;
  298. }
  299. }
  300. }
  301. else
  302. if(flag==0)
  303. i++;
  304. }
  305.  
  306.  
  307. cout<<"Printing remaining burst time";
  308. for(i=1;i<=n;i++)
  309. cout<<"B["<<i<<"]="<<B[i];
  310. cout<<"";
  311.  
  312. while(w<Tt)
  313. {
  314. min=max+1;
  315. i=1;
  316. while(i<=n)
  317. {
  318. if(min>B[i] && S[i]=='T')
  319. {
  320. min=B[i];
  321. j=i;
  322. }
  323. i++;
  324. }
  325. i=j;
  326. if(w==Time && start[i]=='T')
  327. {
  328. w=w+B[i];
  329. S[i]='F';
  330. }
  331. else
  332. {
  333. Wt[i]=Wt[i]+w;
  334. w=w+B[i];
  335. S[i]='F';
  336. }
  337. }
  338.  
  339. cout<<"Weight info";
  340.  
  341. for(i=1;i<=n;i++)
  342. cout<<"WT["<<i<<"]="<<Wt[i];
  343. cout<<"after subtracting arrival time";
  344. for(i=1;i<=n;i++)
  345. {
  346. Wt[i]=Wt[i]-A[i];
  347. cout<<"WT["<<i<<"]="<<Wt[i];
  348. }
  349. //Calculating Average Weighting time
  350. for(i=1;i<=n;i++)
  351. Twt=Twt+Wt[i];
  352. Awt=Twt/n;
  353. cout<<"Average Weighting Time="<<Awt;
  354.  
  355.  
  356.  
  357. }
  358.  
  359. //Round Robin Algorithm
  360. void cpuschedule::RoundRobin()
  361. {
  362.  
  363. int i,j,tq,k,B[10],Rrobin[10][10],count[10];
  364. int max=0;
  365. int m;
  366. Twt=0.0;
  367. for(i=1;i<=n;i++)
  368. {
  369. B[i]=Bu[i];
  370. cout<<"Burst time for process p"<<i<<"= ";
  371. cout<<B[i];
  372. if(max<B[i])
  373. max=B[i];
  374. Wt[i]=0;
  375. }
  376. cout<<"Enter the Time Quantum=";
  377. cin>>tq;
  378. //TO find the dimension of the Rrobin array
  379. m=max/tq+1;
  380.  
  381. //initializing Rrobin array
  382. for(i=1;i<=n;i++)
  383. {
  384. for(j=1;j<=m;j++)
  385. {
  386. Rrobin[i][j]=0;
  387. }
  388. }
  389. //placing value in the Rrobin array
  390. i=1;
  391. while(i<=n)
  392. {
  393. j=1;
  394. while(B[i]>0)
  395. {
  396. if(B[i]>=tq)
  397. {
  398. B[i]=B[i]-tq;
  399. Rrobin[i][j]=tq;
  400. j++;
  401. }
  402. else
  403. {
  404. Rrobin[i][j]=B[i];
  405. B[i]=0;
  406. j++;
  407. }
  408. }
  409. count[i]=j-1;
  410. i++;
  411. }
  412.  
  413. cout<<"Display";
  414. for(i=1;i<=n;i++)
  415. {
  416. for(j=1;j<=m;j++)
  417. {
  418. cout<<"Rr["<<i<<","<<j<<"]="<<Rrobin[i][j];
  419. cout<<" ";
  420. }
  421. cout<<"";
  422. }
  423. //calculating weighting time
  424. int x=1;
  425. i=1;
  426. while(x<=n)
  427. {
  428. for(int a=1;a<x;a++)
  429. {
  430. Wt[x]=Wt[x]+Rrobin[a][i];
  431. }
  432. i=1;
  433. int z=x;
  434. j=count[z];
  435. k=1;
  436. while(k<=j-1)
  437. {
  438. if(i==n+1)
  439. {
  440. i=1;
  441. k++;
  442. }
  443. else
  444. {
  445. if(i!=z)
  446. {
  447. Wt[z]=Wt[z]+Rrobin[i][k];
  448. }
  449. i++;
  450. }
  451. }
  452. x++;
  453. }
  454. for(i=1;i<=n;i++)
  455. cout<<"Weighting Time for process P"<<i<<"="<<Wt[i];
  456.  
  457. //calculating Average Weighting Time
  458. for(i=1;i<=n;i++)
  459. Twt=Twt+Wt[i];
  460. Awt=Twt/n;
  461. cout<<"Total Weighting Time="<<Twt;
  462. cout<<"Average Weighting Time="<<Awt<<"";
  463. }
  464.  
  465. //Application file for cpu Scheduling
  466. #include "cpuh.h"
  467.  
  468. void main()
  469. {
  470. int ch,cho;
  471. cpuschedule c;
  472. do
  473. {
  474. cout<<" MENU";
  475. cout<<"1.Getting BurstTime";
  476. cout<<"2.FirstComeFirstServed";
  477. cout<<"3.ShortestJobFirst";
  478. cout<<"4.RoundRobin";
  479. cout<<"5.Priority";
  480. cout<<"6.EXIT";
  481. cout<<"Enter your choice";
  482. cin>>ch;
  483. switch(ch)
  484. {
  485. case 1:
  486. c.Getdata();
  487. break;
  488. case 2:
  489. cout<<"FIRST COME FIRST SERVED SCHEDULING";
  490. c.Fcfs();
  491. break;
  492. case 3:
  493. cout<<"SHORTEST JOB FIRST SCHEDULING";
  494. do
  495. {
  496. cout<<"1.SJF-Normel";
  497. cout<<"2.SJF-Preemptive";
  498. cout<<"3.SJF-NonPreemptive";
  499. cout<<"Enter your choice";
  500. cin>>cho;
  501. switch(cho)
  502. {
  503. case 1:
  504. c.Sjf();
  505. break;
  506. case 2:
  507. c.SjfP();
  508. break;
  509. case 3:
  510. c.SjfNp();
  511. break;
  512. }
  513. }while(cho<=3);
  514. break;
  515. case 4:
  516. cout<<"ROUND ROBIN SCHEDULING";
  517. c.RoundRobin();
  518. break;
  519. case 5:
  520. cout<<"PRIORITY SCHEDULING";
  521. c.Priority();
  522. break;
  523. case 6:
  524. break;
  525. }
  526. }while(ch<=5);
  527. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sunny7 is offline Offline
2 posts
since Nov 2010

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: Recursion part of string reverse, link error
Next Thread in C++ Forum Timeline: simple browser





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


Follow us on Twitter


© 2011 DaniWeb® LLC