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
The next four octal numbers after 36 is:________.
Papessa [141]

Answer: b. 37, 40, 41, 42

Explanation:

The next four octal numbers after 36 is 37, 40, 41, 42.

The octal numeral system, which is also referred to as "oct" for short, is simply base-8 number system.

It is a number system whereby only digits from 0 to 7 are used and there are no letters or numbers that are above 8 that are used.

In this case, after 36, the next number will be 37 after which we go to 40 as we can't write 38 in the octal system. Therefore, the next four octal numbers after 36 is 37, 40, 41, 42.

7 0
3 years ago
Can i get some help please .
Alex787 [66]

Answer:

Interest

So, lets say that you took a loan from the bank of 20,000. But, when you are supposed to pay it back, you need to pay 25,000. This is because of interest. When the bank gives you the money, they cannot give it to you just like that. There is no profit coming from it. So, there is the thing called interest which basically says that since you took money from the bank, you need to pay a little more because you borrowed money from them.

4 0
2 years ago
Read 2 more answers
(within 200 words) analyzing the importance of information and communication technology in personal life.
Volgvan

Explanation:

Information technology is inescapable in modern day life, turn on the coffee pot and there are tiny microprocessor inside. Start your automobile and every aspect of operation is controlled by dozens of computer control modules. The current mobile phone amazing power and speed for the user. Technology is transforming every aspect of our life. Nowever is this more apparent than our place of employment. Developing of it makes our family to live happy because it helps the members of family to spend their most time with their family. It helps us to communicate the family member who live far from their family. It is also used in house hold work. It's saves our time to do more better and better

6 0
3 years ago
Explain how can we determine that where is the strength of magnetic force maximum in a
snow_lady [41]

Answer:

by making a experiment

Explanation:

8 0
2 years ago
Write a program that dynamically allocates an array large enough to hold a user-defined number of test scores. Once all the scor
Anastasy [175]

Answer:

// Program written in C++

// Comments are used to explain some lines

#include <iostream>

#include <iomanip>

using namespace std;

// Functions

void getData(double *, int);

void selectionSort(double *, int);

double getAverage(double *, int);

void displayData(double *, int, double);

int main() //Main Method

{

double *ToTest, // To dynamically allocate an array

Average; // To hold the average of the scores

int Scores; // To hold number of scores

// Get number of scores

cout << "Number of average to find? ";

cin >> Scores;

// Allocate an array to number of scores

ToTest = new double[Scores];

getData(ToTest, Scores);

selectionSort(ToTest, Scores);

Average = getAverage(ToTest, Scores);

printData(ToTest, Scores, Average);

return 0;

}

//Get Data

void getData(double *ToTest, int Scores)

{

cout << "Enter each scores.\n";

for (int i = 0; i < Scores; i++)

{

do

{

cout << "Score #" << (i + 1) << ": ";

cin >> *(ToTest + i);

if (*(ToTest + i) < 0)

{

cout << "Scores must be greater than 0.\n"

<< "Re-enter ";

}

} while (*(Test + i) < 0);

}

}

// Selection Sort

void selectionSort(double *ToTest, int Scores)

{

int startscan, minIndex;

double minValue;

for (startscan = 0; startscan < (Scores - 1); startscan++)

{

minIndex = startscan;

minValue = *(ToTest + startscan);

for (int i = startscan + 1; i < Scores; i++)

{

if (*(ToTest + i) < minValue)

{

minValue = *(Test + i);

minIndex = i;

}

}

*(ToTest + minIndex) = *(ToTest + startscan);

*(ToTest + startscan) = minValue;

}

}

// Calculate Average

double getAverage(double *ToTest, int Scores)

{

double Total;

for (int i = 0; i < Scores; i++)

{

Total += *(ToTest + i);

}

return Total / Scores;

}

// Print Data

void printData(double *ToTest, int Scores, double Avg)

{

cout << "\n Test scores\n";

cout << "Number of scores: " << Scores << endl;

cout << "Scores in ascending-order:\n";

for (int i = 0; i < Scores; i++)

{

cout << "#" << (i + 1) << ": " << *(ToTest + i) << endl;

}

cout << fixed << showpoint << setprecision(2);

cout << "Average score: " << Avg << endl;

}

6 0
2 years ago
Other questions:
  • Many of the special staff teams require leadership training, which is offered to staff with more than 1 year of service at Camp
    15·1 answer
  • A web browser allows you to manage computer files and programs true false
    15·1 answer
  • Which is most harmful computer virus define​
    15·1 answer
  • Select the guidelines you should follow when creating a resume
    12·2 answers
  • Answer the question ASAP please
    15·2 answers
  • What does mean I can’t turn on my computer and my computer won’t charge at all
    15·1 answer
  • What component of a processor holds instructions waiting to be processed by the alu?
    11·1 answer
  • Please help ASAP!
    7·2 answers
  • A state government is attempting to reduce the digital divide. Which of the following activities has the greatest potential to c
    14·1 answer
  • Type the correct answer in the box. Spell all words correctly. Kenny is designing a document for the employee motivation campaig
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!