I can see three problem -
1) You need to send an end-of-line (a \n) after the command and to close() the stdin stream.
2) You need to process stderr because if the stderr buffer fills before the process has completed then you will get a deadlock. This should be done in a separate
thread.
3) You need to process stdout in a separate thread since again it can cause deadlock if it's buffer becomes full.
You would do well to read the 4 sections of the
traps article and implement ALL the recommendations.