X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/98fd6cb58b5bbc55cd9ceb45bd27b45a3b222df7..2bcb7a074fa1b63e5954092bdcb8752915d3e9e6:/lunaix-os/kernel/process/sched.c diff --git a/lunaix-os/kernel/process/sched.c b/lunaix-os/kernel/process/sched.c index 4ed9d34..5789927 100644 --- a/lunaix-os/kernel/process/sched.c +++ b/lunaix-os/kernel/process/sched.c @@ -6,7 +6,7 @@ #include #include -#include +#include #include #include #include @@ -18,6 +18,8 @@ #include #include +#include + volatile struct proc_info* __current; static struct proc_info dummy_proc; @@ -64,7 +66,7 @@ sched_init_dummy() *execp = (struct exec_param){ .cs = KCODE_SEG, .eflags = cpu_reflags() | 0x0200, - .eip = (void*)my_dummy, + .eip = (ptr_t)my_dummy, .ss = KDATA_SEG, }; @@ -109,14 +111,24 @@ run(struct proc_info* proc) int can_schedule(struct proc_info* proc) { - if (__SIGTEST(proc->sig_pending, _SIGCONT)) { - __SIGCLEAR(proc->sig_pending, _SIGSTOP); - } else if (__SIGTEST(proc->sig_pending, _SIGSTOP)) { + if (!proc) { + return 0; + } + + struct sighail* sh = &proc->sigctx; + + if ((proc->state & PS_PAUSED)) { + return !!(sh->sig_pending & ~1); + } + + if (sigset_test(sh->sig_pending, _SIGCONT)) { + sigset_clear(sh->sig_pending, _SIGSTOP); + } else if (sigset_test(sh->sig_pending, _SIGSTOP)) { // 如果进程受到SIGSTOP,则该进程不给予调度。 return 0; } - return 1; + return (proc->state == PS_READY); } void @@ -141,7 +153,7 @@ check_sleepers() if (atime && now >= atime) { pos->sleep.alarm_time = 0; - __SIGSET(pos->sig_pending, _SIGALRM); + proc_setsignal(pos, _SIGALRM); } if (!wtime && !atime) { @@ -163,6 +175,7 @@ schedule() struct proc_info* next; int prev_ptr = sched_ctx.procs_index; int ptr = prev_ptr; + int found = 0; if (!(__current->state & ~PS_RUNNING)) { __current->state = PS_READY; @@ -171,24 +184,19 @@ schedule() check_sleepers(); // round-robin scheduler -redo: do { ptr = (ptr + 1) % sched_ctx.ptable_len; next = sched_ctx._procs[ptr]; - } while (!next || (next->state != PS_READY && ptr != prev_ptr)); - sched_ctx.procs_index = ptr; - - if (next->state != PS_READY) { - // schedule the dummy process if we're out of choice - next = &dummy_proc; - goto done; - } + if (!(found = can_schedule(next))) { + if (ptr == prev_ptr) { + next = &dummy_proc; + goto done; + } + } + } while (!found); - if (!can_schedule(next)) { - // 如果该进程不给予调度,则尝试重新选择 - goto redo; - } + sched_ctx.procs_index = ptr; done: run(next); @@ -213,12 +221,17 @@ __DEFINE_LXSYSCALL1(unsigned int, sleep, unsigned int, seconds) struct proc_info* root_proc = sched_ctx._procs[0]; __current->sleep.wakeup_time = clock_systime() + seconds * 1000; - llist_append(&root_proc->sleep.sleepers, &__current->sleep.sleepers); + + if (llist_empty(&__current->sleep.sleepers)) { + llist_append(&root_proc->sleep.sleepers, &__current->sleep.sleepers); + } __current->intr_ctx.registers.eax = seconds; block_current(); schedule(); + + return 0; } __DEFINE_LXSYSCALL1(unsigned int, alarm, unsigned int, seconds) @@ -298,7 +311,6 @@ repeat: goto repeat; done: - status_flags |= PEXITSIG * (proc->sig_inprogress != 0); if (status) { *status = proc->exit_code | status_flags; } @@ -324,13 +336,14 @@ alloc_process() proc->state = PS_CREATED; proc->pid = i; + proc->mm.pid = i; proc->created = clock_systime(); proc->pgid = proc->pid; proc->fdtable = vzalloc(sizeof(struct v_fdtable)); proc->fxstate = vzalloc_dma(512); // FXSAVE需要十六位对齐地址,使用DMA块(128位对齐) - llist_init_head(&proc->mm.regions.head); + llist_init_head(&proc->mm.regions); llist_init_head(&proc->tasks); llist_init_head(&proc->children); llist_init_head(&proc->grp_member); @@ -365,7 +378,7 @@ commit_process(struct proc_info* process) // from extern void -__del_pagetable(pid_t pid, uintptr_t mount_point); +__del_pagetable(pid_t pid, ptr_t mount_point); pid_t destroy_process(pid_t pid) @@ -373,8 +386,9 @@ destroy_process(pid_t pid) int index = pid; if (index <= 0 || index > sched_ctx.ptable_len) { __current->k_status = EINVAL; - return; + return -1; } + struct proc_info* proc = sched_ctx._procs[index]; sched_ctx._procs[index] = 0; @@ -400,17 +414,18 @@ destroy_process(pid_t pid) vfree(proc->fdtable); vfree_dma(proc->fxstate); + vmm_mount_pd(VMS_MOUNT_1, proc->page_table); + struct mm_region *pos, *n; - llist_for_each(pos, n, &proc->mm.regions.head, head) + llist_for_each(pos, n, &proc->mm.regions, head) { - vfree(pos); + mem_sync_pages(VMS_MOUNT_1, pos, pos->start, pos->end - pos->start, 0); + region_release(pos); } - vmm_mount_pd(PD_MOUNT_1, proc->page_table); - - __del_pagetable(pid, PD_MOUNT_1); + __del_pagetable(pid, VMS_MOUNT_1); - vmm_unmount_pd(PD_MOUNT_1); + vmm_unmount_pd(VMS_MOUNT_1); cake_release(proc_pile, proc); @@ -423,7 +438,7 @@ terminate_proc(int exit_code) __current->state = PS_TERMNAT; __current->exit_code = exit_code; - __SIGSET(__current->parent->sig_pending, _SIGCHLD); + proc_setsignal(__current->parent, _SIGCHLD); } struct proc_info*