c++ - String written to child process corrupted? -
i want execute program in child process. program works when run directly in terminal: waits input , control+d/eof has send before processes it.
in code below program not seem receive string properly, seems if sees part or nothing @ when receives eof seems happen several times though send once.
if can tell me going wrong here?
int main(int argc, char** argv) { int pipes[2][2]; //pipe[0] read, pipe[1] write #define parent_read_fd ( pipes[1][0] ) #define parent_write_fd ( pipes[0][1] ) #define child_read_fd ( pipes[0][0] ) #define child_write_fd ( pipes[1][1] ) // pipes parent write , read assert( (pipe(pipes[1])) == 0 ); assert( (pipe(pipes[0])) == 0 ); int pid = fork(); if(pid == 0) { char *argv[]={"/workspace/osm/overpass/osm3s_v0.7.51/bin/osm3s_query", "--db-dir=$db_dir"}; assert( (dup2(child_read_fd, stdin_fileno)) != -1); assert( (dup2(child_write_fd, stdout_fileno)) != -1); close(child_read_fd); close(child_write_fd); close(parent_read_fd); close(parent_write_fd); execv(argv[0], argv); } else if(pid > 0) { char buffer[1000]; int count; close(child_read_fd); close(child_write_fd); int query_size = strlen(query.c_str()) + 1; int succesful_write = write(parent_write_fd, &query, query_size); assert(succesful_write==query_size); close(parent_write_fd); // read child’s stdout while( count = read(parent_read_fd, buffer, sizeof(buffer)-1) > 0){ cerr << buffer << endl; } } else { cerr << "could not fork child process" << endl; exit(exit_failure); } }
the error output program example (it changes every time program run):
line 1: parse error: unknown type "�" line 1: parse error: empty query not allowed line 1: parse error: unknown type "n" line 1: parse error: empty query not allowed line 1: parse error: unknown type "�" ...
Comments
Post a Comment