交互式ocaml解释器与另一个进程

时间:2017-03-28 07:53:40

标签: c pipe ocaml fork ipc

我需要将一个* .ml文件加载到Ocaml toplevel(交互式解释器,当你在shell中输入'ocaml')然后从Matlab进程发送一条指令,取回指令的结果,发送回到另一条指令,......

我做了这个C程序。父进程从命名管道获取Matlab的指令,将其发送到子进程(运行ocaml)并获取响应,以便将其发送给Matlab。

但是有一些错误:当我发送指令时,我会找回一些奇怪的字符,我发送另一条指令,然后我收到第一条指令的响应......

(我没有复制perror()测试以减少文本)

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(void) {

    // Parent -> Child
    int pipe_in[2];
    // Child -> parent
    int pipe_out[2];

    /*
        pipe[0] = output
        pipe[1] = input
    */

    pipe(pipe_in);
    pipe(pipe_out);

    pid_t pid;
    if ((pid = fork()) == 0) {
        // CHILD SIDE

        close(pipe_in[1]);
        close(pipe_out[0]);

        dup2(pipe_in[0], STDIN_FILENO);     
        dup2(pipe_out[1], STDOUT_FILENO);
        dup2(pipe_out[1], STDERR_FILENO);

        close(pipe_in[0]);
        close(pipe_out[1]);

        char *args[] = {"ocaml", NULL};
        execvp("ocaml", args);

        printf("FAIL\n");

        exit(EXIT_FAILURE);     

    } else {
        // PARENT SIDE

        printf("[*] PID : %d\n", (int) pid);

        close(pipe_in[0]);
        close(pipe_out[1]);

        char cmd[1024];
        char feedback[1024];
        ssize_t cmd_read;
        ssize_t feedback_read = sizeof(feedback);

        while (1) {

            // Get the instruction from Matlab.
            printf("[>] ");
            int fifo_in = open("/tmp/pipe_in", O_RDONLY);
            cmd_read = read(fifo_in, cmd, sizeof(cmd));
            close(fifo_in);         
            printf("%s\n", cmd);

            // Send the instruction to the ocaml interpreter.
            write(pipe_in[1], cmd, cmd_read);

            // Read the response of the ocaml interpreter.
            while (feedback_read == sizeof(feedback)) {
                feedback_read = read(pipe_out[0], feedback, sizeof(feedback));
                printf("[-] %d\n", (int) feedback_read);
            }

            printf("[<] %s\n", feedback);

            // Send to Matlab the response.
            int fifo_out = open("/tmp/pipe_out", O_WRONLY);
            write(fifo_out, feedback, feedback_read);
            close(fifo_out);

            cmd_read = 0;
            feedback_read = sizeof(feedback);

        }

        close(pipe_in[1]);
        close(pipe_out[0]);
    }
}

我使用gcc -Wall -std=c99 -o tphr tphr.c编译代码 我在一个shell和另一个shell中运行该程序:

> printf 'let x = 10;;\n' > /tmp/pipe_in

> cat /tmp/pipe_out
OCaml version 4.03.0

# %

> printf 'let y = 5;;\n' > /tmp/pipe_in

> cat /tmp/pipe_out
val x : int = 10
# %

如何修复结果?

2 个答案:

答案 0 :(得分:0)

如果,通过&#34;奇怪的字符&#34;,你的意思是

  

OCaml版本4.03.0

这只是因为这是OCaml toplevel在启动时打印出来的。因此,当您自己的程序启动时,您需要阅读此行。

如果您指的是#符号,也称为提示,则可以通过运行ocaml -nopromt将其关闭。

答案 1 :(得分:0)

由于多种原因,您不想在此处运行交互式ocaml:

  1. toplevel解析用户的配置文件,然后加载不同的模块。这可以更改可用值,更改行为,并使您的matlab过程为每个用户获得不同的结果。
  2. toplevel的输出可能会在版本之间发生变化,因此难以解析并将正确的答复返回给matlab
  3. 您是否知道可以从ocaml字节码调用toplevel来解释字符串?我建议转储C代码并编写ocaml字节代码以从管道读取,解释命令并回复结果。

相关问题