首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >停止管道()开口

停止管道()开口
EN

Unix & Linux用户
提问于 2011-01-16 06:00:02
回答 1查看 657关注 0票数 2

目前,我已经有了分叉两个进程的代码。第一个读取http流广播,并将数据向下推到管道(用pipe()打开),以便第二进程使用OSS读取、解码并输出到声卡。

我一直试图调试解码部分(单独的问题),并且在打印管道时遇到了这样一种情况:管道的文件描述符为0。据我所知,这意味着史丁。这是已知的管道问题吗?它可能意外地打开标准文件描述符之一吗?如果是的话,我该如何绕过它呢?

我的管道/叉子代码在下面。有相当多的其他代码,我希望是不相关的。

代码语言:javascript
复制
//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‘时,它会向两个子进程发送一个命令,这些子进程干净地关闭它们,然后关闭所有管道和循环,再次打开它们,然后再次分叉这两个进程。

当它第一次通过循环时,它看起来很有效,但是在第二次循环中,我得到了下面的输出。

代码语言:javascript
复制
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文件描述符。

EN

回答 1

Unix & Linux用户

发布于 2011-01-16 09:28:16

strace可以给出您的可执行文件如何使用文件描述符的图片:

代码语言:javascript
复制
strace -f -e trace=file,desc,ipc -o /tmp/strace.txt /path/to/exe arg1 arg2...
票数 2
EN
页面原文内容由Unix & Linux提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://unix.stackexchange.com/questions/5951

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档