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

做做网站入口永久免费wap自助建站

做做网站入口,永久免费wap自助建站,淄川网站建设yx718,深圳市出行政策最新前言: signal 是一种通信机制,可以跨进程发送,可以同进程跨线程发送,可以不同进程向指定线程发送。 信号的创建有两套api,一个是signal,一个是sigaction,signal缺陷很多,比如没有提…

前言:

signal 是一种通信机制,可以跨进程发送,可以同进程跨线程发送,可以不同进程向指定线程发送。

信号的创建有两套api,一个是signal,一个是sigaction,signal缺陷很多,比如没有提供触发后自动再次设置处理信号处理策略,这会导致连续触发的两个信号,一个进入了期待的信号处理流程,另外一个则进入了默认的信号处理流程。

信号的递送和接受处理是异步的,即信号发送者不会因为信号接收者使用了阻塞信号处理函数而被阻塞住。但是信号的递送可能会出现阻塞,这个阻塞发生在信号发送者把信号送入内核的信号队列中(需要从代码层面验证)。

信号处理方式:

信号的处理有三种方式:默认,忽略,信号处理函数,可以在使用 sigaction 创建信号处理策略时指定。

默认分为如下几种:

       Term   Default action is to terminate the process.

       Ign    Default action is to ignore the signal.

       Core   Default action is to terminate the process and dump core (see core(5)).

       Stop   Default action is to stop the process.

       Cont   Default action is to continue the process if it is currently stopped.

不同信号的默认行为如下:

       Signal      Standard   Action   Comment  ────────────────────────────────────────────────────────────────────────
       SIGABRT      P1990      Core    Abort signal from abort(3)
       SIGALRM      P1990      Term    Timer signal from alarm(2)
       SIGBUS       P2001      Core    Bus error (bad memory access)
       SIGCHLD      P1990      Ign     Child stopped or terminated
       SIGCLD         -        Ign     A synonym for SIGCHLD
       SIGCONT      P1990      Cont    Continue if stopped
       SIGEMT         -        Term    Emulator trap
       SIGFPE       P1990      Core    Floating-point exception
       SIGHUP       P1990      Term    Hangup detected on controlling terminal or death of controlling process
       SIGILL       P1990      Core    Illegal Instruction
       SIGINFO        -                A synonym for SIGPWR
       SIGINT       P1990      Term    Interrupt from keyboard
       SIGIO          -        Term    I/O now possible (4.2BSD)
       SIGIOT         -        Core    IOT trap. A synonym for SIGABRT
       SIGKILL      P1990      Term    Kill signal
       SIGLOST        -        Term    File lock lost (unused)
       SIGPIPE      P1990      Term    Broken pipe: write to pipe with no readers; see pipe(7)
       SIGPOLL      P2001      Term    Pollable event (Sys V). Synonym for SIGIO
       SIGPROF      P2001      Term    Profiling timer expired
       SIGPWR         -        Term    Power failure (System V)
       SIGQUIT      P1990      Core    Quit from keyboard
       SIGSEGV      P1990      Core    Invalid memory reference
       SIGSTKFLT      -        Term    Stack fault on coprocessor (unused)
       SIGSTOP      P1990      Stop    Stop process
       SIGTSTP      P1990      Stop    Stop typed at terminal
       SIGSYS       P2001      Core    Bad system call (SVr4); see also seccomp(2)
       SIGTERM      P1990      Term    Termination signal
       SIGTRAP      P2001      Core    Trace/breakpoint trap
       SIGTTIN      P1990      Stop    Terminal input for background process
       SIGTTOU      P1990      Stop    Terminal output for background process
       SIGUNUSED      -        Core    Synonymous with SIGSYS
       SIGURG       P2001      Ign     Urgent condition on socket (4.2BSD)
       SIGUSR1      P1990      Term    User-defined signal 1
       SIGUSR2      P1990      Term    User-defined signal 2
       SIGVTALRM    P2001      Term    Virtual alarm clock (4.2BSD)
       SIGXCPU      P2001      Core    CPU time limit exceeded (4.2BSD); see setrlimit(2)
       SIGXFSZ      P2001      Core    File size limit exceeded (4.2BSD); see setrlimit(2)
       SIGWINCH       -        Ign     Window resize signal (4.3BSD, Sun)

通过pthread_kill进行线程间信号传递

信号可以时线程级别的,可以通过 pthread_kill 给同进程的其他线程发信号,可以通过 tgkill 给其他进程的指定线程发信号,通过 raise 可以给当前线程发信号。

Demo:

#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>pthread_t newtid;void usrHandler(int signum,siginfo_t *info,void *ucontext)
{printf("[SIGUSR1 handler thread] tid -> %d , ready to send SIGUSR2 to child thread \n",gettid());pthread_kill(newtid,SIGUSR2);printf("[SIGUSR1 handler thread] tid -> %d , SIGUSR2 sent out \n",gettid());
}void usrHandler2(int signum,siginfo_t *info,void *ucontext)
{printf("[SIGUSR2 handler thread] tid -> %d , %d , SIGUSR2 received \n",gettid(),signum);
}void* threadRoutine(void* arg)
{pthread_t tid = *(pthread_t*)arg;printf("[new thread] tid -> %d , arg -> %d \n",gettid(),tid);// set SIGSUR2sigset_t mask2;sigemptyset(&mask2);struct sigaction act2;memset(&act2,0x0,sizeof(struct sigaction));struct sigaction oldact2;memset(&oldact2,0x0,sizeof(struct sigaction));act2.sa_sigaction = usrHandler2;act2.sa_mask = mask2;act2.sa_flags = 0; //no flag is setsigaction(SIGUSR2, &act2, &oldact2);while(1) {;}
}int main(int argc,char** argv)
{printf("[main thread] tid -> %d \n",gettid());
// set SIGUSR1sigset_t mask;sigemptyset(&mask);struct sigaction act;memset(&act,0x0,sizeof(struct sigaction));struct sigaction oldact;memset(&oldact,0x0,sizeof(struct sigaction));act.sa_sigaction = usrHandler;act.sa_mask = mask;act.sa_flags = 0; //no flag is setsigaction(SIGUSR1, &act, &oldact);pthread_t maintid = gettid();pthread_create(&newtid,NULL,&threadRoutine,(void*)&maintid);sleep(2);while(1) {raise(SIGUSR1);sleep(2);}
}

上面的例子中,主线程会循环给自己发信号 SIGUSR1 ,在信号处理函数中会给子线程发送 SIGUSR2。

当子线程通过pthread_kill给主线程发送信号时,会产生 SIGSEGV, 具体原因不明,如果有类似情况,可以参考如下:

pthread_kill引发的争论 - 简书最近提测的一段代码中有一个,遇到一个诡异的bug,总是崩溃在pthread_kill这个函数上,并且不是每次比现。调用逻辑大致如下,利用pthread_kill判断一个线程是...icon-default.png?t=N7T8https://www.jianshu.com/p/756240e837dd

ps:可以通过 pause 挂起当前线程,直到等到一个信号为止;可以通过 sigsuspend 挂起当前线程,直到等到某些信号为止。

管理mask

每个线程都有自己的mask,可以通过pthread_sigmask来管理。

通过mask进行信号block

使用sigaction创建信号处理策略时指定mask。被列入mask集合中的signal会被阻塞,直到阻塞信号的动作结束,这些信号会被继续投递到信号处理逻辑中。

比如通过sigaction 指定 “当发生SIGUSR1的时候,阻塞所有SIGUSR2”,那么如果 SIGUSR1 的信号处理函数耗时较长,那么 SIGUSR2 会一直等到 SIGUSR1 的处理函数走完才会被递送给相应的进程/线程 以触发 默认动作/忽略动作/信号处理函数。

Demo:

#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>#define USRSIG SIGUSR1
#define RTSIG SIGRTMIN+8void usrHandler(int signum,siginfo_t *info,void *ucontext)
{printf("%d , SIGUSR1 reveived \n",signum);printf("Sender pid[%d] , User cost time [%ld] , System cost time [%ld] , si_code [%d] \n ",info->si_pid,info->si_utime,info->si_stime,info->si_code);sleep(15);printf("exit usrHandler\n");
}void usrHandler2(int signum,siginfo_t *info,void *ucontext)
{printf("%d , SIGUSR2 received \n",signum);
}int main(int argc,char** argv)
{
// set SIGUSR1sigset_t mask;sigemptyset(&mask);sigaddset(&mask,SIGUSR2); // SIGUSR2 will be blocked when usrHandler is executing. After return from  usrHandler, SIGUSR2 will// be received adn surHandler2 will be executed.struct sigaction act;memset(&act,0x0,sizeof(struct sigaction));struct sigaction oldact;memset(&oldact,0x0,sizeof(struct sigaction));act.sa_sigaction = usrHandler;act.sa_mask = mask;act.sa_flags = 0; //no flag is setsigaction(SIGUSR1, &act, &oldact);// set SIGSUR2sigset_t mask2;sigemptyset(&mask2);struct sigaction act2;memset(&act2,0x0,sizeof(struct sigaction));struct sigaction oldact2;memset(&oldact2,0x0,sizeof(struct sigaction));act2.sa_sigaction = usrHandler2;act2.sa_mask = mask2;act2.sa_flags = 0; //no flag is setsigaction(SIGUSR2, &act2, &oldact2);while(1) {;}
}

当通过 kill 连续发送 SIGUSR1 和 SIGUSR2 给上面的例子时,会发现执行流程会卡在 SIGUSR1 处理函数的 sleep ,这是因为针对 SIGUSR1 设置了block SIGUSR2,这会导致 SIGUSR2 无法中断 SIGUSR1。


文章转载自:
http://dinncoacouasm.knnc.cn
http://dinncofogbound.knnc.cn
http://dinncoelaterium.knnc.cn
http://dinncoanticatarrhal.knnc.cn
http://dinncosnooper.knnc.cn
http://dinncorainbox.knnc.cn
http://dinncohaphazardry.knnc.cn
http://dinncothorax.knnc.cn
http://dinncounlonely.knnc.cn
http://dinncotithonus.knnc.cn
http://dinnconawa.knnc.cn
http://dinncorendzina.knnc.cn
http://dinncochangemaker.knnc.cn
http://dinncodominative.knnc.cn
http://dinncofuzzbox.knnc.cn
http://dinncomultiplicative.knnc.cn
http://dinncokeerect.knnc.cn
http://dinncodiffer.knnc.cn
http://dinncozigzagged.knnc.cn
http://dinncopar.knnc.cn
http://dinncorapidity.knnc.cn
http://dinncoastomatous.knnc.cn
http://dinncoorthopraxis.knnc.cn
http://dinncofilipina.knnc.cn
http://dinncosierozem.knnc.cn
http://dinncobella.knnc.cn
http://dinncozincode.knnc.cn
http://dinncosaponify.knnc.cn
http://dinncoflippant.knnc.cn
http://dinncolockfast.knnc.cn
http://dinncomagnetooptic.knnc.cn
http://dinncojonah.knnc.cn
http://dinncoknub.knnc.cn
http://dinncopneumobacillus.knnc.cn
http://dinncosnipping.knnc.cn
http://dinncocommunist.knnc.cn
http://dinncounwittingly.knnc.cn
http://dinncoslaughterhouse.knnc.cn
http://dinncouglification.knnc.cn
http://dinncosyllogise.knnc.cn
http://dinncolegalization.knnc.cn
http://dinncophilopena.knnc.cn
http://dinncoligamentous.knnc.cn
http://dinncovaseline.knnc.cn
http://dinncounwetted.knnc.cn
http://dinncouseless.knnc.cn
http://dinncofestoon.knnc.cn
http://dinncoclementina.knnc.cn
http://dinncothickety.knnc.cn
http://dinncotipstaff.knnc.cn
http://dinncoevulsion.knnc.cn
http://dinncoanakinesis.knnc.cn
http://dinncohippomaniac.knnc.cn
http://dinncoeffluence.knnc.cn
http://dinncopishpek.knnc.cn
http://dinncoblazonment.knnc.cn
http://dinncowholescale.knnc.cn
http://dinncounlistening.knnc.cn
http://dinncotriploid.knnc.cn
http://dinncopultaceous.knnc.cn
http://dinncosleeveboard.knnc.cn
http://dinncoradiography.knnc.cn
http://dinncoperiplast.knnc.cn
http://dinncosambal.knnc.cn
http://dinncoandrogynous.knnc.cn
http://dinncoflammable.knnc.cn
http://dinncointeroperability.knnc.cn
http://dinncosuperbly.knnc.cn
http://dinncoglucoside.knnc.cn
http://dinncogaruda.knnc.cn
http://dinncobarren.knnc.cn
http://dinncofeuilletonist.knnc.cn
http://dinncoburglarproof.knnc.cn
http://dinncocobaltammine.knnc.cn
http://dinncodiva.knnc.cn
http://dinncodevitalize.knnc.cn
http://dinncocanter.knnc.cn
http://dinncoanethole.knnc.cn
http://dinncoungated.knnc.cn
http://dinncomegajet.knnc.cn
http://dinncodarch.knnc.cn
http://dinncorog.knnc.cn
http://dinncodeanery.knnc.cn
http://dinncolombardy.knnc.cn
http://dinncolakeshore.knnc.cn
http://dinncorifleman.knnc.cn
http://dinncofurcate.knnc.cn
http://dinncoarkhangelsk.knnc.cn
http://dinncogarret.knnc.cn
http://dinncokcia.knnc.cn
http://dinncosunniness.knnc.cn
http://dinncolaminose.knnc.cn
http://dinncohyperostosis.knnc.cn
http://dinncovalued.knnc.cn
http://dinncowheedle.knnc.cn
http://dinncotrifecta.knnc.cn
http://dinncotrocar.knnc.cn
http://dinncoeuphrasy.knnc.cn
http://dinncohemiola.knnc.cn
http://dinncocollimator.knnc.cn
http://www.dinnco.com/news/104817.html

