Created
October 19, 2013 00:56
-
-
Save kjr247/7050451 to your computer and use it in GitHub Desktop.
Pthreads to spawn a child process and print the Fibonacci sequence (http://en.wikipedia.org/wiki/Fibonacci_number) up to a user specified number of places. You do actually have to COMPUTE the sequence, not just print it out from a hardcoded value. ou should divide the sequence roughly in half (if the user inputs an even number, one half - doesn'…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <pthread.h> | |
#include <stdio.h> | |
#include <iostream> | |
using namespace std; | |
int i, var1, var2; | |
int inputsize=0; | |
int fibarray[100]; | |
void *runner (void *param); | |
int fibfunction(int start, int end); | |
int main () | |
{ | |
fibarray[0] = 0; | |
fibarray[1] = 1; | |
pthread_t tid; | |
pthread_attr_t attr; | |
int inputsize; | |
cout << "Enter number to calculate a Fibonacci sequence" << endl; | |
cin >> inputsize; | |
pthread_attr_init(&attr); | |
int size = inputsize/2; | |
pthread_create (&tid, &attr, runner, (void*)&size); | |
pthread_join (tid, NULL); | |
fibfunction(inputsize/2,inputsize); | |
for(i=0; i<inputsize; i++) | |
{ | |
cout << fibarray[i] << endl; | |
} | |
return 0; | |
} | |
void *runner (void *param) | |
{ | |
int s = *((int*)param); | |
fibfunction(2,s); | |
pthread_exit(0); | |
} | |
int fibfunction(int start, int end) | |
{ | |
if(end<=2) | |
{ | |
fibarray[0] = 0; | |
fibarray[1] = 1; | |
} | |
else if(end<=3) | |
{ | |
fibarray[0] = 0; | |
fibarray[1] = 1; | |
fibarray[2] = 1; | |
} | |
else{ | |
var1 = fibarray[start-2]; | |
var2 = fibarray[start-1]; | |
for ( i = start; i < end; i++) | |
{ | |
fibarray[i] = var1 + var2; | |
var1 = var2; | |
var2 = fibarray[i]; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment