Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.26 KB | None | 0 0
  1. #include<conio.h>
  2. #include<iostream.h>
  3. #include<dos.h>
  4. struct process//contains information about processes in waiting queue
  5. {
  6. int waitt;
  7. int etime;
  8. char name[32];
  9. struct process * next;
  10. };
  11.  
  12. void inc( struct process * q)//increases wait time of all processes in wait queue
  13. {
  14. while(q!=0)
  15. {
  16. q->waitt++;
  17. q=q->next;
  18. }
  19. }
  20.  
  21. int pop(struct process** q)//remove process from wait queue to run state returns wait time of process
  22. {
  23. int wait=(*q)->waitt;
  24. struct process* t=*q;
  25. *q=(*q)->next;
  26. delete t;
  27. return wait;
  28. }
  29.  
  30. void addnode(struct process** q)//add new process to wait queue
  31. {
  32. struct process *temp=new (struct process);
  33. cout<<"nEnter process name ";
  34. cin>>temp->name;
  35. do
  36. {
  37. cout<<"nEnter process execution time ";
  38. cin>>temp->etime;
  39. }while(temp->etime<=0);//to check valid input for process burst time
  40. temp->next=0;
  41. temp->waitt=0;
  42. if(*q==0)
  43. *q=temp;
  44. else
  45. {
  46. struct process* t=*q;
  47. while(t->next!=0)
  48. t=t->next;
  49. t->next=temp;
  50. }
  51. }
  52.  
  53. int main(void)
  54. {
  55. clrscr();
  56. int e=1,wait=0,p=0,tt=1;
  57. struct process* q=0,*t;
  58. //add first process
  59. addnode(&q);
  60. e=q->etime;//Burst time of runing process
  61. cout<<"nProcess "<<q->name<<" startedafter a wait time of "<<q->waitt;
  62. cout<<" time units n";
  63. p++;
  64. wait=pop(&q);
  65. do
  66. {
  67. //to check addition of new process
  68. if(kbhit())
  69. addnode(&q);
  70. //increase wait time of waiting processes
  71. inc(q);
  72. //decrement time left for finishing of process
  73. e--;
  74. if(e==0)//if process ended
  75. {
  76. if(q!=0)/*if process is left in wait queue remove next process from wait queue and add its wait time to total time*/
  77. {
  78. e=q->etime;
  79. cout<<"n"<<q->name<<" started after a wait time of "<<q->waitt<<"time unitsn";
  80. p++;
  81. wait+=pop(&q);
  82. }
  83. }
  84. delay(100);
  85. cout<<tt++<<" ";//to show total time units since start of first process
  86. }while(q!=0||e!=0);//if process ended also no process in wait queue end loop
  87. cout<<"nnTotal no. of prcesses executed ="<<p;
  88. cout<<"nTotal wait time for all prcesses executed ="<<wait;
  89. cout<<"nAverage wait time for prcesses executed ="<<wait/(float)p;
  90. getch();
  91. return 0;
  92. }
  93.  
  94. class process
  95. {
  96. public:
  97. process (const std::string &name, const int burst_time = 0, const int wait_time = 0) ;
  98.  
  99. // getters
  100. std::string name () const ;
  101. int burst_time () const ;
  102. int wait_time () const ;
  103. const process* next () const ;
  104. process* next () ;
  105.  
  106. void decrement_burst_time (const int time) ;
  107.  
  108. private:
  109. int burst_time_;
  110. int wait_time_;
  111. std::string name_;
  112. process *next_;
  113.  
  114. friend std::ostream& operator<< (std::ostream &os, const process &proc) ;
  115. friend class process_queue ;
  116. };
  117.  
  118. class process_queue
  119. {
  120. public:
  121. process_queue () ;
  122. ~process_queue () ;
  123.  
  124. bool empty () const ;
  125. void add_node (const process &proc) ;
  126. void increment_all_wait_times (const int wait) ;
  127. process pop () ;
  128. size_t size () const ;
  129.  
  130. friend std::ostream & operator<< (std::ostream &os, const process_queue &pq) ;
  131.  
  132. private:
  133. size_t size_ ;
  134. process *head_;
  135. process *tail_; // for O(1) insertions
  136. };
  137.  
  138. process::process (const std::string &name, const int burst_time, const int wait_time)
  139. : name_ (name), burst_time_ (burst_time), wait_time_ (wait_time), next_ (NULL)
  140. {
  141. }
  142.  
  143. process_queue::~process_queue ()
  144. {
  145. while (size_ > 0) {
  146. this->pop () ;
  147. }
  148. }
  149.  
  150. #include <iostream>
  151. #include <string>
  152. #include <stdexcept>
  153.  
  154. class process_queue ;
  155.  
  156. class process
  157. {
  158. public:
  159. process (const std::string &name, const int burst_time = 0, const int wait_time = 0) ;
  160.  
  161. std::string name () const ;
  162. int burst_time () const ;
  163. int wait_time () const ;
  164. const process* next () const ;
  165. process* next () ;
  166.  
  167. void decrement_burst_time (const int time) ;
  168.  
  169. private:
  170. int burst_time_;
  171. int wait_time_;
  172. std::string name_;
  173. process *next_;
  174.  
  175. friend std::ostream& operator<< (std::ostream &os, const process &proc) ;
  176. friend class process_queue ;
  177. };
  178.  
  179. process::process (const std::string &name, const int burst_time, const int wait_time)
  180. : name_ (name), burst_time_ (burst_time), wait_time_ (wait_time), next_ (NULL)
  181. {
  182. }
  183.  
  184. std::string process::name () const
  185. {
  186. return name_ ;
  187. }
  188.  
  189. int process::burst_time () const
  190. {
  191. return burst_time_ ;
  192. }
  193.  
  194. int process::wait_time () const
  195. {
  196. return wait_time_ ;
  197. }
  198.  
  199. const process* process::next () const
  200. {
  201. return next_ ;
  202. }
  203.  
  204. process* process::next ()
  205. {
  206. return next_ ;
  207. }
  208.  
  209. void process::decrement_burst_time (const int time)
  210. {
  211. burst_time_ -= time ;
  212. }
  213.  
  214. std::ostream & operator<< (std::ostream &os, const process &proc)
  215. {
  216. os << "{Name = " << proc.name ()
  217. << ", Wait time = " << proc.wait_time ()
  218. << ", Burst time = " << proc.burst_time ()
  219. << ", Next = " ;
  220.  
  221. if (proc.next () != NULL) {
  222. os << proc.next ()->name () ;
  223. }
  224.  
  225. else {
  226. os << "None" ;
  227. }
  228.  
  229. os << "}" ;
  230.  
  231. return os ;
  232. }
  233.  
  234. class process_queue
  235. {
  236. public:
  237. process_queue () ;
  238. ~process_queue () ;
  239.  
  240. bool empty () const ;
  241. void add_node (const process &proc) ;
  242. void increment_all_wait_times (const int wait) ;
  243. process pop () ;
  244. size_t size () const ;
  245.  
  246. friend std::ostream & operator<< (std::ostream &os, const process_queue &pq) ;
  247.  
  248. private:
  249. size_t size_ ;
  250. process *head_;
  251. process *tail_; // for O(1) insertions
  252. };
  253.  
  254. process_queue::process_queue () : head_ (NULL), tail_ (NULL), size_ (0)
  255. {
  256. }
  257.  
  258. void process_queue::add_node (const process &proc)
  259. {
  260. process *temp = new process (proc) ;
  261.  
  262. if (tail_ != NULL) {
  263. tail_->next_ = temp ;
  264.  
  265. if (head_ == tail_) {
  266. head_->next_ = temp ;
  267. }
  268.  
  269. tail_ = tail_->next_ ;
  270. }
  271.  
  272. else {
  273. tail_ = temp ;
  274. head_ = tail_ ;
  275. }
  276.  
  277. ++size_ ;
  278. }
  279.  
  280. void process_queue::increment_all_wait_times (const int wait)
  281. {
  282. process *temp = head_ ;
  283.  
  284. while (temp != NULL) {
  285. temp->wait_time_ += wait ;
  286. temp = temp->next_ ;
  287. }
  288. }
  289.  
  290. // Pops the head off.
  291. process process_queue::pop ()
  292. {
  293. if (head_ == NULL) {
  294. throw std::range_error ("process_queue::pop() called while the process_queue was empty.") ;
  295. }
  296.  
  297. process ret = *head_ ;
  298. process *temp = head_ ;
  299. head_ = head_->next_ ;
  300. delete temp ;
  301. --size_ ;
  302.  
  303. return ret ;
  304. }
  305.  
  306. bool process_queue::empty () const
  307. {
  308. return (size_ == 0) ;
  309. }
  310.  
  311. size_t process_queue::size () const
  312. {
  313. return size_ ;
  314. }
  315.  
  316. std::ostream & operator<< (std::ostream &os, const process_queue &pq)
  317. {
  318. process *temp = pq.head_ ;
  319.  
  320. while (temp != NULL) {
  321. os << *temp << "n" ;
  322. temp = temp->next () ;
  323. }
  324.  
  325. return os ;
  326. }
  327.  
  328. process_queue::~process_queue ()
  329. {
  330. while (size_ > 0) {
  331. this->pop () ;
  332. }
  333. }
  334.  
  335. namespace scheduler
  336. {
  337. void run_first_come_first_serve (process_queue &pq)
  338. {
  339. std::cout << "Running first-come-first-serve scheduler with no preemption." "n" ;
  340.  
  341. while (pq.empty () == false) {
  342. process proc = pq.pop () ;
  343.  
  344. std::cout << "Running " << proc.name () << "n" ;
  345.  
  346. std::cout << proc.name () << " finished running after " << (proc.burst_time () + proc.wait_time ()) << " ns." "n" ;
  347. pq.increment_all_wait_times (proc.burst_time ()) ;
  348.  
  349. std::cout << "Queue:n" << pq << "n" ;
  350. }
  351. }
  352. }
  353.  
  354. int main(void)
  355. {
  356. process notepad ("notepad.exe", 500) ;
  357. process firefox ("firefox.exe", 1500) ;
  358. process excel ("excel.exe", 100) ;
  359. process visual_studio ("visual studio.exe", 200) ;
  360. process super_virus ("super virus.exe", 1000) ;
  361.  
  362. process_queue pq ;
  363. pq.add_node (notepad) ;
  364. pq.add_node (firefox) ;
  365. pq.add_node (excel) ;
  366. pq.add_node (visual_studio) ;
  367. pq.add_node (super_virus) ;
  368.  
  369. scheduler::run_first_come_first_serve (pq) ;
  370.  
  371. return 0 ;
  372. }
  373.  
  374. int number_entities = 100;
  375. float arrival_time = 0.0, end_svc_time = 0.0, begin_svc_time;
  376. for(int i = 0; i < number_entities; ++i) {
  377. arrival_time += interarrival_time();
  378. begin_svc_time = fmaxf(arrival_time, end_svc_time);
  379. end_svc_time = begin_svc_time + svc_time();
  380. /* delay in queue is begin_svc_time - arrival_time */
  381. /* delay in system is end_svc_time - arrival_time */
  382. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement