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
How to code for a database?​
olganol [36]

Answer: <em><u> How to code for a database?​ A database is an organized collection of data, generally stored and accessed electronically from a computer system. Where databases are more complex they are often developed using formal design and modeling techniques. </u></em>

WWW. Wikipedia.com

Explanation: When creating a database first you need to make a software program then you need to start planing on what to do ...

{ A database management program (DBMS) is a software package design designed to manipulate, define, retrieve, and manage data in database.

A DBMS generally manipulates the data itself, the the data format, field names, record structure and file structure.}

<h3><u><em>HoPe ThIs HeLpS YoU!!</em></u></h3>

<u><em /></u>

7 0
2 years ago
What Windows utility can enable you to shut down an unresponsive application?
boyakko [2]

Answer:

Task manager

Explanation:

Allows you to force quit applications.

4 0
2 years ago
Read 2 more answers
When you use the mvc pattern for a java web application, you often add ________________________ to the request object before you
aivan3 [116]
Hi,

JVM - Java Virtual Machine

Hope this helps.
r3t40
6 0
3 years ago
Where is the best place to self learn c++ or c# that is free?
Elina [12.6K]

Answer:

udemy

Explanation:

Hello guys, you might know that C++ is one of the most popular and powerful object-oriented programming languages and if you want to do low-level stuff then it’s the best language to start with.

You will find applications written in C++ on a wide range of fields like embedded programming, server-side application, gaming, and even high-frequency trading applications. Most of the complex software like Operating Systems, Database Management Systems, and Powerful trading systems are written in C++.

While many of us have learned to program by using C and C++, let me honest with you learning C++ is challenging. There are many areas that are tough to grasp like points but you will learn them slowly and that’s where these free C++ courses will help you.

There is no doubt that learning C++ will open several opportunities for you. There is a huge demand for good C++ developers, especially in the area of high-frequency trading where every microsecond matter and power of C++ is absolutely needed.

C++ is the language which provides the best of both world, it allows you to use OOP to manage the complexity of software but at the same time, it also allows you to get close to your machine and access all of your computer’s hardware, which is not easily possible for other object-oriented languages like Java or Python.

That’s the reason C++ is heavily used for writing native device drivers, high-end desktop games and complex artificial intelligence programs where you need high performance.

In this article, I am going to share with you some of the best and free courses to learn C++ online at your own time and place and free of cost. You can use these courses if you are starting with programming or you have some experience in coding but not familiar with C++.

4 0
3 years ago
Read 2 more answers
Java provides a number of interfaces and classes to systematically implement collections.
solmaris [256]

Answer:

A) True

Explanation:

Java provides collections architecture or framework used to store and manipulate a group of objects or collections.

The collection framework has interfaces which include; Set, Queue, Deque, List, as well as classes which include; Hashset, ArrayList, LinkedList, LinkedHashset, PriorityQueue, Vector and TreeSet.

There are also many methods declared in the collection interface which include; add(), addAll(), remove(), removeAll(),retainAll(), clear(), size(), iterator(), toArray() etc

4 0
3 years ago
Other questions:
  • Which statement about the Paste Link option is true? A)Even if the source file (spreadsheet) is deleted, the data updates automa
    11·1 answer
  • The algorithm ADDN implements N-bit fixed-width binary addition for non-negative integers and ignores overflows. For example, AD
    13·1 answer
  • Which type of styles can be applied to a word, phrase, or sentence?
    7·1 answer
  • private members of a class are accessible only from _____________ of the same class or from their friends
    12·1 answer
  • Technical term of stealing personal information in social media.​
    5·1 answer
  • In the last two decades, how have cameras evolved?
    12·2 answers
  • Two types of are desktop computers and notebook computer
    11·2 answers
  • Write a program that asks the user for the name of a file. The program should display the contents of the file with each line pr
    13·1 answer
  • What does business informWhat does business information management do?
    12·1 answer
  • write an algorithm to determine a student's final grade and indicate whether it is passing or failing. the final grade is calcul
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!