TP4: obj1 - obj2

This commit is contained in:
Hugo Meens 2025-04-29 18:38:04 +02:00
parent e9fb35edda
commit fd749300ed
16 changed files with 497 additions and 0 deletions

28
tp4-obj1-meens/Makefile Normal file
View File

@ -0,0 +1,28 @@
LIB_PATH=$(shell heptc -where)
OBJ=extern.c scheduler_c/scheduler.c scheduler_data_c/scheduler_data_types.c
aaa:
heptc -target c scheduler_data.ept
heptc extern.epi
heptc -target c scheduler.ept
gcc -I $(LIB_PATH)/c -I . main.c extern.c -I scheduler_data_c scheduler_c/scheduler.c scheduler_data_c/scheduler_data.c
al: main
a: extern.epi
heptc $<
$(OBJ): scheduler_data.ept scheduler.ept a
heptc -target c scheduler_data.ept
heptc -target c scheduler.ept
main: $(OBJ)
gcc -I $(LIB_PATH)/c -I . main.c $(OBJ)
clean:
$(RM) *.log
$(RM) *.epci
$(RM) *.mls
$(RM) *.o *.obj *.obc
$(RM) -r *_c
$(RM) *.out

30
tp4-obj1-meens/extern.c Normal file
View File

@ -0,0 +1,30 @@
#include "extern.h"
#include <stdio.h>
#include <stdlib.h>
#include "scheduler_data_c/scheduler_data_types.h"
void Extern__deadline_miss_log_step(int data, int task_id, Extern__deadline_miss_log_out *_out)
{
printf("Deadline missed: task: %i, data: %i\n", task_id, data);
}
int Extern__random_step(int max, Extern__random_out *_out)
{
_out->v = (rand() % max) + 1;
/* _out->v = max; */
return _out->v;
}
void Extern__print_scheduler_state_step(Scheduler_data__scheduler_state s, Extern__print_scheduler_state_out *_out)
{
printf("Scheduler state - current_date: %i\n", s.current_date);
for (size_t i = 0; i < Scheduler_data__ntasks; i++) {
int left = s.tasks[i].left;
if (s.tasks[i].status == 2)
left = 0;
printf("[%li] Task - status: %d - current_deadline: %i - left: %i\n",
i, s.tasks[i].status, s.tasks[i].current_deadline, left);
}
printf("\n");
}

View File

@ -0,0 +1,5 @@
open Scheduler_data
fun deadline_miss_log(date:int;task_id:int) returns ()
fun random(max:int) returns (v:int)
fun print_scheduler_state(s:scheduler_state) returns ()

26
tp4-obj1-meens/extern.h Normal file
View File

@ -0,0 +1,26 @@
#ifndef EXTERN_H
#define EXTERN_H
#include "scheduler_data_c/scheduler_data_types.h"
typedef struct {
int data;
int task_id;
} Extern__deadline_miss_log_out;
typedef struct {
int max;
int v;
} Extern__random_out;
typedef struct {
} Extern__print_scheduler_state_out;
void Extern__deadline_miss_log_step(int data, int task_id, Extern__deadline_miss_log_out *_out);
int Extern__random_step(int max, Extern__random_out *_out);
void Extern__print_scheduler_state_step(Scheduler_data__scheduler_state s, Extern__print_scheduler_state_out *_out);
#endif // EXTERN_H

View File

@ -0,0 +1,6 @@
#ifndef EXTERN_TYPES_H
#define EXTERN_TYPES_H
#endif /* EXTERN_TYPES_H */

21
tp4-obj1-meens/main.c Normal file
View File

@ -0,0 +1,21 @@
#include <unistd.h>
/* #include "scheduler_data_c/scheduler_data.h" */
/* #include "scheduler_data_c/scheduler_data_types.h" */
#include "scheduler_c/scheduler.h"
#include "scheduler_c/scheduler_types.h"
#include "stdio.h"
int main() {
Scheduler__main_out _out;
Scheduler__main_mem self;
char buffer[100];
Scheduler__main_reset(&self);
while (1) {
fgets(buffer, 10, stdin);
Scheduler__main_step(&_out, &self);
}
return 0;
}

