【问题标题】:How to communicate with process in Linux?如何在 Linux 中与进程通信?
【发布时间】:2014-08-10 11:43:54
【问题描述】:

大约 5 年前,我有一个完全正确且有效的程序。那时我停止使用它,我升级了操作系统,时间过去了,灰尘覆盖了代码,最后我把它挖出来发现它不再与子进程通信了。

这里是代码(简化了,但它显示了问题):

#include <stdio.h>
#include <signal.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <string.h>
#include <sys/time.h>
#include <sys/stat.h>

#include <iostream>
#include <string>
#include <cassert>

int main()
{
    std::cout << "Creating the pipe for reading from the process" << std::endl;   
    const std::string pipe_name = "/tmp/proc_comm";
    {
        int res = mkfifo(pipe_name.c_str(),0777);
        assert(res==0);    
    }

    std::cout << "Launching subprocess" << std::endl;
    FILE *cmd_handle = popen(("espeak -x -q -z 1> "+pipe_name+" 2> /dev/null").c_str(), "w");
    assert(cmd_handle!=0);

    std::cout << "Opening the pipe" << std::endl;
    int pipe_id = open(pipe_name.c_str(),O_RDONLY);
    assert(pipe_id!=-1);

    const std::string message = "hello\n";

    std::cout << "Sending the message" << std::endl;
    if (!fwrite(message.c_str(),sizeof(char),message.length(),cmd_handle))
        assert(0);
    if (ferror(cmd_handle))
        assert(0);
    if (fflush(cmd_handle)!=0)
        assert(0);

    fd_set output_set;
    FD_ZERO(&output_set);
    FD_SET(pipe_id,&output_set);

    static timeval timeout;
    timeout.tv_sec = 10;
    timeout.tv_usec = 0;

    std::cout << "Selecting the pipe for reading" << std::endl;
    const int inputs = select(pipe_id+1, // max of pipe ids + 1
                              &output_set,0,0,&timeout);

    if (inputs==-1) // error
      assert(0);
    else if (inputs==0) // nothing to read
        assert(0); // HERE (*)
    else  
    {
        // we can only read from our pipe
        assert(inputs==1); 
        assert(FD_ISSET(pipe_id,&output_set));

        const int bufsize = 20000;
        char char_buf[bufsize];
        memset(char_buf,0,sizeof(char_buf));

        std::cout << "Reading from the pipe" << std::endl;
        const int count = read(pipe_id,char_buf,bufsize-1);

        if (count==-1) 
            assert(0);

        std::cout << "Read " << count << std::endl;
    }

    return 0;
}

它编译、运行、创建并打开用于读取进程的管道、启动子进程、发送消息但没有可从进程读取的内容(HERE (*) 行)。

那么你现在如何从流程中读取?如果可能的话,我想保留一般的工作流程,即使用管道进行读取,并使用进程句柄进行写入处理。

【问题讨论】:

  • 你怎么知道子进程在你超时之前发送了一些东西?
  • @jxh,我可以在 bash 中运行该进程并评估超时。 10秒绰绰有余。除此之外它还有效:-)

标签: c++ linux unix process pipe


【解决方案1】:

可能在 select() 返回时 FIFO 是空的。

这个简单的实验证明了这一点。

  • 使用修改后的行运行您的代码
    FILE *cmd_handle = popen (("echo begin; espeak -x -q -z 1&gt;" + pipe_name + " 2&gt;/dev/null; echo end;").c_str (), "w");.
  • cat &lt; /tmp/proc_commn &amp;; ./a.out;
  • 输出是:

    创建用于从进程读取的管道
    启动子进程
    打开管道
    开始
    发送消息
    选择要读取的管道
    a.out: main.cpp:64: int main(): 断言“0”失败。
    h@l'oU       ####
    结束

使用 pclose()。

添加pclose() 调用后,问题消失:

if (!fwrite(message.c_str(),sizeof(char),message.length(),cmd_handle))
    assert(0);
if (ferror(cmd_handle))
    assert(0);
if (fflush(cmd_handle)!=0)
    assert(0);

/* pclose added. */
if (pclose (cmd_hanlde) < 0)
{
    /* handle error. */
}

fd_set output_set;

那么你现在如何解读这个过程?

