From 1946bbd35dfaa4da9321380d3a6c126dd5fe0f7b Mon Sep 17 00:00:00 2001 From: Tanner Babcock Date: Fri, 12 Jul 2024 12:04:19 -0500 Subject: [PATCH] Making these thread programs pointy --- network/http.c | 11 ++++++- thread/condsignal.c | 75 ++++++++++++++++++++++++--------------------- thread/mutex.c | 28 ++++++++++++----- 3 files changed, 70 insertions(+), 44 deletions(-) diff --git a/network/http.c b/network/http.c index 2342785..b27255e 100644 --- a/network/http.c +++ b/network/http.c @@ -105,7 +105,9 @@ static void serve_file(FILE *stream, struct stat *stat, char *filename) { } static void serve_directory(int cfd, char *filename) { - char buf[BBUFSIZE]; + //char buf[BBUFSIZE]; + char *buf; + buf = (char *)malloc((sizeof(char) * BBUFSIZE)); sprintf(buf, "HTTP/1.1 200 OK\r\n%s%s%s%s%s%s%s%s", "Server: My Web Server\r\n", @@ -124,9 +126,13 @@ static void serve_directory(int cfd, char *filename) { DIR *d; if ((fd = open(filename, O_RDONLY, 0)) < 0) { + free(buf); + buf = NULL; error("Could not open directory\n"); } if (write(cfd, buf, strlen(buf)) < 0) { + free(buf); + buf = NULL; error("Could not write to buffer\n"); } d = fdopendir(fd); @@ -154,6 +160,9 @@ static void serve_directory(int cfd, char *filename) { } sprintf(buf, ""); write(cfd, buf, strlen(buf)); + // + free(buf); + buf = NULL; close(fd); closedir(d); diff --git a/thread/condsignal.c b/thread/condsignal.c index 78f0879..97fb994 100644 --- a/thread/condsignal.c +++ b/thread/condsignal.c @@ -14,12 +14,48 @@ int done = 0; pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond = PTHREAD_COND_INITIALIZER; -void *entry(void *id); + +static void *entry(void *id) { + const int realId = (long)id; + int i; + + for (i = 0; i < 5; i++) { + printf("Thread %d: working %d / 5\n", realId, i); + sleep(1); + } + + /* acquire mutex from main(), then release back to main() after + * condition is signalled */ + if (pthread_mutex_lock(&lock) != 0) { + fprintf(stderr, "Thread %d could not lock mutex, error %d\n", realId, errno); + abort(); + } + + /* thread is finished */ + done++; + printf("Thread %d is done. %d threads done. Signalling...\n", realId, done); + + /* wake the sleeping main thread */ + if (pthread_cond_signal(&cond) != 0) { + fprintf(stderr, "Thread %d could not wake sleeping main, error %d\n", realId, errno); + abort(); + } + + if (pthread_mutex_unlock(&lock) != 0) { + fprintf(stderr, "Thread %d could not unlock mutex, error %d\n", realId, errno); + abort(); + } + + pthread_exit(NULL); + return NULL; +} int main(void) { + int t; printf("Main thread starting\n"); pthread_t *threads = (pthread_t *)malloc(sizeof(pthread_t) * THREADS); - for (int t = 0; t < THREADS; t++) + + for (t = 0; t < THREADS; t++) pthread_create(&threads[t], NULL, entry, (void *)(long)t); if (pthread_mutex_init(&lock, NULL) != 0) { @@ -52,41 +88,10 @@ int main(void) { free(threads); return 1; } + + pthread_exit(NULL); pthread_mutex_destroy(&lock); free(threads); return 0; } -void *entry(void *id) { - const int realId = (long)id; - - for (int i = 0; i < 5; i++) { - printf("Thread %d: working %d / 5\n", realId, i); - sleep(1); - } - - /* acquire mutex from main(), then release back to main() after - * condition is signalled */ - if (pthread_mutex_lock(&lock) != 0) { - fprintf(stderr, "Thread %d could not lock mutex, error %d\n", realId, errno); - abort(); - } - - /* thread is finished */ - done++; - printf("Thread %d is done. %d threads done. Signalling...\n", realId, done); - - /* wake the sleeping main thread */ - if (pthread_cond_signal(&cond) != 0) { - fprintf(stderr, "Thread %d could not wake sleeping main, error %d\n", realId, errno); - abort(); - } - - if (pthread_mutex_unlock(&lock) != 0) { - fprintf(stderr, "Thread %d could not unlock mutex, error %d\n", realId, errno); - abort(); - } - - return NULL; -} - diff --git a/thread/mutex.c b/thread/mutex.c index 5be0526..2c07c4b 100644 --- a/thread/mutex.c +++ b/thread/mutex.c @@ -6,40 +6,52 @@ #include #include #include +#define THREADS 4 -pthread_t tid[4]; int counter; -pthread_mutex_t lock; +pthread_mutex_t *lock; -void *handler(void *arg) { - pthread_mutex_lock(&lock); +static void *handler(void *arg) { + pthread_mutex_lock(lock); counter++; + printf("Arg = %s\n", arg); printf("Job %d has started\n", counter); sleep(2); printf("Job %d has finished\n", counter); - pthread_mutex_unlock(&lock); + pthread_mutex_unlock(lock); return NULL; } int main(void) { int i, error; - if (pthread_mutex_init(&lock, NULL) != 0) { + pthread_t *tid = (pthread_t *)malloc((sizeof(pthread_t) * THREADS)); + lock = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t)); + + if (pthread_mutex_init(lock, NULL) != 0) { fprintf(stderr, "Cannot init mutex\n"); return 1; } for (i = 0; i < 4; i++) { - error = pthread_create(&(tid[i]), NULL, &handler, NULL); + error = pthread_create(&tid[i], NULL, &handler, "hello"); if (error != 0) { fprintf(stderr, "Thread can't be created: [%s]\n", strerror(error)); } } + if (error != 0) { + free(lock); + free(tid); + return 1; + } + for (i = 0; i < 4; i++) { pthread_join(tid[i], NULL); } - pthread_mutex_destroy(&lock); + pthread_mutex_destroy(lock); + free(lock); + free(tid); return 0; }