Question
Help fixing my code I need help fixing my code. You can find my code down below and the output it's suppose to produce. It's
Help fixing my code
I need help fixing my code. You can find my code down below and the output it's suppose to produce. It's threads question where it should take input and the number of lines of the input should be calculated and it should be decided how many lines each thread should read. For example, if the input contains 10 lines and there are 5 threads, the threads must read 2 lines at a time. When this value is fractional, the value should be rounded up. For example, if a 14-line file is being read and the number of threads at hand is assumed to be 5, 3 lines will be read in each thread operation. And at the end Each thread will return its own sums after line reading and calculation, and print the total value to the screen. So, basically the output down below should be printed when given the input below.
..................................................................................................................
sample input:
1.23 2.66 3.45 81.23 9.11 3.33 7.78 4.12 1.11 2.22
output:
Thread 1 reads 1. line. The value is 1.23 Thread 1 reads 2. line. The value is 2.66 Shared Resource: 3.89 Thread 2 reads 3. line. The value is 3.45 Thread 2 reads 4. line. The value is 81.23 Shared Resource: 88.57 Thread 3 reads 5. line. The value is 9.11 Thread 3 reads 6. line. The value is 3.33 Shared Resource: 101.01 Thread 4 reads 7. line. The value is 7.78 Thread 4 reads 8. line. The value is 4.12 Shared Resource: 112.91 Thread 5 reads 9. line. The value is 1.11 Thread 5 reads 10. line. The value is 2.22 Shared Resource: 116.24
..................................................................................................................
#include
#include
#include
#include
// The name of the input file
#define INPUT_FILE "input.txt"
// The number of threads
#define NUM_THREADS 5
// Global variables
sem_t sem;
double total_sum = 0;
FILE* file;
// Thread function
void* thread_func(void* arg) {
int thread_num = *((int*) arg);
int num_lines = 0;
double thread_sum = 0;
// Lock the semaphore to protect access to the file and total_sum variables
sem_wait(&sem);
// Read the number of lines in the file
fseek(file, 0, SEEK_END);
num_lines = ftell(file) / sizeof(double);
rewind(file);
// Calculate the number of lines each thread should read
int lines_per_thread = num_lines / NUM_THREADS;
if (num_lines % NUM_THREADS > 0) {
lines_per_thread++;
}
// Read the specified number of lines from the file and calculate the thread's sum
double value;
for (int i = 0; i < lines_per_thread; i++) {
fscanf(file, "%lf", &value);
thread_sum += value;
printf("Thread %d reads %d. line. The value is %.2lf ", thread_num, (i + 1), value);
}
// Add the thread's sum to the total sum
total_sum += thread_sum;
printf("Shared Resource: %.2lf ", total_sum);
// Unlock the semaphore
sem_post(&sem);
return NULL;
}
int main() {
// Initialize the semaphore
sem_init(&sem, 0, 1);
// Open the input file
file = fopen(INPUT_FILE, "r");
if (file == NULL) {
fprintf(stderr, "Error opening file %s ", INPUT_FILE);
exit(EXIT_FA
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started