Skip to content

Commit 1946bbd

Browse files
committed
Making these thread programs pointy
1 parent a3deb7b commit 1946bbd

File tree

3 files changed

+70
-44
lines changed

3 files changed

+70
-44
lines changed

network/http.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ static void serve_file(FILE *stream, struct stat *stat, char *filename) {
105105
}
106106

107107
static void serve_directory(int cfd, char *filename) {
108-
char buf[BBUFSIZE];
108+
//char buf[BBUFSIZE];
109+
char *buf;
110+
buf = (char *)malloc((sizeof(char) * BBUFSIZE));
109111

110112
sprintf(buf, "HTTP/1.1 200 OK\r\n%s%s%s%s%s%s%s%s",
111113
"Server: My Web Server\r\n",
@@ -124,9 +126,13 @@ static void serve_directory(int cfd, char *filename) {
124126
DIR *d;
125127

126128
if ((fd = open(filename, O_RDONLY, 0)) < 0) {
129+
free(buf);
130+
buf = NULL;
127131
error("Could not open directory\n");
128132
}
129133
if (write(cfd, buf, strlen(buf)) < 0) {
134+
free(buf);
135+
buf = NULL;
130136
error("Could not write to buffer\n");
131137
}
132138
d = fdopendir(fd);
@@ -154,6 +160,9 @@ static void serve_directory(int cfd, char *filename) {
154160
}
155161
sprintf(buf, "</tbody></table></body></html>");
156162
write(cfd, buf, strlen(buf));
163+
//
164+
free(buf);
165+
buf = NULL;
157166

158167
close(fd);
159168
closedir(d);

thread/condsignal.c

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,48 @@ int done = 0;
1414

1515
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
1616
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
17-
void *entry(void *id);
17+
18+
static void *entry(void *id) {
19+
const int realId = (long)id;
20+
int i;
21+
22+
for (i = 0; i < 5; i++) {
23+
printf("Thread %d: working %d / 5\n", realId, i);
24+
sleep(1);
25+
}
26+
27+
/* acquire mutex from main(), then release back to main() after
28+
* condition is signalled */
29+
if (pthread_mutex_lock(&lock) != 0) {
30+
fprintf(stderr, "Thread %d could not lock mutex, error %d\n", realId, errno);
31+
abort();
32+
}
33+
34+
/* thread is finished */
35+
done++;
36+
printf("Thread %d is done. %d threads done. Signalling...\n", realId, done);
37+
38+
/* wake the sleeping main thread */
39+
if (pthread_cond_signal(&cond) != 0) {
40+
fprintf(stderr, "Thread %d could not wake sleeping main, error %d\n", realId, errno);
41+
abort();
42+
}
43+
44+
if (pthread_mutex_unlock(&lock) != 0) {
45+
fprintf(stderr, "Thread %d could not unlock mutex, error %d\n", realId, errno);
46+
abort();
47+
}
48+
49+
pthread_exit(NULL);
50+
return NULL;
51+
}
1852

1953
int main(void) {
54+
int t;
2055
printf("Main thread starting\n");
2156
pthread_t *threads = (pthread_t *)malloc(sizeof(pthread_t) * THREADS);
22-
for (int t = 0; t < THREADS; t++)
57+
58+
for (t = 0; t < THREADS; t++)
2359
pthread_create(&threads[t], NULL, entry, (void *)(long)t);
2460

2561
if (pthread_mutex_init(&lock, NULL) != 0) {
@@ -52,41 +88,10 @@ int main(void) {
5288
free(threads);
5389
return 1;
5490
}
91+
92+
pthread_exit(NULL);
5593
pthread_mutex_destroy(&lock);
5694
free(threads);
5795
return 0;
5896
}
5997

60-
void *entry(void *id) {
61-
const int realId = (long)id;
62-
63-
for (int i = 0; i < 5; i++) {
64-
printf("Thread %d: working %d / 5\n", realId, i);
65-
sleep(1);
66-
}
67-
68-
/* acquire mutex from main(), then release back to main() after
69-
* condition is signalled */
70-
if (pthread_mutex_lock(&lock) != 0) {
71-
fprintf(stderr, "Thread %d could not lock mutex, error %d\n", realId, errno);
72-
abort();
73-
}
74-
75-
/* thread is finished */
76-
done++;
77-
printf("Thread %d is done. %d threads done. Signalling...\n", realId, done);
78-
79-
/* wake the sleeping main thread */
80-
if (pthread_cond_signal(&cond) != 0) {
81-
fprintf(stderr, "Thread %d could not wake sleeping main, error %d\n", realId, errno);
82-
abort();
83-
}
84-
85-
if (pthread_mutex_unlock(&lock) != 0) {
86-
fprintf(stderr, "Thread %d could not unlock mutex, error %d\n", realId, errno);
87-
abort();
88-
}
89-
90-
return NULL;
91-
}
92-

thread/mutex.c

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,52 @@
66
#include <stdlib.h>
77
#include <string.h>
88
#include <unistd.h>
9+
#define THREADS 4
910

10-
pthread_t tid[4];
1111
int counter;
12-
pthread_mutex_t lock;
12+
pthread_mutex_t *lock;
1313

14-
void *handler(void *arg) {
15-
pthread_mutex_lock(&lock);
14+
static void *handler(void *arg) {
15+
pthread_mutex_lock(lock);
1616
counter++;
17+
printf("Arg = %s\n", arg);
1718
printf("Job %d has started\n", counter);
1819
sleep(2);
1920
printf("Job %d has finished\n", counter);
20-
pthread_mutex_unlock(&lock);
21+
pthread_mutex_unlock(lock);
2122
return NULL;
2223
}
2324

2425
int main(void) {
2526
int i, error;
2627

27-
if (pthread_mutex_init(&lock, NULL) != 0) {
28+
pthread_t *tid = (pthread_t *)malloc((sizeof(pthread_t) * THREADS));
29+
lock = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t));
30+
31+
if (pthread_mutex_init(lock, NULL) != 0) {
2832
fprintf(stderr, "Cannot init mutex\n");
2933
return 1;
3034
}
3135

3236
for (i = 0; i < 4; i++) {
33-
error = pthread_create(&(tid[i]), NULL, &handler, NULL);
37+
error = pthread_create(&tid[i], NULL, &handler, "hello");
3438
if (error != 0) {
3539
fprintf(stderr, "Thread can't be created: [%s]\n", strerror(error));
3640
}
3741
}
42+
if (error != 0) {
43+
free(lock);
44+
free(tid);
45+
return 1;
46+
}
47+
3848
for (i = 0; i < 4; i++) {
3949
pthread_join(tid[i], NULL);
4050
}
4151

42-
pthread_mutex_destroy(&lock);
52+
pthread_mutex_destroy(lock);
53+
free(lock);
54+
free(tid);
4355

4456
return 0;
4557
}

0 commit comments

Comments
 (0)