- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在从事一个在三个进程之间发送消息的项目:一个有两个 child 的 parent 。第一个进程将接收消息,然后将消息发送给第二个进程。第二个进程将读取一条消息,然后在将其发送到第三个进程之前对其进行转换。第三个进程将读取消息并进一步转换该消息。最后,消息将被发送回第一个进程,打印它。
这将在 UNIX 中使用管道和系统调用来实现。我不太熟悉这个过程,所以我很感激你能提供的任何提示/建议。下面是我的代码。谢谢你!
*#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#define ERR (-1) /* indicates an error condition */
#define READ 0 /* read end of a pipe */
#define WRITE 1 /* write end of a pipe */
#define STDIN 0 /* file descriptor of standard in */
#define STDOUT 1 /* file descriptor of standard out */
int main()
{
int pid_1, /* will be process id of first child, which inverts the string */
pid_2; /* will be process id of second child, which converts the string to uppercase */
int fd[2]; //descriptor array for parent process
int fd2[2]; //descriptor array for first child process
int fd3[2]; //descriptor array for second child process
char ch [100]; //original character array
char ch2 [100]; //character array after reversal
int index = 0; //size
char character;
while((character = getchar()) != '\n') //get input and put it into array
{
ch[index] = character;
index++;
}
if(pipe (fd) == ERR)
{
perror("Parent pipe cannot be created\n");
exit (ERR);
}
if (pipe (fd2) == ERR) /* create a pipe */
{ /* must do before a fork */
perror ("Pipe cannot be created.\n");
exit (ERR);
}
if (pipe (fd3) == ERR) /* create a pipe */
{ /* must do before a fork */
perror ("Second pipe cannot be created.\n");
exit (ERR);
}
if ((pid_1 = fork()) == ERR) /* create 1st child */
{
perror ("Second process cannot be created.\n");
exit (ERR);
}
if (pid_1 != 0) /* in parent */
{
close(fd2[0]); //close read end of first child
close(fd[1]); //close write end of parent
printf("Parent process sends message %s\n", ch);
write(fd2[1], ch, sizeof(ch)); //write to write end of first child
close(fd2[1]); //close write end of first child
close(fd[0]);
if ((pid_2 = fork ()) == ERR) /* create 2nd child */
{
perror ("Third process cannot be created.\n");
exit (ERR);
}
if (pid_2 != 0) /* still in parent */
{
wait ((int *) 0); /* wait for children to die */
wait ((int *) 0);
read(fd[0], ch2, sizeof(ch2)); //read read end of parent process
printf("Parent process receives message %s\n", ch2);
int i = 0;
while (i < index)
{
printf("%c", ch2[i]); //print message
i++;
}
printf("\n");
close(fd3[1]); //close write end of second child
close(fd[0]); //close read end of parent process
}
else /* in 2nd child */
{
close(fd3[1]); //close write end of second child
close(fd2[0]); //close read end of first child
read(fd3[0], ch2, sizeof(ch2)); //read read end of second child
printf("Second child receives %s\n", ch2);
int i = 0;
while (i < index)
{
ch2[i] = toupper(ch2[i]); //convert to uppercase
i ++;
}
printf("Second child sends message %s\n", ch2);
write(fd[1],ch2, sizeof(ch2)); //write to write end of parent process
close(fd3[0]); //close read end of second child
close(fd[1]); //close read end of parent process
}
}
else /* in 1st child */
{
close(fd2[1]); //close write end of first child
close(fd[0]); //close read end of parent process
read(fd2[0], ch, sizeof(ch)); //read read end of first child
printf("First child receives message %s\n", ch);
int i = 0;
while (i < index)
{
ch2[i] = ch[index - 1 - i]; //reverse
i++;
}
printf("First child sends message %s\n", ch2);
write(fd3[1], ch2, sizeof(ch2)); //write to write end of second child
close(fd3[1]); //close write end of second child
close(fd2[0]); //close read end of first child
}
exit(0);
}
最佳答案
一个人不应该在没有测试的情况下编写这么多代码。 (有一种更好的方法,我稍后会讲到。)但是如果您发现自己处于这种状态,可以使用以下一种方法来查找问题。
第 1 步:放入一些诊断输出语句以验证代码是否按照您的意图和期望执行:
...
printf("Here\n");
read(fd3[0], ch2, sizeof(ch2)); //read read end of second child
close(fd3[0]); //close read end of second child
printf("second child receives %s\n", ch2);
...
printf("Input reversed\n");
printf("first child sends %s\n", ch2);
write(fd3[1], ch2, sizeof(ch2)); //write to write end of second child
...
这足以表明某些东西不起作用。
第 2 步:在不改变错误行为的情况下尽可能简化代码,直到错误无处可藏。在这种情况下你会发现你仍然忽略了pipe(fd3)
,即使在我向您指出这一点并且您承认问题并说您已解决它之后。如果这不能使您相信简化的重要性,那么什么都不会。
编写代码的正确方法是慢慢构建,每一步都进行测试。在尝试将其连接到大电路之前,您应该已经测试过 fd3 链接。从小而简单的事情开始,一次增加一点复杂性,单独测试新功能,永远不要添加不起作用的代码。
关于c - 在三个管道之间传输消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29085112/
我一直在做一些关于测量数据传输延迟的实验 CPU->GPU 和 GPU->CPU。我发现对于特定消息大小,CPU->GPU 数据传输速率几乎是 GPU->CPU 传输速率的两倍。谁能解释我为什么会这样
我将 ElasticSearch 用作我的 Post 模型的 Rails pet 项目应用程序的全文引擎。在我的 posts_controller.rb 索引操作中: def index
概述 流经网络的数据总是具有相同的类型:字节,这些字节如何传输主要取决于我们所说的网络传输。用户并不关心传输的细节,只在乎字节是否被可靠地发送和接收 如果使用 Java 网络编程,你会发现,某些时候当
我正在编写一些代码,以便将共享点从该页面转移到另一个页面: Server.Transfer("/DefectManagement/DefectList/default.aspx") 但是我遇到了这个问
我有这个泄漏,任何猜测?这个类有一些奇怪的引用。我的代码的任何地方都没有 contentobserver In com.example:1.5.0:27. com.example.ui.record
我听说过点对点内存传输并阅读了一些关于它的内容,但无法真正理解与标准 PCI-E 总线传输相比它的速度有多快。 我有一个使用多个 GPU 的 CUDA 应用程序,我可能对 P2P 传输感兴趣。我的问题
ftping 文件时,Transmit 中是否有忽略或过滤器列表?我希望它忽略上传 .svn 文件等。 最佳答案 是的。转到首选项并选择 Rules标签。在那里您可以定义要跳过哪些文件的规则。实际上,
我有以下片段来生成声音,在 while 循环中,我想动态更改它,以便它在声音生成期间创建不同频率的声音。 for(uint16_t i = 0; i < sample_N; i++) { da
我正在尝试使用 Delphi 2010 和 Indy 对 Web 服务进行概念验证。我此时的代码是: procedure TForm1.Log(const sEvent, sMsg: String);
我有一个 ActiveMQ JMS 代理,在端口 61616 上使用默认的 openwire TCP 传输公开。 我有许多远程客户端可以绑定(bind)到此代理来监听他们的消息。 如果我想打开 kee
reconnection strategies文档仅使用 JMS 示例,但是 FTP transport documentation确实说明了重新连接策略的使用,但没有任何细节或示例。 进一步,如果你
我有 2 个 TreeView,第一个填充有项目。 try { CheckBoxTreeItem treeRoot = new CheckBoxTreeItem("Root"); tr
在我为学校开发的一个网站上,用户输入他们的学校电子邮件和密码,如果他们已注册,则登录。如果没有,则会显示登录的第二部分,要求输入笔名称并确认密码。正因为如此,以及我复杂的业余 Django 编程,我有
我正在开发一个 Web 服务,我们在其中使用 LINQ-to-SQL 进行数据库抽象。当客户使用我们的网络服务时,对象被序列化为 XML,一切都很好。 现在我们希望开发我们自己的使用本地数据类型的客户
我应该创建一个名为“Backwards”的方法,该方法将列表从尾部横向到头部,但是当我运行代码时,它出现说(第 88 行)它找不到光标 = cusor.prev;象征。我需要在循环中再次设置上一个链接
给定像 Uint8Array 这样的类型化数组,似乎有两种方法可以通过 worker 传输它们。 选项 1 直接发送缓冲区并在接收端进行转换: 发件人:postMessage({fooBuffer:
在 PHP + jQuery 环境中,我和我的 friend 无法得出最佳解决方案。我们正在使用 Ajax 从数据库中获取数据。 解决方案 1 - Ajax 应该只传输数据,而不是 HTML 好处:我
大家好,非常感谢您的宝贵时间。 有一个 std::stringstream 需要传输到远程机器。网络库允许我用以下方法构建数据包: CreatePacket( const void * DATA, s
我正在使用 libcurl 通过 FTP 传输二进制文件 (.exe),并将其保存到本地文件。问题是文件传输后,它已被更改,不再是有效的 Win32 应用程序,因此无法运行。这是我的做法: CURL
各位程序员, 当我将它上传到我的 FTP 时,我的网站出现此错误:资源被解释为样式表,但使用 MIME 类型文本/纯文本传输 BlahBlahi
我是一名优秀的程序员,十分优秀!