80 lines
2.2 KiB
C
80 lines
2.2 KiB
C
|
|
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
proc.c
|
|
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
Forrest Yu, 2005
|
|
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
|
|
|
#include "type.h"
|
|
#include "const.h"
|
|
#include "protect.h"
|
|
#include "proto.h"
|
|
#include "string.h"
|
|
#include "global.h"
|
|
|
|
/*======================================================================*
|
|
schedule TODO 助教的版本
|
|
*======================================================================*/
|
|
PUBLIC void schedule()
|
|
{
|
|
PROCESS* p; // 修改此结构体
|
|
int current_tick = get_ticks();
|
|
while (1) {
|
|
p_proc_ready++;
|
|
if (p_proc_ready >= proc_table + NR_TASKS) {
|
|
p_proc_ready = proc_table;
|
|
}
|
|
if (p_proc_ready->waiting_semaphore == 0 &&
|
|
p_proc_ready->wake <= current_tick) {
|
|
break; // 寻找到进程
|
|
}
|
|
// if (p_proc_ready->wake_tick <= current_tick) {
|
|
// break; // 寻找到进程
|
|
// }
|
|
}
|
|
}
|
|
|
|
/*======================================================================*
|
|
sys_get_ticks
|
|
*======================================================================*/
|
|
PUBLIC int sys_get_ticks()
|
|
{
|
|
return ticks;
|
|
}
|
|
|
|
/*======================================================================*
|
|
TODO sys_ms_delay_proc
|
|
*======================================================================*/
|
|
PUBLIC void sys_ms_delay_proc(int milli_seconds)
|
|
{
|
|
p_proc_ready->wake = get_ticks() + (milli_seconds*(1000/HZ));
|
|
schedule();
|
|
}
|
|
|
|
|
|
/*======================================================================*
|
|
TODO sys_p_impl sys_v_impl
|
|
*======================================================================*/
|
|
|
|
PUBLIC void sys_p_impl(SEMAPHORE *semaphore)
|
|
{
|
|
semaphore->value--;
|
|
if(semaphore->value<0){
|
|
p_proc_ready->waiting_semaphore = semaphore;
|
|
semaphore->queue[semaphore->e] = p_proc_ready;
|
|
semaphore->e++;
|
|
semaphore->e%=1000;
|
|
schedule();
|
|
}
|
|
}
|
|
|
|
|
|
PUBLIC void sys_v_impl(SEMAPHORE *semaphore)
|
|
{
|
|
semaphore->value++;
|
|
if(semaphore->value<=0){
|
|
semaphore->queue[semaphore->s]->waiting_semaphore = 0;
|
|
semaphore->s++;
|
|
semaphore->s%=1000;
|
|
}
|
|
} |