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::MemoryManager * memory_manager = nullptr
 The manager for memory used by this process.
 
resources::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 94 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 280 of file process.cpp.

282 name(p_name)
283{
284 // If it is a kernel process then don't need a new memory manager
286}
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:118
bool is_kernel
Whether this process is a kernel process.
Definition process.h:116
memory::MemoryManager * memory_manager
The manager for memory used by this process.
Definition process.h:121

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 297 of file process.cpp.

298: Process(p_name, is_kernel)
299{
300
301 // Create the main thread
302 auto* main_thread = new Thread(_entry_point, args, arg_amount, this);
303
304 // Add the thread
305 add_thread(main_thread);
306}
A process that can be scheduled by the Scheduler, wraps & manages threads as well as its own address ...
Definition process.h:94
void add_thread(Thread *thread)
Adds a thread to the process.
Definition process.cpp:359
The execution context of a sub-process thread.
Definition process.h:50

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
Warning
This will load the constructed processes memory manager, if execution isn't passed to
Todo:
The elf class should be a subclass of Process

Definition at line 321 of file process.cpp.

322: Process(p_name, is_kernel)
323{
324
325 // Get the entry point
327 auto* entry_point = (void (*)(void*)) elf->header()->entry;
328
329 // Create the main thread
330 auto* main_thread = new Thread(entry_point, args, arg_amount, this);
331 add_thread(main_thread);
332}
elf_64_header_t * header() const
Gets the header of the elf file.
Definition elf.cpp:54
void load_into_memory(memory::VirtualMemoryManager *vmm) const
Loads the elf program into memory if a valid elf file.
Definition elf.cpp:42
uint64_t entry
The starting RIP address where the program begins execution.
Definition elf.h:116

References add_thread(), MaxOS::processes::ELFHeader64::entry, MaxOS::processes::ELF64::header(), MaxOS::processes::ELF64::load_into_memory(), memory_manager, and MaxOS::memory::BumpMemoryManager::vmm().

◆ ~Process()

Process::~Process ( )

Destructor for the Process class.

Definition at line 338 of file process.cpp.

338 {
339
341
342 // Free the threads
343 for (auto thread: m_threads)
344 delete thread;
345
346 // Free the memory manager (only if it was created)
347 if (!is_kernel)
348 delete memory_manager;
349
350 // Log the cleanup
351 Logger::DEBUG() << "Process " << name.c_str() << " cleaned up, memory before: " << (int)pages << " bytes, after cleanup: " << (int)PhysicalMemoryManager::s_current_manager->memory_used() << " bytes\n";
352}
static LogRecord DEBUG()
Gets the active logger for debug logs.
Definition logger.cpp:204
static PhysicalMemoryManager * s_current_manager
The current physical memory manager in use.
Definition physical.h:183
uint64_t memory_used() const
Gets the memory currently used.
Definition physical.cpp:753

References MaxOS::Logger::DEBUG(), is_kernel, memory_manager, MaxOS::memory::PhysicalMemoryManager::memory_used(), 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 359 of file process.cpp.

359 {
360
361 m_lock.lock();
362
363 // Store the thread
364 m_threads.push_back(thread);
365 thread->parent_pid = m_pid;
366
367 m_lock.unlock();
368}
uint64_t parent_pid
The parent process ID.
Definition process.h:69

References MaxOS::processes::Thread::parent_pid.

Referenced by Process(), and Process().

◆ 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 375 of file process.cpp.

375 {
376
377 // Find the thread
378 for (uint32_t i = 0; i < m_threads.size(); i++) {
379
380 // Thread is not what is being removed
381 if (m_threads[i]->tid != tid)
382 continue;
383
384 // Delete the thread
385 Thread* thread = m_threads[i];
386 delete thread;
387
388 // Remove the thread from the list
389 m_threads.erase(m_threads.begin() + i);
390 return;
391 }
392}

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 399 of file process.cpp.

399 {
400
401 // Check if the pid is already set
402 if (m_pid != 0)
403 return;
404
405 // Set the pid
406 m_pid = pid;
407
408 // Assign the pid to the threads
409 for (auto thread: m_threads)
410 thread->parent_pid = pid;
411
412}
uint64_t pid() const
Gets the pid of the process.
Definition process.cpp:429

References pid().

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

◆ threads()

◆ 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 439 of file process.cpp.

439 {
440
441 uint64_t total_ticks = 0;
442 for (auto thread: m_threads)
443 total_ticks += thread->ticks;
444
445 return total_ticks;
446}
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:439

References total_ticks().

Referenced by MaxOS::system::CPU::PANIC(), MaxOS::processes::resources::ProcessResource::read(), and total_ticks().

Member Data Documentation

◆ is_kernel

bool MaxOS::processes::Process::is_kernel

Whether this process is a kernel process.

Definition at line 116 of file process.h.

Referenced by Process(), MaxOS::processes::Scheduler::schedule(), MaxOS::processes::Thread::Thread(), and ~Process().

◆ memory_manager

◆ name

string MaxOS::processes::Process::name

◆ resource_manager

◆ working_directory

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

The working directory of the process.

Definition at line 119 of file process.h.

Referenced by MaxOS::processes::resources::ProcessResource::read(), and MaxOS::processes::resources::ProcessResource::write().


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