void task() { int x = 0; long t = 0; int i, k; int lev1, lev2, col; i = ptask_get_index(); lev1 = LEV0 + DLEV * i - 2; lev2 = LEV0 + DLEV * i - 2 - DEX; col = COLOR_NORM_EXE; while (x < XWIN) { col = COLOR_NORM_EXE; k = 0; while (k <= (prtime[i] / 2)) { t = ptask_gettime(MILLI); x = OFFSET + (t - t_start) / scale; pthread_mutex_lock(&mx_sezNorm); line(screen, x, lev1, x, lev2, col); pthread_mutex_unlock(&mx_sezNorm); while (ptask_gettime(MILLI) == t) ; k++; } if (resource[i] == SEZ_A) { pthread_mutex_lock(&mx_sezA); col = COLOR_SEZ_A; } if (resource[i] == SEZ_B) { pthread_mutex_lock(&mx_sezB); col = COLOR_SEZ_B; } while (k <= prtime[i]) { t = ptask_gettime(MILLI); x = OFFSET + (t - t_start) / scale; line(screen, x, lev1, x, lev2, col); while (ptask_gettime(MILLI) == t) ; k++; } if (resource[i] == SEZ_A) pthread_mutex_unlock(&mx_sezA); if (resource[i] == SEZ_B) pthread_mutex_unlock(&mx_sezB); ptask_wait_for_period(); } printf(" T%d terminated!\n", i); }
int main(void) { int ret = 0, nc; int c; int key = 0; int part = PARTITIONED; // PARTITIONED, GLOBAL int sched = SCHED_FIFO; get_data(); init(); ret = select_prot(); if (ret == -1) { allegro_exit(); return 0; } print_grid(ret); ptask_init(sched, part, ret); t_start = ptask_gettime(MILLI); set_sem_sezC(ret); nc = ptask_getnumcores(); textprintf_ex(screen, font, 480, 10, 7, BGC, "(NumCores = %d)", nc); int gen_id = ptask_create_prio(gen, 100, 30, NOW); if (gen_id < 0) { printf("Could not create task gen\n"); exit(-1); } while (key != KEY_ESC) { if (keypressed()) { c = readkey(); key = c >> 8; } } pmux_destroy(&mx_sezNorm); pmux_destroy(&mx_sezA); pmux_destroy(&mx_sezB); allegro_exit(); return 0; }
int main(void) { int lev1, lev2; int c, key = 0; long x = 0; long t; get_data(); init(SCHED_FIFO, PCP); lev1 = LEV0 - 2; lev2 = LEV0 - 2 - DEX; int gen_id = ptask_create(gen, 100, 30, NOW); if (gen_id < 0) { printf("Could not create task gen\n"); exit(-1); } while (key != KEY_ESC) { if (keypressed()) { c = readkey(); key = c >> 8; } if (x < 640L) { t = ptask_gettime(MILLI); x = OFFSET + t/scale; pthread_mutex_lock(&mxa); line(screen, x, lev1, x, lev2, MAINCOL); pthread_mutex_unlock(&mxa); } } pmux_destroy(&mxa); pmux_destroy(&muxA); pmux_destroy(&muxB); allegro_exit(); return 0; }
void task() { int x; long t; int i, k; int lev1, lev2, col; int at = 0; i = ptask_get_index(); lev1 = LEV0 + DLEV*i - 2; lev2 = LEV0 + DLEV*i - 2 - DEX; col = 1 + i%15; //wait_for_activation(); while (x < 640L) { //dl = at + ptask_get_deadline(i, MILLI); for (k=0; k<prtime[i]; k++) { t = ptask_gettime(MILLI); x = OFFSET + t/scale; pthread_mutex_lock(&mxa); line(screen, x, lev1, x, lev2, col); pthread_mutex_unlock(&mxa); while (ptask_gettime(MILLI) == t); } /*----------------------------------------------*/ /* Critical section guarded by muxA */ /*----------------------------------------------*/ if ((i != 2) && (i != 4)) { pthread_mutex_lock(&muxA); for (k=0; k<csa[i]; k++) { t = ptask_gettime(MILLI); x = OFFSET + t/scale; pthread_mutex_lock(&mxa); line(screen, x, lev1-1, x, lev2+1, CSCOLA); pthread_mutex_unlock(&mxa); while (ptask_gettime(MILLI) == t); } pthread_mutex_unlock(&muxA); } /*----------------------------------------------*/ if (i != 4) { for (k=0; k<prtime[i]; k++) { t = ptask_gettime(MILLI); x = OFFSET + t/scale; pthread_mutex_lock(&mxa); line(screen, x, lev1, x, lev2, col); pthread_mutex_unlock(&mxa); while (ptask_gettime(MILLI) == t); } } /*----------------------------------------------*/ /* Critical section guarded by muxB */ /*----------------------------------------------*/ if (i != 2) { pthread_mutex_lock(&muxB); for (k=0; k<csb[i]; k++) { t = ptask_gettime(MILLI); x = OFFSET + t/scale; pthread_mutex_lock(&mxa); line(screen, x, lev1-1, x, lev2+1, CSCOLB); pthread_mutex_unlock(&mxa); while (ptask_gettime(MILLI) == t); } pthread_mutex_unlock(&muxB); } /*----------------------------------------------*/ if (ptask_deadline_miss()) { // TO BE DONE //sprintf(s, "%d", task_dmiss(i)); //textout_ex(screen, font, s, OFFSET+dl/scale-4, lev1+8, 7, 0); } ptask_wait_for_period(); at = at + ptask_get_period(i,MILLI); } }
int main(void) { int unit = MILLI; int i, id, k; int c; /* character from keyboard */ int ntasks = 0; /* total number of activated tasks*/ int ret = 0; int tipo_prio = PRIO_EQUAL; int sched = SCHED_RR; int part = GLOBAL; int prot = NO_PROTOCOL; int test = TASK_FUN; int modeACT = MOD_NOW; /* {MOD_NOW=0, MOD_DEF_OFFSET=1, MOD_DEF_NO_OFFS = 2} */ int act_flag = NOW; init(TASK_FUN); int num_tasks = NUM_T_TEST; int priority[num_tasks]; ptime v_offset[num_tasks]; /* time offset vector */ for(i = 0; i < num_tasks; i++) v_offset[i] = 0; modeACT = select_act (v_offset, num_tasks); // {MOD_NOW=0, MOD_DEF_OFFSET=1, MOD_DEF_NO_OFFS = 2} if ( modeACT == -1 ) { allegro_exit(); return 0; } ptask_init(sched, part, prot); /* Time reference for timeoffset */ time_t0 = ptask_gettime(MILLI); ptime time_tmp; if(modeACT == 0) { // {MOD_NOW=0, MOD_DEF_OFFSET=1, MOD_DEF_NO_OFFS = 2} act_flag = NOW; } else { act_flag = DEFERRED; } init_vettore_prio(tipo_prio, priority, num_tasks); draw_system_info(sched, part, prot, modeACT, tipo_prio, test, false); draw_Timetask_info(modeACT); pthread_mutex_lock(&mxa); textprintf_ex(screen, font, 2, 25 + 15, FGC, BGC, " Time reference t0 = %ld (time creation/visualization of this screen)", time_t0); pthread_mutex_unlock(&mxa); const char* str_offset = "Time offset(ms) inseriti = "; /* Creation of aperiodic task */ for ( i = 0; i < num_tasks; i++) { if (modeACT != MOD_DEF_OFFSET) { /* MODE WITHOUT OFFSET */ time_tmp = ptask_gettime(MILLI); if(act_flag == NOW) { pthread_mutex_lock(&mxa); textprintf_ex(screen, font, X_LINE_VERT0 + 5, Y_TIMES + i * ALT_RIGA, FGC, BGC, "%ld ", time_tmp); pthread_mutex_unlock(&mxa); } id = ptask_create(periodicLine_testSystemTask, PER, priority[i], act_flag); } else { /* MODE WITH OFFSET */ time_tmp = ptask_gettime(MILLI); pthread_mutex_lock(&mxa); textprintf_ex(screen, font, (X_LINE_VERT0 + X_LINE_VERT1)/2, Y_TIMES + i * ALT_RIGA, FGC, BGC, "-"); pthread_mutex_unlock(&mxa); id = ptask_create(periodicLine_testSystemTaskOFFSET, PER, priority[i], act_flag); printf("act_flag = %d, id = %d, v_offset[%d] = %ld\n", act_flag, id+1, id, v_offset[id]); ret = ptask_activate_at(id, v_offset[i]); if (ret != -1) { draw_activation (ntasks, i, priority[i], false); ntasks++; if (i == 0) { pthread_mutex_lock(&mxa); textout_ex(screen, font, str_offset, XMIN, BASE1 + 2 * ALT_RIGA, FGC, 0); pthread_mutex_unlock(&mxa); } pthread_mutex_lock(&mxa); textprintf_ex(screen, font, XMIN + (strlen(str_offset)+i*(NUM_CIF_OFFSET))*PIXEL_CHAR, BASE1 + 2 * ALT_RIGA,FGC, BGC, "%ld", v_offset[i]); pthread_mutex_unlock(&mxa); } else fprintf(stderr, "Task %d non può essere attivato o già avviato!!!\n", i+1); } if (id != -1) { char* temp = " e attivato!\n"; if(act_flag == DEFERRED) { temp = "!\n"; } printf("Task %d creato%s", id+1, temp); } else { allegro_exit(); printf("Errore nella creazione(o anche attivazione NOW) del task!\n"); exit(-1); } } do { k = 0; if (keypressed()) { c = readkey(); k = c >> 8; } if ((k >= KEY_1) && (k <= KEY_0 + num_tasks) && (modeACT != MOD_DEF_OFFSET)) { id = k - KEY_0 - 1; time_tmp = ptask_gettime(unit); ret = ptask_activate(id); if (ret != -1) { printf("Task %d attivato\n", id); pthread_mutex_lock(&mxa); textprintf_ex(screen, font, X_LINE_VERT0 + 5, Y_TIMES + (id) * ALT_RIGA, FGC, BGC, "%ld ", time_tmp); pthread_mutex_unlock(&mxa); draw_activation (ntasks, id, priority[id], false); ntasks++; } else{ printf("Task %d non può essere attivato o già avviato!!!\n", id); pthread_mutex_lock(&mxa); textprintf_ex(screen, font, X_LINE_VERT0 + 5, Y_TIMES + id * ALT_RIGA,FGC, BGC, "T%d: Gia' attivo! ", id); pthread_mutex_unlock(&mxa); } } } while (k != KEY_ESC); allegro_exit(); return 0; }