Commit 48ef5529 authored by benkma's avatar benkma

Lab 10 Completed

parent de1f3b4f
......@@ -260,8 +260,55 @@ int proc::syscall_map_console(regstate* regs) {
// Handle fork system call.
int proc::syscall_fork(regstate* regs) {
(void) regs;
return E_NOSYS;
pid_t pid = 0;
proc *child = knew<proc>();
x86_64_pagetable* pt = kalloc_pagetable();
assert(child);
{
spinlock_guard guard(ptable_lock);
for(int i = 1; i < NPROC; ++i) {
if(ptable[i] == nullptr) {
pid = i;
ptable[pid] = child;
break;
}
}
}
assert(pid);
child->init_user(pid, pt);
// Copy user-accessible Memory
for (vmiter parentIterator(this); parentIterator.low(); parentIterator.next()) {
if (parentIterator.user()) {
vmiter childIterator = vmiter(pt, parentIterator.va());
auto range = physical_ranges.find((uintptr_t) parentIterator.pa());
if(range->type() == mem_console) {
childIterator.map(parentIterator.pa(), parentIterator.perm());
} else {
// Allocate a new frame for the child
void *newFrame = kalloc(PAGESIZE);
assert(newFrame);
// Copy the parent's page contents to the child's page
memcpy(newFrame, parentIterator.kptr(), PAGESIZE);
// Map the child's virtual address to the new frame with the proper permission
childIterator.map(ka2pa(newFrame), parentIterator.perm());
}
}
}
// Copy Registers
memcpy(child->regs_, regs, sizeof(regstate));
// Arranging for 0 to be returned to the child process
child->regs_->reg_rax = 0;
// Add to run queue
cpus[pid % ncpu].enqueue(child);
return pid;
}
......
......@@ -15,10 +15,10 @@ void process_main() {
int error_code = sys_map_console(console);
assert(!error_code);
console_printf(0x5000, "Hello Professor Foster,\n");
/*console_printf(0x5000, "Hello Professor Foster,\n");
console_printf(0x5000, "We are in week four of the quarter and I'm learning a lot of\n");
console_printf(0x5000, "C++ programming in OS!\n");
console_printf(0x5000, "--Matija Benko");
console_printf(0x5000, "--Matija Benko"); */
// Fork three new copies. (But ignore failures.)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment