forked from MeiK2333/apue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11.6.8.c
123 lines (108 loc) · 2.64 KB
/
11.6.8.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <limits.h>
#include <pthread.h>
#include <sys/time.h>
#include "apue.h"
#define NTHR 8 /* number of threads */
#define NUMNUM 8000000L /* number of numbers to sort */
#define TNUM (NUMNUM / NTHR) /* number to sort per thread */
long nums[NUMNUM];
long snums[NUMNUM];
pthread_barrier_t b;
#ifdef SOLARIS
#define heapsort qsort
#else
extern int heapsort(void *, size_t, size_t,
int (*)(const void *, const void *));
#endif
/*
* Compare two long integers (helper function for heapsort)
*/
int complong(const void *arg1, const void *arg2) {
long l1 = *(long *)arg1;
long l2 = *(long *)arg2;
if (l1 == l2) {
return 0;
} else if (l1 < l2) {
return -1;
} else {
return 1;
}
}
/*
* Worker thread to sort a portion of the set of numbers.
*/
void *thr_fn(void *arg) {
long idx = (long)arg;
// 对该进程所分配到的序列区间进行排序
heapsort(&nums[idx], TNUM, sizeof(long), complong);
// 标记此线程已经完成工作
printf("线程 %ld 完成工作\n", idx / 1000000 + 1);
pthread_barrier_wait(&b);
printf("线程 %ld 结束\n", idx / 1000000 + 1);
/*
* Go off and perform more work ...
*/
return ((void *)0);
}
/*
* Merge the results of the individual sorted ranges.
*/
void merge() {
long idx[NTHR];
long i, minidx, sidx, num;
for (i = 0; i < NTHR; i++) {
idx[i] = i * TNUM;
}
/* 归并 8 段有序的序列 */
for (sidx = 0; sidx < NUMNUM; sidx++) {
num = LONG_MAX;
for (i = 0; i < NTHR; i++) {
if ((idx[i] < (i + 1) * TNUM) && (nums[idx[i]] < num)) {
num = nums[idx[i]];
minidx = i;
}
}
snums[sidx] = nums[idx[minidx]];
idx[minidx]++;
}
}
int main() {
unsigned long i;
struct timeval start, end;
long long startusec, endusec;
double elapsed;
int err;
pthread_t tid;
/*
* 使用随机数创建一个待排序的序列
*/
srandom(1);
for (i = 0; i < NUMNUM; i++) {
nums[i] = random();
}
/*
* 创建 8 个线程来进行排序
*/
gettimeofday(&start, NULL);
pthread_barrier_init(&b, NULL, NTHR + 1);
for (i = 0; i < NTHR; i++) {
err = pthread_create(&tid, NULL, thr_fn, (void *)(i * TNUM));
if (err != 0) {
err_exit(err, "can't create thread");
}
}
pthread_barrier_wait(&b);
merge();
gettimeofday(&end, NULL);
/*
* 输出排序后的序列
*/
startusec = start.tv_sec * 1000000 + start.tv_usec;
endusec = end.tv_sec * 1000000 + end.tv_usec;
elapsed = (double)(endusec - startusec) / 1000000.0;
printf("sort took %.4f seconds\n", elapsed);
// for (i = 0; i < NUMNUM; i++) {
// printf("%ld\n", snums[i]);
// }
exit(0);
}