yet another james molloy kernel tutorial question
Posted: Thu Aug 13, 2020 2:46 am
I am just wondering, in his tutorial, the fork() code seems to have created the kernel stack for the
wrong task, it is new task should receive that kernel stack, not the current_task... can someone please
help me on this? I high lighted the code below...
wrong task, it is new task should receive that kernel stack, not the current_task... can someone please
help me on this? I high lighted the code below...
Code: Select all
int fork()
{
// We are modifying kernel structures, and so cannot be interrupted.
asm volatile("cli");
// Take a pointer to this process' task struct for later reference.
task_t *parent_task = (task_t*)current_task;
// Clone the address space.
page_directory_t *directory = clone_directory(current_directory);
// Create a new process.
task_t *new_task = (task_t*)kmalloc(sizeof(task_t));
new_task->id = next_pid++;
new_task->esp = new_task->ebp = 0;
new_task->eip = 0;
new_task->page_directory = directory;
[b] current_task->kernel_stack = kmalloc_a(KERNEL_STACK_SIZE);[/b]
new_task->next = 0;
// Add it to the end of the ready queue.
// Find the end of the ready queue...
task_t *tmp_task = (task_t*)ready_queue;
while (tmp_task->next)
tmp_task = tmp_task->next;
// ...And extend it.
tmp_task->next = new_task;
// This will be the entry point for the new process.
u32int eip = read_eip();
// We could be the parent or the child here - check.
if (current_task == parent_task)
{
// We are the parent, so set up the esp/ebp/eip for our child.
u32int esp; asm volatile("mov %%esp, %0" : "=r"(esp));
u32int ebp; asm volatile("mov %%ebp, %0" : "=r"(ebp));
new_task->esp = esp;
new_task->ebp = ebp;
new_task->eip = eip;
// All finished: Reenable interrupts.
asm volatile("sti");
// And by convention return the PID of the child.
return new_task->id;
}
else
{
// We are the child - by convention return 0.
return 0;
}
}