Take the 2-minute tour ×

I am struggling to continuously read contents of stdout which is redirected to file. please help me to resolve issue.

My problem in detail is that i have two thread in first thread , i have to execute a c file which is continously writing output to stdout(i.e writing hello world in infinite loop) and in second thread i have to retrieve line by line output periodically from stdout and store in buffer. For second thread implementation i created pipe and redirected stdout to the write end of this pipe and tried to continuously read content of this pipe line by line but i didnt get expeced result. code for second thread is as below:

int redirectPipe[2];
//creation of pipe
//redirection of pipe
//reading continuously line by line from pipe
// some code here

Can somebody please help me in coding how to continuously read line by line from this pipe?

Below is the code: please suggest me is my approach right? and how to implement entryOpThread() method so that it will read contents of stdout periodically whether should i use read() system call or getLine()or any other method to read line by line from pipe(stdout is redirected to this pipe). In this example exec thread is executing test.c which is continuouslly printing hello world on stdout,so according to my requirement i have to read this output line by line("Hello world") continuouslly in Opthread from pipe and store it in buffer

#include <iostream>
const int MAX_LENGTH=100;
using namespace std;

//! file descriptor to duplicate the system standard input
int inputPIPE[2];

//! file descriptor to duplicate the system standard output
int OutputPipe[2];

//!Thread to retrieve output
pthread_t OpThread;

//!Thread to execute script
pthread_t ExecThread;

//! entry point of the exec thread
void *entryExecThread(void * obj)
    //create a child process to launch the script
    int lPid;
    //connect read end of the pipe to the standard input
        //throw exception

    //connect write end of the pipe to the standard output
        //throw exception


        int mProcessId=lPid;
    return NULL;


//! entry point of the output thread
void *entryOpThread(void * obj)
    //read contents of stdout periodically
   /* char *lMssg=new char[MAX_LENGTH];

    FILE *fd=fdopen(OutputPipe[0],"r");
    int lBufferLength=MAX_LENGTH;
            getline(&lMssg,(size_t * )&lBufferLength,fd);
            // push message to the output buffer
            if (lMssg !=NULL)
                pthread_mutex_lock( &mOutputBufferMutex);
                pthread_mutex_unlock( &mOutputBufferMutex);

int main() {

    //call to pipe system call
        printf("ERROR IN OPENING FILE \n");
        printf("ERROR IN OPENING FILE\n ");


    if (pthread_create(&OpThread,0,entryOpThread,NULL)!=0)

        //throw exception
        printf("Creation of Output thread  failed for Script with ID\n");

    //Create execution thread to launch script
    pthread_t execThread;
    if (pthread_create(&ExecThread,0,entryExecThread,NULL)!=0)

        //Stop output thread
        //throw exception
        printf("Creation of Execution thread  failed for Script with ID\n");

#include <stdio.h>
#include "string.h"
//using namespace std;

int main() {
     int i=0;
    printf("!!!Hello World!!! \n" ); // prints !!!Hello World!!!
    //extracting header and data from message
    return 0;
share|improve this question
Are you forking or using pthreads? –  strager Aug 8 '10 at 10:49
yes i am using pthread and forking child process in first thread to run c file –  James Aug 8 '10 at 11:15
Can you post the code you use to set up the threads/processes? It's hard to tell what you are doing from your description, but if you fork then the child process gets a copy of the parent's file descriptors, so calling dup2 from another process after the fork won't have any effect. –  SimonJ Aug 8 '10 at 11:57

1 Answer 1

Could it be an buffering issue (http://www.pixelbeat.org/programming/stdio_buffering/)?

When stdout is connected to a terminal, its output will be line-buffered. When this is not the case, output is page-buffered.

To verify, you can try removing sleep() from your writer thread, and check for output on your consumer thread (printing a handful of bytes every 5 seconds will take a while to fill a page). From there you might consider using fflush() or setvbuf().

share|improve this answer

Your Answer


