Name: ap32198 Date: 10/29/98
The following java program performed interactions with
the exec'd process at a rate of about 1000/sec under 1.0.
This dropped to about 1/sec with 1.1.
I assume the problem is some sort of timeout on the
in.read() call.
JAVA code ------------------------------------------------
import java.io.*;
/*********************************************************/
/* */
/* EXECTEST */
/* */
/* This simple JAVA program starts an executable */
/* and sends it some strings. The executable */
/* converts the strings to upper case and */
/* encases them in brackets. Upon return reciept, */
/* this routine prints them out. */
/* */
/*********************************************************/
public class ExecTest
{
public static void main(String[] args)
{
int i;
Service svc;
String command, response;
svc = new Service("exectest");
for(i=0; i<10; ++i)
{
command = "testing " + i;
response = svc.sendCommand(command);
System.out.println(response);
}
svc.endProcess();
}
}
/*********************************************************/
/* */
/* SERVICE: This class has methods to start an */
/* executable, send it a command and get a response, */
/* and kill the child process. */
/* */
/*********************************************************/
class Service
{
private Runtime runtime = Runtime.getRuntime();
private Process process;
private OutputStream out;
private InputStream in;
// Creation method. Starts up the executable
public Service(String executable)
{
try
{
process = runtime.exec(executable);
out = process.getOutputStream();
in = process.getInputStream ();
}
catch(IOException e)
{
System.err.println("exec error: " + e);
}
}
// Method to send a command and get a response
public String sendCommand(String command)
{
int i, c;
StringBuffer resp;
resp = new StringBuffer(4096);
try
{
for(i=0; i<command.length(); ++i)
out.write((int) command.charAt(i));
out.write('\n');
out.flush();
while((c = in.read()) > -1)
{
if((char)c == '\n')
break;
resp.append((char)c);
}
}
catch(IOException e)
{
System.err.println("exec error: " + e);
}
return(resp.toString());
}
// Method to kill the child
public void endProcess()
{
process.destroy();
}
}
C process code ------------------------------------------
#include <stdio.h>
#include <ctype.h>
main()
{
int i;
char str[1024];
while(1)
{
if(gets(str) == (char *)NULL)
break;
for(i=0; i<strlen(str); ++i)
if(isalpha(str[i]))
str[i] = toupper(str[i]);
fprintf(stdout, "[%s]\n", str);
fflush(stdout);
}
}
---------------------------------------------------------
(Review ID: 41485)
======================================================================