[英]How does Objective C stdin/stdout piping work when called from C/C++?
This is a problem I've been working on all afternoon, I think I have reduced it to its central problem, which appears to be unexpected behavior when piping data to/from an Objective C command line application that is called from a C++ application. 这是我整个下午一直在解决的问题,我认为我已将其归结为核心问题,当从C ++应用程序调用数据到/来自Objective C命令行应用程序时,这似乎是意外行为。
When executed alone, the Objective C program works as expected. 当单独执行时,Objective C程序将按预期工作。 When the C++ Pipe (which is the "master" in this case, the C++ is calling the Objective C executable) is calling a C/C++ executable similar to the below Objective C code, everything also works as expected.
当C ++管道(在这种情况下为“主”,C ++正在调用Objective C可执行文件)正在调用类似于下面的Objective C代码的C / C ++可执行文件时,一切也都按预期工作。
Furthermore, if the input code is removed from the Objective C, or if the C++ program orders Objective C to be piped to a file (so the command would be "./HelloWorld > dump.txt" instead of "./HelloWorld") everything performs as expected. 此外,如果从目标C中删除了输入代码,或者C ++程序命令将目标C通过管道传输到文件中(因此命令将是“ ./HelloWorld> dump.txt”,而不是“ ./HelloWorld”)一切都按预期执行。
However, when the code as presented bellow is executed, the C++ hangs when attempting to read the Objective C's stdout, on the first try before any attempts to read stdin have been made by Objective C. 但是,执行下面显示的代码时,在尝试读取目标C的stdout时,C ++会挂起,这是在目标C进行任何读取stdin的尝试之前的第一次尝试。
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 file) 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 file) 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
Anyone have any ideas what could be wrong with this? 任何人都有任何想法可能有什么问题吗? I've got quite a bit of experience with C/C++, and it appears that the piping code from that side of things is working well.
我在C / C ++方面有丰富的经验,看来从这一方面开始的管道代码运行良好。 It really seems like this is an example of Objective C just not playing nice, I've never seen an example of piping failing like this.
看来这确实是目标C表现不佳的一个示例,我从未见过如此失败的管道示例。
rich ( https://stackoverflow.com/users/1566221/rici ) just provided the answer to this question in the comment above. rich( https://stackoverflow.com/users/1566221/rici )刚刚在上面的评论中提供了此问题的答案。 Here is the updated Objective C code to fix it:
这是修复的更新的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);
}
Apparently stdout needs to be flushed in Objective C for piping to work properly. 显然,需要在目标C中冲洗标准输出,才能使管道正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.