Question

Write a C program to simulate deposit/withdraw activities on a banking account. (Hint: you might want...

Write a C program to simulate deposit/withdraw activities on a banking account. (Hint: you might want to review Lab 3 slides.) (25 points)

 Declare the balance as a global variable and initialize it to 600.  Write two functions, one for withdraw, the other for deposit. Both withdraw and deposit functions have one parameter, which represent the amount to withdraw or deposit. The functions deduct the balance and add to the balance one dollar at a time, respectively. Therefore, to withdraw 600 thousands, for example, the withdraw function has to execute a loop with 600 thousand iterations. The deposit function works in the same way, in the reverse direction.  Create two Posix threads in main(), which call the withdraw and the deposit function respectively.  Use pthread_mutex_lock() and pthread_mutex_unlock() functions to ensure mutual exclusion between the two threads. Also make sure to use pthread_join() to wait for the threads to terminate.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

#include<stdlib.h>
#include<stdio.h>
#include<pthread.h>

int balance=600;
pthread_mutex_t lock;
void *withdraw(void *amt)
{
   pthread_mutex_lock(&lock);
   int i;
   int *amount;
   amount = (int *) amt;
   if(*amount > balance)
   {
       printf("Insufficient balance to withdraw\n");
       return NULL;
   }
   for(i = 0; i < *amount; i++)
       balance -=1;

   pthread_mutex_unlock(&lock);

}

void *deposit(void *amt)
{
   pthread_mutex_lock(&lock);
   int i;
   int *amount;
   amount = (int *) amt;
   for(i = 0; i < *amount; i++)
       balance +=1;

   pthread_mutex_unlock(&lock);
}
int main()
{
   pthread_t dep, withd;
   int amount,choice;
   char ch = 'y';

   do
   {
       printf("\nChoose Option :\n");
       printf("1. Deposit\n2. Withdraw\n");
       scanf("%d",&choice);

       switch(choice)
       {
           case 1:
               printf("Enter amount to deposit : ");
               scanf("%d", &amount);

               pthread_create(&dep, NULL, deposit, (void*) &amount);   //create thread for deposit
               pthread_join(dep, NULL);
               printf("Balance : %d\n", balance);
               break;
        
           case 2:
               printf("Enter amount to withdraw : ");
               scanf("%d", &amount);

               pthread_create(&withd, NULL, withdraw, (void*) &amount); //create thread for withdrawal
               pthread_join(withd, NULL);
               printf("Balance : %d\n", balance);
               break;
        
           default:
               printf("\n Invalid choice\n");
               break;

       }
       printf("\nDo you want to continue[y/n] : ");
       scanf(" %c",&ch);
   }while(ch == 'y');
   return 0;

}

Add a comment
Know the answer?
Add Answer to:
Write a C program to simulate deposit/withdraw activities on a banking account. (Hint: you might want...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using C programming please 5. Write a multithreaded program to do the following: Accepts two positive...

    Using C programming please 5. Write a multithreaded program to do the following: Accepts two positive integers from the command line as the amounts to withdraw a. and deposit from an account. b. Creates two threads to perform withdraw and deposit operations repectively. Passes the amount to withdraw/deposit as the parameter to thread's runner function c. Waits for both threads to terminate. Since both threads need to update the account balance, it is necessary to protect sections. You may use...

  • Consider a banking system that maintains an account balance with two functions: deposit (amount) and withdraw...

    Consider a banking system that maintains an account balance with two functions: deposit (amount) and withdraw (amount). These two functions are passed the amount that is to be deposited or withdrawn from the bank account balance. Assume that a husband and wife share a bank account. Concurrently, the husband calls withdraw ( ) function and the wife calls deposit ( ). Describe how a race condition is possible and what might be done to prevent the race condition from occurring?

  • Lance 1. Write a C++ program that manages a user's bank account. The program must but...

    Lance 1. Write a C++ program that manages a user's bank account. The program must but not limited to: (a) Display a menu for the user once the program runs. The menu display should be done through a user defined function with no parameters. The display should look like the below snapshot. (3 marks) ***********Welcome to the Bank Account Manager program helps you make cash withdrawals and deposits (b) Ask the user for his/her full name. (1.5 marks) (c) Ask...

  • In C++ Create an inheritance hierarchy that a bank might use to represent customers’ bank accounts. All customers at this bank can deposit (i.e., credit) money into their accounts and withdraw (i.e.,...

    In C++ Create an inheritance hierarchy that a bank might use to represent customers’ bank accounts. All customers at this bank can deposit (i.e., credit) money into their accounts and withdraw (i.e., debit) money from their accounts. More specific types of accounts also exist. Savings accounts, for instance, earn interest on the money they hold. Checking accounts, on the other hand, charge a fee per transaction (i.e., credit or debit). Create an inheritance hierarchy containing base class Account and derived...

  • write a code on .C file Problem Write a C program to implement a banking application...

    write a code on .C file Problem Write a C program to implement a banking application system. The program design must use a main and the below functions only. The program should use the below three text files that contain a set of lines. Sample data of these files are provided with the assessment. Note that you cannot use the library string.h to manipulate string variables. For the file operations and manipulations, you can use only the following functions: fopen(),...

  • Please help JAVA Part II: Programming Module (80 marks): Write a banking program that simulates the operation of your l...

    Please help JAVA Part II: Programming Module (80 marks): Write a banking program that simulates the operation of your local bank. You should declare the following collection of classes: 1) An abstract class Customer: each customer has: firstName(String), lastName (String), age (integer), customerNumber (integer) - The Class customer has a class variable lastCustomerNumber that is initialized to 9999. It is used to initialize the customerNumber. Hence, the first customer number is set to 9999 and each time a new customer...

  • Need some help with this C++ code. Please screenshot the code if possible. Purpose: Demonstrate the...

    Need some help with this C++ code. Please screenshot the code if possible. Purpose: Demonstrate the ability to create and manipulate classes, data members, and member functions. This assignment also aims at creating a C++ project to handle multiple files (one header file and two .cpp files) at the same time. Remember to follow documentation and variable name guidelines. Create a C++ project to implement a simplified banking system. Your bank is small, so it can have a maximum of...

  • You are to write a banking application in c# that keeps track of bank accounts. It will consist of three classes, Account, Bank and BankTest. The Account class is used to represent a savings account i...

    You are to write a banking application in c# that keeps track of bank accounts. It will consist of three classes, Account, Bank and BankTest. The Account class is used to represent a savings account in a bank. The class must have the following instance variables: a String called accountID                       a String called accountName a two-dimensional integer array called deposits (each row represents deposits made in a week). At this point it should not be given any initial value. Each...

  • MAIN OBJECTIVE       Develop a polymorphic banking program using the Account hierarchy created (below). C++ Capture...

    MAIN OBJECTIVE       Develop a polymorphic banking program using the Account hierarchy created (below). C++ Capture an output. polymorphism. Write an application that creates a vector of Account pointers to two SavingsAccount and two CheckingAccountobjects. For each Account in the vector, allow the user to specify an amount of money to withdraw from the Account using member function debit and an amount of money to deposit into the Account using member function credit. As you process each Account, determine its...

  • C++ PROGRAM ONLY! For this lab you need to write a program that will read in...

    C++ PROGRAM ONLY! For this lab you need to write a program that will read in two values from a user and output the greatest common divisor (using a recursive implementation of the Euclidean algorithm) to a file. In Lab #3, you implemented this program using an iterative method. Greatest Common Divisor In mathematics, the greatest common divisor (GCD) of two or more integers (when at least one of of them is zero then the larger value is the GCD....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT