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
BRAINLIEST ANSWER AWARDED PLEASE HELP
Aleksandr-060686 [28]
When a formula produces output that is too lengthy to fit in the spreadsheet cell, the error that will show is "#####". When you enter an invalid cell reference in a formula, for instance using "AVE(" instead of "AVERAGE("; the error that will show is "#NAME?". When you type text in cells that accept numeric data, for instance adding 1 + 1 + A; then the error that will show is "#VALUE". Lastly, when you type in a cell reference that does not exist, the error that will show is "#REF".
4 0
3 years ago
Which of the these is tool for creating mobile apps? A:C# B:Apple Pie C:Appy Pie D:C++​
Ratling [72]

Answer:

bhsjsbsbsb

Explanation:

<h3><em>bejwnsusb</em></h3>
4 0
2 years ago
Read 2 more answers
File formats are linked to certain programs.<br><br> True<br> False
Lostsunrise [7]
What was the answer?
7 0
3 years ago
Read 2 more answers
Keeping in mind the role the order of precedence plays in equations, what would Excel display as the result of the following equ
emmasim [6.3K]
The correct answer for the question that is being presented above is this one: "5.5" <span>Keeping in mind the role the order of precedence plays in equations, Excel would display as the result of the following equation? =(24+75)/(6*3): 5.5.</span>

5 0
3 years ago
How many Little mermaid movies are there? I know there is little mermaid 1 and 2, but I am not sure how many total movies there
kupik [55]
There have only been 3 Little Mermaid movies. 

<span>1. The Little Mermaid (1989) </span>
<span>2. The Little Mermaid 2 Return to the Sea (2000) </span>
<span>3. The Little Mermaid 3 Ariel's Beginning (2008)

hope this helps you(:

if not plz let me know (:

have a good day 
-denis</span>
7 0
3 years ago
Read 2 more answers
Other questions:
  • Jenny is working on a laptop computer and notices that the computer is not running very fast. She looks and realizes that the la
    15·2 answers
  • Solve the recurrence relation.<br> S(1)=1<br> S(n)= S(n-1)+(2n-1) for n&gt;=2
    13·1 answer
  • Suppose you have one particular application that is trying to send data on the Internet but none of the data is making it to the
    15·2 answers
  • In this section of your final project, you will write a basic script to create and back up files. You will create this script wi
    11·2 answers
  • A software license gives the owner the to use software.
    12·2 answers
  • Is the computer a device that calculates and has the independence and initiative for the actions it performs?
    13·1 answer
  • In the following nested loop structure, which loop does the program EXIT first?
    14·1 answer
  • What is the decimal value for the jump control?
    12·1 answer
  • It takes Mike 18 minutes to finish reading 4 pages of a book. How long would it take for him to finish 30 pages ?​
    9·1 answer
  • YASHARI earns $27,000 per year, is single, and lives in Wyoming. She has $7000 in Direct Subsidized loans and another $19,000 in
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!