zoukankan      html  css  js  c++  java
  • linux 中断和终端测试程序

    该程序测试相应终端的 ctrl - c 中断和终端的控制等

    /**
    * testsignal.c
    */
    #include
    <stdio.h>
    #include
    <signal.h>
    #include
    <termios.h>
    #include
    <fcntl.h>
    void ctrl_c_hander();
    void set_cr_mode();
    void reset_mode(int flag);
    int main(int ac,char *av[])
    {
    int counts=0,i=0;
    reset_mode(
    0);
    set_cr_mode();
    signal(SIGINT,ctrl_c_hander);
    while(1)
    {
            counts
    ++;
            printf(
    "loop");
            
    for(i=counts;i>0;i--)
                    printf(
    "!");
            printf(
    "\n");
            
    if(ac==2)
            {
                    
    //if(atoi(av[1][0])==counts)
                    
    //      exit(1);
            }
            sleep(
    1);
    }
    reset_mode(
    1);
    }
    void ctrl_c_hander()
    {
    puts(
    "do you want exit.(y/n)\n");
    if(tolower(getchar())=='y')
            {
            reset_mode(
    1);
            exit(
    1);
            }
    return;
    }
    void set_cr_mode()
    {
    struct termios setting;
    tcgetattr(
    0,&setting);
    setting.c_lflag 
    &= ~ICANON;
    setting.c_lflag 
    &= ~ECHO;
    setting.c_cc[VMIN]
    =1;
    tcsetattr(
    0,TCSANOW,&setting);
    }
    void reset_mode(int flag)
    {
    static struct termios info;
    //static int    termflag;
    static int    does;
    if(flag==0)
            {
            tcgetattr(
    0,&info);
    //      termflag=fcntl(0,F_GETFL);
            does=1;
            }
    else if(does==1)
            {
    //      fcntl(0,F_SETFL,termflag);
            tcsetattr(0,TCSANOW,&info);
            }
    }
  • 相关阅读:
    C++中的指针和数组
    windows系统下JDK1.6环境变量配置
    Java Reflection (JAVA反射)
    转载:cin深入分析(下) – cin的错误处理
    OpenGL总结
    OpenGL纹理
    c/C++内存分配
    转载:cin深入分析(上) – cin输入操作处理
    c++中string的用法
    OpenGL颜色
  • 原文地址:https://www.cnblogs.com/ringwang/p/1429871.html
Copyright © 2011-2022 走看看