Skip to content

Instantly share code, notes, and snippets.

@shade34321
Last active August 29, 2015 14:02
Show Gist options
  • Save shade34321/1271214a336fd848fc08 to your computer and use it in GitHub Desktop.
Save shade34321/1271214a336fd848fc08 to your computer and use it in GitHub Desktop.
// CS3243 Operating Systems
// Summer 2014
// Project 3: Forks and Threads
// Matthew Tinney and Shade Alabsa
// Date: 10 June 2014
// File: parttwo.cpp
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>
#include <iostream>
#include <cmath>
#include <sys/types.h>
using namespace std;
struct data{
int numDarts;
int hits;
};
int createThreads(int, int);
void * throwDart(void *);
double getPoint();
double calculatePi(int, int);
bool inside(double, double);
int main(){
int i, numThreads[3] = { 1, 1, 1 };
double pi;
long hit, duration[3] = { 2500, 1000000, 10000000 };
time_t start, end;
srand(time(NULL));
for (i = 0; i < 3; i++) {
time(&start);
hit = createThreads(duration[i], numThreads[i]);
time(&end);
pi = calculatePi(hit, duration[i]);
printf("Number of darts: %ld\n", duration[i]);
printf("Number of hits: %ld\n", hit);
printf("Number of misses: %ld\n", (duration[i] - hit));
printf("Time elapsed was: %f\n", difftime(end, start));
printf("PI ~= %f\n", pi);
}
return 0;
}
int createThreads(int numDarts, int numThreads){
pthread_t threads[numThreads];
data *returnThreads = (data *)malloc(sizeof(data) * numThreads);
int hit = 0;
int i;
void *ret;
for (i = 0; i < numThreads; i++){
returnThreads[i].numDarts = numDarts / numThreads;
if (pthread_create(&threads[i], NULL, &throwDart, &returnThreads[i]) != 0) {
perror("pthread_create() error");
exit(1);
}
}
for (i = 0; i < numThreads; i++){
if (pthread_join(threads[i], &ret) != 0) {
perror("pthread_join() error");
exit(3);
}
printf("Return value is %s\n", ret);
cout << "Hit on return back: " << returnThreads[i].hits << endl;
hit += returnThreads[i].hits;
}
if (returnThreads){
free(returnThreads);
}
return hit;
}
void * throwDart(void *info) {
int i, j;
int hit = 0;
j = ((data *)(info))->numDarts;
double x, y;
for (i = 0; i < j; i++) {
x = getPoint();
y = getPoint();
if (inside(x, y)) {
hit++;
}
}
((data *)(info))->hits = hit;
cout << "Hits in throwDart: " << ((data *)(info))->hits << endl;
return NULL;
}
//Check to see if the point is inside the circle
//Center is at (5,5) with radius 5. The square is from 0-10 on the cartesian plane.
// In general a point is in a circle if it meets the condition below
// (x - center_x)^2 + (y - center_y)^2 < radius^2
// where x and y are the points coordinates and center_x and center_y are the circles center point coordinates and radius is the radius of the circle.
// If they are equal then it's a point on the circle which I will handle as being in the circle.
bool inside(double x, double y) {
return ((pow(x, 2) + pow(y, 2) <= pow(1, 2)) ? true : false);
}
//Generate a number between 0 and 9 for point.
double getPoint() {
double p;
p = (rand() % 20001 - 10000) / (double)10000;
return p;
}
double calculatePi(int hits, int darts) {
double pi;
pi = ((4 * hits) / (double)darts);
return pi;
}
Number of darts: 2500
Number of hits: 1984
Number of misses: 516
Time elapsed was: 0.000000
PI ~= 3.174400
Number of darts: 1000000
Number of hits: 784964
Number of misses: 215036
Time elapsed was: 0.000000
PI ~= 3.139856
Number of darts: 10000000
Number of hits: 7848521
Number of misses: 2151479
Time elapsed was: 0.000000
PI ~= 3.139408
// CS3243 Operating Systems
// Summer 2014
// Project 3: Forks and Threads
// Matthew Tinney and Shade Alabsa
// Date: 10 June 2014
// File: parttwob.cpp
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>
#include <iostream>
#include <cmath>
#include <sys/types.h>
using namespace std;
struct data{
long numDarts;
long hits;
};
long createThreads(long, long);
void * throwDart(void *);
double getPoint();
double calculatePi(long, long);
bool inside(double, double);
int main(){
int i, numThreads[5] = { 1, 2, 4, 16, 64 };
double pi;
long hit, duration = 10000000;
time_t start, end;
srand(time(NULL));
for (i = 0; i < 5; i++) {
time(&start);
hit = createThreads(duration, numThreads[i]);
time(&end);
pi = calculatePi(hit, duration);
printf("Number of darts: %ld\n", duration);
printf("Number of hits: %ld\n", hit);
printf("Number of misses: %ld\n", (duration - hit));
printf("Time elapsed was: %f\n", difftime(end, start));
printf("PI ~= %f\n", pi);
}
return 0;
}
long createThreads(long numDarts, long numThreads){
pthread_t threads[numThreads];
data *returnThreads = (data *)malloc(sizeof(data)* numThreads);
long hit = 0;
int i;
void *ret;
for (i = 0; i < numThreads; i++){
returnThreads[i].numDarts = numDarts / numThreads;
if (pthread_create(&threads[i], NULL, &throwDart, &returnThreads[i]) != 0) {
perror("pthread_create() error");
exit(1);
}
}
for (i = 0; i < numThreads; i++){
if (pthread_join(threads[i], &ret) != 0) {
perror("pthread_join() error");
exit(3);
}
hit += returnThreads[i].hits;
}
if (returnThreads){
free(returnThreads);
}
return hit;
}
void * throwDart(void *info) {
int i, j;
long hit = 0;
j = ((data *)(info))->numDarts;
double x, y;
for (i = 0; i < j; i++) {
x = getPoint();
y = getPoint();
if (inside(x, y)) {
hit++;
}
}
((data *)(info))->hits = hit;
return NULL;
}
//Check to see if the point is inside the circle
//Center is at (5,5) with radius 5. The square is from 0-10 on the cartesian plane.
// In general a point is in a circle if it meets the condition below
// (x - center_x)^2 + (y - center_y)^2 < radius^2
// where x and y are the points coordinates and center_x and center_y are the circles center point coordinates and radius is the radius of the circle.
// If they are equal then it's a point on the circle which I will handle as being in the circle.
bool inside(double x, double y) {
return ((pow(x, 2) + pow(y, 2) <= pow(1, 2)) ? true : false);
}
//Generate a number between 0 and 9 for point.
double getPoint() {
double p;
p = (rand() % 20001 - 10000) / (double)10000;
return p;
}
double calculatePi(long hits, long darts) {
double pi;
pi = ((4 * hits) / (double)darts);
return pi;
}
Number of darts: 10000000
Number of hits: 7852437
Number of misses: 2147563
Time elapsed was: 1.000000
PI ~= 3.140975
Number of darts: 10000000
Number of hits: 7854422
Number of misses: 2145578
Time elapsed was: 0.000000
PI ~= 3.141769
Number of darts: 10000000
Number of hits: 7851275
Number of misses: 2148725
Time elapsed was: 1.000000
PI ~= 3.140510
Number of darts: 10000000
Number of hits: 7855479
Number of misses: 2144521
Time elapsed was: 0.000000
PI ~= 3.142192
Number of darts: 10000000
Number of hits: 7856100
Number of misses: 2143900
Time elapsed was: 0.000000
PI ~= 3.142440
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment