Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

Section 21.5 Source Code 813


38 /*
39 * Needed for logging.
40 */
41 int log_to_stderr = 0;
42 /*
43 * Printer-related stuff.
44 */
45 struct addrinfo *printer;
46 char *printer_name;
47 pthread_mutex_t configlock = PTHREAD_MUTEX_INITIALIZER;
48 int reread;
49 /*
50 * Thread-related stuff.
51 */
52 struct worker_thread *workers;
53 pthread_mutex_t workerlock = PTHREAD_MUTEX_INITIALIZER;
54 sigset_t mask;
55 /*
56 * Job-related stuff.
57 */
58 struct job *jobhead, *jobtail;
59 int jobfd;

[38 – 41] Our logging functions requirethat we define thelog_to_stderrvariable
and set it to 0 to force log messages to be sent to the system log instead of to
the standarderror.Inprint.c, we definedlog_to_stderrand set it to 1,
even though we don’t use the log functions in the user command. We could
have avoided this by splitting the utility functions into two separate files: one
for the server and one for the client commands.
[42 – 48] We use the global variableprinterto hold the network address of the printer.
We storethe host name of the printer inprinter_name.Theconfiglock
mutex protects access to therereadvariable, which is used to indicate that
the daemon needs to reread the configuration file, presumably because an
administrator changed the printer or its network address.
[49 – 54] Next, we define the thread-related variables.We useworkersas the head of a
doubly linked list of threads that arereceiving files from clients. This list is
protected by the mutexworkerlock.The signal mask used by the threads is
held in the variablemask.
[55 – 59] For the list of pending jobs, we definejobheadto be the start of the list and
jobtailto be the tail of the list. This list is also doubly linked, but we need
to add jobs to the end of the list, so we must remember a pointer to the list tail.
With the list of worker threads, the order doesn’t matter, so we can add them
to the head of the list and don’t need to remember the tail pointer. jobfdis
the file descriptor for the job file.
Free download pdf