forked from sysprog21/fibdrv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread.c
65 lines (55 loc) · 1.53 KB
/
thread.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 <assert.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#define NUM_THREADS 2
#define FIB_DEV "/dev/fibonacci"
long long sz;
void Run_fibonacci()
{
char buf[128];
// char write_buf[] = "testing writing";
int offset = 92;
int fd = open(FIB_DEV, O_RDWR);
if (fd < 0) {
perror("Failed to open character device");
exit(1);
}
// write(fd, write_buf, strlen(write_buf));
for (int i = 0; i <= offset; i++) {
lseek(fd, i, SEEK_SET);
sz = read(fd, buf, sizeof(buf));
// sz = write(fd, buf, sizeof(buf));
printf("reading from " FIB_DEV
" at ofset %d, returned the sequence "
"%lld.\n",
i, sz);
}
close(fd);
}
int main()
{
pthread_t threads[NUM_THREADS];
int i;
int result_code;
// create all threads one by one
for (i = 0; i < NUM_THREADS; i++) {
printf("IN MAIN: Creating thread %d.\n", i);
result_code = pthread_create(&threads[i], NULL,
(void *(*) (void *) ) Run_fibonacci, NULL);
assert(!result_code);
}
printf("IN MAIN: All threads are created.\n");
// wait for each thread to complete
for (i = 0; i < NUM_THREADS; i++) {
result_code = pthread_join(threads[i], NULL);
assert(!result_code);
printf("IN MAIN: Thread %d has ended.\n", i);
}
printf("MAIN program has ended.\n");
return 0;
}