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
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的兼职网,源码+论文答辩+毕业论文+视频演示 随着科学技术的飞速发展,社会的方方面面、各行各业都在努力与现代的先进技术接轨,通过科技手段来提高自身的优势,蜗牛兼职网当然也不能排除在外。蜗牛兼职网是以实际运用为开发背景,运用软件工程原理和开发方法,采用springboot框架构建的一个管理系统。整个开发过程首先对软件系统进行需求分析,得出系统的主要功能。接着对系统进行总体设计和详细设计。总体设计主要包括系统功能设计、系统总体结构设计、系统数据结构设计和系统安全设计等;详细设计主要包括系统数据库访问的实现,主要功能模块的具体实现,模块实现关键代码等。最后对系统进行功能测试,并对测试结果进行分析总结,得出系统中存在的不足及需要改进的地方,为以后的系统维护提供了方便,同时也为今后开发类似系统提供了借鉴和帮助。这种个性化的网上蜗牛兼职网特别注重交互协调与管理的相互配合,激发了管理人员的创造性与主动性,对蜗牛兼职网而言非常有利。 本蜗牛兼职网采用的数据库是MySQL,使用springboot框架开发。在设计过程中,充分保证了系统代码的良好可读性、实用性、易扩展性、通用性、便于后期维护、操作方便以及页面简洁等特点。 功能要求:可以管理首页、个人中心、用户管理、企业管理、兼职信息管理、职位申请管理、留言板管理、系统管理等功能模块。 关键词:蜗牛兼职网,springboot框架 MySQL数据库 Java技术
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值