signal

a signal is an event generated by the UNIX and LINUX systems in response to some condition , upon reveipt of which a process may in turn take some action,.there are so many signals which is included in the header file signal.h like:
  
 SIGABORT-----process abort
    SIGALRM------alarm clock
    SIGHUP-------hangup
    SIGILL-------illegal instruvtion
    SIGINT-------teminal interrupt
    SIGKILL------KILL


if a process receives one of these signals without first arranging to catch it,the process will be terminated immediately.
but how we can inform another process that i have send you an message,so it come to the first function kill():

#include <sygnal.h>
#inlcude <sys/types.h>

int kill(pid_t pid,int sig);



the kill function sends the specified signal,sig,to the process whose identifier is giver by pid.returns 0 on success,but one thing you must pay attention to is that you must own the permission to do so,in another word , the both process must have the same user ID,to check your user ID use the ps-l in terminal, check it wether you have the permission to do so.

what should we do next when we receive a signal send by another process, so you need a function to specify the thing that you should do after you receive it.there are two functions you can use to achieve it but i strongly suggested that you have better use the later one :

#inlcude <sygnal.h>

void(*signal(int sig,void (*func)(int)))(int);

int sigaction(int sig,const struct sigactin * act,struct sigaction * oact);


you may find "oh what's that,so confused",but it's ok,you needn't worry about it the only thing you need to do is to learn how use the second function,the sigaction() function work backgroud ,when the process who take the sigaction() funciton receive a signal defined by the parameter sig,it take the action defined by struct sigaction * act.as for the detail of the struct ,later i will show you an example about how to use it.the sigaction function sets the action associated with the signal sig.if oact is not null,sigaction writes the previous signal action to the location it refers to.if act is null,nothing it will do,if act isn't null,the action for the specified signal is set.

you just need to worry about three parameter of the sigation structer :
    void(*)(int) sa_handler   /* function  , SIG_DFL(restore to the default state), SIG_IGN(ignore the signal)
    sigset_t sa_mask          /*signals to block in sa_handler
    int sa_flags              /*signal action modifiers
the sa_handler define the action the process should in a form of function , or the other two:SIG_DFL,SIG_IGN
the sa_mask more like the submask which can ignore the signal bit which happen to the process
the sa_flags field must be set to contain the value SA_RESETHAND if you want to obtain the behavior you saw earlier with signal.



#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <string.h>

void response();
void ouch(int sig);
void mwaitpid(pid_t pid);
/*NOTE:compile with gnu99 not c99*/
void main(){
        pid_t new_pid;
        new_pid=fork();
        switch(new_pid){
            case -1:{
                perror("fork error\n");
                exit(-1);
            }
            case 0:{
                response();
                break;
            }
            default:{
                printf("main process is ok\n");
                sleep(5);
                mwaitpid(new_pid);
            }
        }
  exit(0);
}

void response(){
    struct sigaction * act;
    act=(struct sigaction *)malloc(sizeof(struct sigaction));
    /*employ the ouch function() when receive the signal sig*/
    act->sa_handler=ouch;
    /*no block signal bit*/
    sigemptyset(&act->sa_mask);
    /*restore ot the default state*/
    act->sa_flags=SA_RESETHAND;
    
    /*store the old action8*/
    struct sigaction * oact;
    oact=(struct sigaction *)malloc(sizeof(struct sigaction));
    memset(oact,0,sizeof(struct sigaction));

    /*apply the function*/
    sigaction(SIGINT,act,oact);
    while(1){
        printf("no receive INT signal\n");
        sleep(1);
    }
}

void ouch(int sig){
    printf("receice an about interrupt order\n");
}

void mwaitpid(pid_t pid){
    int stat_loc;
    while(waitpid(pid,&stat_loc,WNOHANG) == 0){
        if(kill(pid,SIGINT)==-1){
            perror("kill error\n");
            /*not a good idea*/
            exit(0);
        }
        printf("try to kill ...\n");
        sleep(1);
    }
    printf("kill ok\n");
    printf("exitcode is %d\n",WEXITSTATUS(stat_loc));

}















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值