wip: multitasking: add status forked

This commit is contained in:
Starnakin 2025-02-03 13:11:38 +01:00
parent 18486a6705
commit 7b7cc86999
5 changed files with 49 additions and 21 deletions

View File

@ -8,7 +8,7 @@
extern struct task *current_task;
enum status { ZOMBIE, THREAD, RUN, WAIT, SLEEP, STOPPED };
enum status { ZOMBIE, THREAD, RUN, WAIT, SLEEP, STOPPED, FORKED };
enum owner { OWNER_KERNEL, OWNER_USER };
#define STACK_SIZE PAGE_SIZE * 4
@ -31,9 +31,13 @@ struct task {
void scheduler(void);
void switch_to_task(struct task *next_task);
void exec_fn(void (*fn)(void));
struct task *create_task(u8 uid);
i8 create_kernel_task(void);
void remove_task(struct task *task);
struct task *copy_task(const struct task *task);
void kfork(struct task *daddy);
// utils
void exec_fn(void (*fn)(void));
u16 fork(void);
u16 wait(void);

View File

@ -43,9 +43,12 @@ static void owo(void)
static void awa(void)
{
if (fork() < 0)
u32 pid = fork();
PRINT_INT(pid);
if (pid < 0)
kprintf("camille il a une grosse bite (18cm)\n");
kprintf("awaille\n");
if (pid)
wait();
}

View File

@ -1,22 +1,27 @@
#include "interrupts.h"
#include "string.h"
#include "kprintf.h"
#include "task.h"
#include "types.h"
u16 fork(void)
{
if (current_task->esp == current_task->esp0 + STACK_SIZE)
current_task->status = FORKED;
scheduler();
cli();
struct task *child = create_task(current_task->uid);
if (!child)
return -1;
child->daddy = current_task;
current_task->child = child;
memcpy(child->esp0, current_task->esp0, STACK_SIZE);
u8 *daddy_esp;
asm("movl %%esp, %0" : "=m"(daddy_esp));
child->esp = child->esp0 + (daddy_esp - current_task->esp0);
toris();
return current_task == child ? 0 : child->pid;
return current_task->child ? current_task->child->pid : 0;
}
void kfork(struct task *daddy)
{
cli();
struct task *child = copy_task(daddy);
if (!child) {
kprintf(KERN_ALERT "Fork failed ! retry at the next loop");
toris();
return;
}
child->daddy = daddy;
daddy->child = child;
daddy->status = RUN;
child->status = RUN;
toris();
}

View File

@ -11,14 +11,18 @@ struct task *current_task;
void scheduler(void)
{
// ZOMBIE, THREAD, RUN, WAIT, SLEEP, STOPPED, FORKED
void (*func[])(struct task *) = {remove_task, NULL, NULL, NULL,
NULL, remove_task, kfork};
if (!current_task) // || current_task->next == current_task)
return;
cli();
struct task *it = current_task->next;
while (it && it->status != RUN) {
if (it->status == STOPPED || it->status == ZOMBIE) {
if (it != current_task && func[it->status]) {
struct task *new_it = it->prev;
remove_task(it);
func[it->status](it);
it = new_it;
}
it = it->next;

View File

@ -5,6 +5,7 @@
#include "kpanic.h"
#include "kprintf.h"
#include "memory.h"
#include "string.h"
#include <stdbool.h>
#include <stdint.h>
@ -81,6 +82,17 @@ void remove_task(struct task *task)
}
}
struct task *copy_task(const struct task *task)
{
struct task *new_task = create_task(task->uid);
if (!new_task)
return NULL;
memcpy(new_task->esp0, task->esp0, STACK_SIZE);
new_task->esp = new_task->esp0 + (task->esp - task->esp0);
new_task->status = task->status;
return new_task;
}
void exit_task(void)
{
cli();