-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathphonebook_opt.c
95 lines (83 loc) · 2.71 KB
/
phonebook_opt.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "phonebook_opt.h"
#include "debug.h"
entry *findName(char lastname[], entry *pHead)
{
size_t len = strlen(lastname);
while (pHead) {
if (strncasecmp(lastname, pHead->lastName, len) == 0
&& (pHead->lastName[len] == '\n' ||
pHead->lastName[len] == '\0')) {
pHead->lastName[len] = '\0';
if (!pHead->dtl)
pHead->dtl = (pdetail) malloc(sizeof(detail));
return pHead;
}
DEBUG_LOG("find string = %s\n", pHead->lastName);
pHead = pHead->pNext;
}
return NULL;
}
thread_arg *createThread_arg(char *data_begin, char *data_end,
int threadID, int numOfThread,
entry *entryPool)
{
thread_arg *new_arg = (thread_arg *) malloc(sizeof(thread_arg));
new_arg->data_begin = data_begin;
new_arg->data_end = data_end;
new_arg->threadID = threadID;
new_arg->numOfThread = numOfThread;
new_arg->lEntryPool_begin = entryPool;
new_arg->lEntry_head = new_arg->lEntry_tail = entryPool;
return new_arg;
}
/**
* Generate a local linked list in thread.
*/
void append(void *arg)
{
struct timespec start, end;
double cpu_time;
clock_gettime(CLOCK_REALTIME, &start);
thread_arg *t_arg = (thread_arg *) arg;
int count = 0;
entry *j = t_arg->lEntryPool_begin;
for (char *i = t_arg->data_begin; i < t_arg->data_end;
i += MAX_LAST_NAME_SIZE * t_arg->numOfThread,
j += t_arg->numOfThread, count++) {
/* Append the new at the end of the local linked list */
t_arg->lEntry_tail->pNext = j;
t_arg->lEntry_tail = t_arg->lEntry_tail->pNext;
t_arg->lEntry_tail->lastName = i;
t_arg->lEntry_tail->pNext = NULL;
t_arg->lEntry_tail->dtl = NULL;
DEBUG_LOG("thread %d t_argend string = %s\n",
t_arg->threadID, t_arg->lEntry_tail->lastName);
}
clock_gettime(CLOCK_REALTIME, &end);
cpu_time = diff_in_second(start, end);
DEBUG_LOG("thread take %lf sec, count %d\n", cpu_time, count);
pthread_exit(NULL);
}
void show_entry(entry *pHead)
{
while (pHead) {
printf("%s", pHead->lastName);
pHead = pHead->pNext;
}
}
static double diff_in_second(struct timespec t1, struct timespec t2)
{
struct timespec diff;
if (t2.tv_nsec-t1.tv_nsec < 0) {
diff.tv_sec = t2.tv_sec - t1.tv_sec - 1;
diff.tv_nsec = t2.tv_nsec - t1.tv_nsec + 1000000000;
} else {
diff.tv_sec = t2.tv_sec - t1.tv_sec;
diff.tv_nsec = t2.tv_nsec - t1.tv_nsec;
}
return (diff.tv_sec + diff.tv_nsec / 1000000000.0);
}