通过C中的管道将术语写入BC [英] Writing a term to bc via pipes in C

查看:105
本文介绍了通过C中的管道将术语写入BC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在进行C练习时遇到了问题. 任务是创建两个过程.两者通过两个以孩子的stdin和stdout终止的管道连接.然后,将子进程替换为bc. 然后,我应该从父进程到子进程(bc)写一个词(例如1 + 2).

I ran into a problem with this C exercise. The task is to create two processes. The two are connected with two pipes that terminate in the stdin and stdout of the child. The child process is then replaced with bc. I am then supposed to write a term (e.g. 1 + 2) from the parent to the child process (bc).

管道正在执行应做的工作,但是,bc似乎不喜欢输入.当我向管道中写入内容时,bc会用以下多行内容进行响应:

The pipes are doing what they're supposed to do, however, bc doesn't seem to like the input. When I write into the pipe, bc responds with multiple lines of the following:

(standard_in) 1: illegal character: ^@

到目前为止,这是我的解决方案:

This is my solution so far:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char *argv[]) {

    /*Create two pipes:
        One from parent to child (pipe_pc)
        and one from child to parent (pipe_cp).
    */
    int pipe_pc[2], pipe_cp[2];

    int cid;

    if (pipe(pipe_pc) == -1 || pipe(pipe_cp) == -1) {
        printf("Could not pipe\n");
        exit(EXIT_FAILURE);
    }

    // Create child process
    cid = fork();

    if (cid == -1) {
        printf("Could not fork.\n");
        exit(EXIT_FAILURE);
    }

    // Child process
    if (cid == 0) {

        // Redirect pipes
        close(STDOUT_FILENO);
        close(STDIN_FILENO);
        close(pipe_pc[1]); // Close writing end
        close(pipe_cp[0]); // Close reading end

        dup2(pipe_pc[0], STDIN_FILENO); // Take stdin from parent
        dup2(pipe_cp[1], STDOUT_FILENO); // Give stdout to parent

        int err;

        // Replace child with bc
        err = execl("/usr/bin/bc", "bc --quiet", (char*) NULL);

        printf("%s %d\n", "Could not start bc:", err);
        exit(err);
    }

    // Parent Process
    else {

        char input[128] = "";
        char buffer[128] = "";

        printf("%s\n", "Parent process running");

        // Copy argv to a single string
        for(int i=1; i < argc; i++) {
            strcat(input, argv[i]);
        }

        // Write the input to the child's stdin
        write(pipe_pc[1], input, sizeof(input);

        // Read the child's stdout
        read(pipe_cp[0], buffer, sizeof(buffer));

        printf("Result: %s\n", buffer);

        return 0;
    }

}

非常感谢您的提示和帮助,在此先感谢您!

Hints and help are greatly appreciated, thanks in advance!

推荐答案

问题是您在管道中的编写不正确,因此bc收到错误的输入.

The problem was that you did not write correctly in the pipe, so bc received bad input.

我重写了else分支.

如果不确定管道中要发送的内容,请暂时将管道描述符替换为0(i/o fd),然后目视检查所做的事情.

If you are not sure what you send in a pipe, replace temporarily the pipe descriptor with 0 (i/o fd) and check visually what you did.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char *argv[]) {

    /* Create two pipes: One from parent to child (pipe_pc) and one
       from child to parent (pipe_cp).
    */
    int pipe_pc[2], pipe_cp[2];

    int cid;

    if (pipe(pipe_pc) == -1 || pipe(pipe_cp) == -1) {
        printf("Could not pipe\n");
        exit(EXIT_FAILURE);
    }

    // Create child process
    cid = fork();

    if (cid == -1) {
        printf("Could not fork.\n");
        exit(EXIT_FAILURE);
    }

    // Child process
    if (cid == 0) {

        // Redirect pipes
        close(STDOUT_FILENO);
        close(STDIN_FILENO);
        close(pipe_pc[1]); // Close writing end
        close(pipe_cp[0]); // Close reading end

        dup2(pipe_pc[0], STDIN_FILENO); // Take stdin from parent
        dup2(pipe_cp[1], STDOUT_FILENO); // Give stdout to parent

        int err;

        // Replace child with bc
        err = execl("/usr/bin/bc", "bc --quiet", (char*) NULL);

        printf("%s %d\n", "Could not start bc:", err);
        exit(err);
    }

    // Parent Process
    else {
        char buffer[128] = "";

        // printf("%s\n", "Parent process running");

        for(int i=1; i < argc; i++)
            write(pipe_pc[1], argv[i], strlen(argv[i]));

        write(pipe_pc[1], "\n", 1);

        read(pipe_cp[0], buffer, sizeof(buffer));

        printf("Result: %s\n", buffer);

        return 0;
    }

}

这篇关于通过C中的管道将术语写入BC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