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
What are the diffrent types of contract
igomit [66]
<span>Fixed Price Contracts, Cost Reimbursable Contracts, and <span>Time and Material Contracts are the three basic types. Not to mention </span></span>Sale contracts, Employment contracts, business contracts, and leases.
4 0
3 years ago
Explain briefly in What Way, Facebook is not free medium
Sergio039 [100]
"If you're not paying for it, you are the product."
Your information and (buzzword warning) "big data" are being collected and potentially sold in ways that you cannot control.
4 0
3 years ago
What is the name of the car on the right?
Kaylis [27]

A buggati or however u spell it


5 0
3 years ago
Read 2 more answers
A small Ecommerce company houses their servers locally but they have outsourced their IT work to a local technology company rece
shusha [124]
Service Level Agreement?
3 0
3 years ago
Which of the following two operating systems are used most frequently on tablets? Select one: A. Android and iOS B. BlackBerry a
zhuklara [117]

Answer:

A. Android and iOS

Explanation:

The two most frequently used operating systems on the tablets are Android and iOS. They are most famous operating systems currently. ios is used by apple hence it's tablets also known as ipads are on ios and other companies like samsung,motorola ,lenovo,hp etc. uses Android operating system on tablets.

4 0
3 years ago
Read 2 more answers
Other questions:
  • 15. The text of a desktop publishing document is often created using
    6·2 answers
  • What question should you ask yourself to determine if a story is newsworthy
    11·2 answers
  • According to the ethical computer use policy, users should be __________ of the rules and, by agreeing to use the system on that
    11·1 answer
  • 1. Very short questions.(any four) 14)<br>a. Write down the working principle of a computer.​
    10·1 answer
  • Most presentation programs allow you to save presentations so they can be viewed online by saving them as ____
    5·2 answers
  • What is garbage in garbage out?​
    15·1 answer
  • What is the significance of the Abstract section of a research paper? A. It contains important information such as the author, c
    5·1 answer
  • An instruction for the computer. Many commands put together to
    5·1 answer
  • Risk taker positive or negative​
    8·1 answer
  • Discovery of a vulnerability in a software program can potentially be sold to the government. Group of answer choices True False
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!