forked from sysprog21/fibdrv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_multhds.c
64 lines (52 loc) · 1.27 KB
/
client_multhds.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
#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include "bignum.h"
#define FIB_DEV "/dev/fibonacci"
#define THREAD_NUM 100
typedef struct {
int func;
int offset;
} thread_arg;
void *thread_func(void *arg)
{
char buf[100000];
thread_arg *args = (thread_arg *) arg;
int func = args->func;
int offset = args->offset;
int fd = open(FIB_DEV, O_RDWR);
if (fd < 0) {
perror("Failed to open character device");
return NULL;
}
lseek(fd, func, SEEK_SET);
if (!write(fd, NULL, 0)) {
printf("not accurate function setting\n");
return NULL;
}
lseek(fd, offset, SEEK_SET);
read(fd, buf, sizeof(buf));
printf("offset : %s\n", buf);
close(fd);
return NULL;
}
int main(int argc, char *argv[])
{
pthread_t threads[THREAD_NUM];
thread_arg args[THREAD_NUM];
for (int i = 0; i < THREAD_NUM; i++) {
args[i].func = atoi(argv[1]);
args[i].offset = atoi(argv[0]) - i;
}
for (int i = 0; i < THREAD_NUM; i++) {
pthread_create(&threads[i], NULL, thread_func, &args[i]);
}
for (int i = 0; i < THREAD_NUM; i++) {
pthread_join(threads[i], NULL);
}
return 0;
}