home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume37 / buffer / part01 next >
Text File  |  1993-05-15  |  54KB  |  1,624 lines

  1. Newsgroups: comp.sources.misc
  2. From: lmjm@doc.ic.ac.uk (Lee M J McLoughlin)
  3. Subject: v37i053:  buffer - Very Fast Tape Writer, Part01/01
  4. Message-ID: <1993May11.194434.24305@sparky.imd.sterling.com>
  5. X-Md4-Signature: 854f99ac7dcc323e8bfd54cd09140d4d
  6. Date: Tue, 11 May 1993 19:44:34 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: lmjm@doc.ic.ac.uk (Lee M J McLoughlin)
  10. Posting-number: Volume 37, Issue 53
  11. Archive-name: buffer/part01
  12. Environment: SUNOS, SYSV, SYSVR4, HPUX, Shared Memory, Semaphores
  13.  
  14. This is a program designed to speed up writing tapes on remote tape
  15. drives.  Requirements are shared memory and locks which normally
  16. means that these are supported in your kernel.
  17.  
  18. Buffer has been tested under SunOS 4.0.*, SunOS 4.1.*, Solarix, HP-UX 7.0,
  19. and Gould UTX 2.1A (sv universe).
  20.  
  21. The program splits itself into two processes.  The first process reads
  22. (and reblocks) from stdin into a shared memory buffer.  The second
  23. writes from the shared memory buffer to stdout.  Doing it this way
  24. means that the writing side effectly sits in a tight write loop and
  25. doesn't have to wait for input.  Similarly for the input side.  It is
  26. this waiting that slows down other reblocking processes, like dd.
  27.  
  28. I run an archive and need to write large chunks out to tape regularly
  29. with an ethernet in the way.  Using 'buffer' in a command like:
  30.  
  31.     tar cvf - stuff | rsh somebox "buffer > /dev/rst8"
  32.  
  33. is a factor of 5 faster than the best alternative, gnu tar with its
  34. remote tape option:
  35.  
  36.     tar cvf somebox:/dev/rst8 stuff
  37.  
  38. We have been using buffer here at Imperial for a couple of years now
  39. for writing tar tapes and the main system dumps.
  40.  
  41. Thanks to Kevin Twidle <kpt@doc.ic.ac.uk> for the -p and -B code.
  42.  
  43. INSTALLATION:
  44.     Check that your kernel supports shared memory and semaphores.
  45.     A quick way to check is to build buffer and run it.
  46.     If it says "couldn't create shared memory segment" you probably
  47.     need to reconfigure and rebuild your kernel.
  48.  
  49.     To install edit the Makefile and tailor the variables to
  50.     your local systems.  Then type make.
  51.  
  52. DISCLAIMER:
  53.     This package is under the GNU GENERAL PUBLIC LICENSE!
  54.     In addtion under NO circumstances can I, or Imperial College,
  55.     be held liable for any event caused by the running or storing 
  56.     of this program or its documentation.
  57.     
  58. Lee McLoughlin.                          Phone: +44 71 589 5111 X 5085
  59. Dept of Computing, Imperial College,     Fax: +44 71 581 8024
  60. 180 Queens Gate, London, SW7 2BZ, UK.    Email: L.McLoughlin@doc.ic.ac.uk
  61. -------------------------------------------------------------------------
  62. #! /bin/sh
  63. # This is a shell archive.  Remove anything before this line, then unpack
  64. # it by saving it into a file and typing "sh file".  To overwrite existing
  65. # files, type "sh file -c".  You can also feed this as standard input via
  66. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  67. # will see the following message at the end:
  68. #        "End of shell archive."
  69. # Contents:  README buffer.man Makefile buffer.c sem.c COPYING
  70. # Wrapped by lmjm@kea.doc.ic.ac.uk on Mon Apr 26 14:22:55 1993
  71. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  72. if test -f 'README' -a "${1}" != "-c" ; then 
  73.   echo shar: Will not clobber existing file \"'README'\"
  74. else
  75. echo shar: Extracting \"'README'\" \(1979 characters\)
  76. sed "s/^X//" >'README' <<'END_OF_FILE'
  77. XThis is a program designed to speed up writing tapes on remote tape
  78. Xdrives.  Requirements are shared memory and locks which normally
  79. Xmeans that these are supported in your kernel.
  80. X
  81. XBuffer has been tested under SunOS 4.0.*, SunOS 4.1.*, Solarix, HP-UX 7.0,
  82. Xand Gould UTX 2.1A (sv universe).
  83. X
  84. XThe program splits itself into two processes.  The first process reads
  85. X(and reblocks) from stdin into a shared memory buffer.  The second
  86. Xwrites from the shared memory buffer to stdout.  Doing it this way
  87. Xmeans that the writing side effectly sits in a tight write loop and
  88. Xdoesn't have to wait for input.  Similarly for the input side.  It is
  89. Xthis waiting that slows down other reblocking processes, like dd.
  90. X
  91. XI run an archive and need to write large chunks out to tape regularly
  92. Xwith an ethernet in the way.  Using 'buffer' in a command like:
  93. X
  94. X    tar cvf - stuff | rsh somebox "buffer > /dev/rst8"
  95. X
  96. Xis a factor of 5 faster than the best alternative, gnu tar with its
  97. Xremote tape option:
  98. X
  99. X    tar cvf somebox:/dev/rst8 stuff
  100. X
  101. XWe have been using buffer here at Imperial for a couple of years now
  102. Xfor writing tar tapes and the main system dumps.
  103. X
  104. XThanks to Kevin Twidle <kpt@doc.ic.ac.uk> for the -p and -B code.
  105. X
  106. XINSTALLATION:
  107. X    Check that your kernel supports shared memory and semaphores.
  108. X    A quick way to check is to build buffer and run it.
  109. X    If it says "couldn't create shared memory segment" you probably
  110. X    need to reconfigure and rebuild your kernel.
  111. X
  112. X    To install edit the Makefile and tailor the variables to
  113. X    your local systems.  Then type make.
  114. X
  115. XDISCLAIMER:
  116. X    This package is under the GNU GENERAL PUBLIC LICENSE!
  117. X    In addtion under NO circumstances can I, or Imperial College,
  118. X    be held liable for any event caused by the running or storing 
  119. X    of this program or its documentation.
  120. X    
  121. XLee McLoughlin.                          Phone: +44 71 589 5111 X 5085
  122. XDept of Computing, Imperial College,     Fax: +44 71 581 8024
  123. X180 Queens Gate, London, SW7 2BZ, UK.    Email: L.McLoughlin@doc.ic.ac.uk
  124. END_OF_FILE
  125. if test 1979 -ne `wc -c <'README'`; then
  126.     echo shar: \"'README'\" unpacked with wrong size!
  127. fi
  128. # end of 'README'
  129. fi
  130. if test -f 'buffer.man' -a "${1}" != "-c" ; then 
  131.   echo shar: Will not clobber existing file \"'buffer.man'\"
  132. else
  133. echo shar: Extracting \"'buffer.man'\" \(5071 characters\)
  134. sed "s/^X//" >'buffer.man' <<'END_OF_FILE'
  135. X.\"    Buffer.  Very fast reblocking filter speedy writing of tapes.
  136. X.\"    Copyright (C) 1990,1991  Lee McLoughlin
  137. X.\"
  138. X.\"    This program is free software; you can redistribute it and/or modify
  139. X.\"    it under the terms of the GNU General Public License as published by
  140. X.\"    the Free Software Foundation; either version 1, or (at your option)
  141. X.\"    any later version.
  142. X.\"
  143. X.\"    This program is distributed in the hope that it will be useful,
  144. X.\"    but WITHOUT ANY WARRANTY; without even the implied warranty of
  145. X.\"    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  146. X.\"    GNU General Public License for more details.
  147. X.\"
  148. X.\"    You should have received a copy of the GNU General Public License
  149. X.\"    along with this program; if not, write to the Free Software
  150. X.\"    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  151. X.\"
  152. X.\"    Lee McLoughlin.
  153. X.\"    Dept of Computing, Imperial College,
  154. X.\"    180 Queens Gate, London, SW7 2BZ, UK.
  155. X.\"
  156. X.\"    Email: L.McLoughlin@doc.ic.ac.uk
  157. X.TH BUFFER 1 "14 May 1990"
  158. X.SH NAME
  159. Xbuffer \- very fast reblocking program
  160. X.SH SYNTAX
  161. X.B buffer
  162. X[\fB\-S size\fP] [\fB\-b blocks\fP] [\fB\-s size\fP] [\fB\-m size\fP]
  163. X[\fB\-p percentage\fP] [\fB\-u microseconds\fP] [\fB-B\fR] [\fB-t\fR]
  164. X[\fB-Z\fR] [\fB-i filename\fR] [\fB-o filename\fR] 
  165. X.SH OPTIONS
  166. X.TP 5
  167. X.B \-i filename
  168. XUse the given file as the input file.  The default is stdin.
  169. X.TP
  170. X.B \-o filename
  171. XUse the given file as the output file.  The default is stdout.
  172. X.TP
  173. X.B \-S size
  174. XAfter every chunk this size has been writen print out how much been writen so far.
  175. XBy default this is not set.
  176. X.B \-s size
  177. XSize in bytes of each block.  The default blocksize is 10k to match
  178. Xthe normal output of the
  179. X.I tar(1)
  180. Xprogram.
  181. X.TP
  182. X.B \-z size
  183. XCombines the
  184. X.B \-S
  185. Xand
  186. X.B \-s
  187. Xflags.
  188. X.TP
  189. X.B \-b blocks
  190. XNumber of blocks to allocate to shared memory circular buffer.
  191. XDefaults to the number required to fill up the shared memory requested.
  192. X.TP
  193. X.B \-m size
  194. XMaximum size of the shared memory chunk to allocate for the circular
  195. Xqueue. Defaults to one megabyte.
  196. X.TP
  197. X.B \-p percentage
  198. XOnly start a write when the given percentage of the internal queue is
  199. Xfull.  A percentage around 75 often proves best. Defaults to zero.
  200. X.TP
  201. X.B \-u microseconds
  202. XAfter every write pause for this many microseconds.  Defaults to zero.
  203. X(Suprisingly a small sleep, 100 usecs, after each write can greatly enhance
  204. Xthroughput on some drives.)
  205. X.TP
  206. X.B \-B
  207. XForce each block writen to be padded out to 1K.  This is needed by some tape
  208. Xand cartridge drives.  Defaults to unpadded.  This only effects the
  209. Xlast block writen.
  210. X.TP
  211. X.B \-t
  212. XOn exiting print to stderr a brief message showing the total number of
  213. Xbytes written.
  214. X.TP
  215. X.B \-Z
  216. XIf reading/writing directly to a character device (like a tape drive)
  217. Xthen after each gigabyte perform an lseek to the start of the file.
  218. XUse this flag with extreme care.  If can only be used on devices where
  219. Xan lseek does not rewind the tape but does reset the kernels position
  220. Xflags.  It is used to allow more than 2 gigabytes to be writen.
  221. X.PP
  222. XSizes are a number with an optional trailing character.   A 'b' 
  223. Xmultiplies the size by 512, a 'k' by 1024 and an 'm' by a meg.
  224. X.SH DESCRIPTION
  225. X.I Buffer
  226. Xreads from standard input reblocking to the given blocksize and writes
  227. Xeach block to standard output.
  228. X.PP
  229. XInternally
  230. X.I buffer
  231. Xis a pair of processes communicating via a large circular queue held
  232. Xin shared memory.  The reader process only has to block when the queue
  233. Xis full and the writer process when the queue is empty.
  234. X.I Buffer
  235. Xis designed to try and keep the writer side continuously busy so that
  236. Xit can stream when writing to tape drives.  When used to write tapes
  237. Xwith an intervening network
  238. X.I buffer
  239. Xcan result in a considerable increase in throughput.
  240. X.PP
  241. XThe default settings for
  242. X.I buffer
  243. Xare normally good enough.  If you are a heavy tape user then it is
  244. Xworth your while trying out various different combinations of options.
  245. XIn particular running a
  246. X.I buffer
  247. Xat both ends of the pipe can provide a substantial increase (see last
  248. Xexample below).
  249. X.SH EXAMPLES
  250. X.br
  251. X$ \fBbuffer < /etc/termcap > /dev/rst8\fP
  252. X.br
  253. X.sp
  254. X$ \fBtar cf - . | rsh somehost 'buffer > /dev/rst8'\fP
  255. X.br
  256. X.sp
  257. X$ \fBdump fu - | rsh somehost 'buffer -s 16k > /dev/nrst8'\fP
  258. X.br
  259. X$ \fBtar cf - . | buffer | 
  260. X.br
  261. X\ \ \ rsh somehost 'buffer -S 500K -p 75 > /dev/rst0'\fP
  262. X.SH BUGS
  263. XInternally, for printing purposes, buffer counts in terms of the
  264. Xnumber of kilobytes output.  If the blocksize you use is not a whole
  265. Xnumber of kilobytes then the numbers printed will be inaccurate.
  266. X
  267. X.SH THANKS
  268. XThanks to Kevin Twidle <kpt@doc.ic.ac.uk> for a lot of early
  269. Xsuggestions and patches to make it work with non-tar/dump tapes to
  270. Xexabyte drives.
  271. X
  272. XThanks to Andi Karrer <karrer@bernina.ethz.ch>, Rumi Zahir
  273. X<rumi@iis.ethz.ch> and Christoph Wicki <wicki@iis.ethz.ch> for patches
  274. Xto make buffer work when trying to write single tape files of greater
  275. Xthan 2 gigabytes.
  276. X
  277. X.SH COPYRIGHT
  278. X.if n Copyright (C) 1990, 1991 by Lee McLoughlin.
  279. X.if t Copyright \(co 1990, 1991 by Lee McLoughlin.
  280. X.SH SEE ALSO
  281. Xdd(1), tar(1), rsh(1)
  282. END_OF_FILE
  283. if test 5071 -ne `wc -c <'buffer.man'`; then
  284.     echo shar: \"'buffer.man'\" unpacked with wrong size!
  285. fi
  286. # end of 'buffer.man'
  287. fi
  288. if test -f 'Makefile' -a "${1}" != "-c" ; then 
  289.   echo shar: Will not clobber existing file \"'Makefile'\"
  290. else
  291. echo shar: Extracting \"'Makefile'\" \(875 characters\)
  292. sed "s/^X//" >'Makefile' <<'END_OF_FILE'
  293. X# Make the buffer program
  294. X
  295. X# Add -DSYS5 for A System 5 (USG) version of unix
  296. X#   You should also add -DSYS5 for Ultrix, AIX, and Solarix.
  297. X# Add -DDEF_SHMEM=n if you can only have n bytes of shared memory
  298. X#   (eg: -DDEF_SHMEM=524288 if you can only have half a meg.)
  299. XCFLAGS=
  300. X
  301. X# Where to install buffer and its manual pages
  302. XINSTBIN=/usr/local/bin
  303. XINSTMAN=/usr/man/manl
  304. X# The manual page section (normally l or 1)
  305. XS=l
  306. X
  307. XRM=/bin/rm
  308. XALL=README buffer.man Makefile buffer.c sem.c COPYING
  309. X
  310. Xall: buffer
  311. X
  312. Xbuffer: buffer.o sem.o
  313. X    $(CC) -o buffer $(CFLAGS) buffer.o sem.o
  314. X
  315. Xclean:
  316. X    $(RM) -f *.o core buffer .merrs
  317. X
  318. Xinstall: buffer
  319. X    cp buffer $(INSTBIN)
  320. X    chmod 111 $(INSTBIN)/buffer
  321. X    cp buffer.man $(INSTMAN)/buffer.$S
  322. X    chmod 444 $(INSTMAN)/buffer.$S
  323. X
  324. Xbuffer.tar: $(ALL)
  325. X    $(RM) -f buffer.tar
  326. X    tar cvf buffer.tar $(ALL)
  327. X
  328. Xbuffer.shar: $(ALL)
  329. X    $(RM) -f buffer.shar
  330. X    shar $(ALL) > buffer.shar
  331. END_OF_FILE
  332. if test 875 -ne `wc -c <'Makefile'`; then
  333.     echo shar: \"'Makefile'\" unpacked with wrong size!
  334. fi
  335. # end of 'Makefile'
  336. fi
  337. if test -f 'buffer.c' -a "${1}" != "-c" ; then 
  338.   echo shar: Will not clobber existing file \"'buffer.c'\"
  339. else
  340. echo shar: Extracting \"'buffer.c'\" \(18127 characters\)
  341. sed "s/^X//" >'buffer.c' <<'END_OF_FILE'
  342. X/*
  343. X    Buffer.  Very fast reblocking filter speedy writing of tapes.
  344. X    Copyright (C) 1990,1991  Lee McLoughlin
  345. X
  346. X    This program is free software; you can redistribute it and/or modify
  347. X    it under the terms of the GNU General Public License as published by
  348. X    the Free Software Foundation; either version 1, or (at your option)
  349. X    any later version.
  350. X
  351. X    This program is distributed in the hope that it will be useful,
  352. X    but WITHOUT ANY WARRANTY; without even the implied warranty of
  353. X    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  354. X    GNU General Public License for more details.
  355. X
  356. X    You should have received a copy of the GNU General Public License
  357. X    along with this program; if not, write to the Free Software
  358. X    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  359. X
  360. X    Lee McLoughlin.
  361. X    Dept of Computing, Imperial College,
  362. X    180 Queens Gate, London, SW7 2BZ, UK.
  363. X
  364. X    Email: L.McLoughlin@doc.ic.ac.uk
  365. X*/
  366. X
  367. X/* This is a reblocking process, designed to try and read from stdin
  368. X * and write to stdout - but to always try and keep the writing side
  369. X * busy.  It is meant to try and stream tape writes.
  370. X *
  371. X * This program runs in two parts.  The reader and the writer.  They
  372. X * communicate using shared memory with semaphores locking the access.
  373. X * The shared memory implements a circular list of blocks of data.
  374. X *
  375. X * L.McLoughlin, Imperial College, 1990
  376. X *
  377. X * $Log: buffer.c,v $
  378. X * Revision 1.15  1992/11/23  23:32:58  lmjm
  379. X * Oops!  This should be outside the ifdef
  380. X *
  381. X * Revision 1.14  1992/11/23  23:29:58  lmjm
  382. X * allow MAX_BLOCKSIZE and DEF_SHMEM to be configured
  383. X *
  384. X * Revision 1.13  1992/11/23  23:22:29  lmjm
  385. X * Printf's use %lu where appropriate.
  386. X *
  387. X * Revision 1.12  1992/11/23  23:17:55  lmjm
  388. X * Got rid of floats and use Kbyte counters instead.
  389. X *
  390. X * Revision 1.11  1992/11/03  23:11:51  lmjm
  391. X * Forgot Andi Karrer on the patch list.
  392. X *
  393. X * Revision 1.10  1992/11/03  22:58:41  lmjm
  394. X * Cleaned up the debugging prints.
  395. X *
  396. X * Revision 1.9  1992/11/03  22:53:00  lmjm
  397. X * Corrected stdin, stout and showevery use.
  398. X *
  399. X * Revision 1.8  1992/11/03  22:41:34  lmjm
  400. X * Added 2Gig patches from:
  401. X * Andi Karrer <karrer@bernina.ethz.ch>
  402. X * Rumi Zahir <rumi@iis.ethz.ch>
  403. X * Christoph Wicki <wicki@iis.ethz.ch>
  404. X *
  405. X * Revision 1.7  1992/07/23  20:42:03  lmjm
  406. X * Added 't' option to print total writen at end.
  407. X *
  408. X * Revision 1.6  1992/04/07  19:57:30  lmjm
  409. X * Added Kevins -B and -p options.
  410. X * Turn off buffering to make -S output appear ok.
  411. X * Added GPL.
  412. X *
  413. X * Revision 1.5  90/07/22  18:46:38  lmjm
  414. X * Added system 5 support.
  415. X * 
  416. X * Revision 1.4  90/07/22  18:29:48  lmjm
  417. X * Updated arg handling to be more consistent.
  418. X * Make sofar printing size an option.
  419. X * 
  420. X * Revision 1.3  90/05/15  23:27:46  lmjm
  421. X * Added -S option (show how much has been writen).
  422. X * Added -m option to specify how much shared memory to grab.
  423. X * Now tries to fill this with blocks.
  424. X * reader waits for writer to terminate and then frees the shared mem and sems.
  425. X * 
  426. X * Revision 1.2  90/01/20  21:37:59  lmjm
  427. X * Reset default number of  blocks and blocksize for best thruput of
  428. X * standard tar 10K blocks.
  429. X * Allow number of blocks to be changed.
  430. X * Don't need a hole in the circular queue since the semaphores prevent block
  431. X * clash.
  432. X * 
  433. X * Revision 1.1  90/01/17  11:30:23  lmjm
  434. X * Initial revision
  435. X * 
  436. X */
  437. X#include <unistd.h>
  438. X#include <stdio.h>
  439. X#include <signal.h>
  440. X#include <fcntl.h>
  441. X#include <sys/types.h>
  442. X#include <sys/stat.h>
  443. X#include <sys/ipc.h>
  444. X#include <sys/shm.h>
  445. X#include <sys/sem.h>
  446. X
  447. X#ifndef lint
  448. Xstatic char *rcsid = "$Header: /a/swan/home/swan/staff/csg/lmjm/src/buffer/RCS/buffer.c,v 1.15 1992/11/23 23:32:58 lmjm Exp lmjm $";
  449. X#endif
  450. X
  451. Xextern char *shmat();
  452. X
  453. X/* General macros */
  454. X#define TRUE 1
  455. X#define FALSE 0
  456. X#define K *1024
  457. X
  458. X/* Some forward declarations */
  459. Xvoid byee();
  460. Xvoid start_reader_and_writer();
  461. X
  462. X/* When showing print a note every this many bytes writen */
  463. Xint showevery = 0;
  464. X#define PRINT_EVERY 10 K
  465. X
  466. X/* Pause after every write */
  467. Xunsigned write_pause;
  468. X
  469. X/* This is the inter-process buffer - it implements a circular list
  470. X * of blocks. */
  471. X
  472. X#ifndef MAX_BLOCKSIZE
  473. X#define MAX_BLOCKSIZE (512 K)
  474. X#endif
  475. X#define DEF_BLOCKSIZE (10 K)
  476. Xint blocksize = DEF_BLOCKSIZE;
  477. X
  478. X/* Numbers of blocks in the queue. 
  479. X */
  480. X#define MAX_BLOCKS 2048
  481. Xint blocks = 1;
  482. X/* Circular increment of a buffer index */
  483. X#define INC(i) (((i)+1) == blocks ? 0 : ((i)+1))
  484. X
  485. X/* Max amount of shared memory you can allocate - can't see a way to look
  486. X * this up.
  487. X */
  488. X#ifndef DEF_SHMEM
  489. X#define DEF_SHMEM (1 K K)
  490. X#endif
  491. Xint max_shmem = DEF_SHMEM;
  492. X
  493. X/* Just a flag to show unfilled */
  494. X#define NONE (-1)
  495. X
  496. X/* the shared memory id of the buffer */
  497. Xint buffer_id = NONE;
  498. Xstruct block {
  499. X    int bytes;
  500. X    char *data;
  501. X} *curr_block;
  502. X
  503. X#define NO_BUFFER ((struct buffer *)-1)
  504. Xstruct buffer {
  505. X    /* writer will hang trying to lock this till reader fills in a block */
  506. X    int blocks_used_lock;
  507. X    /* reader will hang trying to lock this till writer empties a block */
  508. X    int blocks_free_lock;
  509. X
  510. X    int next_block_in;
  511. X    int next_block_out;
  512. X
  513. X    struct block block[ MAX_BLOCKS ];
  514. X
  515. X    /* These actual space for the blocks is here - the array extends
  516. X     * pass 1 */
  517. X    char data_space[ 1 ];
  518. X} *pbuffer = NO_BUFFER;
  519. Xint buffer_size;
  520. X
  521. Xint fdin    = 0;
  522. Xint fdout    = 1;
  523. Xint in_ISCHR    = 0;
  524. Xint out_ISCHR    = 0;
  525. Xint padblock    = FALSE;
  526. Xint writer_pid    = 0;
  527. Xint reader_pid    = 0;
  528. Xint percent    = 0;
  529. Xint debug    = 0;
  530. Xint Zflag    = 0;
  531. Xchar *progname = "buffer";
  532. X
  533. Xchar print_total = 0;
  534. X/* Number of K output */
  535. Xunsigned long outk = 0;
  536. X
  537. Xmain( argc, argv )
  538. X    int argc;
  539. X    char **argv;
  540. X{
  541. X    parse_args( argc, argv );
  542. X
  543. X    set_handlers();
  544. X
  545. X    buffer_allocate();
  546. X
  547. X    start_reader_and_writer();
  548. X
  549. X    byee( 0 );
  550. X}
  551. X
  552. Xparse_args( argc, argv )
  553. X    int argc;
  554. X    char **argv;
  555. X{
  556. X    int c;
  557. X    int iflag = 0;
  558. X    int oflag = 0;
  559. X    int zflag = 0;
  560. X    extern char *optarg;
  561. X    extern int optind;
  562. X    char blocks_given = FALSE;
  563. X    struct stat buf;
  564. X
  565. X
  566. X    while( (c = getopt( argc, argv, "BS:Zdm:s:b:p:u:ti:o:z:" )) != -1 ){
  567. X        switch( c ){
  568. X        case 't': /* Print to stderr the total no of bytes writen */
  569. X            print_total++;
  570. X            break;
  571. X        case 'u': /* pause after write for given microseconds */
  572. X            write_pause = atoi( optarg );
  573. X            break;
  574. X        case 'B':   /* Pad last block */
  575. X            padblock = TRUE;
  576. X            break;
  577. X        case 'Z':   /* Zero by lseek on the tape device */
  578. X            Zflag = TRUE;
  579. X            break;
  580. X        case 'i': /* Input file */
  581. X            iflag++;
  582. X            if( iflag > 1 ){
  583. X                fprintf( stderr, "buffer: -i given twice\n" );
  584. X                byee( -1 );
  585. X            }
  586. X            if( (fdin = open( optarg, O_RDONLY )) < 0 ){
  587. X                perror( "buffer: cannot open input file" );
  588. X                fprintf( stderr, "filename: %s\n", optarg );
  589. X                byee ( -1 );
  590. X            }
  591. X            break;
  592. X        case 'o': /* Output file */
  593. X            oflag++;
  594. X            if( oflag > 1 ){
  595. X                fprintf( stderr, "buffer: -o given twice\n" );
  596. X                byee( -1 );
  597. X            }
  598. X            if( (fdout = open( optarg, O_WRONLY | O_CREAT | O_TRUNC, 0666 )) < 0 ){
  599. X                perror( "buffer: cannot open output file" );
  600. X                fprintf( stderr, "filename: %s\n", optarg );
  601. X                byee ( -1 );
  602. X            }
  603. X            break;
  604. X        case 'S':
  605. X            /* Show every once in a while how much is printed */
  606. X            showevery = do_size( optarg );
  607. X            if( showevery <= 0 )
  608. X                showevery = PRINT_EVERY;
  609. X            break;
  610. X        case 'd':    /* debug */
  611. X            debug++;
  612. X            if( debug == 1 ){
  613. X                setbuf( stdout, NULL );
  614. X                setbuf( stderr, NULL );
  615. X                fprintf( stderr, "debugging turned on\n" );
  616. X            }
  617. X            break;
  618. X        case 'm':
  619. X            /* Max size of shared memory lump */
  620. X            max_shmem = do_size( optarg );
  621. X
  622. X            if( max_shmem < (sizeof( struct buffer ) + (blocksize * blocks)) ){
  623. X                fprintf( stderr, "max_shmem %d too low\n", max_shmem );
  624. X                byee( -1 );
  625. X            }
  626. X            break;
  627. X        case 'b':
  628. X            /* Number of blocks */
  629. X            blocks_given = TRUE;
  630. X            blocks = atoi( optarg );
  631. X            if( (blocks <= 0) || (MAX_BLOCKS < blocks) ){
  632. X                fprintf( stderr, "blocks %d out of range\n", blocks );
  633. X                byee( -1 );
  634. X            }
  635. X            break;
  636. X        case 'p':    /* percent to wait before dumping */
  637. X            percent = atoi( optarg );
  638. X
  639. X            if( (percent < 0) || (100 < percent) ){
  640. X                fprintf( stderr, "percent %d out of range\n", percent );
  641. X                byee( -1 );
  642. X            }
  643. X            if( debug )
  644. X                fprintf( stderr, "percent set to %d\n", percent );
  645. X            break;
  646. X        case 'z':
  647. X            zflag++;
  648. X            /* FALL THRU */
  649. X        case 's':    /* Size of a block */
  650. X            blocksize = do_size( optarg );
  651. X
  652. X            if( (blocksize <= 0) || (MAX_BLOCKSIZE < blocksize) ){
  653. X                fprintf( stderr, "blocksize %d out of range\n", blocksize );
  654. X                byee( -1 );
  655. X            }
  656. X            break;
  657. X        default:
  658. X            fprintf( stderr, "Usage: %s [-B] [-t] [-S size] [-m memsize] [-b blocks] [-p percent] [-s blocksize] [-u pause] [-i infile] [-o outfile] [-z size]\n",
  659. X                progname );
  660. X            fprintf( stderr, "-B = blocked device - pad out last block\n" );
  661. X            fprintf( stderr, "-t = show total amount writen at end\n" );
  662. X            fprintf( stderr, "-S size = show amount writen every size bytes\n" );
  663. X            fprintf( stderr, "-m size = size of shared mem chunk to grab\n" );
  664. X            fprintf( stderr, "-b num = number of blocks in queue\n" );
  665. X            fprintf( stderr, "-p percent = don't start writing until percent blocks filled\n" );
  666. X            fprintf( stderr, "-s size = size of a block\n" );
  667. X            fprintf( stderr, "-u usecs = microseconds to sleep after each write\n" );
  668. X            fprintf( stderr, "-i infile = file to read from\n" );
  669. X            fprintf( stderr, "-o outfile = file to write to\n" );
  670. X            fprintf( stderr, "-z size = combined -S/-s flag\n" );
  671. X            byee( -1 );
  672. X        }
  673. X    }
  674. X
  675. X    if (zflag) showevery = blocksize;
  676. X
  677. X    /* If -b was not given try and work out the max buffer size */
  678. X    if( !blocks_given ){
  679. X        blocks = (max_shmem - sizeof( struct buffer )) / blocksize;
  680. X        if( blocks <= 0 ){
  681. X            fprintf( stderr, "Cannot handle blocks that big, aborting!\n" );
  682. X            byee( -1 );
  683. X        }
  684. X        if( MAX_BLOCKS < blocks  ){
  685. X            fprintf( stderr, "Cannot handle that many blocks, aborting!\n" );
  686. X            byee( -1 );
  687. X        }
  688. X    }
  689. X
  690. X    /* check if fdin or fdout are character special files */
  691. X    if( fstat( fdin, &buf ) != 0 ){
  692. X        perror( "buffer: can't stat input file" );
  693. X        byee( -1 );
  694. X    }
  695. X    in_ISCHR = S_ISCHR( buf.st_mode );
  696. X    if( fstat( fdout, &buf ) != 0 ){
  697. X        perror( "buffer: can't stat output file" );
  698. X        byee( -1 );
  699. X    }
  700. X    out_ISCHR = S_ISCHR( buf.st_mode );
  701. X}
  702. X
  703. X/* The interrupt handler */
  704. Xshutdown()
  705. X{
  706. X    byee( -1 );
  707. X}
  708. X
  709. Xset_handlers()
  710. X{
  711. X    signal( SIGHUP, shutdown );
  712. X    signal( SIGINT, shutdown );
  713. X    signal( SIGQUIT, shutdown );
  714. X    signal( SIGTERM, shutdown );
  715. X
  716. X    if( writer_pid ){
  717. X        /* This is the reader - propogate the signal to the writer */
  718. X        kill( writer_pid, SIGTERM );
  719. X    }
  720. X}
  721. X
  722. Xbuffer_allocate()
  723. X{
  724. X    int i;
  725. X
  726. X    /* Allow for the data space */
  727. X    buffer_size = sizeof( struct buffer ) +
  728. X        ((blocks * blocksize) - sizeof( char ));
  729. X
  730. X    /* Create the space for the buffer */
  731. X    buffer_id = shmget( IPC_PRIVATE,
  732. X               buffer_size,
  733. X               IPC_CREAT|S_IREAD|S_IWRITE );
  734. X    if( buffer_id < 0 ){
  735. X        perror( "buffer: couldn't create shared memory segment" );
  736. X        byee( -1 );
  737. X    }
  738. X
  739. X    get_buffer();
  740. X
  741. X    if( debug )
  742. X        fprintf( stderr, "pbuffer is 0x%08x, buffer_size is %d [%d x %d]\n",
  743. X            (char *)pbuffer, buffer_size, blocks, blocksize );
  744. X
  745. X#ifdef SYS5
  746. X    memset( (char *)pbuffer, '\0', buffer_size );
  747. X#else
  748. X    bzero( (char *)pbuffer, buffer_size );
  749. X#endif
  750. X    pbuffer->blocks_used_lock = -1;
  751. X    pbuffer->blocks_free_lock = -1;
  752. X
  753. X    pbuffer->blocks_used_lock = new_sem();
  754. X    /* Start it off locked - it is unlocked when a buffer gets filled in */
  755. X    lock( pbuffer->blocks_used_lock );
  756. X
  757. X    pbuffer->blocks_free_lock = new_sem();
  758. X    /* start this off so lock() can be called on it for each block
  759. X     * till all the blocks are used up */
  760. X    sem_set( pbuffer->blocks_free_lock, blocks - 1 );
  761. X
  762. X    /* Detattach the shared memory so the fork doesnt do anything odd */
  763. X    shmdt( (char *)pbuffer );
  764. X    pbuffer = NO_BUFFER;
  765. X}
  766. X
  767. Xbuffer_remove()
  768. X{
  769. X    static char removing = FALSE;
  770. X    int i;
  771. X
  772. X    /* Avoid accidental recursion */
  773. X    if( removing )
  774. X        return;
  775. X    removing = TRUE;
  776. X
  777. X    /* Buffer not yet created */
  778. X    if( buffer_id == NONE )
  779. X        return;
  780. X
  781. X    /* There should be a buffer so this must be after its detached it
  782. X     * but before the fork picks it up */
  783. X    if( pbuffer == NO_BUFFER )
  784. X        get_buffer();
  785. X
  786. X    if( debug )
  787. X        fprintf( stderr, "removing semaphores and buffer\n" );
  788. X    remove_sem( pbuffer->blocks_used_lock );
  789. X    remove_sem( pbuffer->blocks_free_lock );
  790. X    
  791. X    if( shmctl( buffer_id, IPC_RMID, (struct shmid_ds *)0 ) == -1 )
  792. X        perror( "buffer: failed to remove shared memory buffer" );
  793. X}
  794. X
  795. Xget_buffer()
  796. X{
  797. X    int b;
  798. X
  799. X    /* Grab the buffer space */
  800. X    pbuffer = (struct buffer *)shmat( buffer_id, (char *)0, 0 );
  801. X    if( pbuffer == NO_BUFFER ){
  802. X        perror( "buffer: failed to attach shared memory" );
  803. X        byee( -1 );
  804. X    }
  805. X
  806. X    /* Setup the data space pointers */
  807. X    for( b = 0; b < blocks; b++ )
  808. X        pbuffer->block[ b ].data =
  809. X            &pbuffer->data_space[ b * blocksize ];
  810. X
  811. X}
  812. X
  813. Xvoid
  814. Xstart_reader_and_writer()
  815. X{
  816. X    int status, deadpid;
  817. X
  818. X    fflush( stdout );
  819. X    fflush( stderr );
  820. X
  821. X    if( (writer_pid = fork()) == -1 ){
  822. X        perror( "buffer: unable to fork" );
  823. X        byee( -1 );
  824. X    }
  825. X    else if( writer_pid == 0 ){
  826. X        reader_pid = getppid();
  827. X
  828. X        /* Never trust fork() to propogate signals - reset them */
  829. X        set_handlers();
  830. X
  831. X        writer();
  832. X    }
  833. X    else {
  834. X        reader();
  835. X
  836. X        /* Now wait for the writer to finish */
  837. X        while( ((deadpid = wait( &status )) != writer_pid) &&
  838. X            deadpid != -1 )
  839. X            ;
  840. X    }
  841. X}
  842. X
  843. X/* Read from stdin into the buffer */
  844. Xreader()
  845. X{
  846. X    if( debug )
  847. X        fprintf( stderr, "R: Entering reader\n" );
  848. X
  849. X    get_buffer();
  850. X
  851. X    while( 1 ){
  852. X        get_next_free_block();
  853. X        if( ! fill_block() )
  854. X            break;
  855. X    }
  856. X
  857. X    if( debug )
  858. X        fprintf( stderr, "R: Exiting reader\n" );
  859. X}
  860. X
  861. Xget_next_free_block()
  862. X{
  863. X    /* Maybe wait till there is room in the buffer */
  864. X    lock( pbuffer->blocks_free_lock );
  865. X
  866. X    curr_block = &pbuffer->block[ pbuffer->next_block_in ];
  867. X
  868. X    pbuffer->next_block_in = INC( pbuffer->next_block_in );
  869. X}
  870. X
  871. Xfill_block()
  872. X{
  873. X    int bytes;
  874. X    char *start;
  875. X    int toread;
  876. X    static char eof_reached = 0;
  877. X    
  878. X    if( eof_reached ){
  879. X        curr_block->bytes = 0;
  880. X        unlock( pbuffer->blocks_used_lock );
  881. X        return 0;
  882. X    }
  883. X
  884. X    start = curr_block->data;
  885. X    toread = blocksize;
  886. X
  887. X    /* Fill the block with input.  This reblocks the input. */
  888. X    while( toread != 0 && (bytes = read( fdin, start, toread )) > 0 ){
  889. X        start += bytes;
  890. X        toread -= bytes;
  891. X    }
  892. X
  893. X    if( bytes == 0 ){
  894. X        eof_reached = 1;
  895. X    }
  896. X
  897. X    if( bytes < 0 ){
  898. X        perror( "buffer: failed to read input" );
  899. X        byee( -1 );
  900. X    }
  901. X
  902. X    /* number of bytes available. Zero will be taken as eof */
  903. X    if( !padblock || toread == blocksize )
  904. X        curr_block->bytes = blocksize - toread;
  905. X    else {
  906. X        if( toread ) bzero( start, toread );
  907. X        curr_block->bytes = blocksize;
  908. X    }
  909. X
  910. X    if( debug > 1 )
  911. X        fprintf( stderr, "R: got %d bytes\n", curr_block->bytes );
  912. X
  913. X    unlock( pbuffer->blocks_used_lock );
  914. X
  915. X    return curr_block->bytes;
  916. X}
  917. X
  918. X/* Write the buffer to stdout */
  919. Xwriter()
  920. X{
  921. X    int filled = 0;
  922. X    int maxfilled = (blocks * percent) / 100;
  923. X    int first_block;
  924. X
  925. X    if( debug )
  926. X        fprintf( stderr, "\tW: Entering writer\n blocks = %d\n maxfilled = %d\n",
  927. X            blocks,
  928. X            maxfilled );
  929. X
  930. X    get_buffer();
  931. X
  932. X    while( 1 ){
  933. X        if( !filled )
  934. X            first_block = pbuffer->next_block_out;
  935. X        get_next_filled_block();
  936. X        if( !data_to_write() )
  937. X            break;
  938. X
  939. X        filled++;
  940. X        if( debug > 1 )
  941. X            fprintf( stderr, "W: filled = %d\n", filled );
  942. X        if( filled >= maxfilled ){
  943. X            if( debug > 1 )
  944. X                fprintf( stderr, "W: writing\n" );
  945. X            write_blocks_to_stdout( filled, first_block );
  946. X            filled = 0;
  947. X        }
  948. X    }
  949. X
  950. X    write_blocks_to_stdout( filled, first_block );
  951. X
  952. X    if( showevery ){
  953. X        pr_out();
  954. X    }
  955. X
  956. X    if( print_total ){
  957. X        fprintf( stderr, "Kilobytes Out %lu\n", outk );
  958. X    }
  959. X
  960. X    if( debug )
  961. X        fprintf( stderr, "\tW: Exiting writer\n" );
  962. X}
  963. X
  964. Xget_next_filled_block()
  965. X{
  966. X    /* Hang till some data is available */
  967. X    lock( pbuffer->blocks_used_lock );
  968. X
  969. X    curr_block = &pbuffer->block[ pbuffer->next_block_out ];
  970. X
  971. X    pbuffer->next_block_out = INC( pbuffer->next_block_out );
  972. X}
  973. X
  974. Xdata_to_write()
  975. X{
  976. X    return curr_block->bytes;
  977. X}
  978. X
  979. Xwrite_blocks_to_stdout( filled, first_block )
  980. X    int filled;
  981. X    int first_block;
  982. X{
  983. X    pbuffer->next_block_out = first_block;
  984. X
  985. X    while( filled-- ){
  986. X        curr_block = &pbuffer->block[ pbuffer->next_block_out ];
  987. X        pbuffer->next_block_out = INC( pbuffer->next_block_out );
  988. X        write_block_to_stdout();
  989. X    }
  990. X}
  991. X
  992. Xwrite_block_to_stdout()
  993. X{
  994. X    static unsigned long out = 0;
  995. X    static unsigned long last_gb = 0;
  996. X    static unsigned long next_k = 0;
  997. X    int written;
  998. X
  999. X    if( next_k == 0 && showevery ){
  1000. X        if( debug > 3 )
  1001. X            fprintf( stderr, "next_k = %lu showevery = %lu\n", next_k, showevery );
  1002. X        showevery = showevery / 1024;
  1003. X        next_k = showevery;
  1004. X    }
  1005. X
  1006. X    if( (written = write( fdout, curr_block->data, curr_block->bytes )) != curr_block->bytes ){
  1007. X        perror( "buffer: write of data failed" );
  1008. X        fprintf( stderr, "bytes to write=%d, bytes written=%d, total written %10luK\n", curr_block->bytes, written, outk );
  1009. X        byee( -1 );
  1010. X    }
  1011. X
  1012. X    if( write_pause ){
  1013. X        usleep( write_pause );
  1014. X    }
  1015. X
  1016. X    out = curr_block->bytes / 1024;
  1017. X    outk += out;
  1018. X    last_gb += out;
  1019. X
  1020. X    /*
  1021. X     * on character special devices (tapes), do an lseek() every 1 Gb,
  1022. X     * to overcome the 2Gb limit. This resets the file offset to
  1023. X     * zero, but -- at least on exabyte SCSI drives -- does not perform
  1024. X     * any actual action on the tape.
  1025. X     */
  1026. X    if( Zflag && last_gb >= 1 K K ){
  1027. X        last_gb = 0;
  1028. X        if( in_ISCHR )
  1029. X            (void) lseek( fdin, 0, SEEK_SET);
  1030. X        if( out_ISCHR )
  1031. X            (void) lseek( fdout, 0, SEEK_SET);
  1032. X    }
  1033. X    if( showevery ){
  1034. X        if( debug > 3 )
  1035. X            fprintf( stderr, "outk = %lu, next_k = %lu\n",
  1036. X                outk, next_k );
  1037. X        if( outk >= next_k ){
  1038. X            pr_out();
  1039. X            next_k += showevery;
  1040. X        }
  1041. X    }
  1042. X
  1043. X    unlock( pbuffer->blocks_free_lock );
  1044. X}
  1045. X
  1046. X
  1047. Xvoid
  1048. Xbyee( exit_val )
  1049. X    int exit_val;
  1050. X{
  1051. X    /* Only the parent (reader) should zap the buffer */
  1052. X    if( writer_pid != 0 )
  1053. X        buffer_remove();
  1054. X    else if( showevery )
  1055. X        fprintf( stderr, "\n" );
  1056. X
  1057. X    exit( exit_val );
  1058. X}
  1059. X
  1060. X/* Given a string of <num>[<suff>] returns a num
  1061. X * suff =
  1062. X *   m/M for 1meg
  1063. X *   k/K for 1k
  1064. X *   b/B for 512
  1065. X */
  1066. Xdo_size( arg )
  1067. X    char *arg;
  1068. X{
  1069. X    char format[ 20 ];
  1070. X    int ret;
  1071. X
  1072. X    *format = '\0';
  1073. X    sscanf( arg, "%d%s", &ret, format );
  1074. X
  1075. X    switch( *format ){
  1076. X    case 'm':
  1077. X    case 'M':
  1078. X        ret = ret K K;
  1079. X        break;
  1080. X    case 'k':
  1081. X    case 'K':
  1082. X        ret = ret K;
  1083. X        break;
  1084. X    case 'b':
  1085. X    case 'B':
  1086. X        ret *= 512;
  1087. X        break;
  1088. X    }
  1089. X    
  1090. X    return ret;
  1091. X}
  1092. X
  1093. Xpr_out()
  1094. X{
  1095. X    fprintf( stderr, "% 10luK\r", outk );
  1096. X}
  1097. X
  1098. X#ifdef SYS5
  1099. X#include <sys/time.h>
  1100. X
  1101. Xbzero( b, l )
  1102. X    char *b;
  1103. X    unsigned l;
  1104. X{
  1105. X    memset( b, '\0', l );
  1106. X}
  1107. X
  1108. Xusleep_back()
  1109. X{
  1110. X}
  1111. X
  1112. Xusleep( u )
  1113. X    unsigned u;
  1114. X{
  1115. X    struct itimerval old, t;
  1116. X    signal( SIGALRM, usleep_back );
  1117. X    t.it_interval.tv_sec = 0;
  1118. X    t.it_interval.tv_usec = 0;
  1119. X    t.it_value.tv_sec = u / 1000000;
  1120. X    t.it_value.tv_usec = u % 1000000;
  1121. X    setitimer( ITIMER_REAL, &t, &old );
  1122. X    pause();
  1123. X    setitimer( ITIMER_REAL, &old, NULL );
  1124. X}
  1125. X#endif
  1126. END_OF_FILE
  1127. if test 18127 -ne `wc -c <'buffer.c'`; then
  1128.     echo shar: \"'buffer.c'\" unpacked with wrong size!
  1129. fi
  1130. # end of 'buffer.c'
  1131. fi
  1132. if test -f 'sem.c' -a "${1}" != "-c" ; then 
  1133.   echo shar: Will not clobber existing file \"'sem.c'\"
  1134. else
  1135. echo shar: Extracting \"'sem.c'\" \(2585 characters\)
  1136. sed "s/^X//" >'sem.c' <<'END_OF_FILE'
  1137. X/*
  1138. X    Buffer.  Very fast reblocking filter speedy writing of tapes.
  1139. X    Copyright (C) 1990,1991  Lee McLoughlin
  1140. X
  1141. X    This program is free software; you can redistribute it and/or modify
  1142. X    it under the terms of the GNU General Public License as published by
  1143. X    the Free Software Foundation; either version 1, or (at your option)
  1144. X    any later version.
  1145. X
  1146. X    This program is distributed in the hope that it will be useful,
  1147. X    but WITHOUT ANY WARRANTY; without even the implied warranty of
  1148. X    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  1149. X    GNU General Public License for more details.
  1150. X
  1151. X    You should have received a copy of the GNU General Public License
  1152. X    along with this program; if not, write to the Free Software
  1153. X    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  1154. X
  1155. X    Lee McLoughlin.
  1156. X    Dept of Computing, Imperial College,
  1157. X    180 Queens Gate, London, SW7 2BZ, UK.
  1158. X
  1159. X    Email: L.McLoughlin@doc.ic.ac.uk
  1160. X*/
  1161. X
  1162. X/* This is a simple module to provide an easier to understand interface to
  1163. X * semaphores */
  1164. X
  1165. X#include <stdio.h>
  1166. X#include <sys/types.h>
  1167. X#include <sys/stat.h>
  1168. X#include <sys/ipc.h>
  1169. X#include <sys/sem.h>
  1170. X
  1171. X#if defined(SYS5) || defined(ultrix) || defined(_AIX)
  1172. Xunion semun {
  1173. X    int val;
  1174. X    struct semid_ds *buf;
  1175. X    ushort *array;
  1176. X};
  1177. X#endif
  1178. X
  1179. X/* Set a semaphore to a particular value - meant to be used before
  1180. X * first lock/unlock */
  1181. Xvoid
  1182. Xsem_set( sem_id, val )
  1183. X    int sem_id;
  1184. X    int val;
  1185. X{
  1186. X    union semun arg;
  1187. X    extern int errno;
  1188. X
  1189. X    arg.val = val;
  1190. X
  1191. X    errno = 0;
  1192. X    semctl( sem_id, 0, SETVAL, arg );
  1193. X    if( errno != 0 ){
  1194. X        perror( "buffer: internal error, sem_set" );
  1195. X        exit( -1 );
  1196. X    }
  1197. X}
  1198. X    
  1199. Xint
  1200. Xnew_sem()
  1201. X{
  1202. X    int sem;
  1203. X
  1204. X    sem = semget( IPC_PRIVATE, 1, IPC_CREAT|S_IREAD|S_IWRITE );
  1205. X    if( sem < 0 ){
  1206. X        perror( "buffer: internal error, couldn't create semaphore" );
  1207. X        exit( -1 );
  1208. X    }
  1209. X    sem_set( sem, 1 );
  1210. X
  1211. X    return sem;
  1212. X}
  1213. X
  1214. Xvoid
  1215. Xlock( sem_id )
  1216. X    int sem_id;
  1217. X{
  1218. X    struct sembuf sembuf;
  1219. X
  1220. X    sembuf.sem_num = 0;
  1221. X    sembuf.sem_op = -1;
  1222. X    sembuf.sem_flg = 0;
  1223. X
  1224. X    if( semop( sem_id, &sembuf, 1 ) == -1 ){
  1225. X        fprintf( stderr, "internal error, lock id %d\n", sem_id );
  1226. X        perror( "buffer: lock error" );
  1227. X        exit( -1 );
  1228. X    }
  1229. X}
  1230. X
  1231. Xvoid
  1232. Xunlock( sem_id )
  1233. X    int sem_id;
  1234. X{
  1235. X    struct sembuf sembuf;
  1236. X
  1237. X    sembuf.sem_num = 0;
  1238. X    sembuf.sem_op = 1;
  1239. X    sembuf.sem_flg = 0;
  1240. X
  1241. X    if( semop( sem_id, &sembuf, 1 ) == -1 ){
  1242. X        fprintf( stderr, "internal error, lock id %d\n", sem_id );
  1243. X        perror( "buffer: unlock error" );
  1244. X        exit( -1 );
  1245. X    }
  1246. X}
  1247. X
  1248. Xvoid
  1249. Xremove_sem( sem_id )
  1250. X    int sem_id;
  1251. X{
  1252. X    if( sem_id == -1 )
  1253. X        return;
  1254. X
  1255. X    if( semctl( sem_id, 0, IPC_RMID, NULL ) == -1 )
  1256. X        perror( "buffer: internal error, failed to remove semaphore" );
  1257. X}
  1258. END_OF_FILE
  1259. if test 2585 -ne `wc -c <'sem.c'`; then
  1260.     echo shar: \"'sem.c'\" unpacked with wrong size!
  1261. fi
  1262. # end of 'sem.c'
  1263. fi
  1264. if test -f 'COPYING' -a "${1}" != "-c" ; then 
  1265.   echo shar: Will not clobber existing file \"'COPYING'\"
  1266. else
  1267. echo shar: Extracting \"'COPYING'\" \(17982 characters\)
  1268. sed "s/^X//" >'COPYING' <<'END_OF_FILE'
  1269. X            GNU GENERAL PUBLIC LICENSE
  1270. X               Version 2, June 1991
  1271. X
  1272. X Copyright (C) 1989, 1991 Free Software Foundation, Inc.
  1273. X                          675 Mass Ave, Cambridge, MA 02139, USA
  1274. X Everyone is permitted to copy and distribute verbatim copies
  1275. X of this license document, but changing it is not allowed.
  1276. X
  1277. X                Preamble
  1278. X
  1279. X  The licenses for most software are designed to take away your
  1280. Xfreedom to share and change it.  By contrast, the GNU General Public
  1281. XLicense is intended to guarantee your freedom to share and change free
  1282. Xsoftware--to make sure the software is free for all its users.  This
  1283. XGeneral Public License applies to most of the Free Software
  1284. XFoundation's software and to any other program whose authors commit to
  1285. Xusing it.  (Some other Free Software Foundation software is covered by
  1286. Xthe GNU Library General Public License instead.)  You can apply it to
  1287. Xyour programs, too.
  1288. X
  1289. X  When we speak of free software, we are referring to freedom, not
  1290. Xprice.  Our General Public Licenses are designed to make sure that you
  1291. Xhave the freedom to distribute copies of free software (and charge for
  1292. Xthis service if you wish), that you receive source code or can get it
  1293. Xif you want it, that you can change the software or use pieces of it
  1294. Xin new free programs; and that you know you can do these things.
  1295. X
  1296. X  To protect your rights, we need to make restrictions that forbid
  1297. Xanyone to deny you these rights or to ask you to surrender the rights.
  1298. XThese restrictions translate to certain responsibilities for you if you
  1299. Xdistribute copies of the software, or if you modify it.
  1300. X
  1301. X  For example, if you distribute copies of such a program, whether
  1302. Xgratis or for a fee, you must give the recipients all the rights that
  1303. Xyou have.  You must make sure that they, too, receive or can get the
  1304. Xsource code.  And you must show them these terms so they know their
  1305. Xrights.
  1306. X
  1307. X  We protect your rights with two steps: (1) copyright the software, and
  1308. X(2) offer you this license which gives you legal permission to copy,
  1309. Xdistribute and/or modify the software.
  1310. X
  1311. X  Also, for each author's protection and ours, we want to make certain
  1312. Xthat everyone understands that there is no warranty for this free
  1313. Xsoftware.  If the software is modified by someone else and passed on, we
  1314. Xwant its recipients to know that what they have is not the original, so
  1315. Xthat any problems introduced by others will not reflect on the original
  1316. Xauthors' reputations.
  1317. X
  1318. X  Finally, any free program is threatened constantly by software
  1319. Xpatents.  We wish to avoid the danger that redistributors of a free
  1320. Xprogram will individually obtain patent licenses, in effect making the
  1321. Xprogram proprietary.  To prevent this, we have made it clear that any
  1322. Xpatent must be licensed for everyone's free use or not licensed at all.
  1323. X
  1324. X  The precise terms and conditions for copying, distribution and
  1325. Xmodification follow.
  1326. X
  1327. X            GNU GENERAL PUBLIC LICENSE
  1328. X   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
  1329. X
  1330. X  0. This License applies to any program or other work which contains
  1331. Xa notice placed by the copyright holder saying it may be distributed
  1332. Xunder the terms of this General Public License.  The "Program", below,
  1333. Xrefers to any such program or work, and a "work based on the Program"
  1334. Xmeans either the Program or any derivative work under copyright law:
  1335. Xthat is to say, a work containing the Program or a portion of it,
  1336. Xeither verbatim or with modifications and/or translated into another
  1337. Xlanguage.  (Hereinafter, translation is included without limitation in
  1338. Xthe term "modification".)  Each licensee is addressed as "you".
  1339. X
  1340. XActivities other than copying, distribution and modification are not
  1341. Xcovered by this License; they are outside its scope.  The act of
  1342. Xrunning the Program is not restricted, and the output from the Program
  1343. Xis covered only if its contents constitute a work based on the
  1344. XProgram (independent of having been made by running the Program).
  1345. XWhether that is true depends on what the Program does.
  1346. X
  1347. X  1. You may copy and distribute verbatim copies of the Program's
  1348. Xsource code as you receive it, in any medium, provided that you
  1349. Xconspicuously and appropriately publish on each copy an appropriate
  1350. Xcopyright notice and disclaimer of warranty; keep intact all the
  1351. Xnotices that refer to this License and to the absence of any warranty;
  1352. Xand give any other recipients of the Program a copy of this License
  1353. Xalong with the Program.
  1354. X
  1355. XYou may charge a fee for the physical act of transferring a copy, and
  1356. Xyou may at your option offer warranty protection in exchange for a fee.
  1357. X
  1358. X  2. You may modify your copy or copies of the Program or any portion
  1359. Xof it, thus forming a work based on the Program, and copy and
  1360. Xdistribute such modifications or work under the terms of Section 1
  1361. Xabove, provided that you also meet all of these conditions:
  1362. X
  1363. X    a) You must cause the modified files to carry prominent notices
  1364. X    stating that you changed the files and the date of any change.
  1365. X
  1366. X    b) You must cause any work that you distribute or publish, that in
  1367. X    whole or in part contains or is derived from the Program or any
  1368. X    part thereof, to be licensed as a whole at no charge to all third
  1369. X    parties under the terms of this License.
  1370. X
  1371. X    c) If the modified program normally reads commands interactively
  1372. X    when run, you must cause it, when started running for such
  1373. X    interactive use in the most ordinary way, to print or display an
  1374. X    announcement including an appropriate copyright notice and a
  1375. X    notice that there is no warranty (or else, saying that you provide
  1376. X    a warranty) and that users may redistribute the program under
  1377. X    these conditions, and telling the user how to view a copy of this
  1378. X    License.  (Exception: if the Program itself is interactive but
  1379. X    does not normally print such an announcement, your work based on
  1380. X    the Program is not required to print an announcement.)
  1381. X
  1382. XThese requirements apply to the modified work as a whole.  If
  1383. Xidentifiable sections of that work are not derived from the Program,
  1384. Xand can be reasonably considered independent and separate works in
  1385. Xthemselves, then this License, and its terms, do not apply to those
  1386. Xsections when you distribute them as separate works.  But when you
  1387. Xdistribute the same sections as part of a whole which is a work based
  1388. Xon the Program, the distribution of the whole must be on the terms of
  1389. Xthis License, whose permissions for other licensees extend to the
  1390. Xentire whole, and thus to each and every part regardless of who wrote it.
  1391. X
  1392. XThus, it is not the intent of this section to claim rights or contest
  1393. Xyour rights to work written entirely by you; rather, the intent is to
  1394. Xexercise the right to control the distribution of derivative or
  1395. Xcollective works based on the Program.
  1396. X
  1397. XIn addition, mere aggregation of another work not based on the Program
  1398. Xwith the Program (or with a work based on the Program) on a volume of
  1399. Xa storage or distribution medium does not bring the other work under
  1400. Xthe scope of this License.
  1401. X
  1402. X  3. You may copy and distribute the Program (or a work based on it,
  1403. Xunder Section 2) in object code or executable form under the terms of
  1404. XSections 1 and 2 above provided that you also do one of the following:
  1405. X
  1406. X    a) Accompany it with the complete corresponding machine-readable
  1407. X    source code, which must be distributed under the terms of Sections
  1408. X    1 and 2 above on a medium customarily used for software interchange; or,
  1409. X
  1410. X    b) Accompany it with a written offer, valid for at least three
  1411. X    years, to give any third party, for a charge no more than your
  1412. X    cost of physically performing source distribution, a complete
  1413. X    machine-readable copy of the corresponding source code, to be
  1414. X    distributed under the terms of Sections 1 and 2 above on a medium
  1415. X    customarily used for software interchange; or,
  1416. X
  1417. X    c) Accompany it with the information you received as to the offer
  1418. X    to distribute corresponding source code.  (This alternative is
  1419. X    allowed only for noncommercial distribution and only if you
  1420. X    received the program in object code or executable form with such
  1421. X    an offer, in accord with Subsection b above.)
  1422. X
  1423. XThe source code for a work means the preferred form of the work for
  1424. Xmaking modifications to it.  For an executable work, complete source
  1425. Xcode means all the source code for all modules it contains, plus any
  1426. Xassociated interface definition files, plus the scripts used to
  1427. Xcontrol compilation and installation of the executable.  However, as a
  1428. Xspecial exception, the source code distributed need not include
  1429. Xanything that is normally distributed (in either source or binary
  1430. Xform) with the major components (compiler, kernel, and so on) of the
  1431. Xoperating system on which the executable runs, unless that component
  1432. Xitself accompanies the executable.
  1433. X
  1434. XIf distribution of executable or object code is made by offering
  1435. Xaccess to copy from a designated place, then offering equivalent
  1436. Xaccess to copy the source code from the same place counts as
  1437. Xdistribution of the source code, even though third parties are not
  1438. Xcompelled to copy the source along with the object code.
  1439. X
  1440. X  4. You may not copy, modify, sublicense, or distribute the Program
  1441. Xexcept as expressly provided under this License.  Any attempt
  1442. Xotherwise to copy, modify, sublicense or distribute the Program is
  1443. Xvoid, and will automatically terminate your rights under this License.
  1444. XHowever, parties who have received copies, or rights, from you under
  1445. Xthis License will not have their licenses terminated so long as such
  1446. Xparties remain in full compliance.
  1447. X
  1448. X  5. You are not required to accept this License, since you have not
  1449. Xsigned it.  However, nothing else grants you permission to modify or
  1450. Xdistribute the Program or its derivative works.  These actions are
  1451. Xprohibited by law if you do not accept this License.  Therefore, by
  1452. Xmodifying or distributing the Program (or any work based on the
  1453. XProgram), you indicate your acceptance of this License to do so, and
  1454. Xall its terms and conditions for copying, distributing or modifying
  1455. Xthe Program or works based on it.
  1456. X
  1457. X  6. Each time you redistribute the Program (or any work based on the
  1458. XProgram), the recipient automatically receives a license from the
  1459. Xoriginal licensor to copy, distribute or modify the Program subject to
  1460. Xthese terms and conditions.  You may not impose any further
  1461. Xrestrictions on the recipients' exercise of the rights granted herein.
  1462. XYou are not responsible for enforcing compliance by third parties to
  1463. Xthis License.
  1464. X
  1465. X  7. If, as a consequence of a court judgment or allegation of patent
  1466. Xinfringement or for any other reason (not limited to patent issues),
  1467. Xconditions are imposed on you (whether by court order, agreement or
  1468. Xotherwise) that contradict the conditions of this License, they do not
  1469. Xexcuse you from the conditions of this License.  If you cannot
  1470. Xdistribute so as to satisfy simultaneously your obligations under this
  1471. XLicense and any other pertinent obligations, then as a consequence you
  1472. Xmay not distribute the Program at all.  For example, if a patent
  1473. Xlicense would not permit royalty-free redistribution of the Program by
  1474. Xall those who receive copies directly or indirectly through you, then
  1475. Xthe only way you could satisfy both it and this License would be to
  1476. Xrefrain entirely from distribution of the Program.
  1477. X
  1478. XIf any portion of this section is held invalid or unenforceable under
  1479. Xany particular circumstance, the balance of the section is intended to
  1480. Xapply and the section as a whole is intended to apply in other
  1481. Xcircumstances.
  1482. X
  1483. XIt is not the purpose of this section to induce you to infringe any
  1484. Xpatents or other property right claims or to contest validity of any
  1485. Xsuch claims; this section has the sole purpose of protecting the
  1486. Xintegrity of the free software distribution system, which is
  1487. Ximplemented by public license practices.  Many people have made
  1488. Xgenerous contributions to the wide range of software distributed
  1489. Xthrough that system in reliance on consistent application of that
  1490. Xsystem; it is up to the author/donor to decide if he or she is willing
  1491. Xto distribute software through any other system and a licensee cannot
  1492. Ximpose that choice.
  1493. X
  1494. XThis section is intended to make thoroughly clear what is believed to
  1495. Xbe a consequence of the rest of this License.
  1496. X
  1497. X  8. If the distribution and/or use of the Program is restricted in
  1498. Xcertain countries either by patents or by copyrighted interfaces, the
  1499. Xoriginal copyright holder who places the Program under this License
  1500. Xmay add an explicit geographical distribution limitation excluding
  1501. Xthose countries, so that distribution is permitted only in or among
  1502. Xcountries not thus excluded.  In such case, this License incorporates
  1503. Xthe limitation as if written in the body of this License.
  1504. X
  1505. X  9. The Free Software Foundation may publish revised and/or new versions
  1506. Xof the General Public License from time to time.  Such new versions will
  1507. Xbe similar in spirit to the present version, but may differ in detail to
  1508. Xaddress new problems or concerns.
  1509. X
  1510. XEach version is given a distinguishing version number.  If the Program
  1511. Xspecifies a version number of this License which applies to it and "any
  1512. Xlater version", you have the option of following the terms and conditions
  1513. Xeither of that version or of any later version published by the Free
  1514. XSoftware Foundation.  If the Program does not specify a version number of
  1515. Xthis License, you may choose any version ever published by the Free Software
  1516. XFoundation.
  1517. X
  1518. X  10. If you wish to incorporate parts of the Program into other free
  1519. Xprograms whose distribution conditions are different, write to the author
  1520. Xto ask for permission.  For software which is copyrighted by the Free
  1521. XSoftware Foundation, write to the Free Software Foundation; we sometimes
  1522. Xmake exceptions for this.  Our decision will be guided by the two goals
  1523. Xof preserving the free status of all derivatives of our free software and
  1524. Xof promoting the sharing and reuse of software generally.
  1525. X
  1526. X                NO WARRANTY
  1527. X
  1528. X  11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
  1529. XFOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN
  1530. XOTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
  1531. XPROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
  1532. XOR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  1533. XMERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS
  1534. XTO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
  1535. XPROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
  1536. XREPAIR OR CORRECTION.
  1537. X
  1538. X  12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
  1539. XWILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
  1540. XREDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
  1541. XINCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
  1542. XOUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
  1543. XTO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
  1544. XYOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
  1545. XPROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
  1546. XPOSSIBILITY OF SUCH DAMAGES.
  1547. X
  1548. X             END OF TERMS AND CONDITIONS
  1549. X
  1550. X    Appendix: How to Apply These Terms to Your New Programs
  1551. X
  1552. X  If you develop a new program, and you want it to be of the greatest
  1553. Xpossible use to the public, the best way to achieve this is to make it
  1554. Xfree software which everyone can redistribute and change under these terms.
  1555. X
  1556. X  To do so, attach the following notices to the program.  It is safest
  1557. Xto attach them to the start of each source file to most effectively
  1558. Xconvey the exclusion of warranty; and each file should have at least
  1559. Xthe "copyright" line and a pointer to where the full notice is found.
  1560. X
  1561. X    <one line to give the program's name and a brief idea of what it does.>
  1562. X    Copyright (C) 19yy  <name of author>
  1563. X
  1564. X    This program is free software; you can redistribute it and/or modify
  1565. X    it under the terms of the GNU General Public License as published by
  1566. X    the Free Software Foundation; either version 2 of the License, or
  1567. X    (at your option) any later version.
  1568. X
  1569. X    This program is distributed in the hope that it will be useful,
  1570. X    but WITHOUT ANY WARRANTY; without even the implied warranty of
  1571. X    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  1572. X    GNU General Public License for more details.
  1573. X
  1574. X    You should have received a copy of the GNU General Public License
  1575. X    along with this program; if not, write to the Free Software
  1576. X    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  1577. X
  1578. XAlso add information on how to contact you by electronic and paper mail.
  1579. X
  1580. XIf the program is interactive, make it output a short notice like this
  1581. Xwhen it starts in an interactive mode:
  1582. X
  1583. X    Gnomovision version 69, Copyright (C) 19yy name of author
  1584. X    Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
  1585. X    This is free software, and you are welcome to redistribute it
  1586. X    under certain conditions; type `show c' for details.
  1587. X
  1588. XThe hypothetical commands `show w' and `show c' should show the appropriate
  1589. Xparts of the General Public License.  Of course, the commands you use may
  1590. Xbe called something other than `show w' and `show c'; they could even be
  1591. Xmouse-clicks or menu items--whatever suits your program.
  1592. X
  1593. XYou should also get your employer (if you work as a programmer) or your
  1594. Xschool, if any, to sign a "copyright disclaimer" for the program, if
  1595. Xnecessary.  Here is a sample; alter the names:
  1596. X
  1597. X  Yoyodyne, Inc., hereby disclaims all copyright interest in the program
  1598. X  `Gnomovision' (which makes passes at compilers) written by James Hacker.
  1599. X
  1600. X  <signature of Ty Coon>, 1 April 1989
  1601. X  Ty Coon, President of Vice
  1602. X
  1603. XThis General Public License does not permit incorporating your program into
  1604. Xproprietary programs.  If your program is a subroutine library, you may
  1605. Xconsider it more useful to permit linking proprietary applications with the
  1606. Xlibrary.  If this is what you want to do, use the GNU Library General
  1607. XPublic License instead of this License.
  1608. END_OF_FILE
  1609. if test 17982 -ne `wc -c <'COPYING'`; then
  1610.     echo shar: \"'COPYING'\" unpacked with wrong size!
  1611. fi
  1612. # end of 'COPYING'
  1613. fi
  1614. echo shar: End of shell archive.
  1615. exit 0
  1616.  
  1617. -- 
  1618. --
  1619. Lee McLoughlin.                          Phone: +44 71 589 5111 X 5085
  1620. Dept of Computing, Imperial College,     Fax: +44 71 581 8024
  1621. 180 Queens Gate, London, SW7 2BZ, UK.    Email: L.McLoughlin@doc.ic.ac.uk
  1622.  
  1623. exit 0 # Just in case...
  1624.