View File

@ -0,0 +1,104 @@
open Scheduler_data
open Extern
fun select_aux(ts:task_status; ta:task_attributes;tid:int ;acc:select_acc)
returns (acc_o:select_acc)
let
acc_o =
if (ts.status = Ready) and (ta.period < acc.speriod) then
{ tid = tid; speriod = ta.period }
else acc
tel
fun select_one_task(ts:task_status^ntasks) returns(selected:int)
var tmp : select_acc ;
let
tmp = foldi<<ntasks>> select_aux (ts,tasks,{ tid = ntasks; speriod = int_max }) ;
selected = tmp.tid ;
tel
fun start_inst(current_date:int;tsi:task_status;ta:task_attributes)
returns (tso:task_status)
var c : bool ;
let
c = (current_date-ta.first_start)%ta.period = 0 ;
tso = merge c
(true -> {
status = Ready;
current_deadline = (current_date when c) + (ta.deadline when c);
left = random(ta.capacity when c)
})
(false -> tsi whenot c)
tel
fun update_selected(ts:task_status;selected:int;tid:int) returns (tso:task_status)
let
tso =
if tid = selected then
{ ts with .status = Running }
else ts
tel
fun rate_monotonic(ts:task_status^ntasks) returns (tso:task_status^ntasks)
var selected : int ;
let
selected = select_one_task(ts) ;
tso = mapi<<ntasks>> update_selected (ts, selected^ntasks) ;
tel
fun check_deadline(current_date:int; tsi:task_status; tid:int)
returns (tso:task_status)
var c: bool ;
let
c = (tsi.status = Ready)
and (tsi.current_deadline = current_date) ;
() = deadline_miss_log(current_date when c, tid when c) ;
tso = if c then { tsi with .status = Waiting} else tsi ;
tel
fun simulate(tsi:task_status)
returns (o:task_status)
let
o =
if tsi.status = Running then
if tsi.left <=1 then
(* Normal termination, move to Waiting state *)
{ tsi with .status = Waiting }
else
(* No termination, yet *)
{
status = Ready;
current_deadline = tsi.current_deadline ;
left = tsi.left - 1
}
else tsi
tel
fun scheduler(si:scheduler_state) returns (so:scheduler_state)
var
new_date : int ;
tmp1,tmp2,tmp3,fin: task_status^ntasks ;
let
new_date = si.current_date + 1 ; (* advance time by 1 *)
tmp1 = map <<ntasks>> simulate (si.tasks) ;
tmp2 = mapi<<ntasks>> check_deadline (new_date^ntasks,tmp1);
tmp3 = map <<ntasks>> start_inst (new_date^ntasks,tmp2,tasks);
fin = rate_monotonic(tmp3) ; (* scheduling policy *)
so = { current_date = new_date; tasks = fin }
tel
const init_sstate : scheduler_state = {
current_date = -1 ;
tasks = {
status=Waiting;current_deadline=0;left=0
}^2
}
node main() returns ()
var sstate, new_sstate : scheduler_state ;
let
new_sstate = scheduler(sstate) ;
sstate = init_sstate fby new_sstate ;
() = print_scheduler_state(new_sstate) ;
tel

View File

@ -0,0 +1,28 @@
const ntasks : int = 2
const int_max : int = 10
type task_attributes = {
period : int ;
capacity : int ;
deadline : int ;
first_start : int
}
type task_state = Running | Ready | Waiting
type task_status = {
status : task_state ;
current_deadline : int ;
left : int
}
type scheduler_state = {
current_date : int ;
tasks : task_status^ntasks
}
type select_acc = { tid : int; speriod : int }
const tasks : task_attributes^ntasks = [
{ period=5; capacity=2; deadline=5; first_start=0 },
{ period=7; capacity=2; deadline=7; first_start=3 }
]

