Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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:

image text in transcribed

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle RMAN For Absolute Beginners

Authors: Darl Kuhn

1st Edition

1484207637, 9781484207635

More Books

Students also viewed these Databases questions