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
Were Al Zink’s actions that of someone trying to be an invisible Sponsor
AlexFokin [52]
Al Zink actions differentiate him as trying to take an invisible role as a project sponsor. That is, Al Zink does not do his chosen role and accountability as the lead project sponsor. Though, in dissimilar occasions, Al Zink is seen to evade making a serious choice regarding the development of the project. Consequently, as a project sponsor, it is his accountability to deliver clear steps in increasing the project, recover every step of project development and balance the start of the project. 
7 0
3 years ago
Although fun and entertainment are the primary functions of video games, they have other uses, as well. An
Zigmanuir [339]

The problem that you think could be addressed by using video games is financial  management skills.

<h3>What problem does gaming handles?</h3>

A lot of Scientific research have seen that video games improve people's creativity and also their skill in terms of problem-solving skills.

Note that it can also help to improve financial management skills and also make people to have stable jobs and good source of income.

Learn more about video games from

brainly.com/question/8870121

6 0
1 year ago
I need help with my homework
lukranit [14]

Answer:

C is not equal to the other two.

Explanation:

A = 35

B = 35

C is actually equal to 36.

8 0
2 years ago
in java how do i Write a method named isEven that accepts an int argument. The method should return true if the argument is even
Veseljchak [2.6K]
<h2>Answer:</h2><h2>============================================</h2>

//Class header definition

public class TestEven {

   

   //Method main to test the method isEven

   public static void main(String args[ ] ) {

       

       //Test the method isEven using numbers 5 and 6 as arguments

       System.out.println(isEven(5));

       System.out.println(isEven(6));

     

   }

   

   //Method isEven

   //Method has a return type of boolean since it returns true or false.

   //Method has an int parameter

   public static boolean isEven(int number){

       //A number is even if its modulus with 2 gives zero

      if (number % 2 == 0){

           return true;

       }

       

       //Otherwise, the number is odd

       return false;

   }

}

====================================================

<h2>Sample Output:</h2>

=========================================================

false

true

==========================================================

<h2>Explanation:</h2>

The above code has been written in Java. It contains comments explaining every part of the code. Please go through the comments in the code.

A sample output has also been provided. You can save the code as TestEven.java and run it on your machine.

3 0
3 years ago
Given the following class, public class Magazine { private static double SUBSCRIPTION_DISCOUNT = .60; private String title;
jonny [76]

Answer:

The answer is "III only"

Explanation:

An accessor method is also an instance function that receives or establishes the value of an object's properties. This method is called methods through obtaining information about an entity. This method reverses it to process, which has two accessory string techniques to reach source string data reference length Accessor method.

6 0
3 years ago
Other questions:
  • Individually or in a group find as many different examples as you can of physical controls and displays.a. List themb. Try to gr
    15·1 answer
  • A _____ is a device that not only provides surge protection, but also furnishes desktop computers and network devices with batte
    7·1 answer
  • The best defenses against covert channels include IDS and intrusion prevention system (IPS) and thoroughly watching all aspects
    15·1 answer
  • What types of storage can be used to access your data on another computer?
    6·2 answers
  • In the Advent of computer technologies and it's applications, to what extent these technologies have influenced the world.
    11·1 answer
  • What is adobe photoshop?
    10·2 answers
  • Choose the type of collection described.
    9·1 answer
  • Explaio mode of Operation of x-ray​
    15·2 answers
  • FOLLOW INSTRUCTIONS BELOW , WRITTEN IN JAVA LANGUAGE PLEASE AND THANK YOU !
    10·1 answer
  • Which 1947 Invention paved the way for the Digital Revolution?
    12·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!