28
tp4-obj2-meens/Makefile Normal file
View File

@ -0,0 +1,28 @@
LIB_PATH=$(shell heptc -where)
OBJ=extern.c scheduler_c/scheduler.c scheduler_data_c/scheduler_data_types.c
aaa:
heptc -target c scheduler_data.ept
heptc extern.epi
heptc -target c scheduler.ept
gcc -I $(LIB_PATH)/c -I . main.c extern.c -I scheduler_data_c scheduler_c/scheduler.c scheduler_data_c/scheduler_data.c
al: main
a: extern.epi
heptc $<
$(OBJ): scheduler_data.ept scheduler.ept a
heptc -target c scheduler_data.ept
heptc -target c scheduler.ept
main: $(OBJ)
gcc -I $(LIB_PATH)/c -I . main.c $(OBJ)
clean:
$(RM) *.log
$(RM) *.epci
$(RM) *.mls
$(RM) *.o *.obj *.obc
$(RM) -r *_c
$(RM) *.out

30
tp4-obj2-meens/extern.c Normal file
View File

@ -0,0 +1,30 @@
#include "extern.h"
#include <stdio.h>
#include <stdlib.h>
#include "scheduler_data_c/scheduler_data_types.h"
void Extern__deadline_miss_log_step(int data, int task_id, Extern__deadline_miss_log_out *_out)
{
printf("Deadline missed: task: %i, data: %i\n", task_id, data);
}
int Extern__random_step(int max, Extern__random_out *_out)
{
_out->v = (rand() % max) + 1;
/* _out->v = max; */
return _out->v;
}
void Extern__print_scheduler_state_step(Scheduler_data__scheduler_state s, Extern__print_scheduler_state_out *_out)
{
printf("Scheduler state - current_date: %i\n", s.current_date);
for (size_t i = 0; i < Scheduler_data__ntasks; i++) {
int left = s.tasks[i].left;
if (s.tasks[i].status == 2)
left = 0;
printf("[%li] Task - status: %d - current_deadline: %i - left: %i\n",
i, s.tasks[i].status, s.tasks[i].current_deadline, left);
}
printf("\n");
}

View File

@ -0,0 +1,5 @@
open Scheduler_data
fun deadline_miss_log(date:int;task_id:int) returns ()
fun random(max:int) returns (v:int)
fun print_scheduler_state(s:scheduler_state) returns ()

26
tp4-obj2-meens/extern.h Normal file
View File

@ -0,0 +1,26 @@
#ifndef EXTERN_H
#define EXTERN_H
#include "scheduler_data_c/scheduler_data_types.h"
typedef struct {
int data;
int task_id;
} Extern__deadline_miss_log_out;
typedef struct {
int max;
int v;
} Extern__random_out;
typedef struct {
} Extern__print_scheduler_state_out;
void Extern__deadline_miss_log_step(int data, int task_id, Extern__deadline_miss_log_out *_out);
int Extern__random_step(int max, Extern__random_out *_out);
void Extern__print_scheduler_state_step(Scheduler_data__scheduler_state s, Extern__print_scheduler_state_out *_out);
#endif // EXTERN_H

View File

@ -0,0 +1,6 @@
#ifndef EXTERN_TYPES_H
#define EXTERN_TYPES_H
#endif /* EXTERN_TYPES_H */

21
tp4-obj2-meens/main.c Normal file
View File

@ -0,0 +1,21 @@
#include <unistd.h>
/* #include "scheduler_data_c/scheduler_data.h" */
/* #include "scheduler_data_c/scheduler_data_types.h" */
#include "scheduler_c/scheduler.h"
#include "scheduler_c/scheduler_types.h"
#include "stdio.h"
int main() {
Scheduler__main_out _out;
Scheduler__main_mem self;
char buffer[100];
Scheduler__main_reset(&self);
while (1) {
fgets(buffer, 10, stdin);
Scheduler__main_step(&_out, &self);
}
return 0;
}

View File

@ -0,0 +1,105 @@
open Scheduler_data
open Extern
fun select_aux(ts:task_status; ta:task_attributes;tid:int ;acc:select_acc)
returns (acc_o:select_acc)
let
acc_o =
if (ts.status = Ready) and (ta.period < acc.speriod) then
{ tid = tid; speriod = ta.period }
else acc
tel
fun select_one_task(ts:task_status^ntasks) returns(selected:int)
var tmp : select_acc ;
let
tmp = foldi<<ntasks>> select_aux (ts,tasks,{ tid = ntasks; speriod = int_max }) ;
selected = tmp.tid ;
tel
fun start_inst(current_date:int;tsi:task_status;ta:task_attributes)
returns (tso:task_status)
var c : bool ;
let
c = (current_date-ta.first_start)%ta.period = 0 ;
tso = merge c
(true -> {
status = Ready;
current_deadline =
(current_date when c) + (ta.deadline when c);
left = (ta.capacity when c)
})
(false -> tsi whenot c)
tel
fun update_selected(ts:task_status;selected:int;tid:int) returns (tso:task_status)
let
tso =
if tid = selected then
{ ts with .status = Running }
else ts
tel
fun rate_monotonic(ts:task_status^ntasks) returns (tso:task_status^ntasks)
var selected : int ;
let
selected = select_one_task(ts) ;
tso = mapi<<ntasks>> update_selected (ts, selected^ntasks) ;
tel
fun check_deadline(current_date:int; tsi:task_status; tid:int)
returns (tso:task_status)
var c: bool ;
let
c = (tsi.status = Ready)
and (tsi.current_deadline = current_date) ;
() = deadline_miss_log(current_date when c, tid when c) ;
tso = if c then { tsi with .status = Waiting} else tsi ;
tel
fun simulate(tsi:task_status)
returns (o:task_status)
let
o =
if tsi.status = Running then
if tsi.left <=1 then
(* Normal termination, move to Waiting state *)
{ tsi with .status = Waiting }
else
(* No termination, yet *)
{
status = Ready;
current_deadline = tsi.current_deadline ;
left = tsi.left - 1
}
else tsi
tel
fun scheduler(si:scheduler_state) returns (so:scheduler_state)
var
new_date : int ;
tmp1,tmp2,tmp3,fin: task_status^ntasks ;
let
new_date = si.current_date + 1 ; (* advance time by 1 *)
tmp1 = map <<ntasks>> simulate (si.tasks) ;
tmp2 = mapi<<ntasks>> check_deadline (new_date^ntasks,tmp1);
tmp3 = map <<ntasks>> start_inst (new_date^ntasks,tmp2,tasks);
fin = rate_monotonic(tmp3) ; (* scheduling policy *)
so = { current_date = new_date; tasks = fin }
tel
const init_sstate : scheduler_state = {
current_date = -1 ;
tasks = {
status=Waiting;current_deadline=0;left=0
}^2
}
node main() returns ()
var sstate, new_sstate : scheduler_state ;
let
new_sstate = scheduler(sstate) ;
sstate = init_sstate fby new_sstate ;
() = print_scheduler_state(new_sstate) ;
tel

View File

@ -0,0 +1,28 @@
const ntasks : int = 2
const int_max : int = 10
type task_attributes = {
period : int ;
capacity : int ;
deadline : int ;
first_start : int
}
type task_state = Running | Ready | Waiting
type task_status = {
status : task_state ;
current_deadline : int ;
left : int
}
type scheduler_state = {
current_date : int ;
tasks : task_status^ntasks
}
type select_acc = { tid : int; speriod : int }
const tasks : task_attributes^ntasks = [
{ period=5; capacity=2; deadline=5; first_start=0 },
{ period=7; capacity=4; deadline=7; first_start=3 }
]