File: fib.c

package info (click to toggle)
massivethreads 1.02-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,924 kB
  • sloc: ansic: 27,814; sh: 4,559; cpp: 3,334; javascript: 1,799; makefile: 1,745; python: 523; asm: 373; perl: 118; lisp: 9
file content (55 lines) | stat: -rw-r--r-- 1,124 bytes parent folder | download
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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/time.h>
#include <pthread.h>

/* Fibonacci with stack variables */

inline double curr_time(void)
{
  struct timeval tv;
  gettimeofday(&tv,NULL);
  return tv.tv_sec + (double) tv.tv_usec * 1e-6;
}

void *fib(void *args)
{
  pthread_t th1, th2;
  intptr_t arg1, arg2;
  intptr_t ret1, ret2;
  intptr_t n = (intptr_t) args;
  int status;
  
  if (n == 0 || n == 1) pthread_exit((void *) n);
  
  arg1 = n - 1;
  arg2 = n - 2;
  if (pthread_create(&th1, NULL, fib, (void*) arg1) != 0) {
    perror(NULL);
    abort();
  }
  if (pthread_create(&th2, NULL, fib, (void*) arg2) != 0) {
    perror(NULL);
    abort();
  }
  pthread_join(th1, (void**) &ret1);
  pthread_join(th2, (void**) &ret2);
  pthread_exit((void*) (ret1 + ret2));
}

int main(int argc,char **argv)
{
  double t0,t1;
  intptr_t n;
  intptr_t ret;
  pthread_t th;

  n = atoi(argv[1]);
  t0 = curr_time();
  pthread_create(&th, NULL, fib, (void *) n);
  pthread_join(th, (void **) &ret);
  t1 = curr_time();
  printf("fib(%d)=%d, took %lf sec\n", (int)n, (int)ret, t1 - t0);
  return 0;
}