Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

We will consider a very simple bank application with two ATMs through which cust

ID: 3724750 • Letter: W

Question

We will consider a very simple bank application with two ATMs through which customers deposit money to an account. Mainly you will implement two programs called bank.c andcustomer.c whose operations are desceribed below. this should be written in C

Implement bank.c

Declare and initialize TOTAL to 0.

Check the existance of two named pipes (say ATM1, ATM2). If needed, create them using mkfifo).

Open both pipes to read

In an infinite loop,

Using select, wait for either pipe to be ready (one which has data).

Read an integer (deposit) from the ready pipe and add it to TOTAL

Print TOTAL

Implement customer.c

Get the name of the named pipe from the command line

Open named pipe (argv[1]) to write

In an infinite loop,

Prompt user to enter an integer to deposit

Read an integer from standard input

If user enters an integer less than zero, break loop

Otherwise, write the given integer into the pipe

Close pipes and quit

Using makefile, compile both bank.c and cusomer.c

Optinal: How about withdawing? Extend bank.c such that it can also monitor standard input along with the named pipes. Basically, it promts user to enter an integer to withdraw. When user enters an integer, bank can read it from standard input and subtract it from TOTAL. While waiting on the prompt, the pipes migt have data, so the bank go ahead and process them first then prints the prompt again.

Optinal: How would you deal with this problem if select was not avaialble? Use polling!

Explanation / Answer

C PROGRAMME :

customer.c:

int main( int argc, char argv[]) / capturing the command line arguments*/

{

int pip; / to store the named pipe descriptor /

if(argc < 1) / validate if the user doesn't give a pipe name /

return -1;

pip = open(argv[1], O_WRONLY); / opening pipe in a write only mode /

if(pip == -1)

exit(1); / if named pipe is not valid /

while(1) / infinite loop /

{

int user_input;

printf(" Enter an integer (negative number to exit) : ");

scanf("%d",user_input); / scanning user input /

if(user_input < 0)

break; / breaking the loop if user input is less than zero /

write(pip,user_input,4); / writing the user input to the name pipe given by user / /* 4 is replaced by the size of int based on the compiler and platform (4 bytes is default size of int)*/

}

close(pip); / closing the pipe /

return 0;

}

bank.c:

int main()

{

struct timeval tv; / time value struct to define time out /

fd_set read_set; / to hold file descriptor set /

int TOTAL = 0;

int pip_atm1, pip_atm2; / holds descriptors for two pipes ATM1 ATM2 /

if( ( access ("ATM1", F_OK) == 0 ) && ( access("ATM2",F_OK) == 0) ) / to check if pipes has access /

{

pip_atm1 = open("ATM1",O_RDONLY);

pip_atm2 = open("ATM2",O_RDONLY);

}

else /* otherwise create pipes*/

{

pip_atm1 = mkfifo("ATM1",0777);

pip_atm2 = mkfifo("ATM2",0777);

}

/* setting the file descriptors to read set*/

FD_SET(pip_atm1,&read_set);

FD_SET(pip_atm2,&read_set);

/ time out values /

tv.tv_sec = 10;

tv.tv_usec = 0;

while(1) /* infinite loop*/

{

int data;

int retval = select (2,&read_set,NULL,NULL,&tv); / waiting until either of the pipes are ready /

if( retval != -1 && retval != 0)

{

/ reading the data from pipe /

if(FD_ISSET(pip_atm1,&read_set))

read(pip_atm1,data,4);

else if (FD_ISSET (pip_atm2, &read_set))

read(pip_atm2,data,4);

TOTAL = TOTAL + data;

}

printf("%d ",TOTAL); / printing total /

}

/ closing file descriptors /

close(pip_atm1);

close(pip_atm2);

return 0;

}

/ include all the dependency header files /

/* for this you have to include sys/select.h header file*/

make file :

all : bank.c customer.c

bank : bank.c

gcc -o bank bank.c

customer : customer.c

gcc -o customer customer.c

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote