Max OS 0.3
Loading...
Searching...
No Matches
MaxOS::processes::Process Class Reference

A process that can be scheduled by the Scheduler, wraps & manages threads as well as its own address space and resources. More...

#include <process.h>

Public Member Functions

 Process (const string &name, bool is_kernel=false)
 Base Constructor for the Process.
 
 Process (const string &name, void(*_entry_point)(void *), void *args, int arg_amount, bool is_kernel=false)
 Constructor for the Process class (from a function)
 
 Process (const string &name, void *args, int arg_amount, ELF64 *elf, bool is_kernel=false)
 Constructor for the Process class (from an elf, will free the elf memory after loading)
 
 ~Process ()
 Destructor for the Process class.
 
common::Vector< Thread * > threads ()
 Gets the threads of the process.
 
void add_thread (Thread *thread)
 Adds a thread to the process.
 
void remove_thread (uint64_t tid)
 Remove a thread by its tid (NOTE: this will not remove it from the scheduler)
 
void set_pid (uint64_t pid)
 Sets the pid of the process once added to the queue.
 
uint64_t pid () const
 Gets the pid of the process.
 
uint64_t total_ticks ()
 Gets the total ticks of the threads in the process (not this does not include any past killed threads)
 

Public Attributes

bool is_kernel
 Whether this process is a kernel process.
 
string name
 The name of the process.
 
string working_directory = "/"
 The working directory of the process.
 
memory::MemoryManagermemory_manager = nullptr
 The manager for memory used by this process.
 
ResourceManager resource_manager
 The manger for resources used by this process.
 

Detailed Description

A process that can be scheduled by the Scheduler, wraps & manages threads as well as its own address space and resources.

Definition at line 85 of file process.h.

Constructor & Destructor Documentation

◆ Process() [1/3]

Process::Process ( const string p_name,
bool  is_kernel = false 
)
explicit

Base Constructor for the Process.

Parameters
p_nameThe name of the process
is_kernelIf the process is a kernel process

Definition at line 181 of file process.cpp.

183 name(p_name)
184{
185 // If it is a kernel process then don't need a new memory manager
187}
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
Handles memory allocation and deallocation.
static MemoryManager * s_kernel_memory_manager
The memory manager for any kernel processes and all kernel allocations.
string name
The name of the process.
Definition process.h:109
bool is_kernel
Whether this process is a kernel process.
Definition process.h:107
memory::MemoryManager * memory_manager
The manager for memory used by this process.
Definition process.h:112

References is_kernel, memory_manager, and MaxOS::memory::MemoryManager::s_kernel_memory_manager.

◆ Process() [2/3]

Process::Process ( const string p_name,
void(*)(void *)  _entry_point,
void args,
int  arg_amount,
bool  is_kernel = false 
)

Constructor for the Process class (from a function)

Parameters
p_nameThe name of the process
_entry_pointThe entry point of the process
argsThe arguments to pass to the process
arg_amountThe amount of arguments
is_kernelIf the process is a kernel process

Definition at line 198 of file process.cpp.

200{
201
202 // Create the main thread
203 auto* main_thread = new Thread(_entry_point, args, arg_amount, this);
204
205 // Add the thread
207}
A process that can be scheduled by the Scheduler, wraps & manages threads as well as its own address ...
Definition process.h:85
void add_thread(Thread *thread)
Adds a thread to the process.
Definition process.cpp:256
The execution context of a sub-process thread.
Definition process.h:49

References add_thread().

◆ Process() [3/3]

Process::Process ( const string p_name,
void args,
int  arg_amount,
ELF64 elf,
bool  is_kernel = false 
)

Constructor for the Process class (from an elf, will free the elf memory after loading)

Parameters
p_nameThe name of the process
argsThe arguments to pass to the process
arg_amountThe amount of arguments
elfThe elf file to load the process from
is_kernelIf the process is a kernel process

Definition at line 218 of file process.cpp.

220{
221
222 // Get the entry point
223 elf->load();
224 auto* entry_point = (void (*)(void*)) elf->header()->entry;
225
226 // Create the main thread
227 auto* main_thread = new Thread(entry_point, args, arg_amount, this);
229}

References add_thread().

◆ ~Process()

Process::~Process ( )

Destructor for the Process class.

Definition at line 235 of file process.cpp.

235 {
236
238
239 // Free the threads
240 for (auto thread: m_threads)
242
243 // Free the memory manager (only if it was created)
244 if (!is_kernel)
245 delete memory_manager;
246
247 // Log the cleanup
248 Logger::DEBUG() << "Process " << name.c_str() << " cleaned up, memory before: " << pages << " bytes, after cleanup: " << PhysicalMemoryManager::s_current_manager->memory_used() << " bytes\n";
249}
static Logger DEBUG()
Gets active logger set to DEBUG level.
Definition logger.cpp:193
static PhysicalMemoryManager * s_current_manager
The current physical memory manager in use.
Definition physical.h:185

