当前位置: 首页 > news >正文

国外 素材 网站logo设计 公司 免费

国外 素材 网站,logo设计 公司 免费,动漫制作专业电脑配置要求,上海推广网站公司目录 一、信号的阻塞 二、信号集操作函数 三、sigprocmask函数 四、pause函数 五、sigsuspend函数 一、信号的阻塞 有时候不希望在接到信号时就立即停止当前执行#xff0c;去处理信号#xff0c;同时也不希望忽略该信号#xff0c;而是延时一段时间去调用信号处理函数。…目录 一、信号的阻塞 二、信号集操作函数 三、sigprocmask函数 四、pause函数  五、sigsuspend函数 一、信号的阻塞 有时候不希望在接到信号时就立即停止当前执行去处理信号同时也不希望忽略该信号而是延时一段时间去调用信号处理函数。这种情况可以通过阻塞信号实现。 信号的阻塞概念信号的”阻塞“是一个开关动作指的是阻止信号被处理但不是阻止信号产生。 信号的状态 信号递达Delivery 实际信号执行的处理过程(3种状态忽略执行默认动作捕获)、信号未决Pending从产生到递达之间的状态。 二、信号集操作函数 sigset_t set; 自定义信号集。 是一个32bit 64bit 128bit的数组。sigemptyset(sigset_t *set); 清空信号集sigfillset(sigset_t *set); 全部置1sigaddset(sigset_t *set, int signum); 将一个信号添加到集合中sigdelset(sigset_t *set, int signum); 将一个信号从集合中移除sigismemberconst sigset_t *setint signum); 判断一个信号是否在集合中。三、sigprocmask函数 #include signal.h int sigprocmask( int how, const sigset_t *restrict set, sigset_t *restrict oset );返回值若成功则返回0若出错则返回-1首先若oset是非空指针那么进程的当前信号屏蔽字通过oset返回。其次若set是一个非空指针则参数how指示如何修改当前信号屏蔽字。how可选用的值注意不能阻塞SIGKILL和SIGSTOP信号SIG_BLOCK 把参数set中的信号添加到信号屏蔽字中 SIG_UNBLOCK 从信号屏蔽字中删除参数set中的信号 SIG_SETMASK 把信号屏蔽字设置为参数set中的信号#includestdio.h #includestdlib.h #includesignal.h #includeunistd.hvoid handle(int sig) {printf(I get the sig %d\n,sig); }int main() {struct sigaction act;act.sa_handler handle;act.sa_flags 0; sigemptyset(act.sa_mask);sigaction(SIGINT,act,NULL);sigset_t set;sigemptyset(set);sigaddset(set,SIGINT);sigprocmask(SIG_BLOCK,set,NULL);sleep(5);sigprocmask(SIG_UNBLOCK,set,NULL);while(1){sleep(1);}return 0; }这段代码注册了一个信号处理函数 handle() 来处理 SIGINT 信号。 然后它创建了一个 sigset_t 类型的信号集 set并将 SIGINT 添加到这个信号集中。接着通过 sigprocmask(SIG_BLOCK, set, NULL) 调用程序阻塞了 SIGINT 信号。 这意味着在这个代码块中SIGINT 信号将被暂时屏蔽不会触发信号处理函数。随后程序调用 sleep(5) 函数来暂停执行 5 秒钟。在此期间由于 SIGINT 被阻塞即使用户发送 SIGINT 信号通常是通过按下 CtrlC信号处理函数 handle() 也不会执行。然后通过 sigprocmask(SIG_UNBLOCK, set, NULL) 调用解除了对 SIGINT 信号的阻塞。最后程序进入一个无限循环每次循环调用 sleep(1) 函数来保持进程处于活动状态。 运行结果 linuxlinux:/mnt/hgfs/linuxshare/linux_code/message2$ ./sigmask_new_t ^C^C^C^C^C^C^CI get the sig 2 ^CI get the sig 2 ^CI get the sig 2 ^CI get the sig 2 ^CI get the sig 2 ^CI get the sig 2 ^CI get the sig 2 ^CI get the sig 2 ^CI get the sig 2 ^CI get the sig 2 ^CI get the sig 2 ^CI get the sig 2 ^CI get the sig 2 ^CI get the sig 2 ^\Quit (core dumped)四、pause函数  调用该函数可以造成进程主动挂起等待信号唤醒。 调用该系统调用的进程将处于阻塞状态(主动放弃cpu) 直到有信号递达将其唤醒。int pause(void); 返回值-1 并设置errno为EINTR pause() 函数是一个系统调用它的作用是使当前进程挂起直到收到一个信号为止。 在收到信号之前pause() 函数会一直阻塞当前进程。 一旦收到信号pause() 函数会返回并且不会执行任何其他代码直接返回到信号处理函数如果有的话或者程序的主体部分。 如下代码中pause() 函数用于等待SIGINT信号的到来。 一旦收到SIGINT信号通常由用户在终端上按下CtrlC触发pause() 函数会返回然后程序会执行信号处理函数handle()。 #includestdio.h #includestdlib.h #includesignal.h #includeunistd.hvoid handle(int sig) {printf(I get the sig %d\n,sig); }int main() {struct sigaction act;act.sa_handler handle;act.sa_flags 0;sigemptyset(act.sa_mask);sigaction(SIGINT,act,NULL);pause();printf(after pause\n);while(1){sleep(1);}return 0; }注意第一次CTRLC会调用handle回调函数且打印after pause但是第二次CTRLC后就不会打印after pause。 linuxlinux:/mnt/hgfs/linuxshare/linux_code/message2$ ./pause_t ^CI get the sig 2 after pause ^CI get the sig 2 ^CI get the sig 2 ^CI get the sig 2 ^CI get the sig 2 ^CI get the sig 2 ^\Quit (core dumped) linuxlinux:/mnt/hgfs/linuxshare/linux_code/message2$ 我们用一个测试程序测试一下 #includestdio.h #includestdlib.h #includesignal.h #includeunistd.hvoid handle(int sig) {printf(I get the sig %d\n,sig); }int main() {struct sigaction act;act.sa_handler handle;act.sa_flags 0;sigemptyset(act.sa_mask);sigaction(SIGINT,act,NULL);pause();printf(after pause\n);while(1){printf(test\n);sleep(1);printf(sleep\n);}return 0; }运行结果 linuxlinux:/mnt/hgfs/linuxshare/linux_code/message2$ ./pause_t ^CI get the sig 2 after pause test sleep test sleep test sleep test sleep test sleep test sleep test ^CI get the sig 2 sleep test sleep test sleep test ^CI get the sig 2 sleep test ^CI get the sig 2 sleep test sleep test ^\Quit (core dumped)可以发现当我用CTRLC接着运行之后程序就运行到while(1)里了当我再CTRLC因为信号捕获的关系才会打印句柄里的语句I get the sig 2。 而对于如下代码 每次CTRLC都会触发mytask中的语句和handle句柄中的打印语句。 #includestdio.h #includestdlib.h #includesignal.h #includeunistd.hvoid handle(int sig) {printf(I get the sig %d\n,sig); }void mytask() {printf(woshigedashabi\n); }int main() {struct sigaction act;act.sa_handler handle;act.sa_flags 0;sigemptyset(act.sa_mask);sigaction(SIGINT,act,NULL);pause();printf(after pause1\n);while(1){mytask();pause();}printf(after pasue2\n);while(1){sleep(1);}return 0; } 运行结果 linuxlinux:/mnt/hgfs/linuxshare/linux_code/message2$ ./test ^CI get the sig 2 after pause1 woshigedashabi ^CI get the sig 2 woshigedashabi ^CI get the sig 2 woshigedashabi ^CI get the sig 2 woshigedashabi ^CI get the sig 2 woshigedashabi ^CI get the sig 2 woshigedashabi ^\Quit (core dumped)对代码进行一定的修改后 #includestdio.h #includestdlib.h #includesignal.h #includeunistd.hvoid handle(int sig) {printf(I get the sig %d\n,sig); }void mytask() {printf(My task start\n);sleep(3);printf(My task end\n); }int main() {struct sigaction act;act.sa_handler handle;act.sa_flags 0;sigemptyset(act.sa_mask);sigaction(SIGINT,act,NULL);sigaction(SIGHUP,act,NULL);sigset_t set;sigaddset(set,SIGHUP);sigaddset(set,SIGINT);pause();printf(after pause1\n);while(1){sigprocmask(SIG_BLOCK,set,NULL);mytask();sigprocmask(SIG_UNBLOCK,set,NULL);pause();} /* while(1){mytask();pause();}*/printf(after pasue2\n);return 0; } 运行结果 第一次CTRLC触发打印完after pause1程序进入while(1)循环在5s内再按下CTRLC会被堵塞直达sigprocmask(SIG_UNBLOCK,set,NULL);只要在5s内按下了CTRLC就会信号捕获打印handle中的语句且这个时候因为pause()再按下CTRLC会再次运行mytask()。 linuxlinux:/mnt/hgfs/linuxshare/linux_code/message2$ ./test ^CI get the sig 2 after pause1 My task start ^CMy task end I get the sig 2 ^CI get the sig 2 My task start My task end ^CI get the sig 2 My task start My task end ^CI get the sig 2 My task start ^C^C^C^C^CMy task end I get the sig 2 ^CI get the sig 2 My task start My task end ^CI get the sig 2 My task start ^C^C^C^C^C^C^C^CMy task end I get the sig 2 ^CI get the sig 2 My task start ^C^\Quit (core dumped)如果上述代码去掉pause(),则输出结果为则会一直运行mytest只是CTRLC触发运行了handle中的打印语句。 linuxlinux:/mnt/hgfs/linuxshare/linux_code/message2$ gcc -o test pause_t_new.c linuxlinux:/mnt/hgfs/linuxshare/linux_code/message2$ ./test ^CI get the sig 2 after pause1 My task start My task end My task start My task end My task start My task end My task start ^CMy task end I get the sig 2 My task start ^C^C^C^C^C^C^C^C^C^CMy task end I get the sig 2 My task start My task end My task start My task end My task start My task end My task start My task end My task start ^\Quit (core dumped)五、sigsuspend函数 int sigsuspend(const sigset_t *sigmask); 功能将进程的屏蔽字替换为由参数sigmask给出的信号集然后挂起进程的执行 参数sigmask希望屏蔽的信号 对比如下代码 运行结果的区别 左边运行结果表示你在阻塞期间按下CTRLC只会捕获一次信号但是不会认为你需要再执行一次mytask()。只有当运行了sigprocmask(SIG_UNBLOCK,set,NULL)才有效。 但是右边在任务中间会接收任务这是因为sigsuspend函数set2是一个空的信号集。sigsuspend(set2); 函数允许程序在任务执行的过程中等待信号一旦收到信号程序就会立即响应。 详细代码如下:  #includestdio.h #includestdlib.h #includesignal.h #includeunistd.hvoid handle(int sig) {printf(I get the sig %d\n,sig); }void mytask() {printf(My task start\n);sleep(3);printf(My task end\n); }int main() {struct sigaction act;act.sa_handler handle;act.sa_flags 0;sigemptyset(act.sa_mask);sigaction(SIGINT,act,NULL);sigaction(SIGHUP,act,NULL);sigset_t set;sigset_t set2;sigemptyset(set2);sigaddset(set,SIGHUP);sigaddset(set,SIGINT);pause();printf(after pause1\n);while(1){sigprocmask(SIG_BLOCK,set,NULL);mytask();sigsuspend(set2);}printf(after pasue2\n);return 0; }先注册了两个信号处理函数 handle分别用于处理 SIGINT 和 SIGHUP 信号 。然后定义了一个自定义函数 mytask()它模拟了一个长时间运行的任务。在 main() 函数中创建了两个信号集 set 和 set2set 中包含了 SIGHUP 和 SIGINT 信号。 然后调用了 pause() 函数来挂起进程直到收到信号为止。接着进入一个无限循环在循环中先将 set 中的信号阻塞然后执行 mytask() 函数模拟长时间运行的任务。 然后使用 sigsuspend() 函数挂起进程等待收到 set2 中的信号。
http://www.zqtcl.cn/news/268985/

相关文章:

  • 橙色企业网站源码建设工程投标文件在哪个网站有发布
  • 服务器可以做网站吗深圳高端网站建设创新
  • 企业平台网站建设方案大连网络广告
  • 如何给网站做宣传新手怎么建立自己网站
  • 酒店和网站对接如何做开发网站那个好
  • 北京建设信源咨询有限公司网站快对小程序入口
  • 湖北人工智能建站系统软件城乡建设官网
  • 广东模板建站平台设计网站
  • 晋江市住房和城乡建设网站二进制可以做网站是吗
  • 企业网站优化的方式网站开发 -(广告)
  • 素材解析网站搭建wordpress 提问
  • 域名解析网站安卓android系统下载
  • 相亲网站做推广的照片是谁广告优化师前景
  • 营销导向的网站建设的主要流程陕煤建设集团网站
  • 电商网站销售数据分析网页美工设计实训报告
  • 百度新网站收录wordpress免刷新插件
  • 如何做好网站外链c#+开发网站开发
  • 展示型网站报价网站目录创建下载链接
  • cloudflare做侵权网站建设网站需要什么知识
  • 软装设计公司名称怎样给网站做优化
  • 如何判断网站是用什么程序做的云南网站建设公司
  • 清远市建设局官方网站软件开发工程师发展前景
  • 韩国做hh网站图片转链接生成器在线
  • 有凡客模版怎么建设网站百度网盘在线观看资源
  • 网站关键字统计龙岩龙硿洞
  • 成都哪个网站建设比较好建设工程交易服务中心
  • 怎么做好网站推广小笨鸟跨境电商平台
  • 建立一个网站需要多少钱?制作ppt模板的软件
  • 百度 手机网站 友好性青岛谷歌优化
  • 免费的200m网站空间谷歌建站哪家好