optimized
Junior Member | Редактировать | Профиль | Сообщение | Цитировать | Сообщить модератору #include<iostream.h> #include<string.h> #include "lists.h" #include "queues" enum pstate { waiting, executing, finished }; void mesg(char* txt) { cerr<<txt<<endl; } class process { protected: int timer; public: char name[40]; pstate s; process(char* name, int time):timer(timer) { strcpy(this->name,name); s=waiting; } virtual void exec(void)=0; }; class q_waiting:private c_queue<process> { public: q_waiting():c_queue(process)(false) {} process* invite() { return empty()? NULL:peek(); } void pop() { c_queue<process>::pop(); } void push(process *pr) { (c_queue<process>::push(pr); }; class controller { private: q_waiting* waiters; char name[40]; c_list<process> users; int size; public: controller(char* name, int size, q_waiting* waits):size(size); waiters(waits) { users.set_clear(false); strcpy(this->name,name); } bool intr(process* pr) { if(users.size<size) { users.add(pr); return true; } else return false; } void exec() { int i=1; while(i<=users.size) { users.get(i)->exec(); if(users.get(i)->s==finished) users.remove(i); else i++; } } void proceed() { process* temp; while(users.size!=0) { exec(); if((temp=waiters->invite())!=NULL) { if(intr(temp)) waiters->pop(); } } } }; q_waiting waiters; controller cpu_share("CPU",4,&waiters); void run(process* pr, controller& c) { if(!c.intr(pr)) waiters.push(pr); } class process_load:public process { public: process_load(char* name, int timer):process(name, timer) {} void exec (void) { cout<<name<<"copying:"<<timer<<"left"<<endl; if(--timer==0) s=finished; } }; class process_delete:public process { public: process_delete(char* name, int timer):process(name, timer) {} void exec(void) { cout<<name<<"deleting:"<<timer<<"left"<<endl; if(--timer==0) s=finished; } }; class process_move:public process { public: process_move(char* name):process(name,4) {} void exec(void) { if(--timer==3) run(new process_copy(name,4), cpu_share); if(timer==0) { run(new; process_delete(name,2), cpu_share); s=finished; } } }; int main() { run(new process_load("paintbrush",2), cpu_share); run(new process_load("photoshop",5), cpu_share); run(new process_load("ACDSee",1), cpu_share); run(new process_copy("dir1",8), cpu_share); run(new process_move("dir2"), cpu_share); run(new process_load("IE",3), cpu_share); cpu_share.proceed(); return 0; } |