Ignore my answer its wrong. 2 (20): The following program creates two threads, B
ID: 3874491 • Letter: I
Question
Ignore my answer its wrong.
2 (20): The following program creates two threads, Both of th lowing program creates two threads Both of these threads keep invoking method foo in an ite loop. Assume there is no syntax error in this code. t is logically wrong with this program? Can it ever print any output? How would you fix it? #include #include unsigned int counter-oi void *foo (void param) int main(int argc, char argv []) void foo (void ¥ poom) uwhile (1) int m counter LOcK(L1) pthread_t tidl, tid2; pthread attr t attr pthread attr_init (&attr;) ; pthread create(&tidl;, &attr;, foo, NULL) unlock ( pthreadcreate (&tid2;, sattr, foo, NULL) unlock (LI) pthreadjoin(tid1, NULL) ; pthread_join(tid2, NULL); prnt id *foo (void +param) ( while (1) -int m counter; counter++ ; m++i (mi-counter)"( printf ("How did if happen?: m-sd, counter-dlrln" , m, counter) itExplanation / Answer
#include <pthread.h>
#include <stdio.h>
//gcc -pthread -o t thread.c
unsigned int counter = 0;
//mutex initiailzation
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
void *foo(void *param);
int main(int argc, char *argv[]){
pthread_t tid1, tid2;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_create(&tid1, &attr, foo, NULL);
pthread_create(&tid2, &attr, foo, NULL);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
}
void *foo(void *param){
while(1){
//lock the critical section
pthread_mutex_lock( &mutex1 );
int m = counter;
counter++;
m++;
//unlock the critical section
pthread_mutex_unlock( &mutex1 );
if( m != counter){
printf("How did it happen?: m=%d, counter=%d ", m, counter);
}
//add condtional statement when they are equal.
//this code is added, in the case when variable counter and m are in sync
//mutex lock will make the variable counter and m are in sync
if( m == counter){
printf("Equals ?: m=%d, counter=%d ", m, counter);
}
}
}
// The code which does not use the lock does not use the attributes properly.
// Any thread which is active can aquire the variable 'm' when the other is intializing
// thus the counter and m value differ
// solution is to use the mutex lock in the critical section
// To see the the display the condtional statement ( m == counter) is added
//When a mutex lock is used, the thread is blocked until the mutex is unlocked.
//the function pthread_mutex_lock() acquires a lock on the specified mutex variable.
//If the mutex is already locked by another thread, this call will block the calling thread until the mutex is unlocked.
// The critical section here it function foo is
// int m = counter;
// counter++;
// m++;
// so the mutex lock has to be provided. The mutex lock is unlocked once the critical section is completed
// To create a lock pthread_mutex_lock and pthread_mutex_unlock is used
//output before mutex lock
// How did it happen?: m=1, counter=2
// How did it happen?: m=3, counter=182
// How did it happen?: m=182, counter=388
// How did it happen?: m=389, counter=555
// How did it happen?: m=556, counter=732
// How did it happen?: m=733, counter=905
// How did it happen?: m=906, counter=1080
// How did it happen?: m=1080, counter=1234
// How did it happen?: m=1235, counter=1408
// How did it happen?: m=1409, counter=1563
//output after mutex lock
// Equals ?: m=1, counter=1
// Equals ?: m=3, counter=3
// Equals ?: m=4, counter=4
// Equals ?: m=5, counter=5
// Equals ?: m=6, counter=6
// Equals ?: m=7, counter=7
// Equals ?: m=8, counter=8
// Equals ?: m=9, counter=9
// Equals ?: m=10, counter=10
// Equals ?: m=11, counter=11