//////////////////////////////////////////////////////////////////////////// // PROCESS.CC // //-The process class contains all the object need to run a single process // in its own address space. // // The process class contain: // -AddressSpace // -Count of threads still alive. // -OpenFile Table // -Join Information // -Page Table Location kept in the address space /////////////////////////////////////////////////////////////////////////// #include "process.h" #include "system.h" #include Lock* processLock = new Lock("processLock"); Lock* Process::joinLock = new Lock("join lock"); Process::processID = 0; Process::Process(OpenFile *executable, bool joinable=false) { processLock->Acquire(); numProcess++; PID=processID; processID++; processMap.add(PID,this); finished=false; argBuffer = new char[256]; threadCount = 1; addrSpace = new AddrSpace(executable); processLock->Release(); for(int i=0; i<10;i++) openFileTable[i]=NULL; } Process::~Process() { ASSERT(finished==true); delete addrSpace; addrSpace=NULL; } int Process::getThreadCount() { return threadCount; } int Process::getPID() { return PID; } void Process::incrementThreadCount() { threadCount = threadCount++; } void Process::decrementThreadCount() { threadCount = threadCount--; } // assumes single-thread processes void Process::Join(Thread* waitingThread) { if(finished) return; else { joinLock->Acquire(); waitingThreads.enqueue(waitingThread); IntStatus oldLevel = interrupt->SetLevel(IntOff); currentThread->Sleep(); (void) interrupt->SetLevel(oldLevel); joinLock->Release(); } } void Process::processFinish() { processLock->Acquire(); finished = true; processMap.remove(PID); numProcess--; if(numProcess==0) interrupt->Halt(); processToBeDestroyed = this; // threads that were waiting for process to end are woken up while(!waitingThreads.empty()) { Thread* waitingThread; waitingThreads.dequeue(waitingThread); scheduler->ReadyToRun(waitingThread); } //printf("We are in processFinish()\n\n"); processLock->Release(); } AddrSpace* Process::getSpace() { return addrSpace; }