I am trying to write a remote control program for omxplayer on my rasperry Pi
I can get omxplayer to run ok in a child process but I don't seem to be able to get the pipes working correctly to actually send commands to the child process.
int fd_pipe[2];
pipe (fd_pipe);
while(1) { // main accept() loop
printf("server: got connection from %s\n", s);
/* Attempt to fork and check for errors */
if( (pid=fork()) == -1){
fprintf(stderr,"Fork error. Exiting.\n"); /* something went wrong */
exit(1);
}
if (pid==0)
{ // this is the child process
dup2(0, fd_pipe[0]);
close(fd_pipe[1]);
if(execl("/usr/bin/top","top",NULL) == -1){
fprintf(stderr,"execl Error!");
exit(1);
}
//try and send command here
write(fd_pipe[0], "q", 1);
exit(0);
} else {
close(new_fd); // parent doesn't need this
dup2(1, fd_pipe[1]);
//try and send here too
write(fd_pipe[0], "q", 1);
}
}
When I was testing with top and ran the program, I can see the top output appear in the terminal window and I can see the q command in the window but it looks like its going to the parent process rather than the child. Am I doing something wrong with the pipes or is it not possible to send commands to the spawned child process?
I tried changing the child dup2 statement to copy from the pipe to stdin
{ // this is the child process
dup2(fd_pipe[0], 0);
But then top fails to start with a failed tty get message