Try switching the joins pthread_join(tid2, NULL); pthread_join(tid1, NULL); Do you see any difference? Please report if and why you do or do not see any difference in terms of the randomness in the result of the shared resource.  In the inc_dec_resource() function, implement mutual exclusion (pthread_mutex_lock) to ensure that the result becomes 0 every time when you execute your program. Put your updated code in the report (highlighted) and show your screenshot of the execution by running the script three time using $ time ./shared_resource_mutex. Hint: Your loop is incrementing/decrementing the resource which should be protected by each thread while it is executing that portion.   /* Compile: gcc -o shared_resouce_mutex shared_resource_mutex.c -lpthread Execute: ./shared_resource_mutex */   #include  #include    #define iterations 300000000 long long shared_resource = 0;   pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // Thread function to modify shared resource void* inc_dec_resource(void* arg){     //get the pointer from main thread and dereference it to put the value in resource_value     int resource_value = *(int *) arg;     for(int i=0; i < iterations; i++){         shared_resource += resource_value;     }     pthread_exit(NULL); }   int main(void){       // Thread 1 to increment shared resource     pthread_t tid1, tid2;     int value1 = 1;     pthread_create(&tid1, NULL, inc_dec_resource, &value1);     // Thread 2 to increment shared resource     int value2 = -1;     pthread_create(&tid2, NULL, inc_dec_resource, &value2);     pthread_join(tid1, NULL);     pthread_join(tid2, NULL);     printf("Shared resource value: %lld\n", shared_resource);       return

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
  • Try switching the joins

pthread_join(tid2, NULL);

pthread_join(tid1, NULL);

Do you see any difference? Please report if and why you do or do not see any difference in terms of the randomness in the result of the shared resource. 

  • In the inc_dec_resource() function, implement mutual exclusion (pthread_mutex_lock) to ensure that the result becomes 0 every time when you execute your program. Put your updated code in the report (highlighted) and show your screenshot of the execution by running the script three time using $ time ./shared_resource_mutex. Hint: Your loop is incrementing/decrementing the resource which should be protected by each thread while it is executing that portion.

 

/*

Compile: gcc -o shared_resouce_mutex shared_resource_mutex.c -lpthread

Execute: ./shared_resource_mutex

*/

 

#include <stdio.h>

#include <pthread.h>

 

#define iterations 300000000

long long shared_resource = 0;

 

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

// Thread function to modify shared resource

void* inc_dec_resource(void* arg){

    //get the pointer from main thread and dereference it to put the value in resource_value

    int resource_value = *(int *) arg;

    for(int i=0; i < iterations; i++){

        shared_resource += resource_value;

    }

    pthread_exit(NULL);

}

 

int main(void){

 

    // Thread 1 to increment shared resource

    pthread_t tid1, tid2;

    int value1 = 1;

    pthread_create(&tid1, NULL, inc_dec_resource, &value1);

    // Thread 2 to increment shared resource

    int value2 = -1;

    pthread_create(&tid2, NULL, inc_dec_resource, &value2);

    pthread_join(tid1, NULL);

    pthread_join(tid2, NULL);

    printf("Shared resource value: %lld\n", shared_resource);

 

    return 0;

}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY