Question

In: Computer Science

Sorry, I made a big mistake in my question bellow posted yesturday. The while statement is...

Sorry, I made a big mistake in my question bellow posted yesturday. The while statement is x<10000 not x < 2.

Also for thread A: the printf is: printf("I am Thread 1: var_A = %d, var_B = %d \n", var_A, var_B) ;

Please help me and consider the new question below: .

Compile and run the deadlockFree.c program below and report if this program does go into deadlock or not.

Modify this deadlockFree.c program so that you change the lock/unlock order of the mutexes. Experiment with the order of locks (and maybe extend the number of loop iterations) until you find a scenario where you can demonstrate deadlock.

//Program: deadlockFree.c A simple threaded program to demonstrate the deadlock condition.Two threads are used to wait on two MUTEXES. Careful ordering of locks should prevent deadlock. Try this and see does deadlock occur.Play with lock order to see if you can achieve deadlock!

#include<stdio.h>

#include<pthread.h>

#include<stdlib.h>

//Function declarations

void *functionA() ;

void *functionB() ;

int var_A, var_B = 0 ; // The shared variables

//Declare mutex

pthread_mutex_t   mutex1 = PTHREAD_MUTEX_INITIALIZER;

pthread_mutex_t   mutex2 = PTHREAD_MUTEX_INITIALIZER;

// MAIN

int main() /*MODIFICATION by "int" type data*/

{

    pthread_t   thread1, thread2 ;

   

    // declare 2 threads

   

    int   rc1, rc2 ;

   

    // Message to the terminal

   

    printf ("\033[2J") ;

   

    // Clear the screen

   

printf ("Deadlock experiment with MUTEX. Does this program lead to deadlock?\n") ;

printf ("=============================================================\n\n") ;

// Create first thread

if ((rc1=pthread_create(&thread1, NULL, &functionA, NULL)))

    {

        printf("Error in creating %d\n",rc1) ;

       

    }

       

        // Create next thread

if ((rc2=pthread_create(&thread2, NULL, &functionB, NULL)))

    {

        printf("Error in creating %d\n",rc2) ;

           

    }

// Remember to use the thread joins

       

pthread_join( thread1, NULL) ;

pthread_join( thread2, NULL) ;

       

pthread_mutex_destroy(&mutex1) ;

pthread_mutex_destroy(&mutex2) ;

       

// main exits

exit(0) ;

   

}

// Thread 1

void *functionA()

{

int   x = 0 ;

// interation counter for the While loop

while (x < 10000)

    {

        pthread_mutex_lock(&mutex1) ;

        printf ("I'm T1, just locked mutex1\n") ;

        pthread_mutex_lock(&mutex2) ;

        printf ("I'm T1, just locked mutex2\n") ;

       

        var_A++ ;

        var_B++ ;

       

        printf("I am Thread 1: var_A = %d, var_B = %d \n", var_A, var_B) ;

        pthread_mutex_unlock(&mutex2) ;

        printf ("I'm T1, just freed mutex2\n") ;

        pthread_mutex_unlock(&mutex1) ;

        printf ("I'm T1, just freed mutex1\n") ;

        x++ ;

       

    }

pthread_exit (NULL) ; // Thread exits

   

}

// Thread 2

void *functionB() {

int x = 0 ;

// interation counter

while (x < 10000)

    {

   

    pthread_mutex_lock(&mutex1) ;

    printf ("I'm T2, just locked mutex1\n") ;

    pthread_mutex_lock(&mutex2) ;

    printf ("I'm T2, just locked mutex2\n") ;

    var_A++ ;var_B++ ;

   printf("I am Thread 2: var_A = %d, var_B = %d \n", var_A, var_B) ;

    pthread_mutex_unlock(&mutex2) ;

    printf ("I'm T2, just freed mutex2\n") ;

    pthread_mutex_unlock(&mutex1) ;

    printf ("I'm T2, just freed mutex1\n") ;

    x++ ;

       

    }

pthread_exit (NULL) ; // Thread exits

   

}

Solutions

Expert Solution

The given code does not and cannot go into a deadlock because both threads lock the mutex in the same order, mutex1, followed by mutex2.

In order for a deadlock to be possible, the threads must lock the mutex in different orders. The deadlock occurs when thread1 has locked mutex1 and is waiting for mutex2, while thread2 has locked mutex2 and is waiting for mutex1.

The modified code is given below, followed by the output snapshot demonstrating the deadlock. T1 has locked mutex1 and T2 has locked mutex2. T1 is waiting for mutex1 while T2 is waiting for mutex2. Thus, a deadlock is created.

//Program: deadlockFree.c A simple threaded program to demonstrate the deadlock condition.Two threads are used to wait on two MUTEXES. Careful ordering of locks should prevent deadlock. Try this and see does deadlock occur.Play with lock order to see if you can achieve deadlock!

#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>

//Function declarations
void *functionA() ;
void *functionB() ;

int var_A, var_B = 0 ; // The shared variables

//Declare mutex
pthread_mutex_t   mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t   mutex2 = PTHREAD_MUTEX_INITIALIZER;

// MAIN
int main() /*MODIFICATION by "int" type data*/
{
    pthread_t   thread1, thread2 ;

    // declare 2 threads
    int   rc1, rc2 ;

    // Message to the terminal
    printf ("\033[2J") ;

    // Clear the screen
    printf ("Deadlock experiment with MUTEX. Does this program lead to deadlock?\n") ;
    printf ("=============================================================\n\n") ;
    // Create first thread
    if ((rc1=pthread_create(&thread1, NULL, &functionA, NULL)))
    {
        printf("Error in creating %d\n",rc1) ;
    }

    // Create next thread
    if ((rc2=pthread_create(&thread2, NULL, &functionB, NULL)))
    {
        printf("Error in creating %d\n",rc2) ;
    }
    // Remember to use the thread joins

    pthread_join( thread1, NULL) ;
    pthread_join( thread2, NULL) ;

    pthread_mutex_destroy(&mutex1) ;
    pthread_mutex_destroy(&mutex2) ;

    // main exits
    exit(0) ;
}

// Thread 1
void *functionA()
{
    int   x = 0 ;
    // interation counter for the While loop
    while (x < 10000)
    {
        pthread_mutex_lock(&mutex1) ;
        printf ("I'm T1, just locked mutex1\n") ;
        pthread_mutex_lock(&mutex2) ;
        printf ("I'm T1, just locked mutex2\n") ;

        var_A++ ;
        var_B++ ;

        printf("I am Thread 1: var_A = %d, var_B = %d \n", var_A, var_B) ;
        pthread_mutex_unlock(&mutex2) ;
        printf ("I'm T1, just freed mutex2\n") ;
        pthread_mutex_unlock(&mutex1) ;
        printf ("I'm T1, just freed mutex1\n") ;
        x++ ;
    }
    pthread_exit (NULL) ; // Thread exits
}

// Thread 2
void *functionB() {
    int x = 0 ;
    // interation counter
    while (x < 10000)
    {
        pthread_mutex_lock(&mutex2) ;
        printf ("I'm T2, just locked mutex2\n") ;
        pthread_mutex_lock(&mutex1) ;
        printf ("I'm T2, just locked mutex1\n") ;
        var_A++ ;var_B++ ;
        printf("I am Thread 2: var_A = %d, var_B = %d \n", var_A, var_B) ;
        pthread_mutex_unlock(&mutex1) ;
        printf ("I'm T2, just freed mutex1\n") ;
        pthread_mutex_unlock(&mutex2) ;
        printf ("I'm T2, just freed mutex2\n") ;
        x++ ;
    }
    pthread_exit (NULL) ; // Thread exits
}


Related Solutions

