Skip to content

Commit

Permalink
HW4
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonKozlov committed Sep 29, 2021
1 parent 2cc5613 commit aa3d05b
Show file tree
Hide file tree
Showing 13 changed files with 199 additions and 2 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ jobs:
- batch
- pool
- syscall
- cosched

steps:
- uses: actions/checkout@v2
Expand Down
52 changes: 50 additions & 2 deletions apps.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <stdlib.h>
#include <stdio.h>

#include "sched.h"
#include "usyscall.h"
#include "pool.h"

Expand All @@ -15,6 +16,8 @@ static int g_retcode;
X(retcode) \
X(pooltest) \
X(syscalltest) \
X(coapp) \
X(cosched) \

#define DECLARE(X) static int X(int, char *[]);
APPS_X(DECLARE)
Expand Down Expand Up @@ -52,6 +55,51 @@ static int retcode(int argc, char *argv[]) {
return 0;
}

struct coapp_ctx {
int cnt;
} ctxarray[16];
struct pool ctxpool = POOL_INITIALIZER_ARRAY(ctxarray);

static void coapp_task(void *_ctx) {
struct coapp_ctx *ctx = _ctx;

printf("%16s id %d cnt %d\n", __func__, 1 + ctx - ctxarray, ctx->cnt);

if (0 < ctx->cnt) {
sched_cont(coapp_task, ctx, 2);
}

--ctx->cnt;
}

static void coapp_rt(void *_ctx) {
struct coapp_ctx *ctx = _ctx;

printf("%16s id %d cnt %d\n", __func__, 1 + ctx - ctxarray, ctx->cnt);

sched_time_elapsed(1);

if (0 < ctx->cnt) {
sched_cont(coapp_rt, ctx, 0);
}

--ctx->cnt;
}

static int coapp(int argc, char* argv[]) {
int entry_id = atoi(argv[1]) - 1;

struct coapp_ctx *ctx = pool_alloc(&ctxpool);
ctx->cnt = atoi(argv[2]);

void (*entries[])(void*) = { coapp_task, coapp_rt };
sched_new(entries[entry_id], ctx, atoi(argv[3]), atoi(argv[4]));
}

static int cosched(int argc, char* argv[]) {
sched_run(atoi(argv[1]));
}

static int exec(int argc, char *argv[]) {
const struct app *app = NULL;
for (int i = 0; i < ARRAY_SIZE(app_list); ++i) {
Expand Down Expand Up @@ -102,12 +150,12 @@ int shell(int argc, char *argv[]) {
char *stcmd;
char *cmd = strtok_r(line, comsep, &stcmd);
while (cmd) {
const char *argsep = " ";
const char *argsep = " \t";
char *starg;
char *arg = strtok_r(cmd, argsep, &starg);
char *argv[256];
int argc = 0;
while (arg) {
while (arg && arg[0] != '#') {
argv[argc++] = arg;
arg = strtok_r(NULL, argsep, &starg);
}
Expand Down
29 changes: 29 additions & 0 deletions sched.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <limits.h>
#include <string.h>
#include <stdio.h>

#include "sched.h"
#include "pool.h"

static int time;

void sched_new(void (*entrypoint)(void *aspace),
void *aspace,
int priority,
int deadline) {

}

void sched_cont(void (*entrypoint)(void *aspace),
void *aspace,
int timeout) {

}

void sched_time_elapsed(unsigned amount) {
time += amount;

}

void sched_run(enum policy policy) {
}
33 changes: 33 additions & 0 deletions sched.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

enum policy {
// first-in, first-out; run tasks in order of their arrival
POLICY_FIFO,

// highest priority task (highest priority value) should be executed
// first. Use round-robin for processes with same priority
// (task from 1st process, from 2nd, ... Nth, 1st, 2nd, ...)
POLICY_PRIO,

// consider deadline, execute process with Earliest Deadline First.
// Fallback to priority policy if deadlines are equal
POLICY_DEADLINE,
};

// Add new task
extern void sched_new(void (*entrypoint)(void *ctx), // entrypoint function
void *ctx, // context of the process
int priority, // priority, [0 - 10], bigger for more priority
int deadline); // absolute time till the task should be completed, <=0 for no deadline

// Continue process from function after some amount of time
extern void sched_cont(void (*entrypoint)(void *aspace), // entrypoint function
void *aspace,// addresses the process can access
int timeout); // when the continuation became runnable

// Notify scheduler that some amount of time passed
extern void sched_time_elapsed(unsigned amount);

// Scheduler loop, start executing tasks until all of them finish
extern void sched_run(enum policy policy);

5 changes: 5 additions & 0 deletions test/cosched/1.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# entryid cnt prio deadline
coapp 2 10 0 0
coapp 2 10 0 0

cosched 0
22 changes: 22 additions & 0 deletions test/cosched/1.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
coapp_rt id 1 cnt 10
coapp_rt id 2 cnt 10
coapp_rt id 1 cnt 9
coapp_rt id 2 cnt 9
coapp_rt id 1 cnt 8
coapp_rt id 2 cnt 8
coapp_rt id 1 cnt 7
coapp_rt id 2 cnt 7
coapp_rt id 1 cnt 6
coapp_rt id 2 cnt 6
coapp_rt id 1 cnt 5
coapp_rt id 2 cnt 5
coapp_rt id 1 cnt 4
coapp_rt id 2 cnt 4
coapp_rt id 1 cnt 3
coapp_rt id 2 cnt 3
coapp_rt id 1 cnt 2
coapp_rt id 2 cnt 2
coapp_rt id 1 cnt 1
coapp_rt id 2 cnt 1
coapp_rt id 1 cnt 0
coapp_rt id 2 cnt 0
6 changes: 6 additions & 0 deletions test/cosched/2.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# entryid cnt prio deadline
coapp 2 2 2 0
coapp 2 2 0 0
coapp 2 2 0 0

cosched 1
9 changes: 9 additions & 0 deletions test/cosched/2.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
coapp_rt id 1 cnt 2
coapp_rt id 1 cnt 1
coapp_rt id 1 cnt 0
coapp_rt id 2 cnt 2
coapp_rt id 3 cnt 2
coapp_rt id 2 cnt 1
coapp_rt id 3 cnt 1
coapp_rt id 2 cnt 0
coapp_rt id 3 cnt 0
6 changes: 6 additions & 0 deletions test/cosched/3.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# entryid cnt prio deadline
coapp 2 2 0 -1
coapp 2 2 0 2
coapp 2 2 0 -1

cosched 2
9 changes: 9 additions & 0 deletions test/cosched/3.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
coapp_rt id 2 cnt 2
coapp_rt id 2 cnt 1
coapp_rt id 2 cnt 0
coapp_rt id 1 cnt 2
coapp_rt id 3 cnt 2
coapp_rt id 1 cnt 1
coapp_rt id 3 cnt 1
coapp_rt id 1 cnt 0
coapp_rt id 3 cnt 0
5 changes: 5 additions & 0 deletions test/cosched/4.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# entryid cnt prio deadline
coapp 1 4 2 0
coapp 2 16 0 0

cosched 1
22 changes: 22 additions & 0 deletions test/cosched/4.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
coapp_task id 1 cnt 4
coapp_rt id 2 cnt 16
coapp_rt id 2 cnt 15
coapp_task id 1 cnt 3
coapp_rt id 2 cnt 14
coapp_rt id 2 cnt 13
coapp_task id 1 cnt 2
coapp_rt id 2 cnt 12
coapp_rt id 2 cnt 11
coapp_task id 1 cnt 1
coapp_rt id 2 cnt 10
coapp_rt id 2 cnt 9
coapp_task id 1 cnt 0
coapp_rt id 2 cnt 8
coapp_rt id 2 cnt 7
coapp_rt id 2 cnt 6
coapp_rt id 2 cnt 5
coapp_rt id 2 cnt 4
coapp_rt id 2 cnt 3
coapp_rt id 2 cnt 2
coapp_rt id 2 cnt 1
coapp_rt id 2 cnt 0
2 changes: 2 additions & 0 deletions test/cosched/run-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

map_inputs checkdiff

0 comments on commit aa3d05b

Please sign in to comment.