zoukankan      html  css  js  c++  java
  • Linux Linux程序练习七

    题目:实现两个程序mysignal、mycontrl,mycontrl给mysignal发送SIGINT信号,控制mysignal是否在屏幕打印“hello”字符串。
    //捕捉信号
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <errno.h>
    
    #include <unistd.h>
    #include <signal.h>
    
    int flag=0;
    
    void catch_sig(int sign)
    {
        switch(sign)
        {
        case SIGINT:
            flag=flag==0?1:0;
            break;
        case SIGALRM:
            exit(0);
        }
    }
    
    int mysignal(int sign,void (*func)(int))
    {
        struct sigaction act,oact;
        act.sa_handler=func;
        sigemptyset(&act.sa_mask);
        act.sa_flags=0;
        return sigaction(sign,&act,&oact);
    }
    
    
    int main(int arg,char *args[])
    {
        //注册信号
        mysignal(SIGINT,catch_sig);
        mysignal(SIGALRM,catch_sig);
        while(1)
        {
            if(flag==1)
                printf("hello
    ");
            sleep(1);
        }
        return 0;
    }
    //发送信号
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <errno.h>
    
    #include <unistd.h>
    #include <sys/types.h>
    #include <signal.h>
    
    int main(int arg,char * args[])
    {
        if(arg<2)
        {
            printf("请输入一个参数!
    ");
            return -1;
        }
        int resid=0;
        pid_t pid=atoi(args[1]);
        resid=kill(pid,SIGALRM);
        if(resid!=0)
        {
            printf("error message:%s
    ",strerror(errno));
            return -1;
        }
        return 0;
    }
    .SUFFIXES:.c .o
    CC=gcc
    SRCS=mycontrl.c
    OBJS=$(SRCS:.c=.o)
    EXEC=contrl
    
    start:$(OBJS)
        $(CC) -o $(EXEC) $(OBJS)
        @echo "^_^-----OK------^_^"
    .c.o:
        $(CC) -Wall -g -o $@ -c $<
    clean:
        rm -f $(OBJS)
        rm -f $(EXEC)
  • 相关阅读:
    【bzoj1036】【ZJOI2008】树的统计
    AE基础(8)PageLayout属性设置和添加元素
    AE基础(7)布局控件与地图控件关联
    UtilityAction扩展
    UtilityAction
    AE基础(6)数据查询与选择
    NavigationAction
    LayerAction
    AE基础(5)鹰眼功能
    AE基础(4)画几何图形
  • 原文地址:https://www.cnblogs.com/zhanggaofeng/p/5851289.html
Copyright © 2011-2022 走看看