JnxF
JnxF

Reputation: 121

Redirecting input to thread

I have a really simple code where I fork once and the child node mutates with execlp and the parent keeps doing its work. Nothing too fancy.

What I want is to redirect all the standard input to the execlp-ed thread, not the parent.

So if I execute

echo "hi" | ./parent.o

this can be captured with a string s; cin >> s; in some_command.cpp (but not in parent.cpp!).

Current situation (parent.cpp):

int main()
{
  int pid = fork();
  if (pid < 0) {
    std::cout << "Something crashed" << std::endl;
    exit(-1);    
  } else if (pid == 0) {
    // We are the forked child
    execlp("/some/command", "some_command", NULL);
    std::cout << "Execlp failed" << std::endl;
    exit(127);
  }
  // We are the parent, and we keep running
  // More stuff...
}

Right now some_command.cpp contains just an empty int main() { }.

Thanks.

Upvotes: 1

Views: 64

Answers (0)

Related Questions