refactored main; monitoring & scheduling not done yet
This commit is contained in:
parent
7748a53eb2
commit
f08c635390
4 changed files with 93 additions and 77 deletions
6
Makefile
6
Makefile
|
|
@ -6,8 +6,8 @@ INCLUDES = coders/codexion.h
|
|||
OBJS = $(SRCS:.c=.o)
|
||||
|
||||
CC = cc
|
||||
CFLAGS = -Wall -Wextra -Werror -pthread
|
||||
# CFLAGS = -Wall -Wextra -Werror -pthread -g -fsanitize=thread
|
||||
# CFLAGS = -Wall -Wextra -Werror -pthread -g
|
||||
CFLAGS = -Wall -Wextra -Werror -pthread -g -fsanitize=thread
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
|
|
@ -27,7 +27,7 @@ $(NAME): $(OBJS)
|
|||
# scheduler
|
||||
|
||||
test: $(NAME)
|
||||
./$(NAME) 2 800 200 100 10 5 0 edf
|
||||
./$(NAME) 2 800 200 100 10 3 0 edf
|
||||
|
||||
test-val: $(NAME)
|
||||
valgrind --leak-check=full --show-leak-kinds=all ./$(NAME) 2 800 200 100 10 5 0 edf
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@
|
|||
#ifndef CODEXION_H
|
||||
# define CODEXION_H
|
||||
# include <pthread.h>
|
||||
# include <stdbool.h>
|
||||
# include <sys/time.h>
|
||||
|
||||
typedef struct s_state t_state;
|
||||
|
|
@ -34,14 +35,14 @@ typedef struct s_args
|
|||
|
||||
typedef struct s_dongle
|
||||
{
|
||||
int index;
|
||||
int idx;
|
||||
pthread_mutex_t lock;
|
||||
long last_used;
|
||||
} t_dongle;
|
||||
|
||||
typedef struct s_coder
|
||||
{
|
||||
int coder_id;
|
||||
int idx;
|
||||
pthread_t thread;
|
||||
t_dongle *right_dongle;
|
||||
t_dongle *left_dongle;
|
||||
|
|
@ -56,7 +57,7 @@ typedef struct s_state
|
|||
t_coder *coders;
|
||||
t_dongle *dongles;
|
||||
long start;
|
||||
int is_over;
|
||||
bool is_over;
|
||||
pthread_mutex_t over_mutex;
|
||||
pthread_mutex_t print_mutex;
|
||||
pthread_t monitor;
|
||||
|
|
|
|||
143
coders/main.c
143
coders/main.c
|
|
@ -12,6 +12,7 @@
|
|||
|
||||
#include "codexion.h"
|
||||
#include <pthread.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/time.h>
|
||||
|
|
@ -25,10 +26,19 @@ long now(void)
|
|||
return (current.tv_sec * 1000L + current.tv_usec / 1000);
|
||||
}
|
||||
|
||||
void print(char *str, t_state *state, int coder_idx)
|
||||
{
|
||||
pthread_mutex_lock(&state->print_mutex);
|
||||
printf(str, now() - state->start, coder_idx + 1);
|
||||
pthread_mutex_unlock(&state->print_mutex);
|
||||
}
|
||||
|
||||
void compile_op(t_coder *coder, t_state *state)
|
||||
{
|
||||
t_dongle *first, *second;
|
||||
if (coder->right_dongle->index < coder->left_dongle->index)
|
||||
t_dongle *first;
|
||||
t_dongle *second;
|
||||
|
||||
if (coder->right_dongle->idx < coder->left_dongle->idx)
|
||||
{
|
||||
first = coder->left_dongle;
|
||||
second = coder->right_dongle;
|
||||
|
|
@ -38,26 +48,15 @@ void compile_op(t_coder *coder, t_state *state)
|
|||
first = coder->right_dongle;
|
||||
second = coder->left_dongle;
|
||||
}
|
||||
// acquire first dongle
|
||||
pthread_mutex_lock(&first->lock);
|
||||
first->last_used = now();
|
||||
pthread_mutex_lock(&state->print_mutex);
|
||||
printf("%ld %d has taken a dongle\n", now() - state->start,
|
||||
coder->coder_id);
|
||||
pthread_mutex_unlock(&state->print_mutex);
|
||||
// acquire second dongle
|
||||
print("%ld %d has taken a dongle\n", state, coder->idx);
|
||||
pthread_mutex_lock(&second->lock);
|
||||
second->last_used = now();
|
||||
pthread_mutex_lock(&state->print_mutex);
|
||||
printf("%ld %d has taken a dongle\n", now() - state->start,
|
||||
coder->coder_id);
|
||||
pthread_mutex_unlock(&state->print_mutex);
|
||||
print("%ld %d has taken a dongle\n", state, coder->idx);
|
||||
coder->last_compile = now();
|
||||
pthread_mutex_lock(&state->print_mutex);
|
||||
printf("%ld %d is compiling\n", now() - state->start, coder->coder_id);
|
||||
pthread_mutex_unlock(&state->print_mutex);
|
||||
print("%ld %d is compiling\n", state, coder->idx);
|
||||
usleep(state->args.time_to_compile * 1000L);
|
||||
// release dongles
|
||||
pthread_mutex_unlock(&first->lock);
|
||||
pthread_mutex_unlock(&second->lock);
|
||||
coder->compiles_done += 1;
|
||||
|
|
@ -65,17 +64,13 @@ void compile_op(t_coder *coder, t_state *state)
|
|||
|
||||
void debug_op(t_coder *coder, t_state *state)
|
||||
{
|
||||
pthread_mutex_lock(&state->print_mutex);
|
||||
printf("%ld %d is debugging\n", now() - state->start, coder->coder_id);
|
||||
pthread_mutex_unlock(&state->print_mutex);
|
||||
print("%ld %d is debugging\n", state, coder->idx);
|
||||
usleep(state->args.time_to_debug * 1000L);
|
||||
}
|
||||
|
||||
void refactor_op(t_coder *coder, t_state *state)
|
||||
{
|
||||
pthread_mutex_lock(&state->print_mutex);
|
||||
printf("%ld %d is refactoring\n", now() - state->start, coder->coder_id);
|
||||
pthread_mutex_unlock(&state->print_mutex);
|
||||
print("%ld %d is refactoring\n", state, coder->idx);
|
||||
usleep(state->args.time_to_refactor * 1000L);
|
||||
}
|
||||
|
||||
|
|
@ -115,55 +110,79 @@ int cleanup(t_args *args, t_coder *coders, t_dongle *dongles)
|
|||
return (0);
|
||||
}
|
||||
|
||||
t_state init_state(t_args *args)
|
||||
{
|
||||
t_state state;
|
||||
|
||||
state.start = now();
|
||||
state.args = *args;
|
||||
state.is_over = false;
|
||||
pthread_mutex_init(&state.print_mutex, NULL);
|
||||
pthread_mutex_init(&state.over_mutex, NULL);
|
||||
return (state);
|
||||
}
|
||||
|
||||
t_dongle *init_dongles(t_args *args, t_state *state)
|
||||
{
|
||||
int i;
|
||||
t_dongle *dongles;
|
||||
|
||||
dongles = malloc(sizeof(t_dongle) * args->number_of_coders);
|
||||
if (!dongles)
|
||||
return (NULL);
|
||||
i = -1;
|
||||
while (++i < args->number_of_coders)
|
||||
{
|
||||
dongles[i].idx = i;
|
||||
pthread_mutex_init(&dongles[i].lock, NULL);
|
||||
dongles[i].last_used = state->start - args->dongle_cooldown;
|
||||
}
|
||||
return (dongles);
|
||||
}
|
||||
|
||||
t_coder *init_coders(t_args *args, t_state *state)
|
||||
{
|
||||
t_coder *coders;
|
||||
int i;
|
||||
|
||||
coders = malloc(sizeof(t_coder) * args->number_of_coders);
|
||||
if (!coders)
|
||||
return (NULL);
|
||||
i = -1;
|
||||
while (++i < args->number_of_coders)
|
||||
{
|
||||
coders[i].idx = i;
|
||||
coders[i].compiles_done = 0;
|
||||
coders[i].last_compile = state->start;
|
||||
coders[i].left_dongle = &state->dongles[i];
|
||||
coders[i].right_dongle = &state->dongles[(i + 1)
|
||||
% args->number_of_coders];
|
||||
coders[i].state = state;
|
||||
}
|
||||
return (coders);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int i;
|
||||
t_args *args;
|
||||
t_coder *coders;
|
||||
t_dongle *dongles;
|
||||
t_state state;
|
||||
|
||||
args = parse_arguments(argc - 1, &argv[1]);
|
||||
if (!args)
|
||||
return (cleanup(args, NULL, NULL));
|
||||
coders = malloc(sizeof(t_coder) * args->number_of_coders);
|
||||
dongles = malloc(sizeof(t_dongle) * args->number_of_coders);
|
||||
if (!coders || !dongles)
|
||||
return (cleanup(args, coders, dongles));
|
||||
state.start = now();
|
||||
state.args = *args;
|
||||
state.is_over = 0;
|
||||
state.coders = coders;
|
||||
state.dongles = dongles;
|
||||
pthread_mutex_init(&state.print_mutex, NULL);
|
||||
pthread_mutex_init(&state.over_mutex, NULL);
|
||||
i = 0;
|
||||
while (i < args->number_of_coders)
|
||||
{
|
||||
dongles[i].index = i;
|
||||
pthread_mutex_init(&dongles[i].lock, NULL);
|
||||
dongles[i].last_used = state.start - args->dongle_cooldown;
|
||||
i++;
|
||||
}
|
||||
i = 0;
|
||||
while (i < args->number_of_coders)
|
||||
{
|
||||
coders[i].coder_id = i + 1;
|
||||
coders[i].compiles_done = 0;
|
||||
coders[i].last_compile = state.start;
|
||||
coders[i].left_dongle = &dongles[i];
|
||||
coders[i].right_dongle = &dongles[(i + 1) % args->number_of_coders];
|
||||
coders[i].state = &state;
|
||||
pthread_create(&coders[i].thread, NULL, run, &coders[i]);
|
||||
i++;
|
||||
}
|
||||
state = init_state(args);
|
||||
state.dongles = init_dongles(args, &state);
|
||||
state.coders = init_coders(args, &state);
|
||||
if (!state.coders || !state.dongles)
|
||||
return (cleanup(args, state.coders, state.dongles));
|
||||
i = -1;
|
||||
while (++i < args->number_of_coders)
|
||||
pthread_create(&state.coders[i].thread, NULL, run, &state.coders[i]);
|
||||
pthread_create(&state.monitor, NULL, monitor, &state);
|
||||
i = 0;
|
||||
while (i < args->number_of_coders)
|
||||
{
|
||||
pthread_join(coders[i].thread, NULL);
|
||||
i++;
|
||||
}
|
||||
i = -1;
|
||||
while (++i < args->number_of_coders)
|
||||
pthread_join(state.coders[i].thread, NULL);
|
||||
pthread_join(state.monitor, NULL);
|
||||
cleanup(args, coders, dongles);
|
||||
cleanup(args, state.coders, state.dongles);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -38,18 +38,14 @@ t_args *parse_arguments(int count, char **args)
|
|||
data = malloc(sizeof(t_args));
|
||||
if (!data)
|
||||
return (NULL);
|
||||
i = 0;
|
||||
while (i < 7)
|
||||
i = -1;
|
||||
while (++i < 7)
|
||||
{
|
||||
if (!isnumeric(args[i]))
|
||||
return (NULL);
|
||||
args_int[i] = atoi(args[i]);
|
||||
i++;
|
||||
}
|
||||
if (!strcmp(args[7], "fifo"))
|
||||
data->scheduler = "fifo";
|
||||
else if (!strcmp(args[7], "edf"))
|
||||
data->scheduler = "edf";
|
||||
data->scheduler = args[7];
|
||||
data->number_of_coders = args_int[0];
|
||||
data->time_to_burnout = args_int[1];
|
||||
data->time_to_compile = args_int[2];
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue