我試圖寫omxplayer一個遠程控制程序對我rasperry皮通過管道/ DUP2用C
我能得到omxplayer運行在一個子進程好,但我不知道似乎能夠使管道正常工作,以實際發送命令到子進程。
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);
}
}
當我與頂級測試和運行程序,我可以看到上面的輸出出現在終端窗口,我可以在窗口中看到的q命令,但它看起來像它的將父進程,而比孩子。我是否在管道上做了錯誤的事情,或者不可能將命令發送到派生的子進程?
我試圖改變孩子DUP2語句從管道複製到標準輸入
{ // this is the child process
dup2(fd_pipe[0], 0);
但隨後頂部未能啓動失敗的tty得到消息
謝謝user315052和@mux - oops我的意思是說我正在寫給fd_pipe [1]。我以最高要求爲例,而不是最終要求。我正嘗試使用它在子進程中運行名爲omxplayer的媒體播放器,然後能夠向該進程發送命令。我已經從子進程中刪除寫入,並且只寫(fd_pipe [1],「q」,1); – Karl 2012-07-30 12:21:57
@Karl:所以,你沒有真正嘗試使用僞tty解決方案?爲什麼不? – jxh 2012-07-30 14:27:26
我試圖但是我有麻煩尋找它的圖書館。我已經安裝了pty.h,但是當我嘗試編譯時,我得到了對'forkpty'錯誤的未定義引用。所以我試圖鏈接-l/usr/lib/libutils,但是它的報告:/ usr/bin/ld:找不到-l/usr/lib/libutils – Karl 2012-07-30 15:01:01