Fibonacci program c pthread
C
/*============================================================================
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>\n");
return -1;
} // valdiate num of args.
if (atoi(argv[1]) < 0)
{
printf("%d must be>=0\n", 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,\n",i,fibseq[i]);
pthread_exit(0); // thread exit.
} // End of else
} // End of run.
Also in C:
- Title
- get a remote branch git
- Category
- C
- Title
- isalpha c
- Category
- C
- Title
- c array
- Category
- C
- Title
- declare variable in c
- Category
- C
- Title
- for loop in c
- Category
- C
- Title
- what is size_t in c
- Category
- C
- Title
- calculate max of three numbers using ternary operator in c
- Category
- C
- Title
- last element from list javascript
- Category
- C
- Title
- how to call the tkinter insert command from another class
- Category
- C
- Title
- networkx remove attributes
- Category
- C
- Title
- functions return type in c
- Category
- C
- Title
- sue murry
- Category
- C
- Title
- strtoul C
- Category
- C
- Title
- c print array
- Category
- C
- Title
- remove element queue
- Category
- C
- Title
- fa fa-facebook
- Category
- C
- Title
- find string in all files powershell
- Category
- C
- Title
- get regedit value cmd
- Category
- C
- Title
- how to pass an array to a thread in c?
- Category
- C
- Title
- c zero out array
- Category
- C
- Title
- c radians
- Category
- C
- Title
- how to get user input in c
- Category
- C
- Title
- accessing elements 2D array using pointers
- Category
- C
- Title
- fgets c
- Category
- C
- Title
- how to put a struct in another struct C
- Category
- C
- Title
- random en c
- Category
- C
- Title
- code wars responsable drinker
- Category
- C
- Title
- typedef vs #define
- Category
- C
- Title
- ModuleNotFoundError: No module named 'tensorboardX'
- Category
- C
- Title
- turn a char array into double C
- Category
- C
- Title
- how to rebasde
- Category
- C
- Title
- How to pull images from Docker Registry
- Category
- C
- Title
- error: dereferencing pointer to incomplete type
- Category
- C
- Title
- arduino define
- Category
- C
- Title
- switch case c
- Category
- C
- Title
- c program to find the sum of given number
- Category
- C
- Title
- select all file from date powershell
- Category
- C
- Title
- error 403
- Category
- C
- Title
- Declaring Variables in C
- Category
- C
- Title
- infinite loop using while
- Category
- C
- Title
- what is a long long int in c
- Category
- C
- Title
- vector aleatorio sin repetir
- Category
- C
- Title
- C why is is & nit used in scan f fr string
- Category
- C
- Title
- '&&' within '||'
- Category
- C
- Title
- hentai clochette
- Category
- C
- Title
- primo
- Category
- C
- Title
- write a binary file c
- Category
- C
- Title
- error: ‘cout’ was not declared in this scope
- Category
- C
- Title
- update ubuntu in terminal
- Category
- C
- Title
- how to globally initlailize a struct
- Category
- C
- Title
- read a binary file c
- Category
- C
- Title
- disable gnu++11 option
- Category
- C
- Title
- diferencia entre * y & en c
- Category
- C
- Title
- how to get random numbers in c
- Category
- C
- Title
- how to free memory in c
- Category
- C
- Title
- how to make a linked list in c
- Category
- C
- Title
- duplicar cadena
- Category
- C
- Title
- how to create an array of char in c++
- Category
- C
- Title
- division recursiva
- Category
- C
- Title
- download file by command line windows
- Category
- C
- Title
- hello world
- Category
- C
- Title
- stack
- Category
- C
- Title
- write array of char to file in c
- Category
- C
- Title
- how to use a pointer as a parameter in c
- Category
- C
- Title
- how to open chrome using cmd
- Category
- C
- Title
- mitch mcconnell
- Category
- C
- Title
- atoi c
- Category
- C
- Title
- slurm array job
- Category
- C
- Title
- version of libgcc
- Category
- C
- Title
- xmlns='' was not expected
- Category
- C
- Title
- lelcetric fied
- Category
- C
- Title
- how to declare 2 d array using malloc
- Category
- C
- Title
- css selector for sibling element
- Category
- C
- Title
- div
- Category
- C
- Title
- c printf right pad with space
- Category
- C
- Title
- azure storage emulator config
- Category
- C
- Title
- change plot line color in matplotlib
- Category
- C
- Title
- multiplicacion recursiva
- Category
- C
- Title
- strrev in c
- Category
- C
- Title
- fibonacci series using recursion
- Category
- C
- Title
- how to mutex lock in c
- Category
- C
- Title
- sum of arrays
- Category
- C
- Title
- How to setup a line length marker in vim
- Category
- C
- Title
- keras conv2d batchnorm
- Category
- C
- Title
- set timezone in debian terminal
- Category
- C
- Title
- Print the number 0 using write()
- Category
- C
- Title
- declaring a volatile in c
- Category
- C
- Title
- c hello world
- Category
- C
- Title
- what are the causes of memory leaks in c
- Category
- C
- Title
- inurl:fiu.edu math faculty
- Category
- C
- Title
- Difference between ** and *
- Category
- C
- Title
- convert string to float c
- Category
- C
- Title
- how to sleep in c
- Category
- C
- Title
- kadane's algorithm
- Category
- C
- Title
- compare two chars c
- Category
- C
- Title
- code in c skipping over scanf
- Category
- C
- Title
- sbatch array set max jobs at once
- Category
- C
- Title
- XAudio2 C
- Category
- C
- Title
- qtableview get selected row
- Category
- C
- Title
- curl post request
- Category
- C