图形
终端控制代码 (ANSI/VT100) 简介
终端 (控制) 代码是用来控制终端的特殊命令,它可以改变颜色和光标的位置,实现那些无法被程序本身完成的操作。
参考文章:终端控制代码 (ANSI/VT100) Terminal Codes 简介 (转载翻译)_终端代码-CSDN博客
#include <stdlib.h>
#include <stdio.h>int main()
{printf("\033[2J\033[1;1H\033[31;4mhello\n\033[0m");return 0;
}
输入设备
#include <stdlib.h>
#include <stdio.h>
#include <termios.h> int main()
{int ch;struct termios new; //用于去掉回显tcgetattr(0,&new);new.c_lflag = new.c_lflag & ~(ICANON |ECHO);new.c_cc[VMIN] = 1; //接受最小长度new.c_cc[VTIME] = 0;//时间接受tcsetattr(0,TCSANOW,&new);while(1){ch = getchar();if(ch == 'Q')break;printf("%x ",ch);// 按照16进制进行输出,输入a,,十进制是97,十六进制输出65, A输出41 回车(LF)输出afflush(NULL);}return 0;
}
执行编译后的代码退出后,发现终端命令不显示,需要输入(reset命令)重启终端设备
#include <stdlib.h>
#include <stdio.h>
#include <termios.h> int main()
{int ch;struct termios new,old; //用于去掉回显tcgetattr(0,&new);tcgetattr(0,&old);new.c_lflag = new.c_lflag & ~(ICANON |ECHO);new.c_cc[VMIN] = 1; //接受最小长度new.c_cc[VTIME] = 0;//时间接受tcsetattr(0,TCSANOW,&new);while(1){ch = getchar();if(ch == 'q')break;printf("%x ",ch);// 按照16进制进行输出,输入a,,十进制是97,十六进制输出65, A输出41 回车(LF)输出afflush(NULL);}printf("\n");tcsetattr(0,TCSANOW,&old);//恢复模式return 0;
}
并发
利用signal信号中的SIGALRM来实现
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>#include <signal.h>void int_handler(int code)
{printf("Get SIGINT\n");//code = 2printf("code = %d \n",code);//code = 2
}void alrm_handler(int code) //code显示对应的值,用kill -l命令查看
{alarm(1);// 每次收到alarm在调用信号printf("Get SIGALRM \n");//code = 14
}
int main()
{signal(SIGINT,int_handler);signal(SIGALRM,alrm_handler);alarm(1);//隔1秒发出SIGALRM信号while(1){int ch;ch = getchar();printf("ch = %x \n",ch);fflush(NULL);
// printf("while(1)\n");
// sleep(1);}return 0;
}