Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<conio.h>
- #include<iostream.h>
- #include<dos.h>
- struct process//contains information about processes in waiting queue
- {
- int waitt;
- int etime;
- char name[32];
- struct process * next;
- };
- void inc( struct process * q)//increases wait time of all processes in wait queue
- {
- while(q!=0)
- {
- q->waitt++;
- q=q->next;
- }
- }
- int pop(struct process** q)//remove process from wait queue to run state returns wait time of process
- {
- int wait=(*q)->waitt;
- struct process* t=*q;
- *q=(*q)->next;
- delete t;
- return wait;
- }
- void addnode(struct process** q)//add new process to wait queue
- {
- struct process *temp=new (struct process);
- cout<<"nEnter process name ";
- cin>>temp->name;
- do
- {
- cout<<"nEnter process execution time ";
- cin>>temp->etime;
- }while(temp->etime<=0);//to check valid input for process burst time
- temp->next=0;
- temp->waitt=0;
- if(*q==0)
- *q=temp;
- else
- {
- struct process* t=*q;
- while(t->next!=0)
- t=t->next;
- t->next=temp;
- }
- }
- int main(void)
- {
- clrscr();
- int e=1,wait=0,p=0,tt=1;
- struct process* q=0,*t;
- //add first process
- addnode(&q);
- e=q->etime;//Burst time of runing process
- cout<<"nProcess "<<q->name<<" startedafter a wait time of "<<q->waitt;
- cout<<" time units n";
- p++;
- wait=pop(&q);
- do
- {
- //to check addition of new process
- if(kbhit())
- addnode(&q);
- //increase wait time of waiting processes
- inc(q);
- //decrement time left for finishing of process
- e--;
- if(e==0)//if process ended
- {
- if(q!=0)/*if process is left in wait queue remove next process from wait queue and add its wait time to total time*/
- {
- e=q->etime;
- cout<<"n"<<q->name<<" started after a wait time of "<<q->waitt<<"time unitsn";
- p++;
- wait+=pop(&q);
- }
- }
- delay(100);
- cout<<tt++<<" ";//to show total time units since start of first process
- }while(q!=0||e!=0);//if process ended also no process in wait queue end loop
- cout<<"nnTotal no. of prcesses executed ="<<p;
- cout<<"nTotal wait time for all prcesses executed ="<<wait;
- cout<<"nAverage wait time for prcesses executed ="<<wait/(float)p;
- getch();
- return 0;
- }
- class process
- {
- public:
- process (const std::string &name, const int burst_time = 0, const int wait_time = 0) ;
- // getters
- std::string name () const ;
- int burst_time () const ;
- int wait_time () const ;
- const process* next () const ;
- process* next () ;
- void decrement_burst_time (const int time) ;
- private:
- int burst_time_;
- int wait_time_;
- std::string name_;
- process *next_;
- friend std::ostream& operator<< (std::ostream &os, const process &proc) ;
- friend class process_queue ;
- };
- class process_queue
- {
- public:
- process_queue () ;
- ~process_queue () ;
- bool empty () const ;
- void add_node (const process &proc) ;
- void increment_all_wait_times (const int wait) ;
- process pop () ;
- size_t size () const ;
- friend std::ostream & operator<< (std::ostream &os, const process_queue &pq) ;
- private:
- size_t size_ ;
- process *head_;
- process *tail_; // for O(1) insertions
- };
- process::process (const std::string &name, const int burst_time, const int wait_time)
- : name_ (name), burst_time_ (burst_time), wait_time_ (wait_time), next_ (NULL)
- {
- }
- process_queue::~process_queue ()
- {
- while (size_ > 0) {
- this->pop () ;
- }
- }
- #include <iostream>
- #include <string>
- #include <stdexcept>
- class process_queue ;
- class process
- {
- public:
- process (const std::string &name, const int burst_time = 0, const int wait_time = 0) ;
- std::string name () const ;
- int burst_time () const ;
- int wait_time () const ;
- const process* next () const ;
- process* next () ;
- void decrement_burst_time (const int time) ;
- private:
- int burst_time_;
- int wait_time_;
- std::string name_;
- process *next_;
- friend std::ostream& operator<< (std::ostream &os, const process &proc) ;
- friend class process_queue ;
- };
- process::process (const std::string &name, const int burst_time, const int wait_time)
- : name_ (name), burst_time_ (burst_time), wait_time_ (wait_time), next_ (NULL)
- {
- }
- std::string process::name () const
- {
- return name_ ;
- }
- int process::burst_time () const
- {
- return burst_time_ ;
- }
- int process::wait_time () const
- {
- return wait_time_ ;
- }
- const process* process::next () const
- {
- return next_ ;
- }
- process* process::next ()
- {
- return next_ ;
- }
- void process::decrement_burst_time (const int time)
- {
- burst_time_ -= time ;
- }
- std::ostream & operator<< (std::ostream &os, const process &proc)
- {
- os << "{Name = " << proc.name ()
- << ", Wait time = " << proc.wait_time ()
- << ", Burst time = " << proc.burst_time ()
- << ", Next = " ;
- if (proc.next () != NULL) {
- os << proc.next ()->name () ;
- }
- else {
- os << "None" ;
- }
- os << "}" ;
- return os ;
- }
- class process_queue
- {
- public:
- process_queue () ;
- ~process_queue () ;
- bool empty () const ;
- void add_node (const process &proc) ;
- void increment_all_wait_times (const int wait) ;
- process pop () ;
- size_t size () const ;
- friend std::ostream & operator<< (std::ostream &os, const process_queue &pq) ;
- private:
- size_t size_ ;
- process *head_;
- process *tail_; // for O(1) insertions
- };
- process_queue::process_queue () : head_ (NULL), tail_ (NULL), size_ (0)
- {
- }
- void process_queue::add_node (const process &proc)
- {
- process *temp = new process (proc) ;
- if (tail_ != NULL) {
- tail_->next_ = temp ;
- if (head_ == tail_) {
- head_->next_ = temp ;
- }
- tail_ = tail_->next_ ;
- }
- else {
- tail_ = temp ;
- head_ = tail_ ;
- }
- ++size_ ;
- }
- void process_queue::increment_all_wait_times (const int wait)
- {
- process *temp = head_ ;
- while (temp != NULL) {
- temp->wait_time_ += wait ;
- temp = temp->next_ ;
- }
- }
- // Pops the head off.
- process process_queue::pop ()
- {
- if (head_ == NULL) {
- throw std::range_error ("process_queue::pop() called while the process_queue was empty.") ;
- }
- process ret = *head_ ;
- process *temp = head_ ;
- head_ = head_->next_ ;
- delete temp ;
- --size_ ;
- return ret ;
- }
- bool process_queue::empty () const
- {
- return (size_ == 0) ;
- }
- size_t process_queue::size () const
- {
- return size_ ;
- }
- std::ostream & operator<< (std::ostream &os, const process_queue &pq)
- {
- process *temp = pq.head_ ;
- while (temp != NULL) {
- os << *temp << "n" ;
- temp = temp->next () ;
- }
- return os ;
- }
- process_queue::~process_queue ()
- {
- while (size_ > 0) {
- this->pop () ;
- }
- }
- namespace scheduler
- {
- void run_first_come_first_serve (process_queue &pq)
- {
- std::cout << "Running first-come-first-serve scheduler with no preemption." "n" ;
- while (pq.empty () == false) {
- process proc = pq.pop () ;
- std::cout << "Running " << proc.name () << "n" ;
- std::cout << proc.name () << " finished running after " << (proc.burst_time () + proc.wait_time ()) << " ns." "n" ;
- pq.increment_all_wait_times (proc.burst_time ()) ;
- std::cout << "Queue:n" << pq << "n" ;
- }
- }
- }
- int main(void)
- {
- process notepad ("notepad.exe", 500) ;
- process firefox ("firefox.exe", 1500) ;
- process excel ("excel.exe", 100) ;
- process visual_studio ("visual studio.exe", 200) ;
- process super_virus ("super virus.exe", 1000) ;
- process_queue pq ;
- pq.add_node (notepad) ;
- pq.add_node (firefox) ;
- pq.add_node (excel) ;
- pq.add_node (visual_studio) ;
- pq.add_node (super_virus) ;
- scheduler::run_first_come_first_serve (pq) ;
- return 0 ;
- }
- int number_entities = 100;
- float arrival_time = 0.0, end_svc_time = 0.0, begin_svc_time;
- for(int i = 0; i < number_entities; ++i) {
- arrival_time += interarrival_time();
- begin_svc_time = fmaxf(arrival_time, end_svc_time);
- end_svc_time = begin_svc_time + svc_time();
- /* delay in queue is begin_svc_time - arrival_time */
- /* delay in system is end_svc_time - arrival_time */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement