Question: The given C source code creates a race condition by which two threads read/write the variable count in an unsynchronized fashion. The count variable initially

 The given C source code creates a race condition by which

The given C source code creates a race condition by which two threads read/write the variable count in an unsynchronized fashion. The count variable initially equals 5, thread1 increments count 3 times, thread2 decrements count 3 times. The final value of count "should" equal the initial value of count but the race condition causes data corruption: #include #include #include #in lude nclude #include int count: // shared resource void *threadl (void *param) ( // threadl is "fast" and increments count char i, tmp: ENIRY SECTION: critical ed "'1+1, count); printf("1st thread Entry#ed / BEGIN CRITICAL SECTION tmpcount aleep () counttmp1: to section, count - END CRITICAL SECTION printf ( "1st thread Exit# d from critical ??ection, count- %d ", i+1, count); EXIT SECTION: pthread exit () void thread2 (void *param) /f thread2 1s "slow" and decrements count char i,tp ENTRY SECTION: print f("2nd thread Entry#9d to critical section, count- adha", iti, count); BEGIN CRITICAL SECTION tmpcount sleep (3) count-tmp-1 // END CRITICAL SECT?ON printf( "2nd thread Exit#8d from critical section, cont- ?d " , i+1, count) ; EXIT SECTION: pthread exic (o) int main ) ( count-5 printf ("Initial value of count = %d ",count); pthread t tidi, tid2: pthread attr_t attr: pthread attr init( iattr pthread create(&tidl, Sater, chreadl,NULL) pthread_create(&tid2 , ?attr, thread2, NULL); pthreadJoin(tidi, NULL) pthread_join(tid2, NULL) printf( "After threads complete, count "%d " , count); exit (0) Part0: Compile the given source code, run and observe the results. o What do you notice about the way that the threads access the count variable concurrently? o With the understanding that thread1 increments count 3 times and thread2 decrements count 3 times, after the threads complete, is the value of count correct Part1: Make the following set of modifications to the program given in Part0: o Modify the given program so that it accepts one command line argument. The command line argument will be converted to integer and used as a random number seed o Modify the threads so that they each sleep for a random amount of time inside their respective critical sections Part2: Use strict alternation to fix the race condition

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!