From 0ed2f796d7723af8321f35d4ef5e6781ea41e36d Mon Sep 17 00:00:00 2001 From: dzwdz Date: Thu, 18 Aug 2022 14:11:31 +0200 Subject: syscall/fork: FORK_SHAREMEM for primitive "threads" --- src/kernel/proc.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'src/kernel/proc.c') diff --git a/src/kernel/proc.c b/src/kernel/proc.c index 2e765c7..906fc29 100644 --- a/src/kernel/proc.c +++ b/src/kernel/proc.c @@ -50,7 +50,17 @@ struct process *process_fork(struct process *parent, int flags) { struct process *child = kmalloc(sizeof *child); memset(child, 0, sizeof *child); - child->pages = pagedir_copy(parent->pages); + if (flags & FORK_SHAREMEM) { + if (!parent->pages_refcount) { + parent->pages_refcount = kmalloc(sizeof *parent->pages_refcount); + *parent->pages_refcount = 1; + } + *parent->pages_refcount += 1; + child->pages_refcount = parent->pages_refcount; + child->pages = parent->pages; + } else { + child->pages = pagedir_copy(parent->pages); + } child->regs = parent->regs; child->state = parent->state; assert(child->state == PS_RUNNING); // not copying the state union @@ -152,7 +162,16 @@ void process_kill(struct process *p, int ret) { p->execbuf.buf = NULL; } - pagedir_free(p->pages); + if (p->pages_refcount) { + assert(*p->pages_refcount != 0); + *p->pages_refcount -= 1; + if (*p->pages_refcount == 0) { + kfree(p->pages_refcount); + pagedir_free(p->pages); + } + } else { + pagedir_free(p->pages); + } // TODO VULN unbounded recursion struct process *c2; -- cgit v1.2.3