References MaxOS::Logger::DEBUG(), is_kernel, memory_manager, name, and MaxOS::memory::PhysicalMemoryManager::s_current_manager.

Member Function Documentation

◆ add_thread()

void Process::add_thread ( Thread thread)

Adds a thread to the process.

Parameters
threadThe thread to add

Definition at line 256 of file process.cpp.

256 {
257
258 m_lock.lock();
259
260 // Store the thread
261 m_threads.push_back(thread);
262 thread->parent_pid = m_pid;
263
264 m_lock.unlock();
265}
void lock()
Lock the spinlock once it is available.
Definition spinlock.cpp:24
void unlock()
Unlock the spinlock.
Definition spinlock.cpp:32

References MaxOS::common::Spinlock::lock(), and MaxOS::common::Spinlock::unlock().

Referenced by Process(), and Process().

◆ pid()

uint64_t Process::pid ( ) const

Gets the pid of the process.

Returns
The pid of the process

Definition at line 326 of file process.cpp.

326 {
327
328 return m_pid;
329}

Referenced by MaxOS::processes::Scheduler::current_process(), MaxOS::processes::GlobalScheduler::force_remove_process(), MaxOS::processes::Scheduler::get_process(), MaxOS::processes::GlobalScheduler::remove_process(), and set_pid().

◆ remove_thread()

void Process::remove_thread ( uint64_t  tid)

Remove a thread by its tid (NOTE: this will not remove it from the scheduler)

Parameters
tid

Definition at line 272 of file process.cpp.

272 {
273
274 // Find the thread
275 for (uint32_t i = 0; i < m_threads.size(); i++) {
276
277 // Thread is not what is being removed
278 if (m_threads[i]->tid != tid)
279 continue;
280
281 // Delete the thread
282 Thread* thread = m_threads[i];
283 delete thread;
284
285 // Remove the thread from the list
286 m_threads.erase(m_threads.begin() + i);
287 return;
288 }
289}

Referenced by MaxOS::processes::Scheduler::force_remove_process(), and MaxOS::processes::Scheduler::schedule_next().

◆ set_pid()

void Process::set_pid ( uint64_t  pid)

Sets the pid of the process once added to the queue.

Parameters
pid

Definition at line 296 of file process.cpp.

296 {
297
298 // Check if the pid is already set
299 if (m_pid != 0)
300 return;
301
302 // Set the pid
303 m_pid = pid;
304
305 // Assign the pid to the threads
306 for (auto thread: m_threads)
307 thread->parent_pid = pid;
308
309}
uint64_t pid() const
Gets the pid of the process.
Definition process.cpp:326

References pid().

Referenced by MaxOS::processes::Scheduler::add_process().

◆ threads()

Vector< Thread * > Process::threads ( )

Gets the threads of the process.

Returns
The threads of the process

Definition at line 316 of file process.cpp.

316 {
317
318 return m_threads;
319}

Referenced by MaxOS::processes::Scheduler::add_process(), MaxOS::processes::Scheduler::force_remove_process(), MaxOS::processes::Scheduler::remove_process(), and MaxOS::processes::Scheduler::schedule_next().

◆ total_ticks()

uint64_t Process::total_ticks ( )

Gets the total ticks of the threads in the process (not this does not include any past killed threads)

Returns
The total ticks of the threads in the process

Definition at line 336 of file process.cpp.

336 {
337
339 for (auto thread: m_threads)
340 total_ticks += thread->ticks;
341
342 return total_ticks;
343}
uint64_t total_ticks()
Gets the total ticks of the threads in the process (not this does not include any past killed threads...
Definition process.cpp:336

References total_ticks().

Referenced by total_ticks().

Member Data Documentation

◆ is_kernel

bool MaxOS::processes::Process::is_kernel

Whether this process is a kernel process.

Definition at line 107 of file process.h.

Referenced by Process(), and ~Process().

◆ memory_manager

memory::MemoryManager* MaxOS::processes::Process::memory_manager = nullptr

The manager for memory used by this process.

Definition at line 112 of file process.h.

Referenced by Process(), and ~Process().

◆ name

string MaxOS::processes::Process::name

The name of the process.

Definition at line 109 of file process.h.

Referenced by MaxOS::processes::GlobalScheduler::add_process(), and ~Process().

◆ resource_manager

ResourceManager MaxOS::processes::Process::resource_manager

The manger for resources used by this process.

Definition at line 113 of file process.h.

◆ working_directory

string MaxOS::processes::Process::working_directory = "/"

The working directory of the process.

Definition at line 110 of file process.h.


The documentation for this class was generated from the following files: