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
//************CODE********************************
include
include
include
include
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;
lPid=fork();
//connect read end of the pipe to the standard input
if(dup2(inputPIPE[0],STDIN_FILENO)==-1)
{
//throw exception
}
//connect write end of the pipe to the standard output
if(dup2(OutputPipe[1],STDOUT_FILENO)==-1)
{
//throw exception
}
if(lPid==0)
{
execvp("./test",NULL);
}
else
{
int mProcessId=lPid;
waitpid(mProcessId,NULL,0);
}
return NULL;
}
//! entry point of the output thread
void *entryOpThread(void * obj)
{
//read contents of stdout periodically
/* char *lMssg=new char[MAX_LENGTH];
read(OutputPipe[0],lMssg,MAX_LENGTH);
FILE *fd=fdopen(OutputPipe[0],"r");
int lBufferLength=MAX_LENGTH;
while(true)
{
getline(&lMssg,(size_t * )&lBufferLength,fd);
// push message to the output buffer
if (lMssg !=NULL)
{
pthread_mutex_lock( &mOutputBufferMutex);
mOutputBuffer.push(lMssg);
pthread_mutex_unlock( &mOutputBufferMutex);
}
}
*/
}
int main() {
//call to pipe system call
if(pipe(inputPIPE)==-1)
{
printf("ERROR IN OPENING FILE \n");
}
if(pipe(OutputPipe)==-1)
{
printf("ERROR IN OPENING FILE\n ");
}
//CREATE OUTPUT THREAD TO RETRIEVE OUTPUT
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
pthread_cancel(OpThread);
//throw exception
printf("Creation of Execution thread failed for Script with ID\n");
}
}
//************************************************888
test.c
include
include "string.h"
//using namespace std;
int main() {
int i=0;
while(i<500)
{
printf("!!!Hello World!!! \n" ); // prints !!!Hello World!!!
//extracting header and data from message
sleep(5);
i++;
}
return 0;
}
//***********************************************