jekyll-theme-gaeblogx 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +9 -0
- data/_includes/algolia.html +75 -0
- data/_includes/backToTop.html +5 -0
- data/_includes/category.html +19 -0
- data/_includes/comments.html +46 -0
- data/_includes/footer.html +33 -0
- data/_includes/head.html +58 -0
- data/_includes/header.html +34 -0
- data/_includes/previousAndNext.html +13 -0
- data/_includes/sidebar-search.html +25 -0
- data/_includes/tag.html +16 -0
- data/_layouts/default.html +26 -0
- data/_layouts/demo.html +10 -0
- data/_layouts/page.html +31 -0
- data/_layouts/post.html +107 -0
- data/_sass/_backToTop.scss +49 -0
- data/_sass/_demo.scss +65 -0
- data/_sass/_footer.scss +63 -0
- data/_sass/_header.scss +174 -0
- data/_sass/_index.scss +174 -0
- data/_sass/_layout.scss +242 -0
- data/_sass/_page.scss +238 -0
- data/_sass/_post-old.scss +109 -0
- data/_sass/_post.scss +83 -0
- data/_sass/_reset.scss +119 -0
- data/_sass/_scrollbar.scss +35 -0
- data/_sass/_syntax-highlighting.scss +99 -0
- data/assets/10.jpg +0 -0
- data/assets/AWS-Introduction-ec2.png +0 -0
- data/assets/Cache-Oblivious-Algorithms-FunnelSort.jpg +0 -0
- data/assets/Condition-Variables-fig1.png +0 -0
- data/assets/Dynamic-Compilation-adaptJVM.pdf +0 -0
- data/assets/Dynamic-Compilation-dyncomp.pdf +0 -0
- data/assets/Fibonacci-Heap-Fig3.png +0 -0
- data/assets/Fibonacci-Heap-Fig5.png +0 -0
- data/assets/Fibonacci-Heap-Fig6.png +0 -0
- data/assets/Fibonacci-Heap-Fig7.png +0 -0
- data/assets/File-System-bc.jpg +0 -0
- data/assets/File-System-filetable.rich.jpg +0 -0
- data/assets/GIL-Battle.png +0 -0
- data/assets/GIL-CPU.png +0 -0
- data/assets/GIL-Check.png +0 -0
- data/assets/GIL-IO.png +0 -0
- data/assets/GIL-Measure-1.png +0 -0
- data/assets/GIL-Measure-2.png +0 -0
- data/assets/GIL-Signal.png +0 -0
- data/assets/GIL-Tick.png +0 -0
- data/assets/Garbage-Collection-Slides-gc.pdf +0 -0
- data/assets/Garbage-Collection-copying-1.png +0 -0
- data/assets/Garbage-Collection-copying-2.png +0 -0
- data/assets/Garbage-Collection-free-list.png +0 -0
- data/assets/Garbage-Collection-generation-1.png +0 -0
- data/assets/Garbage-Collection-mark-sweep.png +0 -0
- data/assets/Garbage-Collection-nursery-1.png +0 -0
- data/assets/Garbage-Collection-nursery-2.png +0 -0
- data/assets/Garbage-Collection-nursery-3.png +0 -0
- data/assets/Garbage-Collection-nursery-4.png +0 -0
- data/assets/Garbage-Collection-nursery-5.png +0 -0
- data/assets/Garbage-Collection-process.png +0 -0
- data/assets/Garbage-Collection-ref-counting.png +0 -0
- data/assets/Interpreter-Optimization-interp.pdf +0 -0
- data/assets/Memory-Management-fig1.png +0 -0
- data/assets/Memory-Management-fig2.png +0 -0
- data/assets/Memory-Management-fig3.png +0 -0
- data/assets/Memory-Management-fig4.png +0 -0
- data/assets/Memory-Management-fig5.png +0 -0
- data/assets/Memory-Management-fig6.png +0 -0
- data/assets/Memory-Management-fig7.png +0 -0
- data/assets/Memory-Management-fig8.png +0 -0
- data/assets/Multicore-GIL-1.png +0 -0
- data/assets/Multicore-GIL-2.png +0 -0
- data/assets/My-Photo.JPG +0 -0
- data/assets/Operating-System-Virtualization-ept.png +0 -0
- data/assets/Operating-System-Virtualization-guest-page.png +0 -0
- data/assets/Operating-System-Virtualization-kvm-arch.png +0 -0
- data/assets/Operating-System-Virtualization-kvm-process.png +0 -0
- data/assets/Operating-System-Virtualization-kvm-qemu.png +0 -0
- data/assets/Operating-System-Virtualization-kvm-state.png +0 -0
- data/assets/Operating-System-Virtualization-vtx.png +0 -0
- data/assets/Operating-System-Virtualization-xen-arch.png +0 -0
- data/assets/Program-Profiling-profiling.pdf +0 -0
- data/assets/Programming-Language-Virtual-Machine-vm.pdf +0 -0
- data/assets/Remote-Shell-Session-Setup-1.png +0 -0
- data/assets/Signals-1.png +0 -0
- data/assets/Signals-2.png +0 -0
- data/assets/Signals-3.png +0 -0
- data/assets/Some-Slides-about-Containers-Kubernetes.pdf +0 -0
- data/assets/Some-Slides-about-Containers-UCSB-nurmi.pdf +0 -0
- data/assets/Some-Slides-about-PaaS-CS293B_CloudPlatforms.pdf +0 -0
- data/assets/avg-1thread.c +172 -0
- data/assets/avg-manythread.c +284 -0
- data/assets/avg-nothread.c +71 -0
- data/assets/file-create1.c +75 -0
- data/assets/file-create2.c +120 -0
- data/assets/file-fd1.c +56 -0
- data/assets/file-fd2.c +25 -0
- data/assets/file-fd3.c +23 -0
- data/assets/file-read1.c +62 -0
- data/assets/file-read2.c +66 -0
- data/assets/file-seek1.c +87 -0
- data/assets/fork-1.c +35 -0
- data/assets/fork-2.c +44 -0
- data/assets/fork-3.c +60 -0
- data/assets/fork-4.c +64 -0
- data/assets/joinall-1.c +163 -0
- data/assets/joinall-2.c +162 -0
- data/assets/market-kthreads.c +465 -0
- data/assets/market-semaphore.c +504 -0
- data/assets/market1.c +478 -0
- data/assets/market2.c +490 -0
- data/assets/market3.c +503 -0
- data/assets/market4.c +509 -0
- data/assets/my-cat.c +35 -0
- data/assets/pipe-1.c +45 -0
- data/assets/pipe-2.c +80 -0
- data/assets/pipe-3.c +83 -0
- data/assets/pipe-4.c +116 -0
- data/assets/race1.c +75 -0
- data/assets/race2.c +77 -0
- data/assets/race3.c +87 -0
- data/assets/race_ABC.c +144 -0
- data/assets/search-by-algolia.svg +1 -0
- data/assets/semaphore.book.ps +8827 -0
- data/assets/sys-call1.c +20 -0
- data/assets/sys-call2.c +16 -0
- data/assets//350/265/253/347/202/2161.PNG +0 -0
- data/feed.xml +30 -0
- data/index.html +164 -0
- data/js/jekyll-search.min.js +1 -0
- data/js/lunr.min.js +2977 -0
- data/js/main.js +57 -0
- data/js/masonry.pkgd.min.js +9 -0
- data/js/pageContent.js +166 -0
- data/js/search.js +17 -0
- data/js/smooth-scroll.min.js +2 -0
- data/js/waterfall.js +214 -0
- data/page/0archives.html +83 -0
- data/page/1category.html +62 -0
- data/page/2tags.html +64 -0
- data/page/3search.html +112 -0
- data/page/4about.md +34 -0
- metadata +270 -0
data/assets/10.jpg
ADDED
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
data/assets/GIL-CPU.png
ADDED
|
Binary file
|
|
Binary file
|
data/assets/GIL-IO.png
ADDED
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
data/assets/GIL-Tick.png
ADDED
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
data/assets/My-Photo.JPG
ADDED
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
#include <unistd.h>
|
|
2
|
+
#include <stdlib.h>
|
|
3
|
+
#include <stdio.h>
|
|
4
|
+
|
|
5
|
+
#include <pthread.h>
|
|
6
|
+
|
|
7
|
+
/*
|
|
8
|
+
* program to find the average value of a set of random numbers
|
|
9
|
+
* which forks off a single thread to do the sum computation
|
|
10
|
+
*
|
|
11
|
+
* usage: avg-1thread count
|
|
12
|
+
*
|
|
13
|
+
* where count is the number of random values to generate
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
char *Usage = "usage: avg-1thread count";
|
|
17
|
+
|
|
18
|
+
#define RAND() (drand48()) /* basic Linux random number generator */
|
|
19
|
+
|
|
20
|
+
/*
|
|
21
|
+
* data type definition for arguments passed to thread
|
|
22
|
+
*/
|
|
23
|
+
struct arg_struct
|
|
24
|
+
{
|
|
25
|
+
int size;
|
|
26
|
+
double *data;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/*
|
|
30
|
+
* data type definition for results passed back from threads
|
|
31
|
+
*/
|
|
32
|
+
struct result_struct
|
|
33
|
+
{
|
|
34
|
+
double sum;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
void *SumThread(void *arg)
|
|
38
|
+
{
|
|
39
|
+
int i;
|
|
40
|
+
double my_sum;
|
|
41
|
+
struct arg_struct *my_args;
|
|
42
|
+
int my_size;
|
|
43
|
+
double *my_data;
|
|
44
|
+
struct result_struct *result;
|
|
45
|
+
|
|
46
|
+
printf("sum thread running\n");
|
|
47
|
+
fflush(stdout);
|
|
48
|
+
|
|
49
|
+
my_args = (struct arg_struct *)arg;
|
|
50
|
+
result = (struct result_struct *)malloc(sizeof(struct result_struct));
|
|
51
|
+
if(result == NULL) {
|
|
52
|
+
exit(1);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
my_size = my_args->size;
|
|
56
|
+
my_data = my_args->data;
|
|
57
|
+
|
|
58
|
+
/*
|
|
59
|
+
* free the arg structure since it isn't needed any more
|
|
60
|
+
*/
|
|
61
|
+
free(my_args);
|
|
62
|
+
|
|
63
|
+
my_sum = 0.0;
|
|
64
|
+
for(i=0; i < my_size; i++) {
|
|
65
|
+
my_sum += my_data[i];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
result->sum = my_sum;
|
|
69
|
+
printf("sum thread done, returning\n");
|
|
70
|
+
fflush(stdout);
|
|
71
|
+
|
|
72
|
+
return((void *)result);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
int main(int argc, char **argv)
|
|
76
|
+
{
|
|
77
|
+
int i;
|
|
78
|
+
int n;
|
|
79
|
+
double *data;
|
|
80
|
+
int count;
|
|
81
|
+
struct arg_struct *args;
|
|
82
|
+
pthread_t thread_id;
|
|
83
|
+
struct result_struct *result;
|
|
84
|
+
int err;
|
|
85
|
+
|
|
86
|
+
/*
|
|
87
|
+
* santity check the arguments
|
|
88
|
+
*/
|
|
89
|
+
if(argc <= 1) {
|
|
90
|
+
fprintf(stderr,"must specify count\n");
|
|
91
|
+
fprintf(stderr,"%s",Usage);
|
|
92
|
+
exit(1);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
count = atoi(argv[1]); /* count is first argument */
|
|
96
|
+
|
|
97
|
+
if(count <= 0) {
|
|
98
|
+
fprintf(stderr,"invalid count %d\n",count);
|
|
99
|
+
exit(1);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/*
|
|
103
|
+
* make an array large enough to hold #count# doubles
|
|
104
|
+
*/
|
|
105
|
+
data = (double *)malloc(count * sizeof(double));
|
|
106
|
+
if(data == NULL) {
|
|
107
|
+
exit(1);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/*
|
|
111
|
+
* pick a bunch of random numbers
|
|
112
|
+
*/
|
|
113
|
+
for(i=0; i < count; i++) {
|
|
114
|
+
data[i] = RAND();
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/*
|
|
118
|
+
* make a argument structure to use to pass the arguments to the
|
|
119
|
+
* thread
|
|
120
|
+
*/
|
|
121
|
+
args = (struct arg_struct *)malloc(sizeof(struct arg_struct));
|
|
122
|
+
if(args == NULL) {
|
|
123
|
+
exit(1);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/*
|
|
127
|
+
* load into the arguments the size and the pointer to the data
|
|
128
|
+
*/
|
|
129
|
+
args->size = count;
|
|
130
|
+
args->data = data;
|
|
131
|
+
|
|
132
|
+
/*
|
|
133
|
+
* fork off a thread to get the sum from the data
|
|
134
|
+
*/
|
|
135
|
+
printf("main thread forking sum thread\n");
|
|
136
|
+
fflush(stdout);
|
|
137
|
+
err = pthread_create(&thread_id, NULL, SumThread, (void *)args);
|
|
138
|
+
if(err != 0) {
|
|
139
|
+
fprintf(stderr,"pthread create failed\n");
|
|
140
|
+
exit(1);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
printf("main thread running after sum thread created, about to call join\n");
|
|
144
|
+
fflush(stdout);
|
|
145
|
+
|
|
146
|
+
/*
|
|
147
|
+
* wait until the sum thread is finished and get the result back
|
|
148
|
+
*/
|
|
149
|
+
err = pthread_join(thread_id,(void **)&result);
|
|
150
|
+
if(err != 0) {
|
|
151
|
+
fprintf(stderr,"pthread_join failed\n");
|
|
152
|
+
exit(1);
|
|
153
|
+
}
|
|
154
|
+
printf("main thread joined with sum thread\n");
|
|
155
|
+
fflush(stdout);
|
|
156
|
+
|
|
157
|
+
printf("the average over %d random numbers on (0,1) is %f\n",
|
|
158
|
+
count, result->sum / (double)count);
|
|
159
|
+
|
|
160
|
+
/*
|
|
161
|
+
* free the result structure
|
|
162
|
+
*/
|
|
163
|
+
free(result);
|
|
164
|
+
|
|
165
|
+
/*
|
|
166
|
+
* free the data array -- it wasn't freed in the sum thread
|
|
167
|
+
*/
|
|
168
|
+
free(data);
|
|
169
|
+
|
|
170
|
+
return(0);
|
|
171
|
+
}
|
|
172
|
+
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
#include <unistd.h>
|
|
2
|
+
#include <stdlib.h>
|
|
3
|
+
#include <stdio.h>
|
|
4
|
+
|
|
5
|
+
#include <pthread.h>
|
|
6
|
+
|
|
7
|
+
/*
|
|
8
|
+
* program to find the average value of a set of random numbers
|
|
9
|
+
* which forks off #t# threads to do the sum computation in parallel
|
|
10
|
+
*
|
|
11
|
+
* usage: avg-manythread count
|
|
12
|
+
*
|
|
13
|
+
* where count is the number of random values to generate
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
char *Usage = "usage: avg-manythread count threads";
|
|
17
|
+
|
|
18
|
+
#define RAND() (drand48()) /* basic Linux random number generator */
|
|
19
|
+
|
|
20
|
+
/*
|
|
21
|
+
* data type definition for arguments passed to thread
|
|
22
|
+
*/
|
|
23
|
+
struct arg_struct
|
|
24
|
+
{
|
|
25
|
+
int id;
|
|
26
|
+
int size;
|
|
27
|
+
double *data;
|
|
28
|
+
int starting_i;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/*
|
|
32
|
+
* data type definition for results passed back from threads
|
|
33
|
+
*/
|
|
34
|
+
struct result_struct
|
|
35
|
+
{
|
|
36
|
+
double sum;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
void *SumThread(void *arg)
|
|
40
|
+
{
|
|
41
|
+
int i;
|
|
42
|
+
double my_sum;
|
|
43
|
+
struct arg_struct *my_args;
|
|
44
|
+
int my_size;
|
|
45
|
+
double *my_data;
|
|
46
|
+
struct result_struct *result;
|
|
47
|
+
int my_start;
|
|
48
|
+
int my_end;
|
|
49
|
+
int my_id;
|
|
50
|
+
|
|
51
|
+
my_args = (struct arg_struct *)arg;
|
|
52
|
+
result = (struct result_struct *)malloc(sizeof(struct result_struct));
|
|
53
|
+
if(result == NULL) {
|
|
54
|
+
exit(1);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
printf("sum thread %d running, starting at %d for %d\n",
|
|
58
|
+
my_args->id,
|
|
59
|
+
my_args->starting_i,
|
|
60
|
+
my_args->size);
|
|
61
|
+
fflush(stdout);
|
|
62
|
+
|
|
63
|
+
my_id = my_args->id;
|
|
64
|
+
my_size = my_args->size;
|
|
65
|
+
my_data = my_args->data;
|
|
66
|
+
my_start = my_args->starting_i;
|
|
67
|
+
|
|
68
|
+
/*
|
|
69
|
+
* free the arg structure -- it isn't needed any more
|
|
70
|
+
*/
|
|
71
|
+
free(my_args);
|
|
72
|
+
|
|
73
|
+
/*
|
|
74
|
+
* figure out the ending index for my subregion
|
|
75
|
+
*/
|
|
76
|
+
my_end = my_start + my_size;
|
|
77
|
+
|
|
78
|
+
my_sum = 0.0;
|
|
79
|
+
for(i=my_start; i < my_end; i++) {
|
|
80
|
+
my_sum += my_data[i];
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
result->sum = my_sum;
|
|
84
|
+
|
|
85
|
+
printf("sum thread %d returning\n",
|
|
86
|
+
my_id);
|
|
87
|
+
fflush(stdout);
|
|
88
|
+
|
|
89
|
+
return((void *)result);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
int main(int argc, char **argv)
|
|
93
|
+
{
|
|
94
|
+
int i;
|
|
95
|
+
int t;
|
|
96
|
+
double sum;
|
|
97
|
+
double *data;
|
|
98
|
+
int count;
|
|
99
|
+
int threads;
|
|
100
|
+
struct arg_struct *args;
|
|
101
|
+
struct result_struct *result;
|
|
102
|
+
int err;
|
|
103
|
+
pthread_t *thread_ids;
|
|
104
|
+
int range_size;
|
|
105
|
+
int index;
|
|
106
|
+
|
|
107
|
+
/*
|
|
108
|
+
* santity check the arguments
|
|
109
|
+
*/
|
|
110
|
+
if(argc <= 2) {
|
|
111
|
+
fprintf(stderr,"must specify count and number of threads to use\n");
|
|
112
|
+
fprintf(stderr,"%s",Usage);
|
|
113
|
+
exit(1);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
count = atoi(argv[1]); /* count is first argument */
|
|
117
|
+
|
|
118
|
+
if(count <= 0) {
|
|
119
|
+
fprintf(stderr,"invalid count %d\n",count);
|
|
120
|
+
exit(1);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
threads = atoi(argv[2]); /* thread count is second arg */
|
|
124
|
+
|
|
125
|
+
if(threads <= 0) {
|
|
126
|
+
fprintf(stderr,"invalid thread count %d\n",threads);
|
|
127
|
+
exit(1);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/*
|
|
131
|
+
* don't allow more threads than data elements
|
|
132
|
+
*/
|
|
133
|
+
if(threads > count) {
|
|
134
|
+
threads = count;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/*
|
|
138
|
+
* make an array large enough to hold #count# doubles
|
|
139
|
+
*/
|
|
140
|
+
data = (double *)malloc(count * sizeof(double));
|
|
141
|
+
if(data == NULL) {
|
|
142
|
+
exit(1);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/*
|
|
146
|
+
* pick a bunch of random numbers
|
|
147
|
+
*/
|
|
148
|
+
for(i=0; i < count; i++) {
|
|
149
|
+
data[i] = RAND();
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
/*
|
|
154
|
+
* make a data structure to hold the thread ids so that the spawning
|
|
155
|
+
* thread can join with each one separately
|
|
156
|
+
*/
|
|
157
|
+
thread_ids = (pthread_t *)malloc(sizeof(pthread_t)*threads);
|
|
158
|
+
if(thread_ids == NULL) {
|
|
159
|
+
exit(1);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/*
|
|
163
|
+
* fork off #threads# threads giving each a more or less even number
|
|
164
|
+
* of values to sum from the master list
|
|
165
|
+
*
|
|
166
|
+
* each thread gets its own arg structure with the same pointer to the
|
|
167
|
+
* data
|
|
168
|
+
*
|
|
169
|
+
* first, divide the list size by the number of threads to get an even
|
|
170
|
+
* distribution of work.
|
|
171
|
+
*/
|
|
172
|
+
range_size = (count / threads) + 1;
|
|
173
|
+
|
|
174
|
+
/*
|
|
175
|
+
* handle the possibility that #threads# divides #count# evenly
|
|
176
|
+
*/
|
|
177
|
+
if(((range_size-1) * threads) == count) {
|
|
178
|
+
range_size -= 1;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
printf("main thread about to create %d sum threads\n",
|
|
182
|
+
threads);
|
|
183
|
+
fflush(stdout);
|
|
184
|
+
/*
|
|
185
|
+
* create one thread to sum each subregion of the data array
|
|
186
|
+
* start with index 0
|
|
187
|
+
*/
|
|
188
|
+
index = 0;
|
|
189
|
+
for(t=0; t < threads; t++) {
|
|
190
|
+
/*
|
|
191
|
+
* make a argument structure to use to pass the arguments to the
|
|
192
|
+
* thread
|
|
193
|
+
*/
|
|
194
|
+
args = (struct arg_struct *)malloc(sizeof(struct arg_struct));
|
|
195
|
+
if(args == NULL) {
|
|
196
|
+
exit(1);
|
|
197
|
+
}
|
|
198
|
+
/*
|
|
199
|
+
* give this thread an integer id
|
|
200
|
+
*/
|
|
201
|
+
args->id = (t+1);
|
|
202
|
+
/*
|
|
203
|
+
* give each thread the size of the subregion and the same pointer to the
|
|
204
|
+
* data array
|
|
205
|
+
*/
|
|
206
|
+
args->size = range_size;
|
|
207
|
+
args->data = data;
|
|
208
|
+
/*
|
|
209
|
+
* set the starting and ending index for the next thread
|
|
210
|
+
*/
|
|
211
|
+
args->starting_i = index;
|
|
212
|
+
/*
|
|
213
|
+
* sanity chek to make sure we don't go out of bounds on the
|
|
214
|
+
* last thread in the case there #count# does not divide
|
|
215
|
+
* evenly
|
|
216
|
+
*/
|
|
217
|
+
if((args->starting_i + args->size) > count) {
|
|
218
|
+
args->size = count - args->starting_i;
|
|
219
|
+
}
|
|
220
|
+
printf("main thread creating sum thread %d\n",
|
|
221
|
+
t+1);
|
|
222
|
+
fflush(stdout);
|
|
223
|
+
/*
|
|
224
|
+
* fork this thread and remember its id in the array of thread
|
|
225
|
+
* ids
|
|
226
|
+
*/
|
|
227
|
+
err = pthread_create(&(thread_ids[t]), NULL, SumThread, (void *)args);
|
|
228
|
+
if(err != 0) {
|
|
229
|
+
fprintf(stderr,
|
|
230
|
+
"pthread create failed for thread %d\n", t);
|
|
231
|
+
exit(1);
|
|
232
|
+
}
|
|
233
|
+
printf("main thread has created sum thread %d\n",
|
|
234
|
+
t+1);
|
|
235
|
+
/*
|
|
236
|
+
* set the starting index for the next thread
|
|
237
|
+
*/
|
|
238
|
+
index += range_size;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/*
|
|
242
|
+
* join with each thread one at a time and
|
|
243
|
+
* get its partial sum and make a global sum
|
|
244
|
+
*/
|
|
245
|
+
sum = 0;
|
|
246
|
+
for(t=0; t < threads; t++) {
|
|
247
|
+
/*
|
|
248
|
+
* wait until the next sum thread is finished and get the result back
|
|
249
|
+
*/
|
|
250
|
+
printf("main thread about to join with sum thread %d\n",t+1);
|
|
251
|
+
fflush(stdout);
|
|
252
|
+
err = pthread_join(thread_ids[t],(void **)&result);
|
|
253
|
+
if(err != 0) {
|
|
254
|
+
fprintf(stderr,"pthread_join failed for thread %d\n",
|
|
255
|
+
t);
|
|
256
|
+
exit(1);
|
|
257
|
+
}
|
|
258
|
+
printf("main thread joined with sum thread %d\n",t+1);
|
|
259
|
+
fflush(stdout);
|
|
260
|
+
/*
|
|
261
|
+
* add in the partial sum
|
|
262
|
+
*/
|
|
263
|
+
sum += result->sum;
|
|
264
|
+
/*
|
|
265
|
+
* free this thread's result structure
|
|
266
|
+
*/
|
|
267
|
+
free(result);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
printf("the average over %d random numbers on (0,1) is %f\n",
|
|
271
|
+
count, sum / (double)count);
|
|
272
|
+
|
|
273
|
+
/*
|
|
274
|
+
* free the thread id array
|
|
275
|
+
*/
|
|
276
|
+
free(thread_ids);
|
|
277
|
+
/*
|
|
278
|
+
* free the data array
|
|
279
|
+
*/
|
|
280
|
+
free(data);
|
|
281
|
+
|
|
282
|
+
return(0);
|
|
283
|
+
}
|
|
284
|
+
|