stoppipe() は標準入力を開きます

stoppipe() は標準入力を開きます

現在、2つのプロセスを分岐するコードがあります。最初のプロセスはhttpストリームブロードキャストを読み取り、pipe()2番目のプロセスがOSSを使用して読み取り、デコード、およびサウンドカードに出力できるように、データをパイプ(として開く)にプッシュします。

デコード部分(別の質問)をデバッグしようとしましたが、印刷時にパイプのファイル記述子がゼロの状況に遭遇しました。私が知る限り、これは標準入力を意味します。これがパイプラインの既知の問題ですか?誤って標準ファイル記述子の1つを開くことはできますか?では、どうすれば解決できますか?

私のパイプライン/フォークコードは次のとおりです。関連がないことを望む他のコードがたくさんあります。

//this is the "switch channel" loop
while(1)
{


    /*create the pipes
    *
    * httpPipe is for transfer of the stream between the readProcess and the playProcess
    *
    * playPPipe is for transfer of commands from the main process to the playProcess
    *
    * readPPipe is for transfer of commands from the main process to the readProcess
    *
    */


    if(pipe(httpPipe) == -1)
    {
        cout << "ERROR:: Error creating httpPipe: " << endl;
    }

    if(pipe(PlayPPipe) == -1)
    {
        cout << "ERROR:: Error creating PlayPPipe: " << endl;
    }
    if(pipe(ReadPPipe) == -1)
    {
        cout << "ERROR:: Error creating ReadPPipe: " << endl;
    }


    cout << "httpPipe:" << httpPipe[0] << ":" << httpPipe[1] << endl;
    cout << "PlayPPipe:" << PlayPPipe[0] << ":" << PlayPPipe[1] << endl;
    cout << "ReadPPipe:" << ReadPPipe[0] << ":" << ReadPPipe[1] << endl;


    pid = fork();
    if(pid == 0)
    {
        /* we are in the readProcess
        *  this process uses libcurl to read the icestream from the url
        *  passed to it in urlList. It then writes this data to writeHttpPipe.
        *  this continues until the "q" command is sent to the process via
        *  readPPipe/readReadPPipe. when this happens the curl Callback function
        *  returns 0, and the process closes all fds/pipes it has access to and cleans
        *  up curl and exits.
        */
        rc = 0;

        close(httpPipe[0]);
        writeHttpPipe = httpPipe[1];

        close(ReadPPipe[1]);
        readReadPPipe = ReadPPipe[0];

        rc = readProcess(urlList.at(playListNum));
        if(rc > 0)
        {
            cout << "ERROR:: has occured in reading stream: " << urlList.at(playListNum) << endl;
            close(writeHttpPipe);
            close(readReadPPipe);
            exit(16);
        }


    }else if(pid > 0)
    {
        pid = fork();
        if(pid ==0)
        {
            /* we are in the PlayProcess
             * the playProcess initialises libmpg123 and the OSS sound subsystem.
             * It then reads from httpPipe[0]/readHttpPipe until it recieves a "q" command
             * via PlayPPipe[0]/readPlayPPipe. at which point it closes all fd's and cleans
             * up libmpg123 handles and exits.
             */
            close(httpPipe[1]);
            sleep(1);

            close(PlayPPipe[1]);
            readPlayPPipe = PlayPPipe[0];

            playProcess();
            exit(0);

        }else if(pid > 0)
        {
            /* This is the main process
             *  this process reads from stdin for commands.
             *  if these are valid commands it processes this command and
             *  sends the relevant commands to the readProccess and the PlayProcess via
             *  the PlayPPipe[1]/writePlayPPipe and ReadPPipe[1]/writeReadPPipe.
             *  It then does suitable clean up.
             */
            string command;

            //close ends of pipe that we don't use.
            close(ReadPPipe[0]);
            close(PlayPPipe[0]);
            close(httpPipe[0]);
            close(httpPipe[1]);

            //assign write ends of pipes to easier variables
            writeReadPPipe = ReadPPipe[1];
            writePlayPPipe = PlayPPipe[1];
            rc = 0;

            //wait for input
            while(1)
            {
                cin >> command;
                cout << command << endl;

                rc = 0;

                /* Next channel command. this sends a q command to
                 * readProccess and playProcess to tell them to cleanup and exit().
                 * then it breaks out of the loop, increments the playListNum and
                 * we start all over again. The two processes get forked, this time with a new channel
                 * and we wait for input.
                 */
                if(command == "n")
                {
                    rc = sendCommand("q");
                    if(rc != 0)
                    {
                        cout << "ERROR: failed to send command: " << command << ":" << endl;
                    }
                    break;
                }

                /* Quit program command.
                 * This sends a command to the two proceesses to cleanup and exit() and then exits.
                 *
                 */
                if(command == "q")
                {
                    rc = sendCommand("q");
                    if(rc != 0)
                    {
                        cout << "ERROR: failed to send command: " << command << ":" << endl;
                    }
                    exit(0);
                }

            }
        }else
        {
            cout << "ERROR:: some thing happened with the fork to playProcess..." << endl;
        }
    }else
    {
        cout << "ERROR:: some thing happened with the fork to readProcess..." << endl;
    }

    //clean up the pipes otherwise we get junk in them.
    close(writePlayPPipe);
    close(writeReadPPipe);
    delete json;


    //Parse JSON got from the above url into the list of urls so we can use it
    JsonConfig *json = new JsonConfig(parms->GetParameter("URL"));
    json->GetConfigJson();
    json->ParseJson();
    json->GetUrls(urlList);

    cout << "####---->UrlListLength: " << urlList.size() << endl;

    //increment which url in the list we are going to be playing next.
    playListNum++;


    //if the playlist is greater than or equal to the urlist size then we are back at the start of the list
    if(playListNum >= (int)urlList.size())
    {

        playListNum = 0;
    }

}

このループを使用すると、ラジオ局のリストを閲覧できます。 「n」を押すと、両方の子プロセスにコマンドを送信して完全に終了し、すべてのパイプを閉じてループバックし、すべてのパイプを再度開き、両方のプロセスを再分岐します。

ループを最初に通過すると動作しているように見えますが、2番目は次のような出力を取得します。

URL: 192.168.0.5:9000/playlist
GetConfigJsonurl: 192.168.0.5:9000/playlist
httpPipe:3:4
PlayPPipe:5:6
ReadPPipe:7:8
GetConfigJsonurl: 192.168.0.5:9000/playlist
####---->UrlListLength: 2
httpPipe:0:4
PlayPPipe:7:8
ReadPPipe:9:10

だから私は基本的にパイプがstdファイル記述子を開くのを防ぐ方法を知りたいと思います。

ベストアンサー1

新しいファイル記述子は、常にまだ使用されていない最も低い整数を占めます。

$猫> test.c
メイン(){終了(開く("/dev/null",0));}
^D
$cctest.c
$./a.出力; $?
サム
$./a.out <&-;エコ$?
0
$./a.out >&-;エコ$?
1

システムは、「標準ファイル記述子」や同様のものには興味がありません。ファイル記述子 0 が閉じられると、新しいファイル記述子が 0 に割り当てられます。

プログラムや実行方法にこの問題が発生する可能性がありますかclose(0)

おすすめ記事