LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-11-2012, 06:29 PM   #1
cmartin0
LQ Newbie
 
Registered: Feb 2012
Location: corvalllis,or
Distribution: fedora
Posts: 2

Rep: Reputation: Disabled
reading and writing to pipes, file descriptors, and file streams


The assignment I am working on requires making a text parser that sends words to multiple child processes that call sort. The sort children have to send their data to a process call suppersor that removes duplicate words from printing.

I know in order for data to be read/writen unused pipes need to be closed. I do with the function close_pipes. I can run the program without segfaults. I have run the program through gdb following child and parent and get no segfaults. When I run the program I get no text from the input file showing up. All i get is ERROR WHILE CLOSING PIPE.

How can I check that the 2D array of pipe file descriptors are valid? I don't understand why in the child process it would be failing to close the pipe.

Code:
    /**
     * @brief pipe from sort to suppressor
     */
    int **pipe1;

    /**
     * @brief pipe from parser to sort.
     */
    int **pipe2;

    sort_count = atoi(argv[1]);
    
    pipe1 = build_pipe_table(sort_count); 
    pipe2 = build_pipe_table(sort_count);
    /* closing pipes here causes no probelm */

    /* fork sort */
    int i;
    for (i = 0; i < sort_count; ++i){
        switch(fork()){
            case -1:
                fprintf(stderr, "forking sort %d process failed\n", i);
                break;
            case  0:
                /* child tasks */
                fprintf(stderr,"in sort child code\n"); 
                dup2(pipe1[i][0], STDIN_FILENO);
                dup2(pipe2[i][1], STDOUT_FILENO);
                
                close_pipes(pipe1, 0, sort_count);
                close_pipes(pipe1, 1, sort_count);

                close_pipes(pipe2, 0, sort_count); 
                close_pipes(pipe2, 1, sort_count);

                /* free the pipe array? */
                execl("/usr/bin/sort", "sort",  (char*) NULL);
                break;
            default:
                break;
        }
    }
Code:
/**
 * @brief Constructs the dynamic two dimenisional array for holding the pipes.
 * @param rows the number of rows in the 2D array.
 * @return pointer to the created 2D array.
 */
int**  build_pipe_table(const int rows){
    int i;
    int **temp;

    temp = (int**)malloc(rows * sizeof(int*));
    
    if (temp == NULL){
        fprintf(stderr, "failed to allocate pipe table\n");
        exit(EXIT_FAILURE);
    }

    for (i = 0; i < rows; ++i){
        /* 2 for write read of pipe fd array */
        temp[i] = (int*)malloc(2 * sizeof(int));
        
        if (temp[i] == NULL){
            fprintf(stderr, "failed to allocate pipe table\n");
            exit(EXIT_FAILURE);
        }

        errno = 0;
        if (pipe(temp[i]) == -1){
            fprintf(stderr, "failed to create pipe. errno = %d\n", errno);
            exit(EXIT_FAILURE);
        }
    }

    return temp;
}
Code:
/**
 * @breif closes one side of a pipe for all pipes in 2D array.
 * @param the_pipe pointer to the 2D array of pipes.
 * @param side which side of the pipe to close 0 = read, 1 = write side
 * @param rows the number for sorts children.
 */
void close_pipes(int **the_pipe, const int side, const int rows){
    int i;
    for (i = 0; i < rows; ++i){
        errno = 0;
        if (close(the_pipe[i][side]) == -1){
            fprintf(stderr, "ERROR WHILE CLOSING PIPE\n");
        }
    }
}
Sample output
Quote:
in sort child code
in sort child code
in supressor code
in sort child code
ERROR WHILE CLOSING PIPE
ERROR WHILE CLOSING PIPE
ERROR WHILE CLOSING PIPE
ERROR WHILE CLOSING PIPE
ERROR WHILE CLOSING PIPE
ERROR WHILE CLOSING PIPE
ERROR WHILE CLOSING PIPE
ERROR WHILE CLOSING PIPE
ERROR WHILE CLOSING PIPE
ERROR WHILE CLOSING PIPE
ERROR WHILE CLOSING PIPE
ERROR WHILE CLOSING PIPE
ERROR WHILE CLOSING PIPE
ERROR WHILE CLOSING PIPE
ERROR WHILE CLOSING PIPE
ERROR WHILE CLOSING PIPE
ERROR WHILE CLOSING PIPE
ERROR WHILE CLOSING PIPE
ERROR WHILE CLOSING PIPE
ERROR WHILE CLOSING PIPE

Last edited by cmartin0; 02-11-2012 at 07:11 PM.
 
Old 02-13-2012, 03:03 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,847
Blog Entries: 1

Rep: Reputation: 1866Reputation: 1866Reputation: 1866Reputation: 1866Reputation: 1866Reputation: 1866Reputation: 1866Reputation: 1866Reputation: 1866Reputation: 1866Reputation: 1866
For a start:

Code:
void close_pipes(int **the_pipe, const int side, const int rows){
    int i;
    for (i = 0; i < rows; ++i){
        int handle= the_pipe[i][side];

        if (close(handle) == -1){
            fprintf (stderr, "ERROR WHILE CLOSING PIPE %d (i=%d, side=%d, rows=%d) errno=%d: %s\n",
                     handle, i, side, rows, errno, strerror (errno));
        }
    }
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
File descriptors with pipes kickarzt Linux - Newbie 5 06-06-2010 07:35 AM
pipes and streams and one file viroos Linux - Newbie 2 11-07-2008 12:56 PM
Question about reading/writing into a Named Pipes(FIFO) Sathya Programming 21 05-05-2008 08:39 AM
writing and reading from a file! sahel Programming 1 12-27-2005 01:33 PM
C File reading and writing AbhishekSamuel Programming 3 05-03-2005 03:59 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration