-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paththreadread.c
More file actions
88 lines (72 loc) · 1.7 KB
/
threadread.c
File metadata and controls
88 lines (72 loc) · 1.7 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define NUM_THREADS 10
#define FILE_PATH "/proc/lfprng"
#define BUF_SIZE 128
int read;
pthread_cond_t cond_read;
pthread_mutex_t mutex_read;
void *thread_func(void *ptr)
{
FILE *fp;
long id = *((long *) ptr);
int i;
char buf[BUF_SIZE];
pthread_mutex_lock(&mutex_read);
pthread_cond_wait(&cond_read, &mutex_read);
if (!(fp = fopen(FILE_PATH, "r")))
{
fprintf(stderr, "thread %d: can't open file\n", id);
pthread_exit(NULL);
}
i = fread(buf, sizeof(char), BUF_SIZE-1, fp);
fclose(fp);
buf[i] = '\0';
printf("thread %d: %s\n", id, &buf);
pthread_mutex_unlock(&mutex_read);
}
int main(int argc, char *argv[])
{
int i, id[NUM_THREADS];
pthread_t thread[NUM_THREADS];
pthread_attr_t attr;
FILE *fp;
char buf[BUF_SIZE];
pthread_mutex_init(&mutex_read, NULL);
pthread_cond_init(&cond_read, NULL);
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
for (i = 0; i < NUM_THREADS; i++)
{
id[i] = i;
pthread_create(&thread[i], NULL, thread_func, (void *) &id[i]);
}
if (!(fp = fopen(FILE_PATH, "w")))
{
fprintf(stderr, "parent: can't open file\n");
return 1;
}
printf("parent: writing file\n");
fprintf(fp, "42");
fclose(fp);
if (!(fp = fopen(FILE_PATH, "r")))
{
fprintf(stderr, "parent: can't open file\n");
return 1;
}
i = fread(buf, sizeof(char), BUF_SIZE-1, fp);
fclose(fp);
buf[i] = '\0';
printf("parent: %s\n", &buf);
pthread_cond_broadcast(&cond_read);
for (i = 0; i < NUM_THREADS; i++)
{
pthread_join(thread[i], NULL);
}
pthread_attr_destroy(&attr);
pthread_mutex_destroy(&mutex_read);
pthread_cond_destroy(&cond_read);
pthread_exit(NULL);
return 0;
}