aboutsummaryrefslogtreecommitdiff
path: root/pa4_shared_array/test.c
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 /pa4_shared_array/test.c
parenteea3d410e709ebac4351f58487ed3c9f49631c44 (diff)
downloadcsci3753-6e6f4151b7f709fbaf62e40479c700a81436512f.tar.gz
csci3753-6e6f4151b7f709fbaf62e40479c700a81436512f.zip
pa4: improvements
Diffstat (limited to 'pa4_shared_array/test.c')
-rw-r--r--pa4_shared_array/test.c77
1 files changed, 25 insertions, 52 deletions
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;
}