1. linux 处理命令行程序时,对输入的参数处理,可以使用get_opt 库函数,方便制作命令行工具。
2. 当程序需要停止等待信号结束时,通常的做法是使用 while(true)循环,这样的话,程序的cpu会占用很高,使用sigpause 函数。
sigpause temporarily replaces the signal mask of the calling process with the mask given by mask and then suspends the process until delivery of a signal whose action is to invoke a signal handler or to terminate a process.
If the signal terminates the process, then sigsuspend() does not return. If the signal is caught, then sigsuspend() returns after the signal handler returns, and the signal mask is resto
sigpause 在使用时需要传入一个mask,当程序执行到此处,sigpause暂时的使用传入的mask替换当前进程的mask,并且阻塞放弃cpu的使用,直到mask之外的信号到来。 Normally, sigsuspend() is used in conjunction with sigprocmask(2) in order to prevent delivery of a signal during the execution of a critical code section. The caller first blocks the signals with sigprocmask(2). When the critical code has completed, the caller then waits for the signals by calling sigsuspend() with the signal mask that was returned bysigprocmask(2) (in the oldset argument).sigsuspend(2) - waits for any signal. It takes a signal mask of signals that are atomically unblocked,
The sigsuspend()
function is the modern analogue of the old pause()
function.
Major change: two code blocks reversed.
It allows you to send your thread to sleep until one of its selected signals occurs. If you want it to sleep until SIGRTMIN+1 arrives, you use:
sigfillset(&mask);
sigdelset(&mask, SIGRTMIN+1);
sigsuspend(&mask);
This blocks every signal except SIGRTMIN+1.
If you want to sleep until a signal other than SIGRTMIN+1 arrives, you use:
sigemptyset(&mask);
sigaddset(&mask, SIGRTMIN+1);
sigsuspend(&mask);
This blocks only SIGRTMIN+1.