Skip to content

Commit

Permalink
1st commit
Browse files Browse the repository at this point in the history
  • Loading branch information
DragosRadut committed Nov 27, 2022
0 parents commit 4124871
Show file tree
Hide file tree
Showing 22 changed files with 2,169 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
CC = gcc
CFLAGS = -g -fPIC -m32 -Wall -pthread
LDFLAGS = -m32

.PHONY: build
build: libscheduler.so

libscheduler.so: scheduler.o
$(CC) $(LDFLAGS) -shared -o $@ $^

scheduler.o: scheduler.c
$(CC) $(CFLAGS) -c $<

.PHONY: clean
clean:
-rm -f *.o libscheduler.so
29 changes: 29 additions & 0 deletions checker-lin/Makefile.checker
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
CPP = gcc
CFLAGS = -Wall -g
LIBS = -pthread -lscheduler -L.
DIR = _test
TEST_EXEC = $(DIR)/run_test
OBJ_FILES = $(patsubst %.c, %.o, $(wildcard $(DIR)/*.c))
STACK_SIZE = 4096 # KB

.PHONY: all clean run pack build-pre build-post

all: build-pre run build-post

build-pre:

$(TEST_EXEC): $(OBJ_FILES)
$(CPP) $(CFLAGS) $^ $(LIBS) -o $@

build-post: $(TEST_EXEC)

run: $(TEST_EXEC)
@export LD_LIBRARY_PATH=. && ulimit -s $(STACK_SIZE) && ./run_all.sh

pack:
zip -r run_test_lin.zip _test/ Makefile.checker \
run_all.sh README

clean:
-rm -f *~ _test/*.o $(TEST_EXEC)

50 changes: 50 additions & 0 deletions checker-lin/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#
# 4 - Thread Scheduler (Linux)
#
# 2017, Operating Systems
#

# Archive:

> Makefile.checker
> README
> run_all.sh
> _test/
> run_test,c
> run_test.h
> scheduler_test.h
> so_scheduler.h
> test_exec.c
> test_io.c
> test_sched.c

# Build:

The local directory must contain the thread scheduler library
(libscheduler.so). Use the Makefile.checker to properly build the run_test
executable:

make -f Makefile.checker

This command will also run all tests and print out the results.

# Run:

In order to run the test suite you can either use the run_all.sh script or
the run_test executable. The loader must be able to locate the library
(libscheduler.so) (use LD_LIBRARY_PATH).

The run_all.sh script runs all tests and computes assignment grade (95 points
maximum):

LD_LIBRARY_PATH=. ./run_all.sh

In order to run a specific test, pass the test number (1 .. 20) to the run_test
executable:

LD_LIBRARY_PATH=. ./_test/run_test 1

# Debug:

For debugging, define the SO_VERBOSE_ERROR symbol.

1 change: 1 addition & 0 deletions checker-lin/_test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
run_test
140 changes: 140 additions & 0 deletions checker-lin/_test/run_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* Testing framework
*
* 2019, Operating Systems
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

/* tests functions */
#include "run_test.h"
/* custom tests */
#include "scheduler_test.h"

#define FILE_NAME "output.ref"

/* global variables used by the test */
static unsigned long test_index;

void test_do_fail(void)
{
exit(EXIT_FAILURE);
}

void basic_test(int test_passed)
{
if (!test_passed)
test_do_fail();
}

static int init_world(void)
{
return 0;
}

static int cleanup_world(void)
{
return 0;
}

struct run_test_t test_fun_array[] = {
/* tests schedule - see test_sched.c */
{ test_sched_01 },
{ test_sched_02 },
{ test_sched_03 },
{ test_sched_04 },
{ test_sched_05 },
{ test_sched_06 },
{ test_sched_07 },
{ test_sched_08 },
{ test_sched_09 },
{ test_sched_10 },
{ test_sched_11 },

/* tests schedule execution - see test_exec.c */
{ test_sched_12 },
{ test_sched_13 },
{ test_sched_14 },
{ test_sched_15 },
{ test_sched_16 },
{ test_sched_17 },
{ test_sched_18 },

/* tests schedule IO operations - see test_io.c */
{ test_sched_19 },
{ test_sched_20 },
{ test_sched_21 },
{ test_sched_22 },
};

/* custom main testing thread */
int main(int argc, char **argv)
{
unsigned long last_test;
int fd, rc;

if (argc != 2) {
fprintf(stderr, "Usage: %s <test_number> | init | cleanup\n",
argv[0]);
return -1;
}

/* check init and cleanup execution */
if (strcmp(argv[1], "init") == 0) {
if (init_world() < 0) {
fprintf(stderr, "test init failed\n");
return -1;
}
return 0;
} else if (strcmp(argv[1], "cleanup") == 0) {
if (cleanup_world() < 0) {
fprintf(stderr, "test cleanup failed\n");
return -1;
}
return 0;
}

test_index = strtoul(argv[1], NULL, 10);
if (errno == EINVAL || errno == ERANGE) {
fprintf(stderr, "%s not a number\n", argv[1]);
return -1;
}

last_test = sizeof(test_fun_array) / sizeof(struct run_test_t) - 1;
if (test_index < 0 || test_index > last_test) {
fprintf(stderr, "Error: Test index is out of range"
"(1 < test_index <= %lu).\n", last_test + 1);
return -1;
}

/* randomize time quantums */
srand((unsigned long)time(NULL));

test_fun_array[test_index].function();

fd = open(FILE_NAME, O_CREAT | O_WRONLY | O_TRUNC, 0644);
if (fd > 0) {
rc = write(fd, "1", 1);
if (rc < 0) {
fprintf(stderr, "write failed: rc=%d\n", rc);
return -1;
}
rc = close(fd);
if (rc < 0) {
fprintf(stderr, "close failed: rc=%d\n", rc);
return -1;
}
} else {
fprintf(stderr, "open failed: fd=%d\n", fd);
return -1;
}

return 0;
}
26 changes: 26 additions & 0 deletions checker-lin/_test/run_test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Testing framework headers
*
* 2012, Operating Systems
*/

#ifndef RUN_TEST_H_
#define RUN_TEST_H_

/* functions exported by the framework */
extern void init_test(void);
extern void cleanup_test(void);
extern void basic_test(int test_passed);

/* prototype executed for each test */
typedef void (run_test_f)(void);

struct run_test_t {
run_test_f *function; /* testing function */
};

/* tests array with each test */
extern struct run_test_t test_fun_array[];

#endif /* RUN_TEST_H_ */

Binary file added checker-lin/_test/run_test.o
Binary file not shown.
110 changes: 110 additions & 0 deletions checker-lin/_test/scheduler_test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Threads scheduler functions
*
* 2012, Operating Systems
*/

#ifndef SCHED_TEST_H_
#define SCHED_TEST_H_

#define DLL_IMPORTS

#include "run_test.h"
#include "so_scheduler.h"
#include <stdio.h>

#define SO_TEST_FAIL 0
#define SO_TEST_SUCCESS 1
#define SO_MAX_UNITS 32

/* functions tested */
extern void test_sched_01(void);
extern void test_sched_02(void);
extern void test_sched_03(void);
extern void test_sched_04(void);
extern void test_sched_05(void);
extern void test_sched_06(void);
extern void test_sched_07(void);
extern void test_sched_08(void);
extern void test_sched_09(void);
extern void test_sched_10(void);
extern void test_sched_11(void);
extern void test_sched_12(void);
extern void test_sched_13(void);
extern void test_sched_14(void);
extern void test_sched_15(void);
extern void test_sched_16(void);
extern void test_sched_17(void);
extern void test_sched_18(void);
extern void test_sched_19(void);
extern void test_sched_20(void);
extern void test_sched_21(void);
extern void test_sched_22(void);

/* debugging macro */
#ifdef SO_VERBOSE_ERROR
#define so_error(msg, ...) fprintf(stderr, "ERR: " msg "\n", ##__VA_ARGS__)
#else
#define so_error(msg, ...)
#endif

/* shows the message and exits */
#define so_fail(msg) \
do { \
so_error(msg); \
exit(-1); \
} while (0)

/* returns unsigned random value between _min and _max - 1 */
#define get_rand(min, max) ((rand() % (max - min)) + min)

/* architecture dependent functions */
#ifdef __linux__

static inline tid_t get_tid(void)
{
return pthread_self();
}

static inline int equal_tids(tid_t t1, tid_t t2)
{
return pthread_equal(t1, t2);
}

/* useful defines */
static inline int this_tid(tid_t t)
{
return pthread_equal(t, get_tid());
}

#elif defined _WIN32

#define inline __inline

static inline tid_t get_tid(void)
{
return GetCurrentThreadId();
}

static inline int equal_tids(tid_t t1, tid_t t2)
{
return t1 == t2;
}

/* useful defines */
static inline int this_tid(tid_t t)
{
return equal_tids(t, get_tid());
}

static inline void sched_yield(void)
{
Sleep(0);
}

#else
#error "Unknown platform"
#endif

#endif /* SCHED_TEST_H_ */

Loading

0 comments on commit 4124871

Please sign in to comment.