1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Bezzdna [24]
3 years ago
10

Write a multithreaded program that generates the Fibonacci series using Pthreads thread library. This program should work as fol

lows: The user will enter on the command line the number of Fibonacci numbers that the program is to generate (* in this case 7 *). The program will then create a separate thread that will generate the Fibonacci sequence, placing the sequence in data-code (section) that is shared by the threads (an array isprobably the most convenient data structure). When the thread finishes execution, the parent thread will output the sequence generated by the child thread. Because the parent thread cannot begin outputting the Fibonacci sequence until the child thread finishes, this will require having the parent thread wait for the child thread.
Computers and Technology
1 answer:
Mamont248 [21]3 years ago
4 0

Answer:

#include <pthread.h>

#include <stdio.h>

#include <stdlib.h>

pthread_mutex_t mutex;

int fib[10]; /* this data is shared by the thread(s) */

int in = 0;

void *genFibo(void *param); /* the thread */

int main(int argc, char *argv[])

{

   pthread_attr_t attr;  

   if (argc != 2) {

   fprintf(stderr,"usage: fibthread <integer value>\n");

   return -1;

   }

   int count = atoi(argv[1]);

   if (count < 1) {

   fprintf(stderr,"%d must be>= 1\n", count);

   return -1;

   }

   pthread_attr_init(&attr);

   // Mutex Lock

   pthread_mutex_init(&mutex, NULL);

   // each thread computes fibonacci

   for(int i = 1;i <= count;i++) {

   pthread_t thread;

   pthread_create(&thread, &attr, genFibo, (void*)i);

   pthread_join(thread, NULL);    

   }    

   // print resulting array

   for (int i = 0; i < in;i++) {

   printf("%d ", fib[i]);

   }

   printf("\n");

   pthread_mutex_destroy(&mutex);

}

void *genFibo(void *param)

{

   pthread_mutex_lock(&mutex);

   fib[in++] = fibonacci((int)param);

   pthread_mutex_unlock(&mutex);

   pthread_exit(0);

}

int fibonacci (int x)

{

   if (x <= 1) {

   return 1;

   }

   return fibonacci(x-1) + fibonacci(x-2);

}

You might be interested in
How do you render and export files on blender
Debora [2.8K]

Answer:

Save your Blender file (. blend) in the same folder as your . obj file, if you didn't already.

Click on File/External Data/Pack All into . blend.

Click on File/External Data/Unpack All Into Files.

Choose "Use files in current directory (create when necessary)"

Explanation:

<em>Hope </em><em>it </em><em>helps </em><em>ya </em><em>ItzAlex</em>

6 0
3 years ago
To make the most informed purchasing decision, you should:
musickatia [10]

Answer:

its either A or B but im leaning more towards B

Explanation:

6 0
1 year ago
A user can set the security and privacy settings on what is displayed in the message bar from the ________ within the options me
djverab [1.8K]
Depends on the Operating System
6 0
2 years ago
The question is in the photo
Anastaziya [24]

Answer:

a

the most suitable answer

8 0
2 years ago
What considerations should you make when deciding on the size of a table?
Kisachek [45]

Answer:

You should consider the number of cells needed for data, labels, titles, and formulas.

Explanation:

4 0
2 years ago
Other questions:
  • you were discussing software privacy with a friend were surprised to learn how software privacy can impact your life everyday. w
    10·1 answer
  • Select the correct statement(s) regarding digital baseband modulation.
    7·1 answer
  • Writenames of eight output device.​
    8·1 answer
  • How do you calculate the total resistance in a series circuit with more than one resistor?
    9·1 answer
  • Xavier buys a computer for $525, which includes taxes. He pays for the computer over a 12-month period by paying $48.13 per mont
    8·2 answers
  • Java code?????
    12·1 answer
  • Suppose the message 111010 is to be transmitted (beginning with the leftmost bit) using
    6·1 answer
  • Which of these is most likely to contribute to the long term of a local ecosystem?
    5·1 answer
  • Difine the term pigment​
    14·3 answers
  • Which of the following best describes a hotspot as
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!