STREAMS Programming Guide

Write Service Procedure

Example 8–4 is an example of a write service procedure.

Example 8–4 Write Service Procedure

static int mod_wsrv(queue_t *q)
 	extern int qenable();
 	mblk_t *mp, *bp;
		while (mp = getq(q)) {
			/* check for priority messages and canput ... */

			/* Allocate a header to prepend to the message.
 		 * If the allocb fails, use bufcall to reschedule.
 		if ((bp = allocb(HDRSZ, BPRI_MED)) == NULL) {
 			if (!(id=bufcall(HDRSZ,BPRI_MED,qenable, q))) {
  				timeout(qenable, (caddr_t)q,
 				 * Put the msg back and exit, we will be
 				 * re-enabled later
 				putbq(q, mp);
 			/* process message .... */

mod_wsrv prefixes each output message with a header.

In this example, mod_wsrv illustrates a potential deadlock case. If allocb(9F) fails, mod_wsrv tends to recover without loss of data and calls bufcall(9F). In this case, the routine passed to bufcall(9F) is qenable(9F). When a buffer is available, the service procedure is automatically re-enabled. Before exiting, the current message is put back in the queue. Example 8–4 deals with bufcall(9F) failure by calling timeout(9F).

timeout(9F) schedules the given function to be run with the given argument in the given number of clock cycles. In this example, if bufcall(9F) fails, the system runs qenable(9F) after two seconds have passed.