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

Please comment every line of code possible and explain what this program is doin

ID: 3793273 • Letter: P

Question

Please comment every line of code possible and explain what this program is doing.

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>

main (void)
{
int fda;
int fdb;
int finish;
int i;

char charbuff[1];
char outchar[7];   
  
memset(charbuff,0,1);
memset(outchar,0,7);


/* Create the fifos and open them */
if ((mkfifo("FIFO1",0666)<0 && errno != EEXIST))
   {
   perror("cant create FIFO1");
   exit(-1);
   }

if ((mkfifo("FIFO2",0666)<0 && errno != EEXIST))
   {
   perror("cant create FIFO2");
   exit(-1);
   }

if((fda=open("FIFO1", O_RDONLY))<0)
printf("cant open fifo to write");

if((fdb=open("FIFO2", O_WRONLY))<0)
printf("cant open fifo to read");
  
finish=read(fda, charbuff, 1);    //read the character
  
printf("Server: just got character: ,%c", charbuff[0]);

for( i = 0; i<5; i++)
outchar[i] = '*';
outchar[5] = charbuff[0];
outchar[6] = 0;
printf(" Server: outchar is,%s", outchar);

write(fdb, outchar, 7);
printf(" Server: Got the characters sent");

if(finish == 1)
printf(" Server: This says I am ready to close ");

close(fda);
close(fdb);
unlink("FIFO1");
unlink("FIFO2");

  

}

Explanation / Answer

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>

main (void)
{
int fda;
int fdb;
int finish;  
int i;  

char charbuff[1];    //charbuff is char array of 1 byte
char outchar[7];     // outchar is char array of 7 bytes

memset(charbuff,0,1); // putting zeros in charbuff
memset(outchar,0,7);   // putting zeros in outchar


/* Create the fifos and open them */
if ((mkfifo("FIFO1",0666)<0 && errno != EEXIST)) // create FIFO1 with read & write permission 0666 = rw-rw-rw
    {
    perror("cant create FIFO1");     // if FIFO1 is exist print error
    exit(-1);
    }

   if ((mkfifo("FIFO2",0666)<0 && errno != EEXIST)) // create FIFO2 with read & write permission 0666 = rw-rw-rw
    {
    perror("cant create FIFO2");   // if FIFO2 is exist print error
    exit(-1);
    }

if((fda=open("FIFO1", O_RDONLY))<0) // open FIFO1 for read only
     printf("cant open fifo to write");

if((fdb=open("FIFO2", O_WRONLY))<0) // open FIFO2 for write only
     printf("cant open fifo to read");

finish=read(fda, charbuff, 1);     //read the character from FIFO1 and put into charbuff

printf("Server: just got character: ,%c", charbuff[0]);

for( i = 0; i<5; i++) // storing * into outchar
    outchar[i] = '*';
outchar[5] = charbuff[0]; // charbuff data assign into last byte of outchar
outchar[6] = 0; // last byte of outchar make as NULL
printf(" Server: outchar is,%s", outchar); // print the data of outchar

write(fdb, outchar, 7);   // put outchar data into FIFO2
printf(" Server: Got the characters sent");

if(finish == 1) //get the successfully when read the data from FIFO1
    printf(" Server: This says I am ready to close ");

close(fda); // closing the FIFO1
close(fdb); // closing the FIFO2
unlink("FIFO1"); // deleting the FIFO1
unlink("FIFO2"); // deleting the FIFO2
}

program is reading data from FIFO1 and adding some data (like *) and sending or writing to FIFO2

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