- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
大约 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 (*)
)。
那么现在你是如何从流程中读取数据的?如果可能的话,我想保留一般的工作流程,即使用管道进行读取,使用流程句柄写入流程。
最佳答案
FILE *cmd_handle = popen (("echo begin; espeak -x -q -z 1>" + pipe_name + " 2>/dev/null; echo end;").c_str (), "w");
.cat < /tmp/proc_commn &; ./a.out;
输出为:
创建用于从进程读取的管道
启动子进程
打开管道
开始
发送消息
选择要读取的管道
a.out: main.cpp:64: int main(): 断言“0”失败。
h@l'oU #### <--- FIFO 中的数据仅在断言后。
结束
加上 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 ( 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) ---
让我们看看source code的 espeak
.这是从 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()
的值(value)很大,它也不会发生。暂停。这里有些奇怪。
关于c++ - Linux 中如何与进程通信?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25228249/
大多数语言都支持双向进程通信。例如,在 Python 中,我可以(草率地)执行以下操作: >>> from subprocess import * >>> p = Popen('nslookup',
致力于使用 C++ 在 arduino 和 PC (Win 7) 之间进行通信。使用 WriteFile 和 ReadFile 创建通信或简单地发送或接收数据没有问题。但是当我想以某种方式“协调”沟通
我们正在开发一个基于微服务的应用程序。它们将使用 Helm Package Manager 部署到 kubernetes,并且它们都存储了自己的存储库和 helm chart。以下是我们微服务的名称。
我正在开发一个大型 MVVM 应用程序。我为此使用了 MVVM 轻量级工具包。该应用程序就像一个带有后退和前进按钮的网络浏览器。主视图是一个用户控件。我在主视图用户控件中放置了后退和前进按钮。主视图又
我在 java 和 freepascal(lazarus) 应用程序之间的通信有问题。我使用套接字。它们正确连接。一切都很顺利,直到我想从一个应用程序向另一个应用程序发送一些东西。在java而不是“a
我已经使用客户端套接字和服务器套接字使用C#编写了群聊。 当我使用VS 2017在自己的PC中运行程序(服务器和客户端)时,客户端和服务器之间的通信工作正常。 当我在笔记本电脑中运行客户端程序,并在自
Kubernetes 中两个不同 Pod 之间的通信是如何发生的? 就我而言,我有两个 Pod:前端和后端,它们都有不同的容器。 我希望我的前端 pod 与后端 pod 通信,但我不想使用后端 pod
我正在尝试在浏览器中嵌入的 flash 实例与在 C# WinForms 应用程序中运行的 flash 实例之间进行通信...我收到一个编译错误,内容为: 1119 Access of possibl
鉴于网络上缺乏信息,请问一个问题:我要在 Android 中创建一个应用程序,使用一个数据库应用程序 rails 。为此,我需要一个手动 session 。所以如果有人准备好了示例/教程显示通信 an
我正在编写一个应用程序,它将通过 MySQL 数据库对用户进行身份验证。我已经用 Java (android) 编写了它,但现在正在移植到 Windows 手机。 PHP 文件使用 $get 然后回显
是否可以通过互联网在两个不同设备上的两个不同应用程序之间建立通信。我想从设备 A 上的应用程序点击一个设备 B 上的应用程序,然后从设备 B 上的应用程序获取数据到设备 A 上的应用程序。如果可能,如
这是脚本: 它被放置在其他网站上。 com 并显示一个 iframe。如果有人点击 iframe 中的某个内容,脚本应该将一个 div 写入 othersite 。 com. 所以我的问题是如何做到
你好我是 php 的新手,我用 c++ 编写了整个代码并想在 php 中使用这段代码。所以我为我的代码制作了 dll 以使用它。但是我不能在 php 中使用这个 dll,可以谁能给我完整的代码来使用
我确定之前已经有人问过(并回答过)此类问题,所以如果是这样,请将我链接到之前的讨论... 在 C++ 中,假设我有一个 ClassA 类型的对象,其中包含一个 ClassB 类型的私有(private
我正在尝试使用 ATmega32 进行串行通信。首先,我使用 RS232,使用 USB-to-RS232 建立使用串行终端的接收和传输(在我的例子中是 tera 术语)。无论我从串行终端 Atmega
我找不到适用于 Ruby 的 SSL 实现。 我的部分项目需要服务器和客户端之间的安全通信链接,我希望为此使用 SSL 以创建安全 session 。 谢谢 最佳答案 如果你使用 Ruby 1.9.x
我正在尝试在客户端/服务器之间进行 SSL 通信。 到目前为止,我已经从 keystore 创建了 java.security.cert.X509Certificate。接下来我应该怎么做才能使这次沟
我在与 Windows 上的 USB 设备 通信时遇到问题。我不能使用 libusb 或 WinUSB,因为我有一个特定的驱动程序(Silabs USB 到 UART,这是一个 USB 到串口的桥接器
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我发现 xcom 实际上是将数据写入数据库并从其他任务中提取数据。我的数据集很大,将其腌制并写入数据库会导致一些不必要的延迟。有没有办法在不使用 xcom 的情况下在同一 Airflow Dag 中的
我是一名优秀的程序员,十分优秀!