all ops done; without synchronization
This commit is contained in:
parent
30ad265779
commit
ebe57f783d
4 changed files with 117 additions and 31 deletions
12
Makefile
12
Makefile
|
|
@ -6,7 +6,7 @@ INCLUDES = coders/codexion.h
|
|||
OBJS = $(SRCS:.c=.o)
|
||||
|
||||
CC = cc
|
||||
CFLAGS = -Wall -Wextra -Werror
|
||||
CFLAGS = -Wall -Wextra -Werror -pthread
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
|
|
@ -16,8 +16,16 @@ $(NAME): $(OBJS)
|
|||
%.o: %.c
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
|
||||
# number_of_coders
|
||||
# time_to_burnout
|
||||
# time_to_compile
|
||||
# time_to_debug
|
||||
# time_to_refactor
|
||||
# number_of_compiles_required
|
||||
# dongle_cooldown
|
||||
# scheduler
|
||||
test: $(NAME)
|
||||
./$(NAME) 0 1 2 3 4 5 6 7
|
||||
./$(NAME) 10 2000 500 200 300 5 50 1
|
||||
|
||||
clean:
|
||||
rm -f $(OBJS)
|
||||
|
|
|
|||
|
|
@ -12,8 +12,10 @@
|
|||
|
||||
#ifndef CODEXION_H
|
||||
# define CODEXION_H
|
||||
# include <pthread.h>
|
||||
# include <sys/time.h>
|
||||
|
||||
typedef struct s_data
|
||||
typedef struct s_args
|
||||
{
|
||||
int number_of_coders;
|
||||
int time_to_burnout;
|
||||
|
|
@ -23,9 +25,17 @@ typedef struct s_data
|
|||
int number_of_compiles_required;
|
||||
int dongle_cooldown;
|
||||
int scheduler;
|
||||
} t_args;
|
||||
|
||||
typedef struct s_data
|
||||
{
|
||||
struct timeval start;
|
||||
int coder_id;
|
||||
t_args args;
|
||||
pthread_mutex_t *left_dongle;
|
||||
pthread_mutex_t *right_dongle;
|
||||
} t_data;
|
||||
|
||||
// utils.h
|
||||
t_data *parse_arguments(int count, char **args);
|
||||
t_args *parse_arguments(int count, char **args);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -11,25 +11,93 @@
|
|||
/* ************************************************************************** */
|
||||
|
||||
#include "codexion.h"
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
void compile_op(t_data *data)
|
||||
{
|
||||
struct timeval now;
|
||||
int diff;
|
||||
|
||||
gettimeofday(&now, NULL);
|
||||
diff = (now.tv_sec - data->start.tv_sec) * 1000L + (now.tv_usec
|
||||
- data->start.tv_usec) / 1000;
|
||||
printf("%d %d is compiling\n", diff, data->coder_id);
|
||||
usleep(data->args.time_to_compile * 1000L);
|
||||
}
|
||||
|
||||
void debug_op(t_data *data)
|
||||
{
|
||||
struct timeval now;
|
||||
int diff;
|
||||
|
||||
gettimeofday(&now, NULL);
|
||||
diff = (now.tv_sec - data->start.tv_sec) * 1000L + (now.tv_usec
|
||||
- data->start.tv_usec) / 1000;
|
||||
printf("%d %d is debugging\n", diff, data->coder_id);
|
||||
usleep(data->args.time_to_debug * 1000L);
|
||||
}
|
||||
|
||||
void refactor_op(t_data *data)
|
||||
{
|
||||
struct timeval now;
|
||||
int diff;
|
||||
|
||||
gettimeofday(&now, NULL);
|
||||
diff = (now.tv_sec - data->start.tv_sec) * 1000L + (now.tv_usec
|
||||
- data->start.tv_usec) / 1000;
|
||||
printf("%d %d is refactoring\n", diff, data->coder_id);
|
||||
usleep(data->args.time_to_refactor * 1000L);
|
||||
}
|
||||
|
||||
void *run(void *param)
|
||||
{
|
||||
int i;
|
||||
t_data *data;
|
||||
|
||||
data = param;
|
||||
i = 0;
|
||||
while (i < 5)
|
||||
{
|
||||
compile_op(data);
|
||||
debug_op(data);
|
||||
refactor_op(data);
|
||||
i++;
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
t_args *args;
|
||||
pthread_t *coders;
|
||||
pthread_mutex_t *dongles;
|
||||
int i;
|
||||
struct timeval start;
|
||||
t_data *data;
|
||||
|
||||
data = parse_arguments(argc - 1, &argv[1]);
|
||||
if (!data)
|
||||
return (0);
|
||||
printf("d = [%d]\n", data->number_of_coders);
|
||||
printf("d = [%d]\n", data->time_to_burnout);
|
||||
printf("d = [%d]\n", data->time_to_compile);
|
||||
printf("d = [%d]\n", data->time_to_debug);
|
||||
printf("d = [%d]\n", data->time_to_refactor);
|
||||
printf("d = [%d]\n", data->number_of_compiles_required);
|
||||
printf("d = [%d]\n", data->dongle_cooldown);
|
||||
printf("d = [%d]\n", data->scheduler);
|
||||
if (data)
|
||||
free(data);
|
||||
data = NULL;
|
||||
args = parse_arguments(argc - 1, &argv[1]);
|
||||
coders = malloc(sizeof(pthread_t) * args->number_of_coders);
|
||||
dongles = malloc(sizeof(pthread_mutex_t) * args->number_of_coders);
|
||||
data = malloc(sizeof(t_data) * args->number_of_coders);
|
||||
i = 0;
|
||||
gettimeofday(&start, NULL);
|
||||
while (i < args->number_of_coders)
|
||||
{
|
||||
data[i].args = *args;
|
||||
data[i].coder_id = i + 1;
|
||||
data[i].start = start;
|
||||
pthread_create(&coders[i], NULL, run, (void *)&data[i]);
|
||||
pthread_mutex_init(&dongles[i], NULL);
|
||||
i++;
|
||||
}
|
||||
i = 0;
|
||||
while (i < args->number_of_coders)
|
||||
{
|
||||
pthread_join(coders[i], NULL);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,15 +26,15 @@ int isnumeric(char *str)
|
|||
return (1);
|
||||
}
|
||||
|
||||
t_data *parse_arguments(int count, char **args)
|
||||
t_args *parse_arguments(int count, char **args)
|
||||
{
|
||||
int args_int[8];
|
||||
t_data *data;
|
||||
t_args *data;
|
||||
int i;
|
||||
|
||||
if (count != 8)
|
||||
return (NULL);
|
||||
data = malloc(sizeof(t_data));
|
||||
data = malloc(sizeof(t_args));
|
||||
if (!data)
|
||||
return (NULL);
|
||||
i = -1;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue