【问题标题】:How does Objective C stdin/stdout piping work when called from C/C++?从 C/C++ 调用时,Objective C stdin/stdout 管道如何工作?
【发布时间】:2015-03-26 04:59:52
【问题描述】:

这是我整个下午都在研究的一个问题,我想我已经将其简化为它的核心问题,当从C++ 应用程序。

单独执行时,Objective C 程序按预期工作。当 C++ 管道(在这种情况下是“主”,C++ 正在调用 Objective C 可执行文件)正在调用类似于以下 Objective C 代码的 C/C++ 可执行文件时,一切都按预期工作。

此外,如果输入代码从 Objective C 中删除,或者如果 C++ 程序命令将 Objective C 传送到文件(因此命令将是“./HelloWorld > dump.txt”而不是“./ HelloWorld") 一切都按预期执行。

但是,当执行如下所示的代码时,C++ 在尝试读取 Objective C 的标准输出时挂起,这是在 Objective C 尝试读取标准输入之前的第一次尝试。

目标 C

#import <Foundation/Foundation.h>

void c_print(NSString* prnt)
{
    printf("%s", [prnt cStringUsingEncoding:NSUTF8StringEncoding]);
}
void c_print_ln(NSString* prnt)
{
    printf("%s\n", [prnt cStringUsingEncoding:NSUTF8StringEncoding]);
}
NSString* read_till(char c)
{
    NSMutableString* ret = [[NSMutableString alloc] initWithString:@""];

    char r = getchar();
    while(r!=c && r!= '\0')
    {
        [ret appendFormat:@"%c",r];
        r = getchar();
    }
    return ret;
}

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        c_print_ln(@"Hello, World!");
        NSString* exmp = read_till('\n');
        c_print_ln([[NSString alloc] initWithFormat:@"String I read: \"%@\"",exmp]);
    }
    return 0;
}

C++(.h 文件)

#ifndef PIPE_H
#define PIPE_H

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

#define PIPE_READ 0
#define PIPE_WRITE 1

class outsideExecutable
{
private:
    char buf[1024];
    bool is_good;

    int infp, outfp;

public:
    outsideExecutable(char* command);
    ~outsideExecutable();

    bool isGood();
    std::string readline();
    void writeline(std::string source);

};
#endif

C++(.cpp 文件)

#include "Pipe.h"

using namespace std;

int main()
{
    cout<<"Testing Pipe"<<endl;
    outsideExecutable* exe = new outsideExecutable((char*)"./HelloWorld");
    exe->readline();
    exe->writeline("reading example");
    exe->readline();
    delete exe;
}
static pid_t popen2(const char *command, int *infp, int *outfp)
{
    int p_stdin[2], p_stdout[2];
    pid_t pid;

    if (pipe(p_stdin) != 0 || pipe(p_stdout) != 0)
        return -1;

    pid = fork();

    if (pid < 0)
        return pid;
    else if (pid == 0)
    {
        close(p_stdin[PIPE_WRITE]);
        dup2(p_stdin[PIPE_READ], PIPE_READ);
        close(p_stdout[PIPE_READ]);
        dup2(p_stdout[PIPE_WRITE], PIPE_WRITE);

        execl("/bin/sh", "sh", "-c", command, NULL);
        perror("execl");
        exit(1);
    }

    if (infp == NULL)
        close(p_stdin[PIPE_WRITE]);
    else
        *infp = p_stdin[PIPE_WRITE];

    if (outfp == NULL)
        close(p_stdout[PIPE_READ]);
    else
        *outfp = p_stdout[PIPE_READ];

    return pid;
}

outsideExecutable::outsideExecutable(char* command)
{
    is_good = false;

    if (popen2(command, &infp, &outfp) <= 0)
        return;

    is_good = true;
}
outsideExecutable::~outsideExecutable()
{

}

bool outsideExecutable::isGood()
{
    return is_good;
}
std::string outsideExecutable::readline()
{
    if(!is_good)
        return "";
    string ret = "";
    char hld;
    read(outfp, &hld, 1);
    while(hld!='\n' && hld!='\0')
    {
        ret = ret + hld;
        read(outfp, &hld, 1);
    }
    cout<<"We read:"<<ret<<endl;
    return ret;
}
void outsideExecutable::writeline(std::string source)
{
    if(!is_good)
        return;
    //Do nothing
    cout<<"Sending command: "<<source<<endl;
    source = source+"\n";
    write(infp, source.c_str(), source.length());
}

#endif

有人知道这有什么问题吗?我在 C/C++ 方面有相当多的经验,而且看起来这方面的管道代码运行良好。看起来这是一个 Objective C 的例子,只是玩得不好,我从来没有见过这样的管道失败的例子。

【问题讨论】:

  • 打印后尝试刷新标准输出。可能是 obj-c 运行时将 stdout 设置为完全缓冲。
  • 好电话!就是这样!很惊讶这就是 Objective C 的工作方式。根据我的经验,C、C++ 和 C# 不需要刷新标准输出来使管道按预期工作,并且鉴于 Objective C 基于 C,它有点奇怪的行为。谢谢!

标签: c++ objective-c c piping


【解决方案1】:

rich (https://stackoverflow.com/users/1566221/rici) 刚刚在上面的评论中提供了这个问题的答案。这是修复它的更新后的 Objective C 代码:

void c_print(NSString* prnt)
{
    printf("%s", [prnt cStringUsingEncoding:NSUTF8StringEncoding]);
    fflush(stdout);
}
void c_print_ln(NSString* prnt)
{
    printf("%s\n", [prnt cStringUsingEncoding:NSUTF8StringEncoding]);
    fflush(stdout);
}

显然 stdout 需要在 Objective C 中刷新以使管道正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    相关资源
    最近更新 更多