In this session you will deal with named pipes and multiple file/pipe descriptor
ID: 3594326 • Letter: I
Question
In this session you will deal with named pipes and multiple file/pipe descriptors 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 and customer.c whose operations are desceribed below Log on to your Linux Account . Create a directory called RecD under cs3733 and do all vour work under RecD ° Implement bank.c o Declare and initialize TOTAL to 0 o Check the existance of two named pipes (say ATM1, ATM2). If needed, create them using mkfifo) o Open both pipes to read o 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 o Get the name of the named pipe from the command line o Open named pipe (argv[1]) to write o 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 loojp - Otherwise, write the given integer into the pipe o Close pipes and quit Using makefile, compile both bank.c and cusomer.c Open three new terminals and goto RecD in each terminal In terminal 1, run bank . In terminal 2, run customer ATM1 . In terminal 3, run customer ATM2 Randomly go into terminal 2 or 3, and enter integers and see what is printed on terminal 1.Explanation / Answer
Solution:
code:
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
Please, please upvote and ask your doubts in the comments.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.