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
xenn [34]
3 years ago
14

Write a C program that creates two threads to run the Fibonacci and the Runner processes. Threads will indicate the start and th

e end of their work by printing statements in the format "Thread K Starting" "Thread K Finished", where K is the thread number. When the threads finish, the parent thread will output the results generated by the two threads by printing in the format, "The sum of first 5 numbers is: 15" and "The first 5 numbers in the Fibonacci sequence are: 0, 1, 1, 2, 3" if the user had input 5 as the value of N.
Computers and Technology
1 answer:
OleMash [197]3 years ago
3 0

Answer:

see explaination

Explanation:

#include <pthread.h>

#include <stdio.h>

int sumOfN = 0;

int arr[1000];

int n;

void * findSumOfN(void *a){

printf("Thread 1 Starting\n");

sumOfN = (n * (n+1)) / 2; //finds sum of first n natural numbers

printf("Thread 1 Finished\n");

pthread_exit(0);

}

void * fibonacci(void *a){

printf("Thread 2 Starting\n");

arr[0]=0;

arr[1]=1;

for(int i=2;i<n;i++) //find fibonacci numbers iteratively

arr[i]=arr[i-1]+arr[i-2];

printf("Thread 2 Finished\n");

pthread_exit(0);

}

int main(void){

printf("Please enter the value of n \n");

scanf("%d", &n);

if (n <= 0)

{

printf("Wrong input ! \n"); //input validation

return 0;

}

pthread_t thread1, thread2;

pthread_create(&thread1,NULL, findSumOfN, NULL); //Create threads

pthread_create(&thread2,NULL, fibonacci, NULL);

pthread_join(thread1,NULL); //Start threads

pthread_join(thread2, NULL);

printf("The sum of first %d numbers is : %d \n",n, sumOfN);

printf("The first %d fibonacci numbers are : \n",n);

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

{

printf("%d ", arr[i]);

}

printf("\n");

return(0);

}

You might be interested in
Data values in a program are held in _________. a data types b names c variables d lockers
Tpy6a [65]

Answer:

Answered below

Explanation:

Data values in a program are held in variables. Variables are like containers for holding different types of data. A variable can be identified depending on the kind of data it holds. Variables can hold data types of integers, strings, arrays, lists, sets, Boolean etc. They hold unique data types and a can not hold different data types. Different rules exist for naming variables in different programming languages. A variable name should start with lowercase letters and be descriptive of the data it holds.

7 0
3 years ago
Quality, personal attention, leadership, and respect are examples of what?
tankabanditka [31]
A good employee, everything u named is a good example of a good employee
8 0
3 years ago
Read 2 more answers
The UNIX operating system started the concept of socket which also came with a set of programming application programming interf
VMariaS [17]

Answer:

(a). as the combination of IP address and port number to allow an application within a computer to set up a connection with another application in another computer without ambiguity.

Explanation:

The explanation is in the answer.

3 0
3 years ago
Do these devices allow you to view photos using the cloud?
ivann1987 [24]

Answer:

Yes, You can view the cloud on those devices

8 0
2 years ago
According to the author, "U.S. Security strategy and U.S. Identity towards Internet governance are all anchored in the idea of f
Shalnov [3]

Answer:

The answer

Explanation:

4 0
3 years ago
Other questions:
  • How would you say an hard drive works
    9·2 answers
  • Select the correct answer.
    13·1 answer
  • Write a java program which uses methods for calculating the sum of any 5 non-zero integer digits that are input. The program mus
    8·1 answer
  • A prime number is an integer greater than 1 that is evenly divisible by only 1 and itself. For example, the number 5 is prime be
    11·1 answer
  • ______ is the ability of a system to do more than one thing at a time. A. Multibusing c. online processing b. Multiprocessing d.
    13·1 answer
  • A type of touch screen that can be up to four feet by six feet is a(n) _____.
    12·2 answers
  • Which of the following will increase the level of security for personal and confidential information on a mobile device if the d
    14·1 answer
  • Lập trình web truy vấn csdl và hienr thị ra màn hình danh sách các bản ghi
    15·1 answer
  • Analog computers are general purpose computers : true ? or false ?​
    8·1 answer
  • Why wont my account update from Expert to Ace? I have all the points and branliests to be Ace but it wont update
    14·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!