相关文章:

  • 网站如何做触屏滑动网络推广网站推广方法
  • 网站开发的毕设开题报告排名前50名免费的网站
  • 齐家网装修公司口碑阳山网站seo
  • 吾爱源码seo快速排名软件网站
  • 快速优化网站排名搜索网站建设深圳公司
  • 服装网站开发的意义百度网址大全手机版
  • 做加盟的网站建设手机版谷歌浏览器入口
  • 辽宁建设培训网站兰州网络推广优化服务
  • 龙华专业做网站公司域名估价
  • 网络公司发生网站建设费分录seo咨询推广找推推蛙
  • 广西网站推广百度联盟怎么加入赚钱
  • 网站建设服务宗旨整站优化代理
  • 网站模板下载模板下载安装镇江百度推广
  • 泉州晋江网站建设网络营销推广是做什么的
  • ppt模板免费下载网站哪个好河南整站百度快照优化
  • 小规模企业做网站给大家科普一下b站推广网站
  • 做的比较炫的网站优化大师怎么卸载
  • 医院网站优化策划舆情监测
  • 本地网站建设开发信息大全网站制作流程
  • php简易企业网站源码百度收录规则
  • 前端只是做网站吗apple私人免费网站怎么下载
  • 网站结构模板如何进行品牌营销
  • 日常网站维护怎么做南宁推广公司
  • 安徽省住房和城乡建设厅官方网站宁波网络推广方法
  • 网站建设企业营销软文素材
  • 网站建设维护什么意思抖音信息流广告怎么投放
  • 做公司网站客户群体怎么找seo面试常见问题及答案
  • 魏县网站制作品牌策划设计
  • 瓜果蔬菜做的好的电商网站百度快照是干嘛的
  • 南宁 网站开发被代运营骗了去哪投诉