【发布时间】:2020-06-27 21:35:56
【问题描述】:
这是我试图用来将数据传递给其他程序的代码。:
static int callWithFile(char* buff) {
int myPipes[2];
if( pipe( myPipes ) < 0 ){
perror("Can't pipe through \n");
exit(13);
}
int pid = fork();
switch(pid){
case 0:
{
if(verbose_flag) printf("pid is %d; pipe fds are.... %d & %d\n", getpid(), myPipes[PIPE_READ], myPipes[PIPE_WRITE]);
//close (myPipes[PIPE_READ]);
write (myPipes[PIPE_WRITE], buff, strlen(buff) + 1);
close (myPipes[PIPE_WRITE]);
char* pipeArg;
if(verbose_flag){
asprintf (&pipeArg, "/proc/%d/fd/%d", getpid(), myPipes[PIPE_READ]);
printf("\n%s\n", pipeArg);
}
asprintf (&pipeArg, "/dev/fd/%d", myPipes[PIPE_READ]);
char* progArgv[] = {
"prog",
"--new_settings",
pipeArg,
//"/dev/fd/0",
NULL
};
// This works just fine
// FILE* fp = fopen(pipeArg, "r");
// if (fp == NULL) {
// perror("Can't open fd pipe file \n");
// exit(14);
// }
// fread(buff, sizeof(char), strlen(buff) + 1, fp);
// printf("buff: %s", buff);
execvp(prog_path, progArgv);
perror("execvp screwed up");
exit(15);
}
case -1:
perror("fork screwed up ");
exit(16);
}
close (myPipes[PIPE_READ]);
close (myPipes[PIPE_WRITE]);
wait(NULL);
puts("done");
}
在所有方面,代码似乎都是正确的,并提供了文件描述符供其他程序读取。 但是,由于某种原因,其他程序告诉它无法打开和读取文件。
这是读取数据的程序:https://github.com/tuxedocomputers/tuxedo-control-center/blob/master/src/common/classes/ConfigHandler.ts#L87
它抱怨:Error on read option --new_settings with path: /dev/fd/4
我已经确认它是正确的 JSON,所以这不应该是问题。
至于调试,由于某种原因,我无法让它在我的机器上运行。
Cannot launch program because corresponding JavaScript cannot be found..
我的目标是在 bash 中拥有与此等价的功能:
program <(echo $buff)
其中$buff 是buff 函数参数的内容。
【问题讨论】:
-
嗯,乍一看,问题似乎在第 2 段到最后一段中描述。
-
@ryyker 我对那段的理解是,它是要上传给某种在线法官/评分者的,而这个问题只存在于 OP 的本地机器上。
-
@brunoais 您需要发布一个最小的、可重现的示例。您的示例不可重现。
-
我不知道这是否是问题所在,但您正在以一种奇怪的方式传输数据。一般是家长写,孩子读。在这里,孩子既在写作又在阅读,可以想象这在这种特定情况下是可行的,但通常会出现问题。而且父级根本不使用管道,那么为什么要在分叉之前创建它呢?
-
为了调试,可以把
prog换成cat吗?