/* scheduler.c */ #include "asm.h" #include "message.h" #include "process.h" #include "scheduler.h" #include "string.h" #include "tty3.h" #include "user.h" int get_running() { int i; for (i = 0; i < PROCESSES; i++) { if (PCB_list[i].status == RUNNING) { return i; } } return -1; } void hold(int pid) { int id = pid - 1; int check_value = PCB_list[id].toggle; while(PCB_list[id].toggle == check_value) ; } void revive(int pid) { int id = pid - 1; if (receive_message() && PCB_list[id].current_message.priority == CRASH_PRIO) { int crashed_pid = string_to_int(PCB_list[id].current_message.data); int crashed_id = crashed_pid - 1; char program[MAX_STR_LEN]; char argv[2][MAX_STR_LEN]; string_memcpy(program, PCB_list[crashed_id].name); string_memcpy(argv[0], PCB_list[crashed_id].arg_1); string_memcpy(argv[1], PCB_list[crashed_id].arg_2); terminate(crashed_pid); int new_pid = new_p(program, argv); PCB_list[new_pid - 1].supervisor_pid = pid; print_string("The program ["); print_string(program); print_string("] was revived.\n"); } } void check_crashes() { int i; for (i = 0; i < PROCESSES; i++) { if (PCB_list[i].status == CRASHED && PCB_list[i].supervisor_pid) { int pid = i + 1; char mess[MAX_STR_LEN]; int_to_string(mess, pid); send_message(mess, CRASH_PRIO, PCB_list[i].supervisor_pid); PCB_list[i].supervisor_pid = -1; } } } void do_delay() { int i; for (i = 0; i < PROCESSES; i++) { if (PCB_list[i].status == DELAYED) { PCB_list[i].delay_time--; if (PCB_list[i].delay_time == 0) { PCB_list[i].status = WAITING; } } } } void increase_wait() { int i; for (i = 0; i < PROCESSES; i++) { if (PCB_list[i].status == WAITING) { PCB_list[i].wait_time++; } } } int get_highest_process() { int highest_priority = 0; int highest_process = -1; int i; for (i = 0; i < PROCESSES; i++) { if (PCB_list[i].status == WAITING) { if (PCB_list[i].priority > highest_priority) { highest_priority = PCB_list[i].priority; highest_process = i; } else if (PCB_list[i].priority == highest_priority && PCB_list[i].wait_time >= PCB_list[highest_process].wait_time) { highest_process = i; } } } return highest_process; } void select_process() { int running = get_running(); if (running != -1) { PCB_list[running].status = WAITING; } int new_running = get_highest_process(); if (new_running != -1) { PCB_list[new_running].status = RUNNING; PCB_list[new_running].wait_time = 0; } }