linuxrt sigsuspendd 能用在线程里面吗

1426人阅读
linux学习之路(88)
分类:&&358人阅读&&&
pause函数:
&&&&&功能:让进程暂停直到信号出现
&&&&#include&unistd.h&
&&&& intpause();
&&&& 函数说明:pause()会令目前的进程暂停(进入睡眠状态),直至信号(signal)所中断。
&&&& 返回值:只返回-1
#include&stdio.h&
#include&unistd.h&
void deal()
&&&printf(“信号干扰!\n”);
void main()
&&&& printf(“进程执行!\n”);
&&&&signal(SIGINT,deal);
&&&&pause();
&&&&printf(“进程结束!\n”);
当程序运行时,pause会使当前的进程挂起(进入睡眠状态),直到我们向该进程发送SIGINT中断信号,进程才会被唤醒,并处理信号,处理完信号后pause函数才返回,并继续运行该程序。注:任何信号都可是pause唤醒。
sigsuspend函数:
&&&#include&signal.h&
&& intsigsuspend(const sigset_t *sigmask);
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 返回值:-1
sigsuspend函数和pause函数一样,可以是进程挂起(进入睡眠状态),直至有信号发生。
sigsuspend函数的参数是一个信号集,这个信号集是用来屏蔽信号的,信号集中存放了要屏蔽的信号。
如果该信号集为空的话,sigsuspend就不屏蔽任何信号,任何信号都可以使进程从挂起状态唤醒,这就与pause函数一样了。
#include&stdio.h&
#include&unistd.h&
#include&signal.h&
void deal()
&&&printf(“信号干扰!\n”);
void main()
sigemptyset(&sigmask);
&&&& printf(“进程执行!\n”);
&&&&signal(SIGINT,deal);
&&&&sigsuspend(&sigmask);
&&&&printf(“进程结束!\n”);
该例子与上面pause函数的例子是一样的。
sigsuspend与pause的不同处:
sigsuspend函数是pause函数的增强版。当sigsuspend函数的参数信号集为空信号集时,sigsuspend函数是和pause函数是一样的,可以接受任何信号的中断。
但,sigsuspend函数可以屏蔽信号,接受指定的信号中断。
sigsuspend函数=pause函数+指定屏蔽信号
注:信号中断的是sigsuspend和pause函数,不是程序代码。
sigsuspend是否影响sigprocmask屏蔽的信号呢?
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 影响.使原来的屏蔽信号全部失效.
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 当sigsuspend返回,恢复原来的屏蔽信号.、
sigsuspend什么时候使用?
&&&&&当我们的程序在进行一些业务处理的时候不想被一些信号所中断,我们就可以先屏蔽这些信号,在这个业务处理结束时我们可以使用sigsuspend函数处理在排队的信号(在这个过程中也可指定屏蔽的信号),处理完成后,在恢复之前的信号屏蔽,并处理下个业务处理。
#include&stdio.h&
#include&unistd.h&
#include&signal.h&
void &&deal()
&&&&&printf(“信号处理中!\n”);
void &&main()
&&&&sigset_t& sigs,
&&&&int &i;
&&&&signal(SIGINT,deal);
&&&& sigemptyset(&sigs);&&&&&&&&&&& //sigsuspend的信号集设置空,表示会sigsuspend会处//理任何信号,如果想在处理排队的信号时屏蔽一些 //信号,可以把相应的信号加到信号集中
&&&&sigemptyset(&sigmask);
&&&&sigaddset(SIGINT,&sigs);
&&&& sigprocmask(SIG_BLOCK,&sigs,0);& //屏蔽sigint信号,使其不能再业务处理过程中干//扰进程。
&&& for(i=0 ; i&5 ; i++)
&&&&&&&& printf(“处理业务中…..\n”);&&& //模拟业务处理
&&&&&&&& sleep(5)
&&&&&&&& printf(“处理业务结束\n”);
&&&&&&&&sigsuspend(&sigmask);&&&&&& //处理正在排队的信号,处理信号完毕后,
//sigsuspend函数才返回,并执行下个业务处理
&&&&&&& printf(“处理业务中…..\n”);&&& //模拟业务处理
&&&&&&&& sleep(5)
&&&&&&&& printf(“处理业务结束\n”);
说明:在sigsuspend函数调用时,会使进程挂起(进入睡眠状态)等待信号的中断,如果没有信号发生,进程会一直挂起,当有信号发生时,但该信号不是sigsuspend函数的信号集中所设置的屏蔽的信号时,sigsuspend会处理该信号,当该信号处理完成后,sigsuspend函数才返回,并执行接下来的代码。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:3631329次
积分:33668
积分:33668
排名:第115名
原创:22篇
转载:1920篇
评论:465条
(37)(14)(28)(148)(78)(74)(36)(80)(90)(42)(139)(241)(117)(357)(181)(63)(56)(57)(5)(47)(1)(30)(23)温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
  int sigsuspend(const sigset_t *mask);作用:  用于在接收到某个信号之前,临时用mask替换进程的信号掩码,并暂停进程执行,直到收到信号为止。  The&sigsuspend()&function replaces the current signal mask of the calling thread with the set of signals pointed to by&sigmask&and then suspends the thread until delivery of a signal whose action is either to execute a signal-catching function or to terminate the process. This will 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()&will never return. If the action is to execute a signal-catching function, thensigsuspend()&will return after the signal-catching function returns, with the signal mask restored to the set that existed prior to thesigsuspend()&call.  It is not possible to block signals that cannot be ignored. This is enforced by the system without causing an error to be indicated.  也就是说,sigsuspend后,进程就挂在那里,等待着开放的信号的唤醒。系统在接收到信号后,马上就把现在的信号集还原为原来的,然后调用处理函数。返回值:  sigsuspend返回后将恢复调用之前的的信号掩码。信号处理函数完成后,进程将继续执行。该系统调用始终返回-1,并将errno设置为EINTR.  Since&sigsuspend()&suspends process execution indefinitely, there is no successful completion return value. If a return occurs, -1 is returned and&errno&is set to indicate the error.  The&sigsuspend()&function will fail if:  [EINTR]  A signal is caught by the calling process and control is returned from the signal-catching function.也就是说,sigsuspend后,进程就挂在那里,等待着开放的信号的唤醒。系统在接受到信号后,马上就把现在的信号集还原为原来的,然后调用处理函数。Stevens在《Unix环境高级编程》一书中是如是回答的“If a signal is caught and if the signal handler returns, then sigsuspend returns and the signal mask of the process is set to its value before the call to sigsuspend.”,由于sigsuspend是原子操作,所以这句给人的感觉就是先调用signal handler先返回,然后sigsuspend再返回。[cpp]&&结果:[cpp]&如果按照sig_handler先返回,那么SIGINT是不该被打印出来的,因为那时屏蔽字还没有恢复,所有信号都是不阻塞的。那么是Stevens说错了么?当然没有,只是Stevens没有说请在sigsuspend的原子操作中到底做了什么?sigsuspend的整个原子操作过程为:(1) 设置新的mask阻塞当前进程;(2) 收到信号,恢复原先mask;(3) 调用该进程设置的信号处理函数;(4) 待信号处理函数返回后,sigsuspend返回。大致就是上面这个过程,噢,原来signal handler是原子操作的一部分,而且是在恢复屏蔽字后执行的,所以上面的例子是没有问题的,Stevens说的也没错。由于Linux和Unix的千丝万缕的联系,所以在两个平台上绝大部分的系统调用的语义是一致的。上面的sigsuspend的原子操作也是从《深入理解Linux内核》一书中揣度出来的。书中的描述如下:[cpp]&int sigsuspend(const sigset_t *sigmask);此函数用于进程的挂起,sigmask指向一个信号集。当此函数被调用时,sigmask所指向的信号集中的信号将赋值给信号掩码。之后进程挂起。直到进程捕捉到信号,并调用处理函数返回时,函数sigsuspend返回。信号掩码恢复为信号调用前的值,同时将errno设为EINTR。进程结束信号可将其立即停止。&??[cpp]&
阅读(1637)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
loftPermalink:'',
id:'fks_',
blogTitle:'linux c
之sigsuspend 进程阻塞',
blogAbstract:'函数原型:  #include &signal.h&  int sigsuspend(const sigset_t *mask);',
blogTag:'',
blogUrl:'blog/static/',
isPublished:1,
istop:false,
modifyTime:5,
publishTime:9,
permalink:'blog/static/',
commentCount:0,
mainCommentCount:0,
recommendCount:0,
bsrk:-100,
publisherId:0,
recomBlogHome:false,
currentRecomBlog:false,
attachmentsFileIds:[],
groupInfo:{},
friendstatus:'none',
followstatus:'unFollow',
pubSucc:'',
visitorProvince:'',
visitorCity:'',
visitorNewUser:false,
postAddInfo:{},
mset:'000',
remindgoodnightblog:false,
isBlackVisitor:false,
isShowYodaoAd:false,
hostIntro:'',
hmcon:'0',
selfRecomBlogCount:'0',
lofter_single:''
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}sigsuspend应当和signal()一起用呢还是应该和sigaction()一起用 - Linux/Unix当前位置:& &&&sigsuspend应当和signal()一起用呢还是应该和sigactsigsuspend应当和signal()一起用呢还是应该和sigaction()一起用&&网友分享于:&&浏览:0次sigsuspend应该和signal()一起用呢还是应该和sigaction()一起用?还是两种组合都可以?------解决思路----------------------sigsuspend&不一定非要与&signal/sigaction()&组合使用
http://blog.csdn.net/yu_xiang/article/details/7003810
http://blog.csdn.net/dlutbrucezhang/article/details/8914250
12345678910
12345678910
12345678910 上一篇:下一篇:文章评论相关解决方案 1234567891011 Copyright & &&版权所有1402人阅读
《UNIX环境高级编程》(55)
函数原型:
  #include &signal.h&
  int sigsuspend(const sigset_t *mask);
  用于在接收到某个信号之前,临时用mask替换进程的信号掩码,并暂停进程执行,直到收到信号为止。
  The&sigsuspend()&function
replaces the current signal mask of the calling thread with the set of signals pointed to by&sigmask&and
then suspends the thread until delivery of a signal whose action is either to execute a signal-catching function or to terminate the process. This will 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()&will
never return. If the action is to execute a signal-catching function, thensigsuspend()&will
return after the signal-catching function returns, with the signal mask restored to the set that existed prior to thesigsuspend()&call.
  It is not possible to block signals that cannot be ignored. This is enforced by the system without
causing an error to be indicated.
  也就是说,sigsuspend后,进程就挂在那里,等待着开放的信号的唤醒。系统在接收到信号后,马上就把现在的信号集还原为原来的,然后调用处理函数。
返回值:
  sigsuspend返回后将恢复调用之前的的信号掩码。信号处理函数完成后,进程将继续执行。该系统调用始终返回-1,并将errno设置为EINTR.
  Since&sigsuspend()&suspends
process execution indefinitely, there is no successful completion return value. If a return occurs, -1 is returned and&errno&is
set to indicate the error.
  The&sigsuspend()&function
will fail if:
  [EINTR]
  A signal is caught by the calling process and control is returned from the signal-catching function.
也就是说,sigsuspend后,进程就挂在那里,等待着开放的信号的唤醒。系统在接受到信号后,马上就把现在的信号集还原为原来的,然后调用处理函数。
Stevens在《Unix环境高级编程》一书中是如是回答的“If a signal is caught and if the signal handler returns, then sigsuspend returns and the signal mask
of the process is set to its value before the call to sigsuspend.”,由于sigsuspend是原子操作,所以这句给人的感觉就是先调用signal handler先返回,然后sigsuspend再返回。
如果按照sig_handler先返回,那么SIGINT是不该被打印出来的,因为那时屏蔽字还没有恢复,所有信号都是不阻塞的。那么是Stevens说错了么?当然没有,只是Stevens没有说请在sigsuspend的原子操作中到底做了什么?
sigsuspend的整个原子操作过程为:
(1) 设置新的mask阻塞当前进程;
(2) 收到信号,恢复原先mask;
(3) 调用该进程设置的信号处理函数;
(4) 待信号处理函数返回后,sigsuspend返回。
大致就是上面这个过程,噢,原来signal handler是原子操作的一部分,而且是在恢复屏蔽字后执行的,所以上面的例子是没有问题的,Stevens说的也没错。由于Linux和Unix的千丝万缕的联系,所以在两个平台上绝大部分的系统调用的语义是一致的。上面的sigsuspend的原子操作也是从《深入理解Linux内核》一书中揣度出来的。书中的描述如下:
int sigsuspend(const sigset_t *sigmask);
此函数用于进程的挂起,sigmask指向一个信号集。当此函数被调用时,sigmask所指向的信号集中的信号将赋值给信号掩码。之后进程挂起。直到进程捕捉到信号,并调用处理函数返回时,函数sigsuspend返回。信号掩码恢复为信号调用前的值,同时将errno设为EINTR。进程结束信号可将其立即停止。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:1898148次
积分:25671
积分:25671
排名:第194名
原创:500篇
转载:316篇
译文:43篇
评论:1054条
技术 | 交流 | 娱乐 可以加下博主的群--
<font color="#FF901
大家没事可以在群里闲聊Android, IOS,C/C++, Linux等技术问题,心得,学习体会等
PS:博主仍是计算机行业的一名小学生
文章:52篇
阅读:91920
文章:38篇
阅读:94283
文章:32篇
阅读:56246
文章:33篇
阅读:54609
文章:21篇
阅读:62771
阅读:44331
阅读:53854
文章:56篇
阅读:245885
文章:20篇
阅读:39348
文章:15篇
阅读:33919
文章:30篇
阅读:161472
文章:28篇
阅读:140423

我要回帖

更多关于 linux suspend 的文章

 

随机推荐