Search
 
SCRIPT & CODE EXAMPLE
 

C

Fibonacci program c pthread

/*============================================================================
   Description :The Fibonacci sequence
  ============================================================================ */
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

int n;                          // size of fibonacci sequence.
int *fibseq;                    // arry holds the value of each fibonacci term.
int i;                          // counter for the threads.

void *runn(void *arg);

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        printf("format is:./a.out <intgervalue>
");
        return -1;
    }                           // valdiate num of args.

    if (atoi(argv[1]) < 0)
    {
        printf("%d must be>=0
", atoi(argv[1]));
        return -1;
    }                           // valdiate value of arg1.

    n = atoi(argv[1]);
    fibseq = (int *)malloc(n * sizeof(int));
    pthread_t *threads = (pthread_t *) malloc(n * sizeof(pthread_t));
    pthread_attr_t attr;        // set of thread attribute

    pthread_attr_init(&attr);

    for (i = 0; i < n; i++)
    {
        pthread_create(&threads[i], &attr, runn, NULL);
    }                           // End of creating threads.

    int j;

    for (j = 0; j < n; j++)
    {
        pthread_join(threads[j], NULL);
    }                           // End of wating the threads to exit.

    // printing fibseq.
    printf("The Fibonacci sequence.:");
    int k;

    for (k = 0; k < n; k++)
    {
        printf("%d,", fibseq[k]);
    }                           // End of printing fibseq.
    return 0;
}                               // End of main.

void *runn(void *arg)
{
    if (i == 0)
    {
        fibseq[i] = 0;
        pthread_exit(0);
    }                           // first fib term

    if (i == 1)
    {
        fibseq[i] = 1;
        pthread_exit(0);
    }                           // seconed fib term
    else
    {
        fibseq[i] = fibseq[i - 1] + fibseq[i - 2];
        // printf("fibseq[%d]%d,
",i,fibseq[i]);
        pthread_exit(0);        // thread exit.
    }                           // End of else
}                               // End of run.
Comment

PREVIOUS NEXT
Code Example
C :: permutation and combination program in c 
C :: check if a number is even and bigger than or equal to 16 using bitwise 
C :: C #if, #elif and #else Directive 
C :: how to pprint otu a double in in c 
C :: convert calendar time to epoch in c programming 
C :: npm fs zip 
C :: Unix socket I/O primitives 
C :: error: invalid type for iteration variable ‘i’ #pragma omp parallel for 
C :: minimun number of moves in c 
C :: false and true in c 
C :: synopsis of fork() 
C :: Multi Select with icons htm; 
C :: python to java translator online 
C :: convert c code to c online 
C :: WARNING: QA Issue: rdepends on 
C :: esp rainmaker led 
C :: Print the number 0 using write() 
C :: python adding calculator 
C :: string compare in c 
C :: ? : em linguagem C 
C :: are two matrcies identical 
C :: what is float in c 
C :: time now C 
C :: arduino analogwrite 
Dart :: circle avatar from image asset flutter 
Dart :: add years to date dart 
Dart :: dart log to console 
Dart :: flutter clear all text in textfield 
Dart :: how to get screen size in flutter 
Dart :: dart move item in stack to bottom 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =