Hello, again!
I have problem with implementing child waiting, after i run program twice, first run works but state of parent are always are "running", but second process after exit, throws to me the #PF, what is reason?
Here how i implement the process waiting:
Code:
if (runningTask->pid == 0 || pid < 0) return;
if (!process_getProcess(pid)) {
printf("process_wait: no such process %d, system halted\n",pid);
return;
}
struct process *p = process_getProcess(pid);
p->state = 2;
// update pointer
process_yield();
process_exit:
Code:
void process_exit(int pid,int exitCode) {
process_getProcess(pid)->state = 1;
}
process_getProcess:
Code:
struct process *process_getProcess(int pid) {
return process_findID(pid);
}
process_findID:
Code:
struct process *process_findID(int id) {
clist_head_t *c = clist_find(task_list,process_findIdDispatcher,id);
if (c) {
return (struct process *)c->data;
}
return NULL;
}
And finally, the schedule function:
Code:
// send end of interrupt to interrupt controller
io_writePort(PIC_SLAVE_COMMAND , 0x20);
io_writePort(PIC_MASTER_COMMAND , 0x20);
struct process *next_task = NULL;
// update clocks
clocks++;
// don't save context on first switch
if (!fswitch || runningTask->state == PROCESS_RUNNING) {
runningTask->esp = stack;
}
if (!fswitch) {
if (!runningTask) {
next_task = process_findByStatus(PROCESS_RUNNING);
} else {
next_task = process_findNextByStatus(PROCESS_RUNNING,runningTask->lAddr);
}
}
if (next_task == NULL) {
// first switch
next_task = process_findByStatus(PROCESS_RUNNING);
}
if (next_task == NULL) {
printf("Next task null\n");
return stack;
}
if (runningTask->state == PROCESS_KILLING) {
if (runningTask->parent != 0) { // idle task hasn't waiting for any pid
struct process *parent = process_findID(runningTask->parent);
if (parent->state != PROCESS_WAIPID) {
printf("Cannot unlock parent: Parent didn't waiting for any child PID: %d\n",runningTask->pid);
} else {
parent->state = PROCESS_RUNNING;
fswitch = true;
return idle->esp;
}
curTasks--;
arch_destroyStack(runningTask->esp);
pmml_free(runningTask->esp);
pmml_free((void *)runningTask->dir);
if (runningTask->kernelESP != 0) {
pmml_free((void *)runningTask->kernelESP);
}
if (runningTask->page_start != 0) {
pmml_freePages((void *)runningTask->page_start,runningTask->pages);
}
clist_delete_entry(task_list,(clist_head_t *)runningTask->lAddr);
pmml_free(runningTask);
}
}
if (next_task->wait_time != 0) {
next_task->wait_time--;
fswitch = true;
return stack;
}
tss_set_stack(0x10,next_task->kernelESP);
vmm_switch((int *)next_task->dir);
fswitch = false;
runningTask = next_task;
return runningTask->esp;
}
Screenshot of error in attachments.
EDIT: I fixed #PF, but now i have #GP with error code 1, i just replace process_yield code to waiting for next interrupt, also i fixed the "ready" state bug
UPDATE 2: I fixed the #GP, but now tasks always points to idle, maybe problem in context saving in process_schedule?