FIFO possible blocking or unknown error

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:

Create a chat program using two FIFOs one for writing and the other for reading. The problem is something blocks or keep the program from running. My first time doing this with FIFO

  1. Relevant commands, code, scripts, algorithms:

open(), mkfifo, etc.

  1. The attempts at a solution (include all code and scripts):

Here is the code:

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

/* Write to the server FIFO */
#define FIFOSERVER        "serverfifo"
/* Read from client FIFO */
#define FIFOCLIENT        "clientfifo"

extern int errno;

void chatty()
{
    pid_t child_pid;
    /* Client and server fd are the file descriptors */
    int serverFD, clientFD, status;
    char buffer[BUFSIZ];

    /* Remove previous FIFOs */
    unlink(FIFOSERVER);
    unlink(FIFOCLIENT);

    // Create client and server FIFOs

    /* Create client FIFO and handle error generation*/
    if ((mkfifo(FIFOCLIENT, 0766)) < 0) {
        printf("%s", strerror (errno));
        exit(1);
    }

    /* Create server FIFO and handle error generation*/
    if ((mkfifo(FIFOSERVER, 0766)) < 0) {
        printf("%s", strerror (errno));
        exit(1);
    }

    /* Parent Process shall write to the serverfifo */

    /* Open the serverfifo for writing and handle any error generation */
    if ((serverFD = open(FIFOSERVER, O_WRONLY)) < 0) {
        printf("%s", strerror (errno));
        exit(1);
    }


    /* Create child process */

    if ((child_pid = fork() ) < 0) {
        printf("%s", strerror (errno));
        exit(1);
    }

    /* The child process shall read from the clientfifo */

    if (child_pid == 0) {

        int x;

        /* Open the clientfifo for reading and handle any error generation */
        if ((clientFD = open(FIFOCLIENT, O_RDONLY)) < 0) {
            printf("%s", strerror (errno));
            exit(1);
        }

        /* Read from the clientfifo and write to standard out */
        while ((x = read(serverFD, buffer, sizeof(buffer))) > 0) {
            write(1, buffer, x);
        }

        /* Close clientFifo file descriptor */
        close(clientFD);

        _exit(0);

    }

    int x;
    /* Parent reads from standard input and writes to serverfifo */
    while ((x = read(0, buffer, sizeof(buffer))) > 0)
        write(serverFD, buffer, x);

    waitpid(child_pid, &status, WNOHANG);

    close(serverFD);
    exit(0);


}

int main(int argc, char *argv[]) {

    printf("Hey");
    int x;

    char fifoNames[3];

    /* No command-line arguments present */
    if (argc == 1) {

        printf("1");

        /* Run chatty */
        chatty();
    }

    /* Command-line arguments present */
    else if (argc > 1) {

        /* Grab the command-line arguments */
        for( x = 1; x < argc; x++ ) {

        fifoNames[x] = *argv[x];

    }
        /* Redefine FIFOSERVER and FIFOCLIENT and call chatty() */
    #undef FIFOSERVER
    #undef FIFOCLIENT
    #define FIFOSERVER fifoNames[1]
    #define FIFOCLIENT fifoNames[2]

        /* Run chatty */
        chatty();
    }

    return 0;
}

  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

Eastern Michigan University, Ypsilanti, MI US Aby Tehranipour COSC421 link through secure site u wont have access too

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

...
    #undef FIFOSERVER
    #undef FIFOCLIENT
    #define FIFOSERVER fifoNames[1]
    #define FIFOCLIENT fifoNames[2]

#define's are, more or less, literal text replacement operators that happen even before the program is compiled.

For example:

#define A  FUNCTION_A

A();

void function_c()
{
        A();
}

#undef A
#define A FUNCTION_B
A();

function_c();

When you compile it, the first step it does is preprocessing, which does the text replacement, reducing the code to this:

FUNCTION_A();

void function_c()
{
        A();
}

FUNCTION_B();

function_c();

meaning function_c calls FUNCTION_A, not FUNCTION_B. So, this text replacement cannot do any logic at runtime. All decisions it makes happen right when the code is compiled. If you want to change what files your function opens, pass it parameters so you can tell it so:

void chatty(const char *rdfile, const char *wrfile)
{
        ...
}

...


chatty(fifonames[0], fifonames[1]);

...

chatty(fifonames[1], fifonames[0]);