C PROGRAM:
In this assignment, you will use the concept of POSIX
threads, semaphores and mutex locks.
Consider a very small bank: XYZ. This bank has only one
cashier (aka bank teller or customer representative) and a
small waiting room for any incoming customers while the cashier is
busy with other customer. There is a sofa which can only
hold 5 people at maximum. The cashier can
only serve one customer at any time. When the
cashier is serving one customer, other customers have to wait in
the sofa. If there are already 5 people waiting for their
turn and the sixth person comes to the bank, then
he/she just leaves the bank. Before the customer is being
served, the customer needs to enter the account
number and each customer will be serviced on first come
first served basis.
Use separate Pthreads to create N customers and cashier. Each of
these customers as well as the cashier should run as a
separate thread. When the customer is being
serviced, print the message clearly saying that the customer with
account number <give_account_number_here> is being serviced.
Assume each customer is done in 5 seconds. You also need to display
the waiting list when the customers comes in and when the customer
is serviced. You need to display if any customer leaves the bank
without getting service (due to the limit of 5 people already
waiting). If there is no customer at any time, the cashier just
waits for the arrival of customer.
ANSWER:
This is the classic producer-consumer problem in Computer
science.
I will provide the generic solution for this problem which can be
easily taken for this banking problem as well. The count of
producer and consumer are made 5 as 5 people can be served at max.
I have added comments inside the program to understand it at every
step. It uses some of C++14, so please compile in g++14 5.3.0 or
below version.
/*PLEASE FIND BELOW COMPILABLE PROGRAM. COPY-PASTE THE
BELOW PROGRAM AND RUN TO GET OUTPUT.*/
#include<iostream>
#include<stdio.h>
#include<pthread.h>
#include<bits/stdc++.h>
#include<unistd.h>
using namespace std;
pthread_mutex_t the_mutex;//mutex to protect buffer from race
conditions
int size = 5;//buffer size
int cnt = 5;//number of producers and consumers
int buffer[5];//buffer on which producer and consumer operate
int in = 0;//index used by producer
int out = 0;//index used by consumer
class semaphore{//implentation of counting semaphore using mutex
and condition variable
private:
std::mutex mutex_;//mutex to lock count
std::condition_variable condition_;//condition variable for threads
to wait on
int count_;//counter for semaphore
public:
semaphore(int count_){
this->count_=count_;//initialise
count_ variable
}
void notify() {
std::unique_lock<decltype(mutex_)> lock(mutex_);//lock mutex
for count variable
++count_;//increase count_
if(count_<=0){//if threads are still waiting on this
condition
condition_.notify_one();//notify one of them
}
}
void wait() {
std::unique_lock<decltype(mutex_)> lock(mutex_);//lock mutex
for count variable
--count_;//decrease count_
if(count_<0){//if thread can't be run now
condition_.wait(lock);//wait on the condition
}
}
};
semaphore notEmpty(0);//counting semaphore for consumer to wait
for not empty condition
semaphore notFull(size);//counting semaphore for producer to wait
for not full condition
void* producer(void *ptr) {
for(int i=1;;++i){
notFull.wait();//wait for buffer to be
not full
pthread_mutex_lock(&the_mutex);//protect
buffer
buffer[in] = i;//insert item
in = (in+1)%size;
cout<<"Customer-"<<i<<" came and
waiting to be serviced."<<endl;
pthread_mutex_unlock(&the_mutex);//release the
buffer
notEmpty.notify();//notify consumer that buffer is not
empty
}
pthread_exit(0);
}
void* consumer(void *ptr) {
for(int i=1;;++i){
notEmpty.wait();//wait for buffer
to be not full
pthread_mutex_lock(&the_mutex);//protect buffer
int item = buffer[out];//remove
item
out = (out+1)%size;
cout<<"Customer with account
number-"<<item<<" is being
serviced."<<endl;
pthread_mutex_unlock(&the_mutex);//release the buffer
notFull.notify();//notify producer
that buffer is not full
}
pthread_exit(0);
}
int main() {
//Define Threads and Mutexes
pthread_t pro, con;
pthread_mutex_init(&the_mutex, NULL);
//Create the threads
pthread_create(&con, NULL, consumer, NULL);
sleep(1);
pthread_create(&pro, NULL, producer, NULL);
//Join Threads
pthread_join(con, NULL);
pthread_join(pro, NULL);
//Destroy Mutexes
pthread_mutex_destroy(&the_mutex);
return 0;
}
/*To RUN THE PROGRAM, USE THE BELOW COMMAND LINE PARAMETER. PLEASE NOTE THAT AN ADDITIONAL PARAMETER IS REQUIRED IN COMMAND LINE TO COMPILE AS IT USES PTHREAD.*/
g++ test.cpp -pthread
PLEASE FIND BELOW SNAPSHOT OF RUNNING CODE:
P.S. I hope the answer is as expected. Please comment if anything additional is required.
C PROGRAM: In this assignment, you will use the concept of POSIX threads, semaphores and mutex...
Needs Help with Java programming language For this assignment, you need to write a simulation program to determine the average waiting time at a grocery store checkout while varying the number of customers and the number of checkout lanes. Classes needed: SortedLinked List: Implement a generic sorted singly-linked list which contains all of the elements included in the unsorted linked list developed in class, but modifies it in the following way: • delete the addfirst, addlast, and add(index) methods and...
In Java, complete the following: During the lunch hour, the ATM machine in a large office complex is in heavy demand. Customers complain that the waiting time is much too long. The local bank considering the addition of a second ATM machine. But first, the bank needs a few statistics to justify the cost of adding a second ATM machine. Using a Queue, simulate a waiting line at the ATM machine for a period of one hour. Make the following...
A university computer science department has a teaching assistant (TA) who helps undergraduate students with their programming assignments during regular office hours. The TA’s office is rather small and the TA can help only one student at a time in the office. There are two chairs in the hallway outside the office where students can sit and wait if the TA is currently helping another student. When there are no students who need help during office hours, the TA sits...
build a bank simulator system using the C++ STL queue library. You are required to create your own ADT for that system that: First, Read from a file the data of incoming customers which includes ArrivalID, StartTime, and ProcessTime. You have one line in your Bank in which you will serve your customers as per to their arrival time. For example if you read the following data from the file A 1 5 B 2 5 C 4 5 D...
build a bank simulator system using the C++ STL queue library. You are required to create your own ADT for that system that: First, Read from a file the data of incoming customers which includes ArrivalID, StartTime, and ProcessTime. You have one line in your Bank in which you will serve your customers as per to their arrival time. For example if you read the following data from the file A 1 5 B 2 5 C 4 5 D...
Hello, I am having some trouble with a supermarket checkout simulation program in C++. What I have so far just basically adds customers to the queue and prints when they arrive. I am struggling with how to implement a way of keeping track of when a given customer finishes(I will attach what I have so far). I had already created queue and node classes (with headers and cpp files) that I modified in my attempt. I would be very grateful...
Instructions Using the BCException.java and the BankCustomer.java from the first part of the assignment you will implement a driver class called Bank.java to manipulate an ArrayList of BankCustomer objects. Your code should read bank customers’ information from the user. It will then create a bank customer objects and add them to an arraylist in a sorted fashion (sorted by account number). The arraylist should be sorted in ascending order at all times. The arraylist should not contain null objects at...
C++ Lists, and a little queuing theory The MegaMicroMart (a subsidiary of Fly-By-Night Industries) is planning their next Big Small-Box Store. They want to ensure good customer service, and you've been hired to do some simulations to advise them on how to set up their checkout lines. This is a discrete-time simulation. This means that for our purposes, time advances in discrete 'ticks'. Each tick, everyone who's going to arrive arrives all at once, while at the same time everyone...
Hello. I am using a Java program, which is console-baed. Here is my question. Thank you. 1-1 Write a Java program, using appropriate methods and parameter passing, that obtains from the user the following items: a person’s age, the person’s gender (male or female), the person’s email address, and the person’s annual salary. The program should continue obtaining these details for as many people as the user wishes. As the data is obtained from the user validate the age to...
This is a c++ program. Use the description from Parking Ticket Simulator (listed below) as a basis, where you need to create a Car class and Police Officer class, to create a new simulation. Write a simulation program (refer to the Bank Teller example listed below) that simulates cars entering a parking lot, paying for parking, and leaving the parking lot. The officer will randomly appear to survey the cars in the lot to ensure that no cars are parked...