--- buffer-1.19/buffer.c 2015-12-06 22:36:43.364143317 +0000(B +++ /tmp/buffer-new.c 2015-12-06 22:36:26.001894230 +0000(B @@ -255,6 +255,7 @@(B int reader_pid = 0; int free_shm = 1; int percent = 0; +int min_percent = 0;(B int debug = 0; int Zflag = 0; int writer_status = 0; @@ -385,6 +386,16 @@(B if( debug ) fprintf( stderr, "percent set to %d\n", percent ); break; + case 'P': /* min percent to maintain in the buffer */(B + min_percent = atoi( optarg );(B +(B + if( (min_percent < 0) || (100 < min_percent) ){(B + fprintf( stderr, "min_percent %d out of range\n", min_percent );(B + byee( -1 );(B + }(B + if( debug )(B + fprintf( stderr, "min_percent set to %d\n", min_percent );(B + break;(B case 'z': zflag++; /* FALL THRU */ @@ -397,7 +408,7 @@(B } break; default: - fprintf( stderr, "Usage: %s [-B] [-t] [-S size] [-m memsize] [-b blocks] [-p percent] [-s blocksize] [-u pause] [-i infile] [-o outfile] [-z size] [-Z] [-d]\n",(B + fprintf( stderr, "Usage: %s [-B] [-t] [-S size] [-m memsize] [-b blocks] [-p percent] [-P min_percent] [-s blocksize] [-u pause] [-i infile] [-o outfile] [-z size] [-Z] [-d]\n",(B progname ); fprintf( stderr, "-B = blocked device - pad out last block\n" ); fprintf( stderr, "-t = show total amount written at end\n" ); @@ -405,6 +416,7 @@(B fprintf( stderr, "-m size = size of shared mem chunk to grab\n" ); fprintf( stderr, "-b num = number of blocks in queue\n" ); fprintf( stderr, "-p percent = don't start writing until percent blocks filled\n" ); + fprintf( stderr, "-P min_percent = min number of blocks to keep in the buffer\n" );(B fprintf( stderr, "-s size = size of a block\n" ); fprintf( stderr, "-u usecs = microseconds to sleep after each write\n" ); fprintf( stderr, "-i infile = file to read from\n" ); @@ -421,6 +433,11 @@(B byee( -1 ); } + if (min_percent > percent) {(B + fprintf( stderr, "min_percent > percent, aborting!\n" );(B + byee( -1 );(B + }(B +(B if (zflag) showevery = blocksize; /* If -b was not given try and work out the max buffer size */ @@ -748,12 +765,14 @@(B { int filled = 0; int maxfilled = (blocks * percent) / 100; + int minfilled = (blocks * min_percent) / 100;(B int first_block = 0; if( debug ) - fprintf( stderr, "\tW: Entering writer\n blocks = %d\n maxfilled = %d\n",(B + fprintf( stderr, "\tW: Entering writer\n blocks = %d\n"(B + " maxfilled = %d\n minfilled = %d\n",(B blocks, - maxfilled );(B + maxfilled, minfilled );(B while( 1 ){ if( !filled ) @@ -765,10 +784,10 @@(B filled++; if( debug > 1 ) fprintf( stderr, "W: filled = %d\n", filled ); - if( filled >= maxfilled ){(B + if( filled >= maxfilled && filled > minfilled ){(B if( debug > 1 ) fprintf( stderr, "W: writing\n" ); - write_blocks_to_stdout( filled, first_block );(B + write_blocks_to_stdout( filled - minfilled, first_block );(B filled = 0; } }