-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic-reader-writer.c
65 lines (53 loc) · 1.45 KB
/
basic-reader-writer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <semaphore.h>
// When building, you must link with the external pthread library: for example, 'gcc basic-reader-writer.c -lpthread'
// What the threads do is based on the contents of file-system.fun.c
#define MAX_LEN 1024
pthread_rwlock_t rwlock;
void init() {
pthread_rwlock_init(&rwlock, NULL);
}
void cleanup() {
pthread_rwlock_destroy(&rwlock);
}
void write_data(FILE* f) {
fprintf(f, "\nYAY IT WORKED\n");
}
void read_data(FILE* f) {
char token[MAX_LEN + 1];
while (fscanf(f, " %s", &token[0]) == 1) { // the space in front of %s is important
printf("%s\n", token);
}
}
void* writer(void* arg) {
pthread_rwlock_wrlock(&rwlock);
write_data((FILE*) arg);
pthread_rwlock_unlock(&rwlock);
pthread_exit(NULL);
}
void* reader(void* arg) {
pthread_rwlock_rdlock(&rwlock);
read_data((FILE*) arg);
pthread_rwlock_unlock(&rwlock);
pthread_exit(NULL);
}
int main(int argc, char** argv) {
FILE* my_file = fopen(argv[1], "r+");
if (my_file == NULL) {
printf("Unable to open file! Is %s a valid name?", argv[1]);
return -1;
}
pthread_t read;
pthread_t write;
init();
pthread_create(&read, NULL, reader, my_file);
pthread_create(&write, NULL, writer, my_file);
pthread_join(read, NULL);
pthread_join(write, NULL);
cleanup();
fclose(my_file);
pthread_exit(0);
}