Prevent the race condition by adding appropriate protection in the form of valid
ID: 3596536 • Letter: P
Question
Prevent the race condition by adding appropriate protection in the form of valid entry/exit sections in both main and second_thread. You may not change the original code in any way – if you were to delete sleep(1)call in main, for example, the race condition would almost certainly seem to go away all right, but that's hardly the point of the exercise, since the two threads would still contain unprotected critical sections and a race condition would still be possible, just not likely (they never are). So leave the code exactly as it is and just add code to create and use a synchronization mechanism for use in the new entry and exit sections you'll create to protect the critical sections. Probably the easiest way to solve this problem is to use pthread_mutex_lock and ...unlock but you're welcome to try to use a binary semaphore instead if you wish.
Explanation / Answer
//Pls find my chenges with ChegEA keyword ,,in main when setting race_point.x and race_point.y and in thread
//declared a variable lock of type pthread_mutex_t before main which is required by lock and unlock functions.
//given whole program ,, even with sleep in main ,, race condition doent occur after using mutex lock and unlock
#include <stdio.h>
#include <pthread.h> /* Needed for the POSIX thread functions, specifically pthread_create() */
#include <errno.h> /* Needed for the perror() function used for debugging system service calls */
#include<unistd.h> /* Need for the sleep function */
/********************************************************************************************************************/
/****************** GLOBAL VARIABLES ********************************************************************************/
/* Here is the shared resource (a global structure) over which the two threads will */
struct /* have critical sections and a race condition. I've initialized the point's */
{ /* coordinates to [0,0] and the idea is that the x and y coordinates of this point */
int x,y; /* are always supposed be remain equal to one another after a series of updates by */
} race_point={0,0}; /* concurrently executing threads */
/********************************************************************************************************************/
/**************************************************************************** Main ******************************/
/*
*/
//Added by Chegg EA for mutex lock and unlock
pthread_mutex_t lock;
int main() /* 'main' is obviously the first thread, no? Every process must have at least one */
{ /* kernel-level thread. In C, that has to be the one that starts executing 'main' */
void *second_thread(void *); /* Prototype, full definition follows 'main' */
pthread_t new_tid_no=0;
int errorCode; /* Used to check for success or failure after the pthread_join */
printf(" Main: Initially, race_point is [0,0]; after creating another thread, main will set it to [1,1] ");
if ( pthread_create(&new_tid_no, NULL, second_thread, NULL) == 0) /* Create another thread; check for success */
{
/****** Put your ENTRY SECTION code here. ******/
/*************** START OF MAIN'S CRITICAL SECTION where we update race_point coordinates to [1,1] *********/
//Added by Chegg EA for mutex lock and unlock
//lock the resource when using it
pthread_mutex_lock(&lock);
race_point.x=1; /* By sleeping here in the middle of the critical section we ensure that the second */
/* thread can start to execute, thus simulating a genuine preemption at exactly */
sleep(1); /* this (horribly wrong) time and guaranteeing that we'll get a race condition. */
/* Comment out the 'sleep' here and you'll see that the race condition */
race_point.y=1; /* disappears; but that's not the way I want you to fix it for this assignment! ;-) */
//ChegEA unlock the resource after using it
pthread_mutex_unlock(&lock);
/*************** END OF THE CRITICAL SECTION **************************************************************/
/****** Put your EXIT SECTION code here. ******/
if ((errorCode = pthread_join(new_tid_no, NULL)) != 0) /* Wait for the second thread to terminate */
{ /* before printing the results */
printf(" Bad pthread_join: %s", strerror(errorCode));
return -1;
}
printf(" Main now done;"); /* Check to see if the race condition occurred and print the results */
if (race_point.x == race_point.y)
printf(" x=%d and y=%d so there was no race condition ", race_point.x, race_point.y);
else
printf(" but since x=%d while y=%d there *HAS* been a race condition. ", race_point.x, race_point.y);
}
else perror(" No thread created "); /* If pthread_create() was unsuccessful, print the reason */
/* */
} /******************* end of function 'main' *********************************************************************/
void *second_thread(void *dummy) /* The thread created by 'main' starts execution here. */
{
/****** Put your ENTRY SECTION code here. ************* */
//Added by Chegg EA for mutex lock and unlock
//lock the resource when using it
pthread_mutex_lock(&lock);
race_point.x=2; /* CRITICAL SECTION, in which the shared resource, the */
race_point.y=2; /* structure named 'race_point', gets updated to [2,2] */
/****** Put your EXIT SECTION code here. ************** */
//ChegEA unlock the resource after using it
pthread_mutex_unlock(&lock);
printf(" Second thread terminating after setting race_point to [2,2] ");
}
-----------------------------------------------------------------------------------------------------
//output after making changes with mutex_lock and unlock
Main: Initially, race_point is [0,0]; after creating another thread, main will set it to [1,1]
Second thread terminating after setting race_point to [2,2]
Main now done; x=2 and y=2 so there was no race condition
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.