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.
When executed alone, the Objective C program works as expected. 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.
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.
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.
Objective 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)
#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)
#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. 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.
rich (https://stackoverflow.com/users/1566221/rici) just provided the answer to this question in the comment above. Here is the updated Objective C code to fix it:
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.