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?
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
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
![](/static/compass_v2/shared-icons/check-mark.png)
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 2 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
Knowledge Booster
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.Recommended textbooks for you
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
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)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
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)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education