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]
4 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]4 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
What is the name of the keyboard developed in the 1930s?
Alexandra [31]
The Dvorak Keyboard.
4 0
4 years ago
Which process is based on repulsion of oil and water?
Tasya [4]
E.........................
4 0
3 years ago
I need a quick way to see all hardware connected to my computer.
timofeeve [1]
Look it up..............
7 0
3 years ago
Which of the following statement is true for Service Request Floods A. An attacker or group of zombies attempts to exhaust serve
klio [65]

Answer:

The answer is "Option A and Option B"

Explanation:

This is a type of attack, which is mainly used to bring down a network or service by flooding large amounts of traffic. It is a high-rate server from legitimate sources, where an attacker or group of zombies is attempting to drain server resources by creating and disconnecting the TCP link, and wrong choices can be described as follows:

In option C, It can't initiate a single request because when the servers were overloaded with legitimate source links, the hacker may then set up and uninstall TCP links.

7 0
3 years ago
In object oriented programming, what is another name for the "attributes" of an object?
Ket [755]
I believe the word you're looking for is properties.
7 0
3 years ago
Other questions:
  • Match terms in the first column with the order descriptions in column two.
    11·1 answer
  • What do you adjust to allow more or less vertical space between lines of a paragraph?
    14·1 answer
  • Which of the following is not the name of an air mass?
    7·1 answer
  • Which of the following is optional equipment on a basic life support​ ambulance? A. Lubricating jelly B. Flashlights C. Disinfec
    11·1 answer
  • 5. How would you describe the relationship between blocks of code and commands?​
    14·2 answers
  • Write a C++ program that produces a simple personalized adventure game called Lost Fortune about a band of explorers that finds
    12·1 answer
  • Which of the following is not part of the processes involved in data valida
    11·1 answer
  • Trade Periodicals are usually highly scholarly resources<br> True<br><br> False
    5·1 answer
  • Keith would like to compare data he has collected from research. The data includes the electrical output
    13·1 answer
  • What report indicates where users start or exit the conversion funnel?.
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!