From e110594ab26b2639f488af08f2f504cbbeaaa040 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Wed, 2 Nov 2011 15:41:35 +0100 Subject: [PATCH] Processes are listed in a global array sorted after pid. --- sortix/process.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ sortix/process.h | 7 +++++++ 2 files changed, 51 insertions(+) diff --git a/sortix/process.cpp b/sortix/process.cpp index de9e4bb4..a08fd6d3 100644 --- a/sortix/process.cpp +++ b/sortix/process.cpp @@ -25,6 +25,7 @@ #include "platform.h" #include #include +#include #include "thread.h" #include "process.h" #include "memorymanagement.h" @@ -94,10 +95,13 @@ namespace Sortix firstthread = NULL; mmapfrom = 0x80000000UL; pid = AllocatePID(); + Put(this); } Process::~Process() { + Remove(this); + ResetAddressSpace(); // Avoid memory leaks. @@ -304,6 +308,43 @@ namespace Sortix return nextpidtoallocate++; } + int ProcessCompare(Process* a, Process* b) + { + if ( a->pid < b->pid ) { return -1; } + if ( a->pid > b->pid ) { return 1; } + return 0; + } + + int ProcessPIDCompare(Process* a, pid_t pid) + { + if ( a->pid < pid ) { return -1; } + if ( a->pid > pid ) { return 1; } + return 0; + } + + SortedList* pidlist; + + Process* Process::Get(pid_t pid) + { + size_t index = pidlist->Search(ProcessPIDCompare, pid); + if ( index == SIZE_MAX ) { return NULL; } + + return pidlist->Get(index); + } + + bool Process::Put(Process* process) + { + return pidlist->Add(process); + } + + void Process::Remove(Process* process) + { + size_t index = pidlist->Search(process); + ASSERT(index != SIZE_MAX); + + pidlist->Remove(index); + } + void Process::Init() { Syscall::Register(SYSCALL_EXEC, (void*) SysExecute); @@ -312,6 +353,9 @@ namespace Sortix Syscall::Register(SYSCALL_GETPPID, (void*) SysGetParentPID); nextpidtoallocate = 0; + + pidlist = new SortedList(ProcessCompare); + if ( !pidlist ) { Panic("could not allocate pidlist\n"); } } addr_t Process::AllocVirtualAddr(size_t size) diff --git a/sortix/process.h b/sortix/process.h index 97dd2209..c361b8a0 100644 --- a/sortix/process.h +++ b/sortix/process.h @@ -111,6 +111,13 @@ namespace Sortix public: addr_t AllocVirtualAddr(size_t size); + public: + static Process* Get(pid_t pid); + + private: + static bool Put(Process* process); + static void Remove(Process* process); + }; Process* CurrentProcess();