Question: Problem Description: The purpose of this create a multithreaded program that uses mutex to protect critical code sections. 1 . Your C code will manage

Problem Description: The purpose of this create a multithreaded program that uses mutex to protect critical code sections.
1. Your C code will manage one person bank account, and its ATM withdrawal.
2. The program will 10 threads, then will attempt 10 withdraws in parallel.
3. The withdraw function calls the following 3 functions in sequence:
a. Debit: this simulates the operation of deducting the withdrawn amount from the person account
b. Dispense: This simulates dispensing the cash
c. Receipt: This simulates printing an summary receipt of the operation
void debit(long tid, long amount){ account_balance-=amount; printf("In thread %ld: Debiting %ld dollars, balance after: %ld
", tid, amount, account_balance); }
void dispense (long tid, long amount){ printf("In thread %ld: dispensing %ld dollars from ATM
", tid, amount); }
void receipt(long tid, long amount){ printf("In thread %ld: Receipt: Balance before: %ld,Amount withdrawn: %ld,Balance after: %ld
", tid, account_balance+amount, amount, account_balance); }
void *withdraw(void *threadid){ long tid =(long)threadid; debit(tid,(tid+1)*10); dispense(tid,(tid+1)*10); receipt(tid,(tid+1)*10); }
4. Without treating the 3 functions described above as a critical code section, verify that you will be getting some abnormal results.
5. Now, protect all the code inside the withdraw method with a mutex variable, and notice the execution output difference.
6. Submit your code including the mutex protected withdraw. Follow class coding and submission guidelines .

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 Programming Questions!