Rountines
user_0531932
c_cpp
7 months ago
5.0 kB
3
Indexable
Never
static void *cpu_routine(void *args) { struct timer_id_t *timer_id = ((struct cpu_args *)args)->timer_id; int id = ((struct cpu_args *)args)->id; /* Check for new process in ready queue */ int time_left = 0; struct pcb_t *proc = NULL; while (1) { /* Check the status of current process */ if (proc == NULL) { /* No process is running, the we load new process from * ready queue */ proc = get_proc(); if (proc == NULL) { next_slot(timer_id); continue; /* First load failed. skip dummy load */ } } else if (proc->pc == proc->code->size) { /* The porcess has finish it job */ printf("\tCPU %d: Processed %2d has finished\n", id, proc->pid); free(proc); proc = get_proc(); time_left = 0; } else if (time_left == 0) { /* The process has done its job in current time slot */ printf("\tCPU %d: Put process %2d to run queue\n", id, proc->pid); put_proc(proc); proc = get_proc(); } /* Recheck process status after loading new process */ if (proc == NULL && done) { /* No process to run, exit */ printf("\tCPU %d stopped\n", id); break; } else if (proc == NULL) { /* There may be new processes to run in * next time slots, just skip current slot */ next_slot(timer_id); continue; } else if (time_left == 0) { printf("\tCPU %d: Dispatched process %2d\n", id, proc->pid); time_left = time_slot; } /* Run current process */ run(proc); time_left--; next_slot(timer_id); } detach_event(timer_id); pthread_exit(NULL); } static void *ld_routine(void *args) { #ifdef MM_PAGING struct memphy_struct *mram = ((struct mmpaging_ld_args *)args)->mram; struct memphy_struct **mswp = ((struct mmpaging_ld_args *)args)->mswp; struct memphy_struct *active_mswp = ((struct mmpaging_ld_args *)args)->active_mswp; struct timer_id_t *timer_id = ((struct mmpaging_ld_args *)args)->timer_id; #else struct timer_id_t *timer_id = (struct timer_id_t *)args; #endif int i = 0; printf("ld_routine\n"); while (i < num_processes) { struct pcb_t *proc = load(ld_processes.path[i]); #ifdef MLQ_SCHED proc->prio = ld_processes.prio[i]; #endif while (current_time() < ld_processes.start_time[i]) { next_slot(timer_id); } #ifdef MM_PAGING proc->mm = malloc(sizeof(struct mm_struct)); init_mm(proc->mm, proc); proc->mram = mram; proc->mswp = mswp; proc->active_mswp = active_mswp; #endif // printf("%lu\n",ld_processes.prio[i]); printf("\tLoaded a process at %s, PID: %d PRIO: %lu\n", ld_processes.path[i], proc->pid, ld_processes.prio[i]); add_proc(proc); free(ld_processes.path[i]); i++; next_slot(timer_id); } free(ld_processes.path); free(ld_processes.start_time); done = 1; detach_event(timer_id); pthread_exit(NULL); } static void read_config(const char *path) { FILE *file; if ((file = fopen(path, "r")) == NULL) { printf("Cannot find configure file at %s\n", path); exit(1); } fscanf(file, "%d %d %d\n", &time_slot, &num_cpus, &num_processes); ld_processes.path = (char **)malloc(sizeof(char *) * num_processes); ld_processes.start_time = (unsigned long *) malloc(sizeof(unsigned long) * num_processes); #ifdef MM_PAGING int sit; #ifdef MM_FIXED_MEMSZ /* We provide here a back compatible with legacy OS simulatiom config file * In which, it have no addition config line for Mema, keep only one line * for legacy info * [time slice] [N = Number of CPU] [M = Number of Processes to be run] */ memramsz = 0x100000; memswpsz[0] = 0x1000000; for (sit = 1; sit < PAGING_MAX_MMSWP; sit++) memswpsz[sit] = 0; #else /* Read input config of memory size: MEMRAM and upto 4 MEMSWP (mem swap) * Format: (size=0 result non-used memswap, must have RAM and at least 1 SWAP) * MEM_RAM_SZ MEM_SWP0_SZ MEM_SWP1_SZ MEM_SWP2_SZ MEM_SWP3_SZ */ fscanf(file, "%d\n", &memramsz); for (sit = 0; sit < PAGING_MAX_MMSWP; sit++) fscanf(file, "%d", &(memswpsz[sit])); fscanf(file, "\n"); /* Final character */ #endif #endif ld_processes.prio = (unsigned long *)malloc(sizeof(unsigned long) * num_processes); #ifdef MLQ_SCHED int i; for (i = 0; i < num_processes; i++) { ld_processes.path[i] = (char *)malloc(sizeof(char) * 100); ld_processes.path[i][0] = '\0'; strcat(ld_processes.path[i], "input/proc/"); char proc[100]; #endif #ifndef MLQ_SCHED fscanf(file, "%lu %s %lu", &ld_processes.start_time[i], proc, &ld_processes.prio[i]); #else fscanf(file, "%lu %s\n", &ld_processes.start_time[i], proc); #endif strcat(ld_processes.path[i], proc); } }