Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with the following questions: 1) What value needs to be synchronized with the sum_mutex variable? Why? 2) If you remove the pthread_mutex_lock and

Need help with the following questions:

1) What value needs to be synchronized with the sum_mutex variable? Why?

2) If you remove the pthread_mutex_lock and pthread_mutex_unlock statement from the program, does it still produce a correct output? Why or why not?

Program:

#include // printf()

#include // atoi(), exit(), ...

#include // strtok(), strcmp(), ...

#include // pthread types and functions

#define MAX_LINE_LENGTH 100

#define NUM_THREADS 4

int sum = 0;

pthread_mutex_t sum_mutex; // Mutex variables help keep the threads synchronized

void *add_line_numbers(void *arg) {

char *line = (char *) arg;

int line_sum = 0;

// TODO Break down the line and add every number

char *token = strtok(line, " ");

while(token != NULL){

line_sum += atoi(token);

token = strtok(NULL, " ");

}

// What does pthread_mutex_lock/unlock do?

// It ensures the section between this lock/unlock statements can only be executed by 1 process,

// if another needs access it must wait until the first runs the unlock statement

pthread_mutex_lock(&sum_mutex);

// TODO Add the result of the line to the global sum

int x = sum;

x += line_sum;

sum = x;

pthread_mutex_unlock(&sum_mutex);

pthread_exit(NULL);

}

int main() {

FILE *file;

char line[MAX_LINE_LENGTH];

pthread_t threads[NUM_THREADS];

pthread_mutex_init(&sum_mutex, NULL);

file = fopen("numbers.txt", "r");

if (file == NULL) {

printf("Error: cannot open file ");

exit(EXIT_FAILURE);

}

int line_count = 0;

while (fgets(line, MAX_LINE_LENGTH, file) != NULL) {

// TODO Create your threads with the given line (remember sharing address locations is not good with threads)

char *thread_line = (char*)malloc(MAX_LINE_LENGTH);

strncpy(thread_line,line,MAX_LINE_LENGTH);

pthread_create(&threads[line_count],NULL,add_line_numbers,(void*)thread_line);

line_count++;

}

int i;

for (i = 0; i < NUM_THREADS; i++) {

// TODO Wait until all threads finish reading the lines

pthread_join(threads[i],0);

}

printf("Sum of all numbers in the file is %d ", sum);

fclose(file);

pthread_mutex_destroy(&sum_mutex); // destroys the mutex variable

pthread_exit(NULL); // destroys main thread

}

Step by Step Solution

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

Step: 3

blur-text-image

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

Database Security XI Status And Prospects

Authors: T.Y. Lin, Shelly Qian

1st Edition

0412820900, 978-0412820908

More Books

Students also viewed these Databases questions