我不认为您的代码在任何意义上都过时了。我不是专家,所以我对这个问题没有很好的答案。但我希望这篇文章能帮助您解决问题。

更新。

使用 strace 和 espeak 1.46.2 进行调试。

使用 strace (strace -f ./a.out) 和任何 select() 超时运行代码:
Porcess 31985 是父进程(主进程)。
进程 31987 是子进程 (espeak)。

# Child process starts trying to read from stdin.
[pid 31987] read(0,  <unfinished ...>
[pid 31985] <... sync resumed> )        = 0
[pid 31985] write(1, "Sending the message\n",  ...
[pid 31985] fstat64(4, {st_mode=S_IFIFO|0600, st_size=0, ...}) = 0
[pid 31985] mmap2(NULL, 4096, PROT_READ|PROT_WRITE,  ...

# Main process writes message to the pipe.
[pid 31985] write(4, "hello\n", 6)      = 6

# Child process reads message from the pipe.
[pid 31987] <... read resumed> "hello\n", 4096) = 6
[pid 31985] write(1, "Selecting the pipe for reading:3"..., 3 ...
) = 33
[pid 31987] fstat64(1,  <unfinished ...>

# Main process calls select().
[pid 31985] select(4, [3], NULL, NULL, {10, 0} <unfinished ...>
[pid 31987] <... fstat64 resumed> {st_mode=S_IFIFO|0755, st_size=0, ...}) = 0
[pid 31987] mmap2(NULL, 4096, PROT_READ ...

# Child process calls read() again (second time).
# Seems like it waits another portion of input.
# Note: The previous input don't processed yet.
[pid 31987] read(0,  <unfinished ...>

# Despite big value of `select()` timeout, there is no data processing here.
# Select returns by timeout.
[pid 31985] <... select resumed> )      = 0 (Timeout)
[pid 31985] write(2, "a.out: main.cpp:69: i ...
) = 54
[pid 31985] rt_sigprocmask(SIG_UNBLOCK, [ABRT], NULL, 8) = 0
[pid 31985] gettid()                    = 31985
[pid 31985] tgkill(31985, 31985, SIGABRT) = 0
[pid 31985] --- SIGABRT (Aborted) @ 0 (0) ---

为什么strace日志中没有输入处理?

我们来看看espeaksource code。 这是从stdin (src/espeak.cpp) 读取输入的代码(假设在一组内部变量上找出命令行参数的映射是正确的):

 728     // line by line input on stdin
 729     while(fgets(p_text,max,stdin) != NULL) // <--- * here first and second 
 730     {                                      // <--- * read() call.
 731         p_text[max-1] = 0;
 732         espeak_Synth(p_text,max,0,POS_CHARACTER,0,synth_flags,NULL,NULL);
 733 
 734     }

评论espeak_Synth()函数(src/speak_lib.h:268):

 /*
  * Synthesize speech for the specified text.  
  * The speech sound data is passed to the calling      
  * program in buffers by means of the callback function specified by 
  * espeak_SetSynthCallback(). The command is asynchronous: 
  * it is internally buffered and returns as soon as possible.
  * ...
  */

espeak_Synth() 是异步函数(这就是为什么我们会连续看到两个read() 调用)。 我假设espeak_Synth() 的调用应该导致数据写入stdout(经过一些延迟)。但即使select() 超时值很大,它也不会发生。这里有些奇怪。

【讨论】:

  • 非常感谢您的帮助 +1。我暂时不接受你的回答(我真的很感激,不要误会我的意思),因为select 有点奇怪——你可以增加超时,但它仍然会报告没有数据(尽管有是!)。与pclose 一样,它会终止流程,因此将其添加到工作流程中会受到很大的惩罚(想想对子流程的数千个请求)。您应该能够启动子进程(守护程序)并与之通信,而不会在每个请求上杀死它。总而言之,你给了我很好的提示,谢谢!
  • 感谢您的所有帮助和工作。我得出的结论是 espeak 中存在一个细微的错误,并且通信本身还可以。我尝试了简单的回显(我的)程序,它打印回输入,它很好。
猜你喜欢
  • 2020-03-16
  • 2012-10-20
  • 1970-01-01
  • 2013-11-13
  • 1970-01-01
  • 1970-01-01
  • 2012-11-22
  • 2015-06-19
  • 2020-04-03
相关资源
最近更新 更多