Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

820 Communicating with a Network Printer Chapter 21


225 /*
226 * Get the next job number.
227 *
228 * LOCKING: acquires and releases joblock.
229 */
230 int32_t
231 get_newjobno(void)
232 {
233 int32_t jobid;

234 pthread_mutex_lock(&joblock);
235 jobid=nextjob++;
236 if (nextjob <= 0)
237 nextjob=1;
238 pthread_mutex_unlock(&joblock);
239 return(jobid);
240 }

241 /*
242 * Add a new job to the list of pending jobs. Then signal
243 * the printer thread that a job is pending.
244 *
245 * LOCKING: acquires and releases joblock.
246 */
247 void
248 add_job(struct printreq *reqp, int32_t jobid)
249 {
250 struct job *jp;

251 if ((jp = malloc(sizeof(struct job))) == NULL)
252 log_sys("malloc failed");
253 memcpy(&jp->req, reqp, sizeof(struct printreq));

[225 – 240] Theget_newjobnofunction is used to get the next job number.Wefirst
lock thejoblockmutex. Weincrement thenextjobvariable and handle
the case where it wraps around. Then we unlock the mutex and return the
valuenextjobhad before we incremented it. Multiple threads can call
get_newjobnoat the same time; we need to serialize access to the next job
number so that each thread gets a unique job number.(Refer to Figure11.9
to see what could happen if we don’t serialize the threads in this case.)
[241 – 253] Theadd_jobfunction is used to add a new print request to the end of the
list of pending print jobs.We start by allocating space for thejobstructure.
If this fails, we log an error message and exit. At this point, the print request
is stored safely on disk; when the printer spooling daemon is restarted, it
will pick the request up. After we allocate memory for the new job, we copy
the request structurefromthe client into the job structure. Recall from
print.hthat ajobstructureconsists of a pair of list pointers, a job ID, and
acopy of theprintreqstructuresent to us by the clientprintcommand.
Free download pdf