aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2024-10-03 11:45:34 -0600
committerJack Sangdahl <[email protected]>2024-10-03 11:45:34 -0600
commit6e6f4151b7f709fbaf62e40479c700a81436512f (patch)
treed7bc4fd81a5ab9d163e6e94bb37058c6130eebd2
parenteea3d410e709ebac4351f58487ed3c9f49631c44 (diff)
downloadcsci3753-6e6f4151b7f709fbaf62e40479c700a81436512f.tar.gz
csci3753-6e6f4151b7f709fbaf62e40479c700a81436512f.zip
pa4: improvements
-rw-r--r--pa4_shared_array/array.c39
-rw-r--r--pa4_shared_array/array.h39
-rw-r--r--pa4_shared_array/test.c77
3 files changed, 40 insertions, 115 deletions
diff --git a/pa4_shared_array/array.c b/pa4_shared_array/array.c
index 3a44566..dfda1b7 100644
--- a/pa4_shared_array/array.c
+++ b/pa4_shared_array/array.c
@@ -5,51 +5,32 @@
int array_init(array *s) {
s->top = 0;
- s->array = (char **)malloc(ARRAY_SIZE * sizeof(char *));
- for (int i = 0; i < ARRAY_SIZE; i++)
- s->array[i] = (char *)malloc(MAX_NAME_LENGTH * sizeof(char));
sem_init(&s->mutex, 0, 1);
- sem_init(&s->space, 0, ARRAY_SIZE);
- sem_init(&s->items, 0, 0);
+ sem_init(&s->avail, 0, ARRAY_SIZE);
+ sem_init(&s->empty, 0, 0);
return 0;
}
int array_put(array *s, char *hostname) {
- if (s->top == ARRAY_SIZE - 1) {
- printf("ERROR: stack full\n");
- return -1;
- }
- if (strlen(hostname) > MAX_NAME_LENGTH) {
- printf("ERROR: %s size > %d\n", hostname, MAX_NAME_LENGTH);
- return -1;
- }
- sem_wait(&s->space);
+ sem_wait(&s->avail);
sem_wait(&s->mutex);
- strcpy(s->array[s->top++], hostname);
+ s->data[s->top++] = hostname;
sem_post(&s->mutex);
- sem_post(&s->items);
+ sem_post(&s->empty);
return 0;
}
int array_get(array *s, char **hostname) {
- if (!s->top) {
- printf("ERROR: stack empty\n");
- return -1;
- }
- sem_wait(&s->items);
+ sem_wait(&s->empty);
sem_wait(&s->mutex);
- *hostname = (char *)malloc(MAX_NAME_LENGTH * sizeof(char));
- strcpy(*hostname, s->array[--s->top]);
+ *hostname = s->data[--s->top];
sem_post(&s->mutex);
- sem_post(&s->space);
+ sem_post(&s->avail);
return 0;
}
void array_free(array *s) {
- for (int i = 0; i < ARRAY_SIZE; i++)
- free(s->array[i]);
- free(s->array);
sem_destroy(&s->mutex);
- sem_destroy(&s->space);
- sem_destroy(&s->items);
+ sem_destroy(&s->avail);
+ sem_destroy(&s->empty);
}
diff --git a/pa4_shared_array/array.h b/pa4_shared_array/array.h
index 9d67e1e..3522530 100644
--- a/pa4_shared_array/array.h
+++ b/pa4_shared_array/array.h
@@ -2,47 +2,18 @@
// CSCI3753 SP24
// PA4 Shared Array
#include <semaphore.h>
-#include <stdint.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#define ARRAY_SIZE 8 // number of hostnames in data/ is 618
-#define MAX_NAME_LENGTH 16 // longest string length in data/
+#define ARRAY_SIZE 8
typedef struct {
- char **array;
- uint8_t top;
+ void *data[ARRAY_SIZE];
+ int top;
sem_t mutex;
- sem_t space;
- sem_t items;
+ sem_t avail;
+ sem_t empty;
} array;
-/**
- * @brief initialise the array
- * @param s
- * @return int
- */
int array_init(array *s);
-
-/**
- * @brief place element into array. block when full
- * @param s
- * @param hostname
- * @return int
- */
int array_put(array *s, char *hostname);
-
-/**
- * @brief remove element from the array. block when empty
- * @param s
- * @param hostname
- * @return int
- */
int array_get(array *s, char **hostname);
-
-/**
- * @brief free array's resources
- * @param s
- */
void array_free(array *s);
diff --git a/pa4_shared_array/test.c b/pa4_shared_array/test.c
index 7333a16..14c0a3c 100644
--- a/pa4_shared_array/test.c
+++ b/pa4_shared_array/test.c
@@ -2,76 +2,49 @@
// CSCI3753 SP24
// PA4 Shared Array
#include "array.h"
-#include <dirent.h>
#include <pthread.h>
#include <stdio.h>
-#define MAX_FILE_SIZE 255
-#define MAX_FILE_COUNT 30
+#include <stdlib.h>
#define NUM_THREADS 10
+#define HOSTNAME_CNT 6
array stack;
-char *data, **hostnames;
-
-void gen_hostnames(char **hostnames) {
- DIR *dptr = opendir("data");
- struct dirent *entry;
- char filenames[MAX_FILE_COUNT][MAX_FILE_SIZE];
- uint8_t file_count = 0;
- while ((entry = readdir(dptr)))
- if (strstr(entry->d_name, ".txt")) {
- strncpy(filenames[file_count], entry->d_name, MAX_FILE_SIZE);
- file_count++;
- if (file_count >= MAX_FILE_COUNT)
- break;
- }
- closedir(dptr);
- for (uint8_t i = 0; i < MAX_FILE_COUNT; i++) {
- char *curr_file = (char *)malloc(strlen(filenames[i] + 6));
- strcpy(curr_file, "data/");
- strcat(curr_file, filenames[i]);
- FILE *fptr = fopen(curr_file, "r");
- char *buffer = (char *)malloc(sizeof(char) * MAX_NAME_LENGTH);
- uint8_t hostname_count = 0;
- while (fgets(buffer, sizeof(buffer), fptr)) {
- hostnames[hostname_count] =
- (char *)malloc((strlen(buffer)) * sizeof(char));
- strcpy(hostnames[hostname_count], buffer);
- hostname_count++;
- }
- free(buffer), free(curr_file);
- fclose(fptr);
- }
-}
+static char *data;
+static char *hostnames[] = {"facebook.com", "youtube.com", "yahoo.com",
+ "live.com", "wikipedia.org", "msn.com"};
void *thread_produce(void *a) {
- for (uint8_t i = 0; i < ARRAY_SIZE; i++)
+ for (int i = 0; i < HOSTNAME_CNT; i++) {
array_put(&stack, hostnames[i]);
+ printf("PUSHED: %s\n", hostnames[i]);
+ }
return NULL;
}
void *thread_consume(void *a) {
- for (uint8_t i = 0; i < ARRAY_SIZE; i++)
+ for (int i = 0; i < HOSTNAME_CNT; i++) {
array_get(&stack, &data);
+ printf("POPPED: %s\n", data);
+ }
return NULL;
}
-int main(int argc, char **argv) {
- pthread_t *produce = malloc(NUM_THREADS * sizeof(pthread_t));
- pthread_t *consume = malloc(NUM_THREADS * sizeof(pthread_t));
- hostnames = (char **)malloc(sizeof(char *) * MAX_FILE_SIZE);
- gen_hostnames(hostnames);
- for (int i = 0; i < ARRAY_SIZE; i++)
- printf("%s\n", hostnames[i]);
+int main(void) {
+ pthread_t *produce_threads =
+ (pthread_t *)malloc(NUM_THREADS * sizeof(pthread_t));
+ pthread_t *consume_threads =
+ (pthread_t *)malloc(NUM_THREADS * sizeof(pthread_t));
array_init(&stack);
- for (uint8_t i = 0; i < NUM_THREADS; i++) {
- pthread_create(&produce[i], NULL, thread_produce, &stack);
- pthread_create(&consume[i], NULL, thread_consume, &stack);
+ for (int i = 0; i < NUM_THREADS; i++) {
+ pthread_create(&produce_threads[i], NULL, thread_produce, NULL);
+ pthread_create(&consume_threads[i], NULL, thread_consume, NULL);
}
- for (uint8_t i = 0; i < NUM_THREADS; i++) {
- pthread_join(produce[i], NULL);
- pthread_join(consume[i], NULL);
+ for (int i = 0; i < NUM_THREADS; i++) {
+ pthread_join(produce_threads[i], NULL);
+ pthread_join(consume_threads[i], NULL);
}
- free(produce), free(consume), free(hostnames);
+ free(produce_threads);
+ free(consume_threads);
array_free(&stack);
- return 0;
+ return EXIT_SUCCESS;
}