sorry no question - this was a mistake - please withdraw my question
sorry no question - this was a mistake - please withdraw my question
Hello my name is Pete. I posted my previous question in error by forgetting to mention...
Hello my name is Pete. I posted my previous question in error by forgetting to mention that I am located in New York State and am required to go by those rules and regulations. Erica Swanson (SSN 376-38-4930), age 46, is a single parent with three children: Her biological son, Sean Swanson (age 19) is a part-time college student who works fulltime as a landscaper Her biological daughter, Brooklyn Swanson (age 14) Her adopted daughter, Sydney Swanson (age 12) Although...
I am taking history of psychology the bellow is my question Identify something new that you...
I am taking history of psychology the bellow is my question Identify something new that you learned about Darwin in this unit. For example, beyond his famous theory of evolution, did you know his thoughts on geography or slavery, or were you aware of his five-year voyage? Additionally, use this forum to brainstorm with your professor and classmates on contributions of early theorists, such as how Weber’s two-point threshold speaks to modern neurology.
I'm sorry I didn't post the question in details ,this is the full question , Using...
I'm sorry I didn't post the question in details ,this is the full question , Using the acronym, A.C.T.I.O.N identify and explain the sequence of the tool recommends you follow to address all medication problems 2) Give a brief description of the focus in each section, Situation : Your client is 84 and has rheumatoid arthritis, Each week the RN distributes her medication for each day in a diverted. Today your client's joints seem very inflamed and she asks you...
second part of the previous question already i have posted Income statement for Mars for 2018...
second part of the previous question already i have posted Income statement for Mars for 2018 and 2019 follows: Particulars 2019 ($) 2018 ($) Sales 250,000 180,000 Cost of Goods sold 140,000 110,000 Income before taxes 25000 25000 Income Tax expenses 4000 3000 Selling expenses 20,000 19,000 Interest expenses 3,500 4,500 Compute the ratios from the given balance sheet extract for 2018. Assets $ Cash 30,000 Marketable securities 17,000 Accounts receivables 12,000 Stock 10,000 Land and Building 200,000 Accumulated depreciation...
I need implement the operation bellow. Please add comment on some lines to compare with my...
I need implement the operation bellow. Please add comment on some lines to compare with my code. The program will implement the following operations on the Matrix class: 1. Matrix addition 2. Matrix subtraction 3. Matrix multiplication 4. Matrix scalar multiplication 5. Matrix transpose Vector class will implement the following operations: 1. Vector length 2. Vector inner (dot product) 3. Vector angle Vector 3D Class The class Vector3D is derived from the Vector class, and it implements a vector in...
I posted this question looking for a new paper not an answer that's already posted on...
I posted this question looking for a new paper not an answer that's already posted on chegg since if someone already turned in that paper it will show as a copied paper... the question is.. write a paper of 300-600 words presenting your analysis of the following topic- Discuss shortages and surpluses and how they re-direct resources
Paraphrase material below A lot of the material I find myself relating to my son sorry...
Paraphrase material below A lot of the material I find myself relating to my son sorry if this seems a bit repetitive but I feel that this discussion board too can be related to my nine month old son. I have found it so amazing as my son has developed that he has known my voice as his mother. I think that part of it is the nurture factor that I have been there for him consistently in the sense...
I am currently learning about internal influences in my marketing class. The statement was made that...
I am currently learning about internal influences in my marketing class. The statement was made that "Consumers are often very passionate about their attitudes and perceptions". The questions are: I) Think about brand loyalty and think of some attitudes that consumers hold and look beyond trying to assign rationality to attitudes. 2) What are some of the reasons a persons attitude towards a particular brand does not necessarily mean it is rational at all?
I need step by step to know where is my mistake ? Account Name: Credit Card...
I need step by step to know where is my mistake ? Account Name: Credit Card – Visa $0 November 30, 2021 Checking - Raincross Business Bank 4673 $13,000 November 30, 2021 Savings - Raincross Business Bank$5,000 November 30, 2021 Display your trial balance report for 12/01/2021 Wednesday, December 1, 2021 •Received cash from Dan Greany for Hand Wash with Interior and Electronic Odor Elimination services, using Sales Receipt number 21-102. (Remember to deposit to Undeposited Funds.) •Received check number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT