Cのpipe/dup2を介して子プロセスにコマンドを送信する
質問
ラスペリーパイでomxplayer用のリモートコントロールプログラムを作成しようとしています
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);
}
}
top でテストしてプログラムを実行すると、top の出力がターミナル ウィンドウに表示され、ウィンドウ内に q コマンドが表示されますが、子プロセスではなく親プロセスに送られているように見えます。パイプで何か間違ったことをしているのでしょうか、それとも生成された子プロセスにコマンドを送信できないのでしょうか?
子dup2ステートメントをパイプから標準入力にコピーするように変更してみました
{ // this is the child process
dup2(fd_pipe[0], 0);
しかし、その後、失敗した tty get メッセージが表示されて、top の起動に失敗します。
解決
端末を操作できることが期待されるプログラムと対話するには、疑似 tty を使用する必要があります。これにより、 failed tty get
エラー。
/*...*/
#include <pty.h>
void do_child () {
if (execlp("top", "top", (const char *)0) < 0) {
perror("exec top");
exit(EXIT_FAILURE);
}
/* NOTREACHED */
}
void do_parent (int fd, pid_t p) {
sleep(5);
char r;
write(fd, "q", 1);
while (read(fd, &r, 1) > 0) { write(1, &r, 1); }
waitpid(p, 0, 0);
close(fd);
}
int main () {
int fd;
pid_t p = forkpty(&fd, 0, 0, 0);
switch (p) {
case 0: do_child();
/* NOTREACHED */
case -1: perror("forkpty");
exit(EXIT_FAILURE);
default: break;
}
do_parent(fd, p);
return 0;
}
ご了承ください forkpty
は POSIX ではありませんが、UNIX の BSD および Linux フレーバーで使用できるインターフェイスです。
実行できます top
バッチモードでは実行できますが、コマンドを使用して終了することはできません。それを殺さなければなりません。
void do_child () {
if (execlp("top", "top", "-b", (const char *)0) < 0) {
perror("exec top");
exit(EXIT_FAILURE);
}
/* NOT REACHED */
}
void do_parent (pid_t p) {
sleep(5);
if (kill(p, SIGINT) < 0) {
perror("kill");
exit(EXIT_FAILURE);
}
waitpid(p, 0, 0);
}
int main () {
pid_t p;
switch ((p = fork())) {
case 0: do_child();
case -1: perror("fork"); exit(EXIT_FAILURE);
default: break;
}
do_parent(p);
return 0;
}
ただし、バッチ モードで実行すると、より単純な呼び出し (次のような) でプロセスを開くことができます。 popen
mux が示唆しているように)、その呼び出しが子のプロセス ID を返さない限り、それを強制終了することはできません ( pkill
, 、またはプロセス テーブルを調べて、強制終了する適切な子プロセスを見つけます)。
使い方に戸惑いがあると思います dup2
. 。マニュアルページでは次の用語が使用されています。 oldfd
そして newfd
, 、ということです。 oldfd
となります newfd
. 。説明のために、stdout と stderr をログ ファイルにリダイレクトし、関数を呼び出し、その後 stdout と stderr を復元する単純なプログラムを次に示します。
void do_something () {
fputs("Error message\n", stderr);
puts("This is regular output.");
fputs("Error message\n", stderr);
puts("This is regular output.");
}
int main () {
int fd = creat("/tmp/output.log", 0664);
int outfd = dup(fileno(stdout));
int errfd = dup(fileno(stderr));
fflush(stdout);
fflush(stderr);
dup2(fd, fileno(stdout));
dup2(fd, fileno(stderr));
setlinebuf(stdout);
do_something();
fflush(stdout);
fflush(stderr);
dup2(outfd, fileno(stdout));
dup2(errfd, fileno(stderr));
close(outfd);
close(errfd);
fputs("Error to the screen\n", stderr);
puts("Regular output to screen");
fputs("Error to the screen\n", stderr);
puts("Regular output to screen");
return 0;
}
mux が指摘したように、プログラムはパイプの間違った端に書き込んでいます。の pipe
コマンドは一方向のファイル記述子のペアを返します。ここには何が書き込まれますか fd_pipe[1]
から読み取ることができます fd_pipe[0]
.