diff --git a/Entities/Process.cpp b/Entities/Process.cpp index 64f479d..a7ab6d4 100644 --- a/Entities/Process.cpp +++ b/Entities/Process.cpp @@ -5,7 +5,10 @@ Process::Process(int id, int AT, int cpuT) : responseTime(-1), waitingTime(-1), terminationTime(-1) , turnAroundTime(-1), finishedTime(0), currentIoTime(0), state(NEW) , child(nullptr) {} - +int Process::getId() const +{ + return processId; +} int Process::getIoTime() const { return currentIoTime; diff --git a/Entities/Process.h b/Entities/Process.h index 3a2a64d..30e840b 100644 --- a/Entities/Process.h +++ b/Entities/Process.h @@ -19,6 +19,7 @@ class Process Process* child; public: Process(int id, int AT, int cpuT); + int getId() const; int getIoTime() const; int getWaitingTime() const; int getRemainingTime() const; diff --git a/Entities/processor/FCFSProcessor.cpp b/Entities/processor/FCFSProcessor.cpp index fcab391..3fcc9cb 100644 --- a/Entities/processor/FCFSProcessor.cpp +++ b/Entities/processor/FCFSProcessor.cpp @@ -1 +1,88 @@ #include "FCFSProcessor.h" +FCFSProcessor::FCFSProcessor() +{ +} + +int FCFSProcessor::getFinishTime() +{ + if (currentProcess) + { + return finishTime + currentProcess->getRemainingTime(); + } + return finishTime; +} + +void FCFSProcessor::addProcess(Process* process) +{ + readyQueue.insertEnd(process); +} + +//kill process with passed id if found at passed timestep +void FCFSProcessor::killProcess(int processId, int sigKill) +{ + if (currentProcess && currentProcess->getId() == processId) + { + currentProcess->setTerminationTime(sigKill); + //--->move the process to terminated list + currentProcess = nullptr; + return; + } + + Node* current = readyQueue.getHead(); + Node* prev = nullptr; + + // Search for the process in the ready queue + while (current) + { + if (current->getItem()->getId() == processId) + { + current->getItem()->setTerminationTime(sigKill); + // --->Move the process to the terminated list + + // Remove the process from the ready queue + if (prev) + { + prev->setNext(current->getNext()); + } + else + { + readyQueue.setHead(current->getNext()); + } + return; + } + prev = current; + current = current->getNext(); + } +} + +void FCFSProcessor::getNextProcess(int clock) +{ + if (!currentProcess && !readyQueue.isEmpty()) + { + currentProcess = readyQueue.getHead()->getItem(); + readyQueue.deleteFirst(); + currentProcess->setState(RUN); + currentProcess->setResponseTime(clock); + } +} + +void FCFSProcessor::run(int clock) +{ + if (currentProcess && !currentProcess->isFinished()) + { + busyTime++; + currentProcess->run(); + + if (currentProcess->isFinished()) + { + currentProcess->setTerminationTime(clock); + currentProcess->setState(TRM); + finishTime = clock; + currentProcess = nullptr; + } + } + else + freeTime++; + //clock++; +} + diff --git a/Entities/processor/FCFSProcessor.h b/Entities/processor/FCFSProcessor.h index 0dfa564..d942613 100644 --- a/Entities/processor/FCFSProcessor.h +++ b/Entities/processor/FCFSProcessor.h @@ -1,9 +1,19 @@ #pragma once #include "Processor.h" - +#include"../../Utils/DataStructure/LinkedList.h" class FCFSProcessor : public Processor { +private: + LinkedList readyQueue; +public: + FCFSProcessor(); + virtual int getFinishTime(); + virtual void addProcess(Process* process); + virtual void run(int clock); + void killProcess(int processId, int sigKill); + virtual void getNextProcess(int clock); + }; diff --git a/Entities/processor/Processor.cpp b/Entities/processor/Processor.cpp new file mode 100644 index 0000000..a3c18d6 --- /dev/null +++ b/Entities/processor/Processor.cpp @@ -0,0 +1,16 @@ +#include "Processor.h" + +Processor::Processor():busy(false),busyTime(0),freeTime(0),currentProcess(nullptr) +{ + +} + +bool Processor::isBusy() +{ + return busy; +} + +int Processor::getUtilization() +{ + return (busyTime / (busyTime + freeTime)); +} diff --git a/Entities/processor/Processor.h b/Entities/processor/Processor.h index 22aacd7..c681fba 100644 --- a/Entities/processor/Processor.h +++ b/Entities/processor/Processor.h @@ -1,6 +1,23 @@ #pragma once - +#include "../Process.h" +#include"../../Control/Scheduler.h" class Processor { +private: + bool busy; + Scheduler* schedulerPtr; +protected: + int busyTime; + int finishTime; + int freeTime; + Process* currentProcess; +public: + Processor(); + virtual int getFinishTime() = 0; + virtual void getNextProcess(int clock) = 0;//get one from the DS + virtual void run(int clock)=0; + bool isBusy(); + int getUtilization(); + virtual void addProcess(Process* process) = 0; }; \ No newline at end of file diff --git a/OS-Scheduler.vcxproj b/OS-Scheduler.vcxproj index 63da28c..53bb523 100644 --- a/OS-Scheduler.vcxproj +++ b/OS-Scheduler.vcxproj @@ -134,11 +134,15 @@ + + + + diff --git a/OS-Scheduler.vcxproj.filters b/OS-Scheduler.vcxproj.filters index 08d2439..f18283b 100644 --- a/OS-Scheduler.vcxproj.filters +++ b/OS-Scheduler.vcxproj.filters @@ -39,6 +39,15 @@ Header Files + + Header Files + + + Header Files + + + Header Files + @@ -62,5 +71,8 @@ Source Files + + Source Files + \ No newline at end of file diff --git a/Utils/DataStructure/LinkedList.h b/Utils/DataStructure/LinkedList.h new file mode 100644 index 0000000..d91fdd0 --- /dev/null +++ b/Utils/DataStructure/LinkedList.h @@ -0,0 +1,280 @@ +#pragma once +#ifndef _LINKEDLIST +#define _LINKEDLIST + +#include "Node.h" +#include +using namespace std; +template +class LinkedList +{ +private: + Node* Head; //Pointer to the head of the list +public: + LinkedList(); + LinkedList(const LinkedList& LL); + void printList() const; + int getSize(); + void setHead(Node* ptr); + Node* getHead(); + void insertBeg(const T& data); + void insertEnd(const T& data); + void deleteFirst(); + void deleteLast(); + void deleteAll(); + bool deleteNode(T& value); + bool isEmpty(); + Node* getPointerTo(const T& target) const; + ~LinkedList(); + +}; +///////////////////////////////////////////////////////////////////////////////////////// + +/* +Function: LinkedList() +The constructor of the LinkedList class. + +*/ +template +LinkedList::LinkedList() +{ + Head = nullptr; +} +///////////////////////////////////////////////////////////////////////////////////////// + +/* +Function: LinkedList(const LinkedList& LL) +The copy constructor of the LinkedList class. + +*/ + +template +LinkedList::LinkedList(const LinkedList& LL) +{ + Node* origChainPtr = LL->Head; + if (!origChainPtr) + Head = nullptr; // Original LinkedList is empty; so is copy + else + { + // Copy first node + Head = new Node(); + Head->setItem(origChainPtr->getItem()); // Copy remaining nodes + Node* newChainPtr = Head; // Last-node pointer + while (origChainPtr) + { + origChainPtr = origChainPtr->getNext();// Advance pointer + // Get next item from original chain + T nextItem = origChainPtr->getItem(); + // Create a new node containing the next item + Node* newNodePtr = new Node(nextItem); + + + + // Link new node to end of new chain + newChainPtr->setNext(newNodePtr); + // Advance pointer to new last node + newChainPtr = newChainPtr->getNext(); + } // end while + + newChainPtr->setNext(nullptr); // Flag end of new chain + } // end if +} // end copy constructor +//////////////////////////////////////////////////////////////////////// +/* +* Function: PrintList. +* prints the values of all nodes in a linked list. +*/ +template +void LinkedList::printList() const +{ + cout << "\nprinting list contents:\n\n"; + Node* p = Head; + + while (p) + { + cout << "[ " << p->getItem() << " ]"; + cout << "--->"; + p = p->getNext(); + } + cout << "NULL\n"; +} +//////////////////////////////////////////////////////////////////////// +/* +* Function: insertBeg. +* Creates a new node and adds it to the beginning of a linked list. +* +* Parameters: +* - data : The value to be stored in the new node. +*/ + +template +void LinkedList::insertBeg(const T& data) +{ + Node* R = new Node(data); + R->setNext(Head); + Head = R; + +} +//////////////////////////////////////////////////////////////////////// +/* +* Function: insertEnd. +* Creates a new node and adds it to the End of a linked list. +* +* Parameters: +* - data : The value to be stored in the new node. +*/ +template +void LinkedList::insertEnd(const T& data) +{ + Node* ptr = new Node; + ptr->setItem(data); + ptr->setNext(NULL); + Node* temp = Head; + if (temp) + { + while (temp->getNext()) + { + temp = temp->getNext(); + } + temp->setNext(ptr); + return; + } + else + Head = ptr; +} +//////////////////////////////////////////////////////////////////////// +/* +* Function: deleteAll. +* Deletes all nodes of the list. +*/ +template +void LinkedList::deleteAll() +{ + Node* P = Head; + while (Head) + { + P = Head->getNext(); + delete Head; + Head = P; + } +} +//////////////////////////////////////////////////////////////////////// +/* +* Function: getSize. +*returns the size of the linkedlist. +*/ +template +int LinkedList::getSize() +{ + int size = 0; + Node * temp = Head; + while (temp) + { + size++; + temp = temp->getNext(); + } + return size; +} +template +void LinkedList::setHead(Node* ptr) +{ + Head = ptr; +} +template +Node* LinkedList::getHead() +{ + return Head; +} +//////////////////////////////////////////////////////////////////////// +/* +* Function: deleteFirst. +*Deletes the first Node of the LinkedList. +*/ +template +void LinkedList ::deleteFirst() +{ + Node* CurPtr = Head->getNext(); + delete Head; + Head = CurPtr; +} +//////////////////////////////////////////////////////////////////////// +/* +* Function: DeleteLast. +*Deletes the last Node of the LinkedList. +*/ +template +void LinkedList ::deleteLast() +{ + Node* CurPtr = Head; + while (CurPtr->getNext()->getNext()) + { + CurPtr = CurPtr->getNext(); + } + CurPtr->DeleteNext(); +} +//////////////////////////////////////////////////////////////////////// +/* +* Function: deleteNode. +*deletes the first node with the given value (if found) and returns true +*if not found, returns false. +*/ +template +bool LinkedList::deleteNode(T& value) +{ + Node* key1, * key2; + key1 = Head; + key2 = key1->getNext(); + if (Head->getItem() == value) + { + Head = Head->getNext(); + delete key1; + return true; + } + while (key2) + { + if (key2->getItem() == value) + { + key1->setNext(key2->getNext()); + delete key2; + return true; + } + key1 = key1->getNext(); + key2 = key2->getNext(); + } + return false; +} + +template +bool LinkedList::isEmpty() +{ + return (Head==nullptr); +} + +template +Node* LinkedList::getPointerTo(const T& target) const +{ + + bool found = false; + Node* curPtr = Head; + while (!found && (curPtr != nullptr)) + { + if (target == curPtr->getItem()) + found = true; + else + curPtr = curPtr->getNext(); + } // end while + return curPtr; +} // end getPointerTo The definition +/////////////////////////////////////////////////////////////////////////////////// +/* +Function: destructor +removes all nodes from the LinkedList +*/ +template +LinkedList:: ~LinkedList() +{ + deleteAll(); +} + +#endif + diff --git a/Utils/DataStructure/Node.h b/Utils/DataStructure/Node.h new file mode 100644 index 0000000..893827e --- /dev/null +++ b/Utils/DataStructure/Node.h @@ -0,0 +1,62 @@ +#pragma once +#ifndef _NODE +#define _NODE +template < typename T> +class Node +{ +private: + T item; // A data item + Node* next; // Pointer to next node +public: + Node(); + Node(const T& r_Item); + Node(const T& r_Item, Node* nextNodePtr); + void setItem(const T& r_Item); + void setNext(Node* nextNodePtr); + T getItem() const; + Node* getNext() const; +}; // end Node +#endif + +template < typename T> +Node::Node() +{ + next = nullptr; +} + +template < typename T> +Node::Node(const T& r_Item) +{ + item = r_Item; + next = nullptr; +} + +template < typename T> +Node::Node(const T& r_Item, Node* nextNodePtr) +{ + item = r_Item; + next = nextNodePtr; +} +template < typename T> +void Node::setItem(const T& r_Item) +{ + item = r_Item; +} + +template < typename T> +void Node::setNext(Node* nextNodePtr) +{ + next = nextNodePtr; +} + +template < typename T> +T Node::getItem() const +{ + return item; +} + +template < typename T> +Node* Node::getNext() const +{ + return next; +} diff --git a/Utils/DataStructure/Queue.h b/Utils/DataStructure/Queue.h new file mode 100644 index 0000000..05a3c68 --- /dev/null +++ b/Utils/DataStructure/Queue.h @@ -0,0 +1,221 @@ +#pragma once +#include "Node.h" +using namespace std; + + +template +class Queue +{ +private: + Node * backPtr; + Node * frontPtr; +public: + Queue(); + int queueSize(); + bool isEmpty() const; + bool enqueue(const T& newEntry); + bool dequeue(); + T peek() const; + void printQueue(); + ~Queue(); + + //copy constructor + Queue(const Queue & LQ); +}; +///////////////////////////////////////////////////////////////////////////////////////// + +/* +Function: Queue() +The constructor of the Queue class. + +*/ + +template +Queue::Queue() +{ + backPtr = nullptr; + frontPtr = nullptr; + +} +///////////////////////////////////////////////////////////////////////////////////////// +/* +Function: QueueSize +To return the size of the queue, + +Input: none +Output: size of the queue +*/ + +template +int Queue::queueSize() +{ + int size = 0; + Queue temp; + while (!isEmpty()) + { + temp.enqueue(frontPtr->getItem()); + dequeue(); + size++; + + } + while (!temp.isEmpty()) + { + enqueue(temp.peek()); + temp.dequeue(); + } + return size; +} +///////////////////////////////////////////////////////////////////////////////////////// +/* +Function: isEmpty +Sees whether this queue is empty. + +Input: None. +Output: True if the queue is empty; otherwise false. +*/ +template +bool Queue::isEmpty() const +{ + return (frontPtr == nullptr); +} + +///////////////////////////////////////////////////////////////////////////////////////// + +/*Function:enqueue +Adds newEntry at the back of this queue. + +Input: newEntry . +Output: True if the operation is successful; otherwise false. +*/ + +template +bool Queue::enqueue(const T& newEntry) +{ + Node* newNodePtr = new Node(newEntry); + // Insert the new node + if (isEmpty()) //special case if this is the first node to insert + frontPtr = newNodePtr; // The queue is empty + else + backPtr->setNext(newNodePtr); // The queue was not empty + + backPtr = newNodePtr; // New node is the last node now + return true; +} // end enqueue + + + ///////////////////////////////////////////////////////////////////////////////////////////////////////// + + /*Function: dequeue + Removes the front of this queue. That is, removes the item that was added + earliest. + + Input: None. + Output: True if the operation is successful; otherwise false. + */ + +template +bool Queue::dequeue() +{ + if (isEmpty()) + return false; + + Node* nodeToDeletePtr = frontPtr; + frontPtr = frontPtr->getNext(); + // Queue is not empty; remove front + if (nodeToDeletePtr == backPtr) // Special case: last node in the queue + backPtr = nullptr; + + // Free memory reserved for the dequeued node + delete nodeToDeletePtr; + return true; + +} + +///////////////////////////////////////////////////////////////////////////////////////// +/* +Function: peek +copies the front of this queue to the passed param. The operation does not modify the queue. + +Input: None. +Output: The front of the queue. +*/ +template +T Queue::peek() const +{ + if (isEmpty()) + throw std::out_of_range("Queue is empty"); + return frontPtr->getItem(); +} +/////////////////////////////////////////////////////////////////////////////////// +/* +Function: destructor +removes all nodes from the queue by dequeuing them +*/ +template +Queue::~Queue() +{ + //Free (Dequeue) all nodes in the queue + while (dequeue()); +} +///////////////////////////////////////////////////////////////////////////////////////// +/* +Function: Copy constructor +To avoid shallow copy, +copy constructor is provided + +Input: Queue: The Queue to be copied +Output: none +*/ + +template +Queue::Queue(const Queue& LQ) +{ + Node* NodePtr = LQ.frontPtr; + if (!NodePtr) //LQ is empty + { + frontPtr = backPtr = nullptr; + return; + } + + //insert the first node + Node* ptr = new Node(NodePtr->getItem()); + frontPtr = backPtr = ptr; + NodePtr = NodePtr->getNext(); + + //insert remaining nodes + while (NodePtr) + { + Node* ptr = new Node(NodePtr->getItem()); + backPtr->setNext(ptr); + backPtr = ptr; + NodePtr = NodePtr->getNext(); + } +} +///////////////////////////////////////////////////////////////////////////////////////// +/* +Function: PrintQueue +To print the Queue, + +Input: none +Output: Queue elements +*/ +template +void Queue ::printQueue() +{ + Queue temp; + cout << "\nQueue contents: "; + while (!isEmpty()) + { + cout << frontPtr->getItem() << " "; + temp.enqueue(frontPtr->getItem()); + dequeue(); + + } + while (!temp.isEmpty()) + { + enqueue(temp.peek()); + temp.dequeue(); + } + cout << endl; +} + diff --git a/x64/Debug/FCFSProcessor.obj b/x64/Debug/FCFSProcessor.obj new file mode 100644 index 0000000..b28f6ae Binary files /dev/null and b/x64/Debug/FCFSProcessor.obj differ diff --git a/x64/Debug/OS-Scheduler.Build.CppClean.log b/x64/Debug/OS-Scheduler.Build.CppClean.log new file mode 100644 index 0000000..e69de29 diff --git a/x64/Debug/OS-Scheduler.exe b/x64/Debug/OS-Scheduler.exe new file mode 100644 index 0000000..6ecabd1 Binary files /dev/null and b/x64/Debug/OS-Scheduler.exe differ diff --git a/x64/Debug/OS-Scheduler.exe.recipe b/x64/Debug/OS-Scheduler.exe.recipe new file mode 100644 index 0000000..cebcae6 --- /dev/null +++ b/x64/Debug/OS-Scheduler.exe.recipe @@ -0,0 +1,11 @@ + + + + + C:\Users\abozaid\Desktop\DS_Project\x64\Debug\OS-Scheduler.exe + + + + + + \ No newline at end of file diff --git a/x64/Debug/OS-Scheduler.ilk b/x64/Debug/OS-Scheduler.ilk new file mode 100644 index 0000000..7bfa158 Binary files /dev/null and b/x64/Debug/OS-Scheduler.ilk differ diff --git a/x64/Debug/OS-Scheduler.log b/x64/Debug/OS-Scheduler.log new file mode 100644 index 0000000..f9863c8 --- /dev/null +++ b/x64/Debug/OS-Scheduler.log @@ -0,0 +1,10 @@ + Process.cpp + FCFSProcessor.cpp + Processor.cpp + SJFProcessor.cpp + RRProcessor.cpp + Scheduler.cpp + Source.cpp + UI.cpp + Generating Code... + OS-Scheduler.vcxproj -> C:\Users\abozaid\Desktop\DS_Project\x64\Debug\OS-Scheduler.exe diff --git a/x64/Debug/OS-Scheduler.pdb b/x64/Debug/OS-Scheduler.pdb new file mode 100644 index 0000000..3bef1c2 Binary files /dev/null and b/x64/Debug/OS-Scheduler.pdb differ diff --git a/x64/Debug/OS-Scheduler.tlog/CL.command.1.tlog b/x64/Debug/OS-Scheduler.tlog/CL.command.1.tlog new file mode 100644 index 0000000..13adbcb Binary files /dev/null and b/x64/Debug/OS-Scheduler.tlog/CL.command.1.tlog differ diff --git a/x64/Debug/OS-Scheduler.tlog/CL.read.1.tlog b/x64/Debug/OS-Scheduler.tlog/CL.read.1.tlog new file mode 100644 index 0000000..f096484 Binary files /dev/null and b/x64/Debug/OS-Scheduler.tlog/CL.read.1.tlog differ diff --git a/x64/Debug/OS-Scheduler.tlog/CL.write.1.tlog b/x64/Debug/OS-Scheduler.tlog/CL.write.1.tlog new file mode 100644 index 0000000..b654b71 Binary files /dev/null and b/x64/Debug/OS-Scheduler.tlog/CL.write.1.tlog differ diff --git a/x64/Debug/OS-Scheduler.tlog/OS-Scheduler.lastbuildstate b/x64/Debug/OS-Scheduler.tlog/OS-Scheduler.lastbuildstate new file mode 100644 index 0000000..5aa1945 --- /dev/null +++ b/x64/Debug/OS-Scheduler.tlog/OS-Scheduler.lastbuildstate @@ -0,0 +1,2 @@ +PlatformToolSet=v143:VCToolArchitecture=Native64Bit:VCToolsVersion=14.31.31103:TargetPlatformVersion=10.0.19041.0: +Debug|x64|C:\Users\abozaid\Desktop\DS_Project\| diff --git a/x64/Debug/OS-Scheduler.tlog/link.command.1.tlog b/x64/Debug/OS-Scheduler.tlog/link.command.1.tlog new file mode 100644 index 0000000..c99dbf3 Binary files /dev/null and b/x64/Debug/OS-Scheduler.tlog/link.command.1.tlog differ diff --git a/x64/Debug/OS-Scheduler.tlog/link.read.1.tlog b/x64/Debug/OS-Scheduler.tlog/link.read.1.tlog new file mode 100644 index 0000000..9f32fa9 Binary files /dev/null and b/x64/Debug/OS-Scheduler.tlog/link.read.1.tlog differ diff --git a/x64/Debug/OS-Scheduler.tlog/link.write.1.tlog b/x64/Debug/OS-Scheduler.tlog/link.write.1.tlog new file mode 100644 index 0000000..a07ab10 Binary files /dev/null and b/x64/Debug/OS-Scheduler.tlog/link.write.1.tlog differ diff --git a/x64/Debug/OS-Scheduler.vcxproj.FileListAbsolute.txt b/x64/Debug/OS-Scheduler.vcxproj.FileListAbsolute.txt new file mode 100644 index 0000000..7095296 --- /dev/null +++ b/x64/Debug/OS-Scheduler.vcxproj.FileListAbsolute.txt @@ -0,0 +1 @@ +C:\Users\abozaid\Desktop\DS_Project\x64\Debug\OS-Scheduler.exe diff --git a/x64/Debug/Process.obj b/x64/Debug/Process.obj new file mode 100644 index 0000000..b7e4e90 Binary files /dev/null and b/x64/Debug/Process.obj differ diff --git a/x64/Debug/Processor.obj b/x64/Debug/Processor.obj new file mode 100644 index 0000000..d038cfa Binary files /dev/null and b/x64/Debug/Processor.obj differ diff --git a/x64/Debug/RRProcessor.obj b/x64/Debug/RRProcessor.obj new file mode 100644 index 0000000..e3bc8a1 Binary files /dev/null and b/x64/Debug/RRProcessor.obj differ diff --git a/x64/Debug/SJFProcessor.obj b/x64/Debug/SJFProcessor.obj new file mode 100644 index 0000000..947ffa9 Binary files /dev/null and b/x64/Debug/SJFProcessor.obj differ diff --git a/x64/Debug/Scheduler.obj b/x64/Debug/Scheduler.obj new file mode 100644 index 0000000..5722a97 Binary files /dev/null and b/x64/Debug/Scheduler.obj differ diff --git a/x64/Debug/Source.obj b/x64/Debug/Source.obj new file mode 100644 index 0000000..f70654c Binary files /dev/null and b/x64/Debug/Source.obj differ diff --git a/x64/Debug/UI.obj b/x64/Debug/UI.obj new file mode 100644 index 0000000..64445b4 Binary files /dev/null and b/x64/Debug/UI.obj differ diff --git a/x64/Debug/vc143.idb b/x64/Debug/vc143.idb new file mode 100644 index 0000000..32e157d Binary files /dev/null and b/x64/Debug/vc143.idb differ diff --git a/x64/Debug/vc143.pdb b/x64/Debug/vc143.pdb new file mode 100644 index 0000000..3ff9fac Binary files /dev/null and b/x64/Debug/vc143.pdb differ