Files
42_KFS/src/multitasking/process.c

53 lines
1.0 KiB
C

#include "process.h"
#include "alloc.h"
#include "interrupts.h"
#include "memory.h"
#include "thread.h"
#include "string.h"
struct pcb *create_process(uint8_t uid)
{
static uint32_t pid = 1;
struct pcb *new_pcb = vmalloc(sizeof(struct pcb));
if (!new_pcb)
return NULL;
new_pcb->uid = uid;
new_pcb->pid = pid++;
new_pcb->heap = alloc_pages(4096, &new_pcb->cr3);
if (!new_pcb->heap) {
vfree(new_pcb);
return NULL;
}
memcpy(new_pcb->heap, current_pd, 4096);
new_pcb->next = new_pcb;
new_pcb->prev = new_pcb;
if (current_pcb) {
new_pcb->next = current_pcb->next;
new_pcb->prev = current_pcb;
current_pcb->next = new_pcb;
if (current_pcb->prev == current_pcb)
current_pcb->prev = new_pcb;
} else {
current_pcb = new_pcb;
}
new_pcb->signals.pending = SIG_IGN;
new_pcb->thread_list = NULL;
return new_pcb;
}
void remove_pcb(struct pcb *pcb)
{
struct pcb *left = pcb->prev;
struct pcb *right = pcb->next;
if (pcb->heap)
free_pages(pcb->heap, 4096);
left->next = right;
right->prev = left;
vfree(pcb);
}