147 lines
No EOL
4.2 KiB
C
147 lines
No EOL
4.2 KiB
C
#include <errno.h>
|
|
#include <pthread.h>
|
|
#include <signal.h>
|
|
#include <stdbool.h>
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include "adder.h"
|
|
#include "mailbox.h"
|
|
#include "main.h"
|
|
|
|
/**
|
|
* Parse a line of input into an input struct.
|
|
*
|
|
* @param input_line A line of input.
|
|
* @param res An already allocated input struct to store the result in.
|
|
*
|
|
* @return int -1 if the input is malformed, and 0 otherwise.
|
|
*/
|
|
static int parse_input_line(char *input_line, struct input *res) {
|
|
// if tmp has anything in it, then we know that extra values have been entered.
|
|
char tmp[2];
|
|
int num_matched = sscanf(input_line, " %d %d %1s", &(res->value), &(res->recipient), tmp);
|
|
|
|
return num_matched == 2 ? 0 : -1;
|
|
}
|
|
|
|
/**
|
|
* Convert the given input to an array of input structs.
|
|
*
|
|
* @param num_threads The number of threads that the program has been spawned with.
|
|
* @param res A pointer that will be allocated as an array of inputs. Must be freed by the caller.
|
|
*
|
|
* @return int the number of input lines that were read.
|
|
*/
|
|
static int get_input(int num_threads, struct input **res) {
|
|
int input_index = 0;
|
|
int current_buffer_size = BUFFER_SIZE;
|
|
char *input_buffer = malloc(current_buffer_size);
|
|
// Keep reaidng until we hit an EOF
|
|
while (true) {
|
|
char *read_result;
|
|
while((read_result = fgets(input_buffer, current_buffer_size, stdin))) {
|
|
int read_size = strlen(input_buffer);
|
|
if (input_buffer[read_size - 1] == '\n') {
|
|
break;
|
|
}
|
|
|
|
// Grow our buffer if we nede to read more input.
|
|
current_buffer_size *= 2;
|
|
input_buffer = realloc(input_buffer, current_buffer_size);
|
|
}
|
|
if (read_result == NULL) {
|
|
break;
|
|
}
|
|
|
|
struct input current_input;
|
|
int parse_result = parse_input_line(input_buffer, ¤t_input);
|
|
if (parse_result == -1) {
|
|
// On bad input, we should assume an EOF and stop.
|
|
break;
|
|
} else if (current_input.recipient >= num_threads || current_input.recipient < 0) {
|
|
printf("Invalid recipient id.\n");
|
|
continue;
|
|
}
|
|
|
|
*res = realloc(*res, sizeof(struct input));
|
|
memcpy(*res + input_index, ¤t_input, sizeof(struct input));
|
|
input_index++;
|
|
}
|
|
|
|
free(input_buffer);
|
|
|
|
return input_index;
|
|
}
|
|
|
|
/**
|
|
* Start the adder threads
|
|
*
|
|
* @param thread_ids An array of all of the thread ids to spawn, in order.
|
|
* @param num_threads The number of threads to spawn
|
|
* @param threads A pre-allocated array to hold the spawned threads.
|
|
*
|
|
* @return int -1 on error, 0 otherwise.
|
|
*/
|
|
static int start_adder_threads(int *thread_ids, int num_threads, pthread_t *threads) {
|
|
for (int i = 0; i < num_threads; i++) {
|
|
int create_result = pthread_create(&threads[i], NULL, adder, &thread_ids[i]);
|
|
if (create_result != 0) {
|
|
// Clean up all of the threads that have started if we have an error.
|
|
for (int j = 0; j < i; j++) {
|
|
pthread_kill(threads[i], SIGKILL);
|
|
}
|
|
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int main(int argc, char *argv[]) {
|
|
struct input *inputs = NULL;
|
|
if (argc < 2 || argc > 3) {
|
|
printf("%s\n", USAGE_STRING);
|
|
return -1;
|
|
} else if (argc == 3) {
|
|
printf("Not yet implemented.");
|
|
return -1;
|
|
}
|
|
|
|
int num_threads = strtol(argv[1], NULL, 10);
|
|
if (errno == ERANGE || (num_threads == 0 && errno == EINVAL)) {
|
|
printf("Invalid number of threads.\n");
|
|
}
|
|
num_threads = num_threads > MAXTHREAD ? MAXTHREAD : num_threads;
|
|
|
|
// TODO: Remove this. It's for debugging.
|
|
int num_inputs = get_input(num_threads, &inputs);
|
|
for (int i = 0; i < num_inputs; i++) {
|
|
printf("#%d recipient=%d value=%d\n", i, inputs[i].recipient, inputs[i].value);
|
|
}
|
|
free(inputs);
|
|
|
|
int init_result = init_mailboxes(num_threads);
|
|
if (init_result != 0) {
|
|
printf("Failed to setup mailboxes.\n");
|
|
return 1;
|
|
}
|
|
|
|
// Must allocate an array of the ids to prevent a race condition when adder starts
|
|
// We don't want the thread id to be deallocated.
|
|
// We don't want to allocate an id for thread id 0, so we store all of the others.
|
|
int mailbox_ids[num_threads];
|
|
for (int i = 0; i < num_threads; i++) {
|
|
mailbox_ids[i] = i + 1;
|
|
}
|
|
pthread_t threads[num_threads];
|
|
int start_result = start_adder_threads(mailbox_ids, num_threads, threads);
|
|
if (start_result != 0) {
|
|
printf("Failed to start adder threads. (%s)\n", strerror(errno));
|
|
free_mailboxes();
|
|
return 1;
|
|
}
|
|
|
|
free_mailboxes();
|
|
} |