質問

Linux CLIコマンドを実行し、その標準出力をCから取得する必要があります。

pipe()を使用してパイプを作成し、fork / execして、exec()を呼び出す前に子のstdout記述子をパイプにリダイレクトし、親のパイプから読み取ることができます。さらに、子供を待つ必要があります。

system()がfork + exec + waitを実行するように、fork + redirect + exec + waitを実行する単純な呼び出しがあります。system()のみがリダイレクトを実行します。

popen()があります。これはfork + redirect + execを実行しますが、待機を実行しないため、終了ステータスを取得できません。

役に立ちましたか?

解決

これですか?

NAME
       popen, pclose - process I/O

SYNOPSIS
       #include <stdio.h>  

       FILE *popen(const char *command, const char *type);

       int pclose(FILE *stream);

DESCRIPTION
       The  popen()  function opens a process by creating a pipe, forking, 
and invoking the shell.  Since a pipe is by definition unidirectional, the 
type argument may specify only reading or writing, not both; the resulting 
stream is correspondingly read-only or write-only.

       The command argument is a pointer to a null-terminated string 
containing a shell command line.  This command is passed to /bin/sh 
using the -c flag; interpretation, if any, is performed by the shell.  
The type argument is a pointer to a null-terminated string which must be 
either ‘r’ for reading or ‘w’ for writing.

       The  return  value  from popen() is a normal standard I/O stream in 
all respects save that it must be closed with pclose() rather than fclose().  
Writing to such a stream writes to the standard input of the command; the 
command’s standard output is the same as that of the process that called 
popen(), unless this is altered by the command itself.  Conversely, reading 
from a ‘‘popened’’ stream reads the command’s standard output, and the 
command’s standard input is the same as that of the process that called 
popen().

       Note that output popen() streams are fully buffered by default.

       The pclose() function waits for the associated process to terminate 
and returns the exit status of the command as returned by wait4().

他のヒント

GLibにはこのための便利な機能があります- g_spawn_sync() http://library.gnome。 org / devel / glib / stable / glib-Spawning-Processes.html#g-spawn-sync

たとえば、コマンドを実行して終了ステータスと出力を取得するには:

const char *argv[] = { "your_command", NULL };
char *output = NULL; // will contain command output
GError *error = NULL;
int exit_status = 0;
if (!g_spawn_sync(NULL, argv, NULL, 0, NULL, NULL, 
                  &output, NULL, &exit_status, &error))
{
  // handle error here
}

popen()および pclose()を使用します。


もちろん、

popen()は実際には待機しませんが、パイプでの読み取りは利用可能なデータがあるまでブロックされます。

pclose()は待機しますが、時期尚早に呼び出すと、フォークされたプロセスからの出力が一部切断される可能性があります。子が完了したときにストリームから判断する必要があります...


おそらく 方法Cから外部プログラムを実行し、その出力を解析できますか?

使用するものは次のとおりです。

   /* simply invoke a app, pipe output*/
    pipe = popen(buf, "r" );
    if (pipe == NULL ) {
        printf("invoking %s failed: %s\n", buf, strerror(errno));
        return 1;
    }

    waitfor(10);

    while(!feof(pipe) ) {
        if( fgets( buf, 128, pipe ) != NULL ) {
            printf("%s\n", buf );
        }
    }

    /* Close pipe */
    rc = pclose(pipe);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top