Skip to content

Commit

Permalink
Making these thread programs pointy
Browse files Browse the repository at this point in the history
  • Loading branch information
Babkock committed Jul 12, 2024
1 parent a3deb7b commit 1946bbd
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 44 deletions.
11 changes: 10 additions & 1 deletion network/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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);
Expand Down Expand Up @@ -154,6 +160,9 @@ static void serve_directory(int cfd, char *filename) {
}
sprintf(buf, "</tbody></table></body></html>");
write(cfd, buf, strlen(buf));
//
free(buf);
buf = NULL;

close(fd);
closedir(d);
Expand Down
75 changes: 40 additions & 35 deletions thread/condsignal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}

28 changes: 20 additions & 8 deletions thread/mutex.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,52 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#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;
}
Expand Down

0 comments on commit 1946bbd

Please sign in to comment.