feature (proc): implemented basic child processes

This commit is contained in:
antifallobst 2023-03-03 23:51:24 +01:00
parent 7cc053be5c
commit 1ab3a2bd22
5 changed files with 85 additions and 22 deletions

View File

@ -36,16 +36,22 @@ typedef enum {
typedef struct process_T process_T; typedef struct process_T process_T;
struct process_T{ struct process_T{
char name[128]; char name [128];
pid_t id; pid_t id;
void* chunk; void* chunk;
uint32_t chunk_id; uint32_t chunk_id;
process_T* parent; uint32_t num_threads;
void* threads; void* threads;
process_T* parent;
process_T* childs;
process_T* prev;
process_T* next;
}; };
pid_t process_spawn (pid_t parent, string_t name); pid_t process_spawn (pid_t parent, string_t name);
void process_kill (pid_t process); void process_kill_pid (pid_t pid);
void process_kill (process_T* process);
#endif //NOX_PROCESS_H #endif //NOX_PROCESS_H

View File

@ -54,6 +54,7 @@ typedef struct {
void scheduler_init (); void scheduler_init ();
cpu_state_T* scheduler_start (cpu_state_T* state); cpu_state_T* scheduler_start (cpu_state_T* state);
bool scheduler_is_initialized (); bool scheduler_is_initialized ();
void scheduler_dump_info (process_T* process, uint8_t indent);
thread_T* scheduler_register_thread (thread_T* thread); thread_T* scheduler_register_thread (thread_T* thread);
void scheduler_pause_thread (thread_T* thread); void scheduler_pause_thread (thread_T* thread);

View File

@ -110,16 +110,18 @@ void kmain(boot_info_T boot_info) {
elf_init_kernel_exec(&boot_info); elf_init_kernel_exec(&boot_info);
pid_t proc1 = process_spawn(PROCESS_NONE, "test process 1"); pid_t proc1 = process_spawn(PROCESS_KERNEL, "test_1");
thread_T* proc1_thread1 = thread_spawn(proc1, test1);
thread_T* proc1_thread2 = thread_spawn(proc1, test2);
thread_T* proc1_thread3 = thread_spawn(proc1, test3);
thread_start(proc1_thread1); pid_t proc1_1 = process_spawn(proc1, "test_1-1");
thread_start(proc1_thread2); thread_spawn(proc1_1, NULL);
thread_start(proc1_thread3); pid_t proc1_2 = process_spawn(proc1, "test_1-2");
process_kill(proc1); pid_t proc2 = process_spawn(PROCESS_KERNEL, "test_2");
scheduler_dump_info(scheduler_get_process(PROCESS_KERNEL), 0);
process_kill_pid(proc1);
scheduler_dump_info(scheduler_get_process(PROCESS_KERNEL), 0);
// vfs_node_T* node = vfs_resolve_path(&g_root_fs, "/initrd/test.elf"); // vfs_node_T* node = vfs_resolve_path(&g_root_fs, "/initrd/test.elf");
// //

View File

@ -28,8 +28,9 @@
pid_t process_spawn(pid_t parent, string_t name) { pid_t process_spawn(pid_t parent, string_t name) {
process_T* process = memory_allocate(sizeof(process_T)); process_T* process = memory_allocate(sizeof(process_T));
process->threads = NULL; process->num_threads = 0;
process->parent = scheduler_get_process(parent); process->threads = NULL;
process->parent = scheduler_get_process(parent);
memory_copy(name, process->name, MIN(string_length(name), 127)); memory_copy(name, process->name, MIN(string_length(name), 127));
@ -38,10 +39,12 @@ pid_t process_spawn(pid_t parent, string_t name) {
return scheduler_register_process(process); return scheduler_register_process(process);
} }
void process_kill(pid_t process) { void process_kill_pid(pid_t pid) {
process_T* proc = scheduler_get_process(process); process_kill(scheduler_get_process(pid));
}
scheduler_kill_process(proc); void process_kill(process_T* process) {
scheduler_kill_process(process);
memory_free(proc); memory_free(process);
} }

View File

@ -39,6 +39,21 @@ void scheduler_update_info() {
graphics_buffer_draw_string(g_scheduler_info_buffer, 0, 0, g_color_palette[COLOR_PAL_GREEN], buffer); graphics_buffer_draw_string(g_scheduler_info_buffer, 0, 0, g_color_palette[COLOR_PAL_GREEN], buffer);
} }
void scheduler_dump_info(process_T* process, uint8_t indent) {
char buf[indent+1];
memory_set(buf, ' ', indent);
buf[indent] = '\0';
log(LOG_NONE, "%s<%d> %s [%d]", buf, process->id, process->name, process->num_threads);
process_T* child = process->childs;
while (child != NULL) {
scheduler_dump_info(child, indent+2);
child = child->next;
}
}
scheduler_processes_chunk_T* scheduler_processes_chunk_create(scheduler_processes_chunk_T* prev) { scheduler_processes_chunk_T* scheduler_processes_chunk_create(scheduler_processes_chunk_T* prev) {
scheduler_processes_chunk_T* chunk = memory_allocate(sizeof(scheduler_processes_chunk_T)); scheduler_processes_chunk_T* chunk = memory_allocate(sizeof(scheduler_processes_chunk_T));
@ -114,6 +129,7 @@ thread_T* scheduler_register_thread(thread_T* thread) {
thread->process->threads = thread; thread->process->threads = thread;
thread->process->num_threads++;
g_scheduler.num_threads++; g_scheduler.num_threads++;
log(LOG_INFO, "<Scheduler> Registered thread"); log(LOG_INFO, "<Scheduler> Registered thread");
@ -149,6 +165,7 @@ void scheduler_kill_thread(thread_T* thread) {
thread->local_prev->local_next = thread->local_next; thread->local_prev->local_next = thread->local_next;
} }
thread->process->num_threads--;
g_scheduler.num_threads--; g_scheduler.num_threads--;
log(LOG_INFO, "<Scheduler> Killed thread"); log(LOG_INFO, "<Scheduler> Killed thread");
@ -178,6 +195,20 @@ pid_t scheduler_register_process(process_T* process) {
process->chunk = chunk; process->chunk = chunk;
process->chunk_id = i; process->chunk_id = i;
process->id = pid + i; process->id = pid + i;
process->childs = NULL;
process->prev = NULL;
if (process->parent != NULL) {
process->next = process->parent->childs;
if (process->next != NULL) {
process->next->prev = process;
}
process->parent->childs = process;
} else {
process->next = NULL;
}
chunk->num_free_pids--; chunk->num_free_pids--;
g_scheduler.num_processes++; g_scheduler.num_processes++;
@ -199,6 +230,12 @@ void scheduler_kill_process(process_T* process) {
bitmap_set(&chunk->processes_bitmap, process->chunk_id, false); bitmap_set(&chunk->processes_bitmap, process->chunk_id, false);
chunk->processes[process->chunk_id] = NULL; chunk->processes[process->chunk_id] = NULL;
process_T* child = process->childs;
while (child != NULL) {
process_T* next = child->next;
process_kill(child);
child = next;
}
thread_T* thread = process->threads; thread_T* thread = process->threads;
while (thread != NULL) { while (thread != NULL) {
@ -207,6 +244,20 @@ void scheduler_kill_process(process_T* process) {
thread = next; thread = next;
} }
if (process->parent != NULL) {
if (process->parent->childs == process) {
process->parent->childs = process->next;
}
if (process->next != NULL) {
process->next->prev = process->prev;
}
if (process->prev != NULL) {
process->prev->next = process->next;
}
}
chunk->num_free_pids++; chunk->num_free_pids++;
g_scheduler.num_processes--; g_scheduler.num_processes--;