Doug Zwick wrote:
>The IPC buffers used by the streams are quite small (IIRC they are on the
>order of 256 bytes).
A few months ago, I had a need to know exactly how big the pipe buffers
were, so I write a Java program to help me find out. Across several machines
running 10.4, the buffer is 16KB in length, which is larger than I expected.
However, this could vary for any number of reasons, and is not guaranteed
by any parameter or configurable value in the OS I could find. My guess is
it's wired into a kernel header file somewhere, but that's just a guess.
Below is the test program I wrote. It's an informative exercise to
intentionally try to deadlock a parent process. I recommend it highly.
This one deadlocks at 16385 or above, but ends normally at 16384 or below,
at least for me. YMMV.
-- GG
// Released into the public domain. No warranties. Use as you will.
public class Pipelen
{
/**
** Static entry point of application.
** First arg is a number: a byte-count to test.
** If this app deadlocks, the buffer is less than that number.
** If this app exits normally, the buffer is equal to or more than that number.
** You can use binary search to rapidly narrow down the exact size.
**<p>
** To terminate a deadlocked app, press command-dot (keyboard interrupt).
**<p>
** Property "write" is a boolean, "count" is a number:
** true to perform child task: write count N's to stdout.
** In the absence of "write", spawns child by exec(), then awaits child's exit.
*/
public static void
main( String[] args )
{
try
{
Pipelen task = new Pipelen( args[ 0 ] );
task.task();
}
catch ( Exception caught )
{ caught.printStackTrace(); }
}
private String count;
/** Create */
public
Pipelen( String str )
{ count = str; }
/** Choose task from "write" property. */
public void
task()
throws Exception
{
if ( Boolean.getBoolean( "write" ) )
write();
else
spawn();
}
/** Spawn child Process and waitFor() it. */
private void
spawn()
throws Exception
{
// Pass along classpath.
String classpath = System.getProperty( "java.class.path" );
String[] cmd = new String[]
{
"java", "-cp", classpath,
"-Dwrite=true", "-Dcount=" + count,
this.getClass().getName(), count
};
Process child = Runtime.getRuntime().exec( cmd );
// Close child's stdin immediately, which gives it an EOF on its input.
// This will terminate any child that's waiting for input.
child.getOutputStream().close();
System.out.println( "Waiting for child..." );
child.waitFor();
}
/** Write and flush bytes to System.out, "count" sys prop. */
private void
write()
throws Exception
{
PrintStream out = System.out; // System.out or System.err
int count = Integer.getInteger( "count", 1 ).intValue();
for ( int i = 0; i < count; ++i )
{ out.print( Character.forDigit( count & 7, 10 ) ); out.flush(); }
}
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden
This email sent to email@hidden