当前位置: 技术问答>linux和unix
调用pause()的进程/线程会让出处理器么?
来源: 互联网 发布时间:2016-05-26
本文导语: 内核驱动采用阻塞式读时,没有数据时会被阻塞,让出处理; 而采用异步通知的方法时,用户态进程会在ready后调用puase()/sleep(),等待内核程序发出异步通知。 这里想比较一下,哪种方法更好?更节省资源,pause()源代码不大...
内核驱动采用阻塞式读时,没有数据时会被阻塞,让出处理;
而采用异步通知的方法时,用户态进程会在ready后调用puase()/sleep(),等待内核程序发出异步通知。
这里想比较一下,哪种方法更好?更节省资源,pause()源代码不大看得懂,貌似没有让出处理器的操作,如此的话,难道是阻塞式读写比异步通知更好?但是这个结论和感觉相悖。
而采用异步通知的方法时,用户态进程会在ready后调用puase()/sleep(),等待内核程序发出异步通知。
这里想比较一下,哪种方法更好?更节省资源,pause()源代码不大看得懂,貌似没有让出处理器的操作,如此的话,难道是阻塞式读写比异步通知更好?但是这个结论和感觉相悖。
int
__libc_pause (void)
{
sigset_t set;
__sigemptyset (&set);
__sigprocmask (SIG_BLOCK, NULL, &set);
/* pause is a cancellation point, but so is sigsuspend.
So no need for anything special here. */
return __sigsuspend (&set);
}
|
__sigsuspend 会阻塞等待并释放CPU。下面是信号处理 sigsuspend 的一些说明:
int sigsuspend(const sigset_t *sigmask);
The sigsuspend() function shall replace the current signal mask of the calling thread with the set of
signals pointed to by sigmask and then suspend the thread until delivery of a signal whose action is
either to execute a signal-catching function or to terminate the process. This shall not cause any other
signals that may have been pending on the process to become pending on the thread.
If the action is to terminate the process then sigsuspend() shall never return. If the action is to exe-
cute a signal-catching function, then sigsuspend() shall return after the signal-catching function
returns, with the signal mask restored to the set that existed prior to the sigsuspend() call.
It is not possible to block signals that cannot be ignored. This is enforced by the system without caus-
ing an error to be indicated.
也就是说,sigsuspend后,进程就挂在那里,等待着开放的信号的唤醒。系统在接受到信号后,马上就把现在的信号集还原为原来的,然后调用处理函数。
int sigsuspend(const sigset_t *sigmask);
The sigsuspend() function shall replace the current signal mask of the calling thread with the set of
signals pointed to by sigmask and then suspend the thread until delivery of a signal whose action is
either to execute a signal-catching function or to terminate the process. This shall not cause any other
signals that may have been pending on the process to become pending on the thread.
If the action is to terminate the process then sigsuspend() shall never return. If the action is to exe-
cute a signal-catching function, then sigsuspend() shall return after the signal-catching function
returns, with the signal mask restored to the set that existed prior to the sigsuspend() call.
It is not possible to block signals that cannot be ignored. This is enforced by the system without caus-
ing an error to be indicated.
也就是说,sigsuspend后,进程就挂在那里,等待着开放的信号的唤醒。系统在接受到信号后,马上就把现在的信号集还原为原来的,然后调用处理函数。
|
让他sleep吧,这样不占资源。
阻塞貌似是要占资源的。
所以就有mutex_cond。
阻塞貌似是要占资源的。
所以就有mutex_cond。