- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我一直在做这个作业,这已经是第 10 个版本了。问题是只有一条消息通过管道,并计算出正确的结果。以下字符串根本不通过,或者在摆弄缓冲区后只有一些字符。请帮忙,我真的在这个问题上浪费了很多时间,我需要学习这些东西以便即将进行的测试。
#include <ctype.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <string.h>
#include <stdio_ext.h>
/* Prototypes */
void usage(void);
void calchild(void);
char command[] = "<not yet set>";
int main(int argc, char **argv)
{
char input1[512];
char input2[512];
char tmp[512];
char *endptr;
char c = 0;
int a, b, result;
pid_t cpid;
int status = 0;
int stocpipe[2]; /* Server to client pipe. [0] -read; [1]- write*/
int ctospipe[2]; /* Client to server pipe. - || - */
int i = 0;
FILE *send, *receive;
if(argc > 1)
{
usage();
}
/* Pipe Setup */
if(pipe(stocpipe) != 0 || pipe(ctospipe) != 0)
{
fprintf(stderr, "ERROR: Can't create unnamed pipe! \n");
exit(EXIT_FAILURE);
}
switch(cpid = fork())
{
case -1:
fprintf(stderr, "ERROR: Can't fork! \n");
exit(EXIT_FAILURE);
break;
case 0:
/* calchild */
close(stocpipe[1]);
close(ctospipe[0]);
receive = fdopen(stocpipe[0], "r");
send = fdopen(ctospipe[1], "w");
/*Gets the string from the parent process and does the computation.*/
while(fgets(input2, 17, receive) != NULL)
{
strcpy(tmp, input2);
fprintf(stdout, "After receive: %s", tmp);
a = strtol(tmp, &endptr, 10);
fprintf(stdout, "a = %d\n", a);
b = strtol(endptr, &endptr, 10);
fprintf(stdout, "b = %d\n", b);
c = endptr[0];
/*Loops until it finds a non-space char*/
for(i = 0; isspace(c = endptr[i]); i++);
switch(c)
{
case '+':
/*add*/
result = a + b;
break;
case '-':
/*subtract*/
result = a - b;
break;
case '*':
/*multiply*/
result = a * b;
break;
case '/':
/*divide*/
result = a / b;
break;
default:
fprintf(stderr, "the funk!? %c\n", c);
break;
}
fprintf(stderr, "%d\n", result);
fprintf(send, "%d", result);
}
break;
default:
close(stocpipe[0]);
close(ctospipe[1]);
send = fdopen(stocpipe[1], "w");
receive = fdopen(ctospipe[0], "r");
/*Reads string from stdin and sends it to the child process through a pipe. */
while(fgets(input1, 17, stdin) != NULL)
{
fprintf(stdout, "Before send: %s", input1);
fwrite(input1, 17, 1, send);
if(fflush(send) == EOF)
{
fprintf(stderr, "Flush error!");
}
}
(void) waitpid(cpid, &status, 0);
if(status != 0)
{
fprintf(stderr, "ERROR: Child calculator exited with %d \n", status);
}
break;
}
return 0;
}
void usage(void)
{
fprintf(stderr,"Usage: %s", command);
exit(EXIT_FAILURE);
}
该程序是一个计算器。它的目的是学习IPC。父进程接受来自标准输入的字符串(例如 3 5 +)并将其发送给子进程。 child 解析字符串并计算结果。然后它将结果发送回父进程,然后将其打印到标准输出。
我在将字符串发送给 child 的部分卡住了。第一个接受的字符串被发送给 child 。它计算结果很好。第二个字符串和之后的每个字符串都是空的,或者至少看起来是空的。
最佳答案
注意行 fwrite(input1, 17, 1, send);
。 parent 进程可能已将 '\n' 字符后的随机内容发送到 child 进程。在 child while(fgets(input2, 17, receive) != NULL)
中,fgets
在获取 '\n' 时停止并且可能获取更少超过 17-1 个字符。它的下一个阅读管道将获得随机的东西。
一个直接的修复方法是 fwrite(input1, strlen(input1), 1, send);
。引用'man fwrite',最好使用fwrite(input1, sizeof (input1[0]), strlen(input1), send);
。
无论如何使用魔法数字 17 是危险的。请记住,PIPE 是一个连续的字符流。
关于c - 只有一条消息通过管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8631252/
我正在使用 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 *
我是一名优秀的程序员,十分优秀!