doushi6932 2018-03-21 08:07
浏览 107

使用exec()时,命名管道不起作用

I'm trying to send some data from php staight into a running cpp script. I'm trying to do this using a named pipe in a "send" application. Let me share some code first.

loop.cpp

main( int argc, char* argv[] ){
    //open pipe
    int action;
    int value;
    char action_result[ BUFSIZ ];
    char value_result[ BUFSIZ ];

    mkfifo( FIFO_ACTION, 0666 );
    mkfifo( FIFO_VALUE, 0666 );

    //loop variables
    bool close_loop = false;
    unsigned int i = 0;

    while( !close_loop ){

        action = open( FIFO_ACTION, O_RDONLY );
        value = open( FIFO_VALUE, O_RDONLY );
        read( action, action_result, BUFSIZ );
        read( value, value_result, BUFSIZ );

        if( strcmp( "", action_result ) != 0 ){
            if( strcmp( "ly", action_result ) == 0 ){

                cout << "Recieved: " << value_result << endl;

            } else if( strcmp( "cl", action_result ) == 0 ){
                break;
            }
        }

        memset( action_result, 0, sizeof(action_result) );
        memset( value_result, 0, sizeof(value_result) );

        close( value );
        close( action );

        usleep( 5000 );
    }

    close( action );
    close( value );

    unlink( FIFO_ACTION );
    unlink( FIFO_VALUE );

    cout << "Closing output" << endl;

    return 0;

}

send.cpp

int main( int argc, char* argv[] ){
    int action;
    int value;

    /* write str to the FIFO */
    action = open( FIFO_ACTION, O_WRONLY );
    value = open( FIFO_VALUE, O_WRONLY );

    write( value, argv[ 1 ], sizeof( argv[ 1 ] ) );
    write( action, "ly", sizeof( "ly" ) );

    cout << "Sended: " << argv[ 1 ] << endl;

    close( value );
    close( action );

    /* remove the FIFO */

    return 0;
}

If I run both apps from the terminal, everything works fine and I get my output. But I have to use the following code to execute the send.cpp file from php.

exec( "/home/pi/Bureaublad/send $id", $output );
print_r($output);

When I do this, I get the correct result "Sended: 14" as in the terminal, however, the pipes seem not to work as I don't have any output in my loop.cpp running from the terminal.

I personaly suspect there is some unix user stuff going on but I can't find anything about it using google. Whatever I try, I don't get an output.

Can somebody please point me in the right direction?

Thanks a thousand times!!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 对于这个问题的代码运行
    • ¥50 三种调度算法报错 有实例
    • ¥15 关于#python#的问题,请各位专家解答!
    • ¥200 询问:python实现大地主题正反算的程序设计,有偿
    • ¥15 smptlib使用465端口发送邮件失败
    • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
    • ¥15 对于squad数据集的基于bert模型的微调
    • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
    • ¥20 steam下载游戏占用内存
    • ¥15 CST保存项目时失败