📄 00000017.htm
字号:
c = '\003'; <BR> block_write(&c, 1); /* Control-C interrupts the PS job */ <BR> block_write(&eofc, 1); /* followed by EOF */ <BR> proc_upto_eof(1); /* read & ignore up through EOF */ <BR> clear_alrm(); <BR> get_page(&end_page); <BR> do_acct(); <BR> exit(EXIT_SUCCESS); /* success since user lprm'ed the job */ <BR>} <BR>void <BR>set_intr(void) /* enable signal handler */ <BR>{ <BR> if (signal_intr(SIGINT, sig_int) == SIG_ERR) <BR> log_sys("set_intr: signal_intr error"); <BR>} <BR>void <BR>clear_intr(void) /* ignore signal */ <BR>{ <BR> if (signal(SIGINT, SIG_IGN) == SIG_ERR) <BR> log_sys("clear_intr: signal error"); <BR>} <BR>_______________________________________________________________________ <BR>______ <BR>程序17.6 处理中断信号的interrupt.c文件 <BR> 图17.6写明了哪些函数设置超时时间。我们只是在以下情况下设置超时:查询 <BR>打印机状态(get_status)、读取打印机的页码(get_page)或者当我们正中断打印机 <BR>时(handle_intr)。如果发生了超时,我们只需要记录下错误,过一段时间后终 <BR>止。程序17.7是alarm.c文件。 <BR>_______________________________________________________________________ <BR>______ <BR>#include "lprps.h" <BR>static void <BR>sig_alrm(int signo) /* SIGALRM handler */ <BR>{ <BR> alrm_flag = 1; <BR> return; <BR>} <BR>void <BR>handle_alrm(void) <BR>{ <BR> log_ret("printer not responding"); <BR> sleep(60); /* it will take at least this long to warm up */ <BR> exit(EXIT_REPRINT); <BR>} <BR>void /* Establish the signal handler and set the alarm. */ <BR>set_alrm(unsigned int nsec) <BR>{ <BR> alrm_flag = 0; <BR> if (signal_intr(SIGALRM, sig_alrm) == SIG_ERR) <BR> log_sys("set_alrm: signal_intr error"); <BR> alarm(nsec); <BR>} <BR>void <BR>clear_alrm(void) <BR>{ <BR> alarm(0); <BR> if (signal(SIGALRM, SIG_IGN) == SIG_ERR) <BR> log_sys("clear_alrm: signal error"); <BR> alrm_flag = 0; <BR>} <BR>_______________________________________________________________________ <BR>______ <BR>程序17.7 处理超时的alarm.c文件 <BR> 程序17.8是函数get_status,这个函数由main函数调用。它发送一个Control <BR>-T到打印机以获取打印机的状态消息。打印机回送一行消息。如果我们接到的消息 <BR>是: <BR>%%[ status : idle]%% <BR> 这意味着打印机准备好执行一个新的作业。这个消息被函数proc_some_input <BR>读取和处理(下面我们会讨论这个函数)。 <BR>_______________________________________________________________________ <BR>______ <BR>#include "lprps.h" <BR>/* Called by main() before printing job. <BR> * We send a Control-T to the printer to fetch its status. <BR> * If we timeout before reading the printer's status, something <BR> * is wrong. */ <BR>void <BR>get_status(void) <BR>{ <BR> char c; <BR> set_alrm(5); /* 5 second timeout to fetch status */ <BR> tty_flush(); <BR> c = '\024'; <BR> block_write(&c, 1); /* send Control-T to printer */ <BR> init_input(0); <BR> while (status == INVALID) <BR> proc_some_input(); /* wait for something back */ <BR> switch (status) { <BR> case IDLE: /* this is what we're looking for ... */ <BR> clear_alrm(); <BR> return; <BR> case WAITING: /* printer thinks it's in the middle of a job */ <BR> block_write(&eofc, 1); /* send EOF to printer */ <BR> sleep(5); <BR> exit(EXIT_REPRINT); <BR> case BUSY: <BR> case UNKNOWN: <BR> sleep(15); <BR> exit(EXIT_REPRINT); <BR> } <BR>} <BR>_______________________________________________________________________ <BR>______ <BR>程序17.8 get_status函数 <BR> 如果我们收到下列消息: <BR>%%[ status: waiting ]%% <BR> 这说明打印机正等待我们发送更多的数据以用于当前正打印的作业,这很可能 <BR>是前一打印作业出了些问题。为了清除这个状态,我们发送给打印机一个EOF终止 <BR>符。 <BR> PostScript打印机维护着一个页码计数器。这个计数器每打印一页就会增加, <BR>它即使在关闭电源时也会保存着。为了读此计数器,我们需要发送给打印机一个P <BR>ostScript程序。文件pagecount.c(程序17.9)包含了这个小PostScript程序(含 <BR>有大约10个PostScript操作符)和函数get_page。 <BR>_______________________________________________________________________ <BR>______ <BR>#include "lprps.h" <BR>/* PostScript program&nb
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -