|
@@ -0,0 +1,59 @@
|
|
|
|
+#include <stdio.h>
|
|
|
|
+#include <stdlib.h>
|
|
|
|
+#include <pthread.h>
|
|
|
|
+
|
|
|
|
+struct fibo {
|
|
|
|
+ int limit;
|
|
|
|
+ int answer;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+int
|
|
|
|
+fib(int n)
|
|
|
|
+{
|
|
|
|
+ int i=0;
|
|
|
|
+ if (n==1 || n==0) return 1;
|
|
|
|
+ for(i=0;i<n;i++){
|
|
|
|
+ return (fib(n-1) + fib(n-2));
|
|
|
|
+ }
|
|
|
|
+ return (-1);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void*
|
|
|
|
+runner1(void* arg)
|
|
|
|
+{
|
|
|
|
+ struct fibo* fibl = (struct fibo*) arg;
|
|
|
|
+ fibl->answer = fib(fibl->limit);
|
|
|
|
+ pthread_exit(0);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+int
|
|
|
|
+main (int argc, char** argv)
|
|
|
|
+{
|
|
|
|
+
|
|
|
|
+ int i;
|
|
|
|
+ pthread_t id[argc];
|
|
|
|
+ pthread_attr_t attr;
|
|
|
|
+ struct fibo d[argc];
|
|
|
|
+
|
|
|
|
+ if (argc < 2) {
|
|
|
|
+ printf("usage: %s num1 num2 num3... numN", argv[0]);
|
|
|
|
+ return (1);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ pthread_attr_init(&attr);
|
|
|
|
+ for (i=1; i<argc;i++){
|
|
|
|
+ d[i-1].limit = atoi(argv[i]);
|
|
|
|
+ d[i-1].answer = 0;
|
|
|
|
+ pthread_create(&id[i-1], &attr, runner1, &d[i-1]);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for (i=0;i<argc-1;i++){
|
|
|
|
+ pthread_join(id[i], NULL);
|
|
|
|
+ printf("\n\nFib of limit: %d is %d", d[i].limit, d[i].answer);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return (0);
|
|
|
|
+
|
|
|
|
+}
|