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?

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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 <stdio.h> // printf()
  #include <stdlib.h> // atoi(), exit(), ...
  #include <string.h> // strtok(), strcmp(), ...
  #include <pthread.h> // 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, " \n");
  while(token != NULL){
  line_sum += atoi(token);
  token = strtok(NULL, " \n");
  }
  // 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\n");
  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\n", sum);
  fclose(file);
  pthread_mutex_destroy(&sum_mutex); // destroys the mutex variable
  pthread_exit(NULL); // destroys main thread
  }
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Datatypes
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education