- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对用 C++ 编写非常陌生,并且正在研究使用管道在进程之间进行通信。我写了一个非常简单的程序,当我发送字符串或整数时它可以工作,但是当我尝试发送一个结构(在这种情况下是消息)时,当我尝试在另一端读取它时,我得到 null 。有没有人对此有一些见解可以分享?感谢您的宝贵时间。
#include <unistd.h>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define BUFFER_LEN sizeof(message)
using namespace std;
struct message{
int from;
string msg;
};
void childCode(int *pipeOUT, int *pipeIN, message buffer){
// Local Buffer for input from pipeIN
cout << "Child: Sending Message"<< endl;
buffer.msg = "Child:I am the child!!";
write(pipeOUT[1],(char*) &buffer, BUFFER_LEN); // Test Child -> Parent comms
cout << "Child: Message Sent"<<endl;
read(pipeIN[0],(char*) &buffer,BUFFER_LEN); // Test Child <- Parent comms
cout << "Child: Recieved: "<< buffer.msg << endl;
cout << "Child Exiting..."<< endl;
exit(0); // Child process End
}
int main(int argCount, char** argVector){
pid_t pid;
int childPipeIN[2];
int childPipeOUT[2];
message buffer; // Buffer for reading from pipe
// Make Parent <- Child pipe
int ret = pipe(childPipeIN);
if (ret == -1){
perror("There was an error creating the childPipeIN. Exiting...");
exit(1);
}
// Make Parent -> Child pipe
ret = pipe(childPipeOUT);
if (ret == -1){
perror("There was an error creating the childPipeOUT. Exiting...");
exit(1);
}
// Fork off Child
pid = fork();
if (pid == -1){
perror("There has been an issue forking off the child. Exiting...");
exit(1);
}
if (pid == 0){ // Child code
cout << "Child PID = " << getpid() << endl;
childCode(childPipeIN,childPipeOUT,buffer);
}
else{ // Parent Code
cout << "Parent PID = " << getpid() << endl;
// Test Parent <- Child comms
read(childPipeIN[0], (char*) &buffer, BUFFER_LEN);
cout << "Parent: I recieved this from the child...\n" << buffer.msg << endl;
buffer.msg = "Parent: Got you message!";
// Test Parent -> Child comms
write(childPipeOUT[1], (char*) &buffer, BUFFER_LEN);
wait(null);
cout << "Parent: Children are done. Exiting..." << endl;
}
exit(0);
}
最佳答案
是的。我投票关闭。然后我更仔细地阅读了 Dupe 并意识到它没有很好地解释问题或解决方案,并且该解决方案并不真正符合 OP 的意图。
一个人不会简单地将 std::string
写入管道。 std::string
不是一个微不足道的数据。那里有不 sleep 的指针。
想想看,将 std::string
写入任何东西都是非常危险的。包括另一个 std::string
。我不会,不能用文件。这个 smurf 很难押韵,所以我不会再继续讲苏斯博士了。
对于另一个进程,引用包含 string
数据的存储的指针,允许 string
调整大小的魔法,可能毫无意义,并且如果它确实意味着什么,你可以打赌它不是你想要弄乱的东西,因为它肯定不是 string
的数据。
即使在另一个 std::string
的同一进程中,这两个字符串也不能和平共存并指向同一内存。当超出范围、调整大小或执行几乎任何其他会改变 string
的操作时,就会发生错误。
不相信我?检查 BUFFER_LEN
。无论您的消息有多大,BUFFER_LEN
都不会改变。
这适用于您想要写入的所有内容,而不是简单的数据 block 。整数,写掉。整数结构和固定大小的字符数组,写掉。 std::vector
?没有这样的运气。您可以编写 std::vector::data
当且仅当它包含的任何内容都是微不足道的。
std::is_pod
may help you decide what you can and cannot read and write the easy way.
Serialize the data.建立定义数据格式的通信协议(protocol),然后使用该协议(protocol)作为您读写代码的基础。
移动 string
的典型解决方案是 null 终止缓冲区,就像在 C 的美好时光一样,并将 string
的大小添加到string
就像 Pascal 的美好时光。
我喜欢 Pascal 方法,因为它允许您提前调整接收器缓冲区的大小。对于空终止,您必须玩几十轮 Getta 字节来寻找空终止符,并希望您的缓冲区足够大,或者将丑陋与缓冲区大小调整带来的动态分配和复制结合起来。
写作几乎就是您现在正在做的事情,但是逐个结构成员。在上面的例子中
message.from
写入管道。message.msg
的长度写入管道。message.msg.data()
写入管道。两个注意事项:
int
可能是另一个人的 long
的大小,所以使用 fixed width integers .读取有点复杂,因为对读取的单个调用将返回最多 请求的长度。获取所需的所有数据可能需要多次读取,因此您需要一个循环函数,直到所有数据到达或由于管道、文件、套接字等关闭而无法到达。
message.from
都到达。message.msg
的所有长度。message.msg.resize(length)
调整 message.msg
的大小以保存消息。message.msg
都到达。您可以将消息直接读入 message.msg.data()
。关于c++ - 如何通过管道 C++ 发送结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42728684/
我正在使用 Assets 管道来管理我的 Grails 3.0 应用程序的前端资源。但是,似乎没有创建 CoffeeScript 文件的源映射。有什么办法可以启用它吗? 我的 build.gradle
我有一个我想要的管道: 提供一些资源, 运行一些测试, 拆资源。 我希望第 3 步中的拆卸任务运行 不管 测试是否通过或失败,在第 2 步。据我所知 runAfter如果前一个任务成功,则只运行一个任
如果我运行以下命令: Measure-Command -Expression {gci -Path C:\ -Recurse -ea SilentlyContinue | where Extensio
我知道管道是一个特殊字符,我需要使用: Scanner input = new Scanner(System.in); String line = input.next
我再次遇到同样的问题,我有我的默认处理方式,但它一直困扰着我。 有没有更好的办法? 所以基本上我有一个运行的管道,在管道内做一些事情,并想从管道内返回一个键/值对。 我希望整个管道返回一个类型为 ps
我有三个环境:dev、hml 和 qa。 在我的管道中,根据分支,阶段有一个条件来检查它是否会运行: - stage: Project_Deploy_DEV condition: eq(varia
我有 Jenkins Jenkins ver. 2.82 正在运行并想在创建新作业时使用 Pipeline 功能。但我没有看到这个列为选项。我只能在自由式项目、maven 项目、外部项目和多配置之间进
在对上一个问题 (haskell-data-hashset-from-unordered-container-performance-for-large-sets) 进行一些观察时,我偶然发现了一个奇
我正在寻找有关如何使用管道将标准输出作为其他命令的参数传递的见解。 例如,考虑这种情况: ls | grep Hello grep 的结构遵循以下模式:grep SearchTerm PathOfFi
有没有办法不因声明性管道步骤而失败,而是显示警告?目前我正在通过添加 || exit 0 来规避它到 sh 命令行的末尾,所以它总是可以正常退出。 当前示例: sh 'vendor/bin/phpcs
我们正在从旧的 Jenkins 设置迁移到所有计划都是声明性 jenkinsfile 管道的新服务器……但是,通过使用管道,我们无法再手动清除工作区。我如何设置 Jenkins 以允许 手动点播清理工
我在 Python 中阅读了有关 Pipelines 和 GridSearchCV 的以下示例: http://www.davidsbatista.net/blog/2017/04/01/docume
我有一个这样的管道脚本: node('linux'){ stage('Setup'){ echo "Build Stage" } stage('Build'){ echo
我正在使用 bitbucket 管道进行培训 这是我的 bitbucket-pipelines.yml: image: php:7.2.9 pipelines: default:
我正在编写一个程序,其中输入文件被拆分为多个文件(Shamir 的 secret 共享方案)。 这是我想象的管道: 来源:使用 Conduit.Binary.sourceFile 从输入中读取 导管:
我创建了一个管道,它有一个应该只在开发分支上执行的阶段。该阶段还需要用户输入。即使我在不同的分支上,为什么它会卡在这些步骤的用户输入上?当我提供输入时,它们会被正确跳过。 stage('Deplo
我正在尝试学习管道功能(%>%)。 当试图从这行代码转换到另一行时,它不起作用。 ---- R代码--原版----- set.seed(1014) replicate(6,sample(1:8))
在 Jenkins Pipeline 中,如何将工件从以前的构建复制到当前构建? 即使之前的构建失败,我也想这样做。 最佳答案 Stuart Rowe 还在 Pipeline Authoring Si
我正在尝试使用 执行已定义的作业构建 使用 Jenkins 管道的方法。 这是一个简单的例子: build('jenkins-test-project-build', param1 : 'some-
当我使用 where 过滤器通过管道命令排除对象时,它没有给我正确的输出。 PS C:\Users\Administrator> $proall = Get-ADComputer -filter *
我是一名优秀的程序员,十分优秀!