Hi,
I am calling a program that greps and returns 72536 bytes of data on STDOUT, say about 7000 lines of data on STDOUT.
I use pipe from the program am calling the above program. Naturally, I execute the above program (through execl() ) throught the child process and try to read the STDOUT data through parent process by doing dup2(inpipe[1], STDOUT_FILENO) on child and doing read(inpipe[0], line, 10000), line = char[10000];
I am unable to read all 7000 lines of data from STDOUT into the character array. If I run it straight, only around 450 lines of data is obtained. Then, if I put a sleep(5) function / getchar() in the parent process, the number of lines that I read increases to 900 and 1300. waitpid() is not helping me here. This seems to be some memory problem, but there is no problem with the size of the char array.
Following is the program:
The executable program that prodoces 7000 lines (dfs-zgrep.cc) is:
# include <stdio.h>
# include <iostream>
using namespace std;
int
main(int argc, char * argv[])
{
string file_list = "";
string search_string = "a";
file_list = "/home/vivekv/input_file-2.txt"; // file contains 7366 lines of character a (one char only for each line)
string query_string = "grep -nr " + search_string + " " + file_list + " | awk -F: '{printf \"%s %s\", $1 \" -\", $2 \" \\n\"}' ";
system(query_string.c_str());
exit(0);
}
The program (check_pipes.cc) from which am calling the above binary is:
# include <stdio.h>
# include <iostream>
# include <signal.h>
# include <sys/wait.h>
# define MAXLINE 100000
using namespace std;
int
main(int argc, char * argv[])
{
int inpipe[2];
pid_t pid;
pid_t child_pid;
if ( pipe(inpipe) < 0 )
{
cerr << "PIPE ERROR" << endl;
return -2;
}
if ( (pid = fork()) < 0 )
{
cerr << "FORK ERROR" << endl;
return -3;
}
else if (pid == 0) // CHILD PROCESS
{
child_pid = getpid();
cout << endl << endl << "INSIDE CHILD:-----> pid = " << child_pid << endl << endl;
close(inpipe[0]);
if (inpipe[1] != STDOUT_FILENO)
{
if (dup2(inpipe[1], STDOUT_FILENO) != STDOUT_FILENO)
{
cerr << "dup2 error to stdout" << endl;
}
execl("/home/vivekv/dfs-fm-1.0.0/dfs-zgrep", "dfs-zgrep", NULL );
close(inpipe[1]);
}
return 0;
}
else // PARENT PROCESS
{
// sleep(5);
cout << endl << endl << "INSIDE PARENT: waiting for " << pid << " to finish " << endl << endl;
cout << endl << endl << "CHILD: -----> child_pid = " << child_pid << endl << endl;
cout << endl << endl << "PARENT:-----> pid = " << pid << endl << endl;
// waitpid(child_pid, 0, 0);
int rv;
char line[MAXLINE];
close(inpipe[1]);
if ( (rv = read(inpipe[0], line, MAXLINE)) < 0 )
{
cerr << "READ ERROR FROM PIPE" << endl;
}
else if (rv == 0)
{
cerr << "Child Closed Pipe" << endl;
return 0;
}
cout << endl << endl << "INSIDE PARENT PROCESS...." << endl << line << endl;
close(inpipe[0]);
return 0;
}
return 0;
}
How do I read all the data and pass it to STDOUT (through cout) without losing data?
Thanks,
vivek