Files
42_KFS/src/multitasking/process.c

61 lines
1.3 KiB
C

#include "process.h"
#include "alloc.h"
#include "interrupts.h"
#include "memory.h"
#include "thread.h"
#include "string.h"
#include <stdint.h>
struct pcb *create_process(uid_t uid)
{
static pid_t pid = 1;
struct pcb *new_pcb = kmalloc(sizeof(struct pcb));
if (!new_pcb)
return NULL;
new_pcb->uid = uid;
new_pcb->pid = pid++;
new_pcb->tid = 1;
new_pcb->heap = kalloc_pages(4096);
if (!new_pcb->heap) {
kfree(new_pcb);
return NULL;
}
new_pcb->cr3 =
(void *)((uint32_t)VA2PTE((uint32_t)new_pcb->heap) & PAGE_MASK);
memcpy(new_pcb->heap, PD,
4096); // TODO optimize to copy only used bytes
new_pcb->heap[1023] = (uint32_t)new_pcb->cr3 | INIT_FLAGS;
new_pcb->daddy = NULL;
new_pcb->children = NULL;
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_process(struct pcb *pcb)
{
struct pcb *left = pcb->prev;
struct pcb *right = pcb->next;
if (pcb->heap)
kfree_pages(pcb->heap, 4096);
left->next = right;
right->prev = left;
kfree(pcb);
}