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 is returned by datetime(1970, 1, 1).strftime('%Y-%d-%B') in Python?
slava [35]

Answer:

1970-01-January

Explanation:

Given

datetime(1970, 1, 1).strftime('%Y-%d-%B')

We start by analysing the give code segment.

date time means that we want to work with dates and time

1970,1,1 is the date time we're working with

strftime represents the format we want the time to be

('%Y-%d-%B) = Year - Day - Month as full name

Analysing the date itself, we have

(1970, 1, 1) = ('%Y-%d-%B')

By comparison

%Y = Year = 1970

%d = Day = 01

%B = Full name of the month

If month = 1, then we understand that the month is January..

So,

%B = January

Bringing the results together;

(1970, 1, 1) = ('%Y-%d-%B') = "1970-01-January"

5 0
4 years ago
Hey yall wanna send me some just ask for my phone #
GuDViN [60]

Answer:

Send you some what?

Explanation:

the answer is 12

7 0
3 years ago
Jerry suspects that information about his internet usage was sent to external websites without his knowledge. He is wary about t
Leviafan [203]

Since advertisement and newsletters are filled in inbox of jerry. There is no external software to do scanning removing unnecessary mails such as advertisement and newsletter.

<u>Explanation:</u>

Best away or practice, jerry has to open each mail of advertisement and newsletter and unsubscribe the email address.

In future jerry should not click any link which comes in the inbox.

Once unsubscribe the newsletter and advertisements better to scan the computer any mail ware or spyware is attached on jerry laptop or workstation or pc.

There is no external web tool is available to unsubscribe in newsletter or advertisement for jerry mailbox.

3 0
3 years ago
Which factors have led to the fast growth in IT​
-Dominant- [34]

Answer:

Factors influencing population growth

Economic development. ...

Education. ...

Quality of children. ...

Welfare payments/State pensions. ...

Social and cultural factors. ...

Availability of family planning. ...

Female labour market participation. ...

Death rates – Level of medical provision

7 0
3 years ago
Create two algorithms for an everyday problem such as computing sales tax or figuring out the number of tables to seat guests. T
tamaranim1 [39]

Answer:

b

Explanation:

3 0
3 years ago
Other questions:
  • Which of the following statements is true?a. Old paper documents, which were previously only available offline, are increasingly
    10·1 answer
  • Each processor or core processes two threads at the same time is called _________.
    5·1 answer
  • Frances is rearranging her furniture. (1) Also, she is moving the heavy oak bookcase to the back bedroom. (2) She has decided to
    7·2 answers
  • Is cloud computing a hardware service used to keep computers working? True or False
    11·1 answer
  • What is DBMS software used for?
    5·1 answer
  • A #1 Phillips screwdriver has a shaft diameter of 
    13·1 answer
  • Some in the security community argue that a lack of diversity is security vulnerability. For example, Firefox and Internet Explo
    6·1 answer
  • Which user characteristic may not be used to change keyword bids in adwords?:?
    11·1 answer
  • You need to secure your wireless network. which security protocol would be the best choice
    10·1 answer
  • Order the numbers (1.1)2, (1.4)10, and (1.5)16 from smallest to largest
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!