I am writing a program to communicate with the SPIN model checker's interactive module programatically. For this, I need to redirect SPIN's I/O, launch it from my program, and then repetitively read from and write to it. For testing, I am using, instead of spin, the following short program with only one input and one output:
#include <string>
#include <iostream>
using std::cin;
using std::cout;
using std::string;
void sprint(string s);
int main()
{
std::string s = "empty";
cin >> s;
cout << "\n\tthe text is: " << s;
return 0;
}
My program, taken mostly from this answer, is:
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <cstring>
#include <unistd.h>
#include <errno.h>
#include <iostream>
#define PIPE_READ 0
#define PIPE_WRITE 1
#define maxReadSize 2048
using std::cout;
using std::cin;
using std::string;
int createChild(const char* szCommand, const char* aArguments, const char* szMessage);
int main()
{
createChild("./inout" , "inout", "hello");
return 0;
}
int createChild(const char* szCommand, const char* aArguments, const char* szMessage)
{
int cStdinPipe[2];
int cStdoutPipe[2];
int nChild;
char nChar;
int nResult;
if (pipe(cStdinPipe) < 0)
{
return -1;
}
if (pipe(cStdoutPipe) < 0)
{
close(cStdinPipe[PIPE_READ]);
close(cStdinPipe[PIPE_WRITE]);
return -1;
}
nChild = fork();
if (0 == nChild)
{
// child continues here
cout.flush();
close(cStdinPipe[PIPE_WRITE]);
// redirect stdin
if (dup2(cStdinPipe[PIPE_READ], STDIN_FILENO) == -1)
{
exit(errno);
}
// redirect stdout
if (dup2(cStdoutPipe[PIPE_WRITE], STDOUT_FILENO) == -1)
{
exit(errno);
}
// all these are for use by parent only
close(cStdinPipe[PIPE_READ]);
close(cStdoutPipe[PIPE_READ]);
close(cStdoutPipe[PIPE_WRITE]);
// run child process image
nResult = execlp(szCommand, aArguments, NULL);
// if we get here at all, an error occurred, but we are in the child
// process, so just exit
exit(nResult);
}
else if (nChild > 0)
{
// parent continues here
string messageFromChild = "";
string messageFromParent = "";
char readBuffer[maxReadSize];
int bytesWritten = 0;
int bytesRead = 0;
// close unused file descriptors, these are for child only
close(cStdinPipe[PIPE_READ]);
close(cStdoutPipe[PIPE_WRITE]);
// write to child
if (NULL != szMessage)
{
bytesWritten = write(cStdinPipe[PIPE_WRITE], szMessage, strlen(szMessage));
}
// read from child
bytesRead = read(cStdoutPipe[PIPE_READ], readBuffer, maxReadSize);
cout << "\nChild says: " << readBuffer << "\n";
// done with these in this example program, you would normally keep these
// open of course as long as you want to talk to the child
close(cStdinPipe[PIPE_WRITE]);
close(cStdoutPipe[PIPE_READ]);
std::cout << "\n\nParent ending";
}
else
{
// failed to create child
close(cStdinPipe[PIPE_READ]);
close(cStdinPipe[PIPE_WRITE]);
close(cStdoutPipe[PIPE_READ]);
close(cStdoutPipe[PIPE_WRITE]);
}
return nChild;
}
Running this program results in a deadlock where the child is stuck on cin
and the parent on read()
. Removing either of these calls causes both programs to run to termination and exit normally. write()
and cout
both work properly.