NAME(名稱)
clear - 清除終端屏幕
SYNOPSIS(總覽)
clear
DESCRIPTION(描述)
- clear可以在允許的情況下清屏.
- 它會在環境變量中查找終端的類型, 然后到terminfo數據庫中找出清屏的方法.
《man手冊》
#include <stdio.h>int clear_main(int argc, char **argv) {/* This prints the clear screen and move cursor to top-left corner control* characters for VT100 terminals. This means it will not work on* non-VT100 compliant terminals, namely Windows' cmd.exe, but should* work on anything unix-y. */fputs("\x1b[2J\x1b[H", stdout);
return 0;
fflush(stdout):清空輸出緩沖區,并把緩沖區內容輸出。
“\x1b[2J”,//清除整個屏幕,行屬性變成單寬單高,光標位置不變
“\x1b[H”,//光標移動
代碼測試
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>int main()
{int pid = fork();if(pid > 0){while(1){printf(" father \n");sleep(1);}}else if(pid == 0){ while(1){fflush(stdout);fputs("\x1b[2J\x1b[H", stdout);sleep(5);}}else{perror("fork");}
}
動圖展示結果
系統默認是用CTRl + Z 來暫停進程,但是我們把SIGTSTP信號截斷,改成CTRl + L 會有什么效果呢?
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>void sigHandler(int sig)
{ char * p[8] ;printf("ctrl+z\n");p[0] = "clear";execvp(p[0],p);
}int main()
{while(1){if(signal(SIGTSTP,sigHandler) == SIG_ERR)perror("signal"),exit(0);}
}
實驗結果
這個時候程序退出了。為什么呢