Question: A4q3.c code: #include #include #include int sum = 0; /* Deposit */ void *Deposit(void *arg) { int i, upper; upper = atoi( (char *) arg

A4q3.c code:

#include  #include  #include  int sum = 0; /* Deposit */ void *Deposit(void *arg) { int i, upper; upper = atoi( (char *) arg ); for (i = 1; i  Deposit, done "); pthread_exit(NULL); } /* Withdraw */ void *Withdraw(void *arg) { int i, upper; upper = atoi( (char *) arg ); for (i = 1; i  Withdraw, done "); pthread_exit(NULL); } int main(int argc, char *argv[]) { pthread_t tid1, tid2, tid3, tid4, tid5; /* Thread ID */ printf("Transaction, start. "); /* create the threads */ pthread_create(&tid1, NULL, Deposit, "5000000"); pthread_create(&tid2, NULL, Withdraw, "1000000"); pthread_create(&tid3, NULL, Withdraw, "2000000"); pthread_create(&tid4, NULL, Deposit, "3000000"); pthread_create(&tid5, NULL, Deposit, "1500000"); /* now wait for the thread to exit */ pthread_join(tid1, NULL); pthread_join(tid2, NULL); pthread_join(tid3, NULL); pthread_join(tid4, NULL); pthread_join(tid5, NULL); printf("Transaction, done, sum = %d ",sum); return 0; }

Consider the C program A4q3.c which is available in the Google Classroom. In this program, five worker threads are created. They are adding (Deposit) or subtracting (Withdraw) on a shared variable. Three worker threads are adding while two worker threads are subtracting. The answer should be 6500000.However, when running this C program, we see an incorrect answer, for example, as shown below:

A4q3.c code: #include #include #include int sum = 0; /* Deposit */

Revise the C program A4q3.c by apply the pthread mutex subroutines such that we can get the correct answer (specifically, sum = 6500000). Thereafter, put the code here

Transaction, start. Withdraw, done > Deposit, done > Withdraw, done > Deposit, done Deposit, done Transaction, done, sum =4644751

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!