Example

Fibonacci

With the purpose of illustrating an Anahy application, it is presented how to calculate Fibonacci numbers using it.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include "athread.h"

void *fibo(void *);

int main(int argc, char **argv) {
    int ret_value, iter, i, n;
    struct timeval tv_i, tv_f;
    struct timezone tz_i, tz_f;

    if (argc < 3)
    {
        printf ("syntax: %s [rabbits] [iterations]\n", argv[0]);
        exit (EXIT_FAILURE);
    }

    n = atoi (argv[1]);
    iter = atoi (argv[2]);
    aInit (argc, argv);

    gettimeofday (&tv_i, &tz_i);
    for (i = 0; i < iter; i++)
    {
        ret_value = (int) fibo ((void *) n);
        printf ("RESULTADO = %d\n", ret_value );
    }

    gettimeofday (&tv_f, &tz_f);
    printf ("Processing time for %d iterations: %0ldsec %0ldusec\n", iter,
        tv_f.tv_sec - tv_i.tv_sec, tv_f.tv_usec - tv_i.tv_usec);

        aTerminate ();
        exit (EXIT_SUCCESS);
}


void *fibo (void *void_rabbit) {
    athread_t thread, thread2;
    ulong rabbit, ret, ret2;

    rabbit = (ulong) void_rabbit;
    if (rabbit <= 2) {
        return((void *)1);
    } else {
        athread_create(&thread, NULL, fibo, (void *)(rabbit-1));
        athread_create(&thread2, NULL, fibo, (void *)(rabbit-2));
        athread_join(thread, (void *) &ret);
        athread_join(thread2, (void *) &ret2);
    }
    ret += ret2;
    return((void *) ((int)ret));
}

Compiling Fibonacci:

If Anahy has been compiled manually, $LD_LIBRARY_PATH must contain the path to the Anahy library. The steps to add it to the environment variable are described in the Downloads section.

After doing that, compile the example like this:

gcc fibo.c -o fibo -Ipath/to/Anahy/include -lathread -Lpath/to/Anahy/library

Fibonacci is also available for download form Anahy CVS. To download, try:

cvs -d :pserver:anonymous@anahy.cvs.sourceforge.net:/cvsroot/anahy \
		checkout SimpleFibo