all ops done; without synchronization

This commit is contained in:
Semere M. Mebrahtom 2026-03-24 20:16:27 +01:00
parent 30ad265779
commit ebe57f783d
Signed by: semere
GPG key ID: 7FC937214DF30488
4 changed files with 117 additions and 31 deletions

View file

@ -6,7 +6,7 @@ INCLUDES = coders/codexion.h
OBJS = $(SRCS:.c=.o) OBJS = $(SRCS:.c=.o)
CC = cc CC = cc
CFLAGS = -Wall -Wextra -Werror CFLAGS = -Wall -Wextra -Werror -pthread
all: $(NAME) all: $(NAME)
@ -16,8 +16,16 @@ $(NAME): $(OBJS)
%.o: %.c %.o: %.c
$(CC) $(CFLAGS) -c -o $@ $< $(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) test: $(NAME)
./$(NAME) 0 1 2 3 4 5 6 7 ./$(NAME) 10 2000 500 200 300 5 50 1
clean: clean:
rm -f $(OBJS) rm -f $(OBJS)

View file

@ -12,20 +12,30 @@
#ifndef CODEXION_H #ifndef CODEXION_H
# define CODEXION_H # define CODEXION_H
# include <pthread.h>
# include <sys/time.h>
typedef struct s_args
{
int number_of_coders;
int time_to_burnout;
int time_to_compile;
int time_to_debug;
int time_to_refactor;
int number_of_compiles_required;
int dongle_cooldown;
int scheduler;
} t_args;
typedef struct s_data typedef struct s_data
{ {
int number_of_coders; struct timeval start;
int time_to_burnout; int coder_id;
int time_to_compile; t_args args;
int time_to_debug; pthread_mutex_t *left_dongle;
int time_to_refactor; pthread_mutex_t *right_dongle;
int number_of_compiles_required; } t_data;
int dongle_cooldown;
int scheduler;
} t_data;
// utils.h t_args *parse_arguments(int count, char **args);
t_data *parse_arguments(int count, char **args);
#endif #endif

View file

@ -11,25 +11,93 @@
/* ************************************************************************** */ /* ************************************************************************** */
#include "codexion.h" #include "codexion.h"
#include <pthread.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.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) int main(int argc, char **argv)
{ {
t_data *data; 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]); args = parse_arguments(argc - 1, &argv[1]);
if (!data) coders = malloc(sizeof(pthread_t) * args->number_of_coders);
return (0); dongles = malloc(sizeof(pthread_mutex_t) * args->number_of_coders);
printf("d = [%d]\n", data->number_of_coders); data = malloc(sizeof(t_data) * args->number_of_coders);
printf("d = [%d]\n", data->time_to_burnout); i = 0;
printf("d = [%d]\n", data->time_to_compile); gettimeofday(&start, NULL);
printf("d = [%d]\n", data->time_to_debug); while (i < args->number_of_coders)
printf("d = [%d]\n", data->time_to_refactor); {
printf("d = [%d]\n", data->number_of_compiles_required); data[i].args = *args;
printf("d = [%d]\n", data->dongle_cooldown); data[i].coder_id = i + 1;
printf("d = [%d]\n", data->scheduler); data[i].start = start;
if (data) pthread_create(&coders[i], NULL, run, (void *)&data[i]);
free(data); pthread_mutex_init(&dongles[i], NULL);
data = NULL; i++;
}
i = 0;
while (i < args->number_of_coders)
{
pthread_join(coders[i], NULL);
i++;
}
} }

View file

@ -26,15 +26,15 @@ int isnumeric(char *str)
return (1); return (1);
} }
t_data *parse_arguments(int count, char **args) t_args *parse_arguments(int count, char **args)
{ {
int args_int[8]; int args_int[8];
t_data *data; t_args *data;
int i; int i;
if (count != 8) if (count != 8)
return (NULL); return (NULL);
data = malloc(sizeof(t_data)); data = malloc(sizeof(t_args));
if (!data) if (!data)
return (NULL); return (NULL);
i = -1; i = -1;