Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The program you will create will perform the calculation ( n1 * n2 ) / n3 on groups of three numbers. You only need to

The program you will create will perform the calculation ( n1 * n2 ) / n3 on groups of three numbers. You only need to read a maximum count of 100 groups of 3 numbers, or 300 numbers, either from a file or from stdin. The numbers will not be passed on the command line. Your program will perform this calculation across the data using two threads: the main thread and a child thread. The first half of the numbers read will be calculated by the child thread. The second half of the numbers read will be calculated by the main thread after the child thread has started but before joining with the parent thread.

The main thread will read all the numbers into an integer array. The main thread will then create a child thread using the pthread_create(...) API, passing half of the integer array as a parameter to the thread. After the child thread has been launched, the parent thread will perform the calculation on the second half of the integer array, storing the results to be displayed after joining to the child thread.

The child thread will perform the calculation from three of the integers at a time from the first half of the array and store the result into a floating-point array. This floating-point array will be returned to the parent thread. This floating point array will not be allocated on the stack of the thread, but any other allocation method is fine. The child thread must not display any data. Your only output should be produced by the main thread, and only after waiting for the thread to complete.

The parent thread will receive the results via the pthread_join(...) API parameter. Even if the results are stored in a floating-point array that is local to the main thread, the pointer to that array must be passed into the return result variable of pthread_join(...) API. The parent thread will then display the results calculated by the child thread, followed by the results calculated by the main thread. No global variables are allowed for this assignment. Only one thread should be created, and that one thread should perform the first half of the calculations. All other calculations should be made by the main thread.

If there is only one set of three numbers to read, then the child thread should perform the calculation and the main thread should not calculate any.

code

#include<stdio.h>

#include<pthread.h>

void *thread_func(void *ptr) {

/* Cast the void pointer to something useful */

/* Prepare a way for the results to be returned to the parent thread */


/* Calculate the results and store them to be returned to the parent thread */

/* Return the results */

return NULL;

}

int main(int argc, char *argv[]) {

printf("You need to finish this programn");

pthread_t handle;

/* Prepare your memory where you will store the incoming data */

/* You only need to handle 100 groups of three integers */

FILE *fp = stdin;

if(argc > 1) {

fp = fopen(argv[1], "r");

if(fp == NULL) {

perror("Unable to open file");

return 1;

}

}

/* Next, read numbers in groups of three */

int n1, n2, n3;

while(3 == fscanf(fp, "%d %d %d", &n1, &n2, &n3)) {

/* Just store the numbers you read into the memory you prepared earlier */

/* You will also likely keep track of how many groups of three integers have been read */

}

/* Now create the thread and send half the numbers */

/* Once the thread has started, this main thread should calculate the other half of the numbers */

/* Now join to the child thread, receiving the results */

/* And print the results out */

/* Make sure you clean up any memory you allocated */

}

Step by Step Solution

3.41 Rating (154 Votes )

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Java Programming

Authors: Joyce Farrell

9th edition

1337397075, 978-1337397070

More Books

Students also viewed these Operating System questions

Question

What is a contra-asset? Give an example of one.

Answered: 1 week ago

Question

The symbol Answered: 1 week ago

Answered: 1 week ago