95.8k views
1 vote
Write a POSIX multithreaded program (in C) called thread.c that works as follows: A list of integers will be given as an input to the program. Create two separate worker threads, where Thread-1 will find out the sum of all elements in the array and outputs the array that has the elements as (sum+array[i]). Thread-2 will find out the sum of the elements in the array and outputs the array that has the elements as sum-array[i]. The output of the entire program is two arrays from the two threads. You need to collect the output of the program into a new file.

Example:
Given the list of integers -{7, 2, 1, 3, 8, 4, -5, 6, 9}
Thread 1: will find out the sum of the array, in the above example the sum is 35. Now the output array should have elements with values as (sum+array[I]). So the output will be
{42 , 37 , 36 , 38 , 43 , 39 , 30 , 41 , 44}
The first element of the output array = 35+array[1]= 35+ 7 = 42.
The second element of the output array = 35+array[2] = 35+2 = 37 and so on.
Thread 2: will find out the sum of the array, in the above example the sum is 35. Now the output array should have elements with values as (sum-array[I]). So the output will be
{28 , 33 , 34 , 32 , 27 , 31 , 40 , 29 , 26}
The first element of the output array = 35 - array[1] = 35- 7 = 28.

The second element of the output array = 35 - array[2] = 35 - 2 = 33 and so on.

1 Answer

2 votes

Final answer:

To solve this problem, you can use the POSIX threads API in C. Create two separate worker threads to calculate the sum of the array and modify the elements accordingly. An example implementation of the program is provided.

Step-by-step explanation:

To solve this problem, you can use the POSIX threads API in C. First, create two separate worker threads using the pthread_create function. In Thread-1, calculate the sum of all elements in the array and store it in a variable. Then, iterate over the array and calculate the output values as (sum + array[i]). In Thread-2, calculate the sum of all elements in the array and store it in a variable. Then, iterate over the array and calculate the output values as (sum - array[i]).

Here is an example implementation of the program:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

#define ARRAY_SIZE 9

int array[ARRAY_SIZE] = {7, 2, 1, 3, 8, 4, -5, 6, 9};
int sum;

void* thread1(void* arg) {
sum = 0;
for (int i = 0; i < ARRAY_SIZE; i++) {
sum += array[i];
}

for (int i = 0; i < ARRAY_SIZE; i++) {
array[i] = sum + array[i];
}
pthread_exit(NULL);
}

void* thread2(void* arg) {
sum = 0;
for (int i = 0; i < ARRAY_SIZE; i++) {
sum += array[i];
}

for (int i = 0; i < ARRAY_SIZE; i++) {
array[i] = sum - array[i];
}
pthread_exit(NULL);
}

int main() {
pthread_t t1, t2;

pthread_create(&t1, NULL, thread1, NULL);
pthread_create(&t2, NULL, thread2, NULL);

pthread_join(t1, NULL);
pthread_join(t2, NULL);

// Print the output arrays
printf("Thread 1 Output: ");
for (int i = 0; i < ARRAY_SIZE; i++) {
printf("%d ", array[i]);
}
printf("\\");

printf("Thread 2 Output: ");
for (int i = 0; i < ARRAY_SIZE; i++) {
printf("%d ", array[i]);
}
printf("\\");

return 0;
}



In this program, we use the pthread_create function to create two threads, t1 and t2. In the thread1 function, the sum of the array is calculated and stored in the sum variable. Then, the output array is calculated as (sum + array[i]). Similarly, in the thread2 function, the sum is calculated and the output array is calculated as (sum - array[i]). Finally, the output arrays are printed.

User Pseudoremy
by
7.7k points