// Run the Arduino standard functions in the main loop int main(int argc, char** argv) { // Let simulated program have access to argc and argv _simulator_argc = argc; _simulator_argv = argv; start_millis = time_in_millis(); // Seed the random number generator srand(getpid() ^ (unsigned) time(NULL)/2); setup(); while (1) loop(); }
process_info_t init_process(void) { process_info_t process; process.pid = rand() % 1000 + 1000; process.start_time = time_in_millis(); process.static_priority = get_priority(); process.sleep_avg = 0; process.last_execution = 0; process.dynamic_priority = process.static_priority; process.expected_execution_time = 200 + rand() % 2000; process.accumulated_execution_time = 0; if (process.static_priority < 100) process.scheduling_type = (rand() % 2) ? RR : FIFO; else process.scheduling_type = NORM; return process; }
// Arduino equivalent, milliseconds since process start unsigned long millis() { return time_in_millis() - start_millis; }
void *consumer_function(void *arg){ int cpu_num = (int) arg; consumer_t *cpu = &cpus[cpu_num]; cpu_queue_t *cpu_queue; process_info_t task; long start_time, end_time; int execution_time; int sleep_avg; printf("Hi i'm the consumer %d\n", cpu_num); while(!thread_finished || cpu->size !=0){ //CRITICAL SECTION pthread_mutex_lock(&cpu->mutex); if (cpu->ready_queue[0].size > 0) { cpu_queue = &cpu->ready_queue[0]; task = cpu_queue->queue[cpu_queue->head]; cpu_queue->head = (cpu_queue->head + 1) % Q_CAPACITY; cpu_queue->size--; } else if (cpu->ready_queue[1].size > 0) { task = get_max_priority(&cpu->ready_queue[1]); } else if (cpu->ready_queue[2].size > 0) { task = get_max_priority(&cpu->ready_queue[2]); } else { pthread_mutex_unlock(&cpu->mutex); sleep(1); continue; } cpu->size--; pthread_mutex_unlock(&cpu->mutex); if (task.scheduling_type == NORM) { start_time = time_in_millis(); sleep_avg = (start_time - task.last_execution) / 200; task.sleep_avg = min(10, sleep_avg + task.sleep_avg); } if (task.scheduling_type == FIFO) execution_time = task.expected_execution_time; else execution_time = min(quantum(task.static_priority), task.expected_execution_time - task.accumulated_execution_time); int io_operation = task.scheduling_type == RR && rand() % 2; if (io_operation) execution_time = 10; // I/O Wait start_time = time_in_millis(); usleep(execution_time * 1000); end_time = time_in_millis(); task.accumulated_execution_time += end_time - start_time; printf("The following process was selected by cpu: %d to run for %dms\n", cpu_num, execution_time); print_process_info(task); if (io_operation) { printf("I/O Operation In Progress for process %d\n", task.pid); usleep(1200 * 1000); // I/O Wait } sleep(1); // Prevents rediculous output speeds. task.last_cpu = cpu_num; if (task.accumulated_execution_time < task.expected_execution_time) { if (task.scheduling_type == NORM) { printf("Process %d updated the dynamic priority from %d to ", task.pid, task.dynamic_priority); sleep_avg = (end_time - start_time) / 200; task.sleep_avg = max(0, task.sleep_avg - sleep_avg); task.dynamic_priority = max(100, min(139, task.dynamic_priority - task.sleep_avg + 5)); printf("%d.\n", task.dynamic_priority); } //CRITICAL SECTION pthread_mutex_lock(&cpu->mutex); if (task.dynamic_priority < 100) cpu_queue = &cpu->ready_queue[0]; else if (task.dynamic_priority < 130) cpu_queue = &cpu->ready_queue[1]; else cpu_queue = &cpu->ready_queue[2]; cpu_queue->queue[cpu_queue->tail] = task; cpu_queue->tail = (cpu_queue->tail + 1) % Q_CAPACITY; cpu_queue-> size++; cpu->size++; pthread_mutex_unlock(&cpu->mutex); continue; } printf("CPU %d finished process %d in %d ms\n", cpu_num, task.pid, task.accumulated_execution_time); } thread_finished += 1; printf("CPU %d Exiting\n", cpu_num); pthread_exit(NULL); }
static dlist * skippy_run(MainWin *mw, dlist *clients, Window focus, Window leader, Bool all_xin) { XEvent ev; int die = 0; Bool refocus = False; int last_rendered; /* Update the main window's geometry (and Xinerama info if applicable) */ mainwin_update(mw); #ifdef XINERAMA if(all_xin) mw->xin_active = 0; #else /* ! XINERAMA */ if(all_xin); #endif /* XINERAMA */ /* Map the main window and run our event loop */ if(mw->lazy_trans) { mainwin_map(mw); XFlush(mw->dpy); } clients = do_layout(mw, clients, focus, leader); if(! mw->cod) return clients; /* Map the main window and run our event loop */ if(! mw->lazy_trans) mainwin_map(mw); last_rendered = time_in_millis(); while(! die) { int i, j, now, timeout; int move_x = -1, move_y = -1; struct pollfd r_fd; XFlush(mw->dpy); r_fd.fd = ConnectionNumber(mw->dpy); r_fd.events = POLLIN; if(mw->poll_time > 0) timeout = MAX(0, mw->poll_time + last_rendered - time_in_millis()); else timeout = -1; i = poll(&r_fd, 1, timeout); now = time_in_millis(); if(now >= last_rendered + mw->poll_time) { REDUCE(if( ((ClientWin*)iter->data)->damaged ) clientwin_repair(iter->data), mw->cod); last_rendered = now; } i = XPending(mw->dpy); for(j = 0; j < i; ++j) { XNextEvent(mw->dpy, &ev); if (ev.type == MotionNotify) { move_x = ev.xmotion.x_root; move_y = ev.xmotion.y_root; } else if(ev.type == DestroyNotify || ev.type == UnmapNotify) { dlist *iter = dlist_find(clients, clientwin_cmp_func, (void *)ev.xany.window); if(iter) { ClientWin *cw = (ClientWin *)iter->data; clients = dlist_first(dlist_remove(iter)); iter = dlist_find(mw->cod, clientwin_cmp_func, (void *)ev.xany.window); if(iter) mw->cod = dlist_first(dlist_remove(iter)); clientwin_destroy(cw, True); if(! mw->cod) { die = 1; break; } } } else if (mw->poll_time >= 0 && ev.type == mw->damage_event_base + XDamageNotify) { XDamageNotifyEvent *d_ev = (XDamageNotifyEvent *)&ev; dlist *iter = dlist_find(mw->cod, clientwin_cmp_func, (void *)d_ev->drawable); if(iter) { if(mw->poll_time == 0) clientwin_repair((ClientWin *)iter->data); else ((ClientWin *)iter->data)->damaged = True; } } else if(ev.type == KeyRelease && ev.xkey.keycode == mw->key_q) { DIE_NOW = 1; die = 1; break; } else if(ev.type == KeyRelease && ev.xkey.keycode == mw->key_escape) { refocus = True; die = 1; break; } else if(ev.xany.window == mw->window) die = mainwin_handle(mw, &ev); else if(ev.type == PropertyNotify) { if(ev.xproperty.atom == ESETROOT_PMAP_ID || ev.xproperty.atom == _XROOTPMAP_ID) { mainwin_update_background(mw); REDUCE(clientwin_render((ClientWin *)iter->data), mw->cod); } } else if(mw->tooltip && ev.xany.window == mw->tooltip->window) tooltip_handle(mw->tooltip, &ev); else { dlist *iter; for(iter = mw->cod; iter; iter = iter->next) { ClientWin *cw = (ClientWin *)iter->data; if(cw->mini.window == ev.xany.window) { die = clientwin_handle(cw, &ev); if(die) break; } } if(die) break; } } if(mw->tooltip && move_x != -1) tooltip_move(mw->tooltip, move_x + 20, move_y + 20); }