Question
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 threads 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 mutex variable or semaphore for thread synchronization. You may assume that the initial account balance is $1000. // declare global variables here main function must initialize the global variables, mutex variable/semaphore, create threads and wait for them to terminate / the amounts to withdraw and deposit are command line int main (int arge, char *argv)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the exact program as specified. Please comment for any clarification. Thanks

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

// Let us create a global variable to change it in threads
int accountBalance;
// thread array
pthread_t tid[2];

//mutex for mutual exclusion
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

// The function to be executed by all threads
void
addBalance (void *vargp)
{
// Store the value argument passed to this thread
int *myid = (int *) vargp;
//locking mutex function
pthread_mutex_lock (&mutex);
accountBalance += *myid;
//unlock thread
pthread_mutex_unlock (&mutex);
// Print the argument, static and global variables
printf ("Account Balance deposit: %d\n", accountBalance);
}

// The function to be executed by all threads
void
withdrawBalance (void *vargp)
{
// Store the value argument passed to this thread
int *myid = (int *) vargp;
//locking mutex function
pthread_mutex_lock (&mutex);
accountBalance -= *myid;
//unlock thread
pthread_mutex_unlock (&mutex);
// Print the global variables
printf ("Account Balance withdraw: %d\n", accountBalance);
}

int
main (int argc, char *argv[])
{
//initialise global variable
accountBalance = 10000;
  
//converting command line argument to integer values.
//first argument for withdraw
int withdraw=atoi(argv[1]);
  
//second argument for withdraw
int deposit=atoi(argv[2]);
  
//creating thread for withdrawBalance
pthread_create (&(tid[1]), NULL, withdrawBalance, (void *) &withdraw);
  
//creating thread for deposit balance
pthread_create (&(tid[0]), NULL, addBalance, (void *) &deposit);
  
//wait for thread to finish
pthread_join (tid[0], NULL);
  
//wait for thread to finish
pthread_join (tid[1], NULL);
pthread_exit (NULL);
return 0;
}

Account Balance deposit: 10200 Account Balance withdraw: 10100 . Program finished with exit code 0 Press ENTER to exit console-

Add a comment
Know the answer?
Add Answer to:
Using C programming please 5. Write a multithreaded program to do the following: Accepts two positive...
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
  • operating system engineering , please read the question and solve on the given skeleton code ....

    operating system engineering , please read the question and solve on the given skeleton code . Write a multi-threaded program with Semaphores as counters and pthread_mutex_t mutex to solve the producer-consumer problem: A bounded buffer is simply a global integer array of size N (2) which can be accessed by multiple threads. • Create two types of threads - Producer (2) and Consumer (2). Producers will write to the buffer, and the consumers will read the buffer. In this scenario,...

  • 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,...

  • Can someone help create this program for Linux. Part 3. IPC (InterProcess Communication) Programming In Part...

    Can someone help create this program for Linux. Part 3. IPC (InterProcess Communication) Programming In Part 3, you are asked to develop a multithreaded program and a Makefile to automate the compilation on Linux platform. This assignment assists you for better understanding of processes and threads management, multithreaded programming, and enhancing programming skills and experience with programming on a Unix-like environment. You are asked to implement a multithreaded producer-consumer problem with PThreads library in this programming assignment. The producer-consumer is...

  • Part 3. IPC (InterProcess Communication) Programming In Part 3, you are asked to develop a multit...

    Part 3. IPC (InterProcess Communication) Programming In Part 3, you are asked to develop a multithreaded program and a Makefile to automate the compilation on Linux platform. This assignment assists you for better understanding of processes and threads management, multithreaded programming, and enhancing programming skills and experience with programming on a Unix-like environment. You are asked to implement a multithreaded producer-consumer problem with PThreads library in this programming assignment. The producer-consumer is a common problem that requires cooperating processes or...

  • How do I do this C++ in a Unix Environment assignment Given dot1m.c 1. The program (dot1m.c) uses...

    How do I do this C++ in a Unix Environment assignment Given dot1m.c 1. The program (dot1m.c) uses mutex to lock and unlock the shared resource (dotstr.sum) for access control as shown below. pthread_mutex_lock (&mutexsum); dotstr.sum += mysum; printf("Thread %ld did %d to %d: mysum=%f global sum=%f\n", offset,start,end,mysum,dotstr.sum); pthread_mutex_unlock (&mutexsum); 2. Modify dot1m.c program to use reader-writer lock (instead of mutex).      Replace the codes (for mutex) by the codes (for reader-writer lock).            To initialize reader-writer lock, pthread_rwlock_initializer.            At the end,...

  • 1. Specification Write a C program to implement a simple calculator that accepts input in the...

    1. Specification Write a C program to implement a simple calculator that accepts input in the following format and displays the result of the computation: calc [operand_1] [operator] [operand_2] The operands operand_1 and operand_2 are non-negative integers. The operator is one of the following: addition (+), subtraction (-), multiplication (x), division (/) and modulo (%). Note: For the multiplication operator, use letter ‘x’. If you use the asterisk ‘*’, your program will not work properly 2. Implementation • The program...

  • Using Unix/Linux Command line in terminal and C programming. (b) Write a basic C program contained...

    Using Unix/Linux Command line in terminal and C programming. (b) Write a basic C program contained in a1q4.c and perform the following tasks in your program’s main() function: (8 marks) • Declare and initialize at least one variable of these types: char, int, unsigned int, float, double and long double. When initializing your variables, ensure the constants you use are of the same type as your variable. See the “Constants” section of Topic 7 for more information. • Use printf(3)...

  • Help with a Parallel and Distributed Programming assignment. In this assignment, you will be expl...

    Help with a Parallel and Distributed Programming assignment. In this assignment, you will be exploring different methods of counting the prime numbers between 1 and N. You will use 8 threads, and each will be given a range in which to count primes. The question is: where do you store your counter? Do you use a local variable? Do you use a global variable? Please use the following function to determine whether an integer number is a prime. // Return...

  • In c programming The Consumer Submits processing requests to the producer by supplying a file name, its location and a character. It also outputs the contents of the file provided by the producer...

    In c programming The Consumer Submits processing requests to the producer by supplying a file name, its location and a character. It also outputs the contents of the file provided by the producer to the standard output. The Producer Accepts multiple consumer requests and processes each request by creating the following four threads. The reader thread will read an input file, one line at a time. It will pass each line of input to the character thread through a queue...

  • 1. (50 pts) Write a C or C++ program A6p1.c(pp) that accepts one command line argument which is an integer...

    1. (50 pts) Write a C or C++ program A6p1.c(pp) that accepts one command line argument which is an integer n between 2 and 6 inclusive. Generate a string of 60 random upper case English characters and store them somewhere (e.g. in a char array). Use pthread to create n threads to convert the string into a complementary string ('A'<>'Z', 'B'<->'Y', 'C''X', etc). You should divide this conversion task among the n threads as evenly as possible, Print out the...

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