Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trapping a broken pipe error

Status
Not open for further replies.

lewisp

Programmer
Aug 5, 2001
1,238
GB
I'm writing the following code on HP-UX:
Code:
    int pipefd[2],
        ret_code,
        status;

    pid_t pid;

    pipe (pipefd);

    if (l_debug)
    {
        printf("In ViewFile\n");
        fflush(stdout);
    }

    if ((pid = fork()) == (pid_t)0)
    {
        close(1);
        dup(pipefd[1]);
        close(pipefd[0]);
        execlp("get","get","-sp",SccsFileName,(char *)0);
    }
    else if (pid < (pid_t)0) 
    {
        printf("[%d]: Unable to create fork process.\n",__LINE__-9);
        return 1;
    }
    else if (pid > (pid_t)0)
    {
        close(0);
        dup(pipefd[0]);
        close(pipefd[1]);
        execlp("more","more",(char *)0);
    }

    return 0;

The "get" command basically prints the contents of a text file to the screen. The get is piped through a "more". When the text fills more than a screens worth, it waits for the user to press a button, or quit. If they choose to quit (with the Q button), "more" terminates but the parent (child?) process reports a broken pipe error. If ignore the SIGPIPE signal, I get an fgets error because the reading process no longer exists that the writing process is writing to. (Or is it the other way round? [ponder])

How can I trap and handle the broken pipe so the process terminates cleanly?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top