
個人在做Unix-Linux 編程實踐一書時,做到這里就懵逼,我嘗試利用stat函式去讀取/dev/pts/19(ps命令中顯示的終端名字),獲取atim以及mtim,以及/dev/tty7中的atim以及mtim,
我的思路是這樣的
設定一個itimer,一開始先讀取/dev/pts/19的atime,讓程式讀取我所設定的最大時間taccess,設定itimer的倒計時為taccess秒,并且在超時后,觸發一個SIG_ALRM信號,喚醒信號服務程式,再一次讀取atime的值,如果沒有變化,程式退出,如果有變化,則服務程式回傳。
ps:這樣計時確實不夠精確
但我嘗試服務程式只在計時器跑完后列印tty的atime,發現tty的atime 并不會有變化,即使我在bash中輸入命令也不會重繪,陷入苦惱中,難道是我讀的地方不對?話不多說,先上代碼。
#include <stdio.h>
#include <curses.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <aio.h>
#include <sys/stat.h>
#include <stdlib.h>
void sendKill( int s );
int set_ticker( int n_msecs );
int Tcount = 0;
int pid = 0;
int Taccess = 0;
int times = 0;
struct stat file_info;
//set a timer ,when it drained, program will send a sigkill signal to the pid you want
int main( int ac, char** av )
{
Tcount = atoi( av[1] );
pid = atoi( av[2] );
printf("Tcount:%d\t", Tcount);
printf("pid:%d\n", pid);
if(stat("/dev/pts/19", &file_info))
perror("stat");
Taccess = file_info.st_atim.tv_nsec;
//set itimer
set_ticker(5000);
alarm(5);
signal( SIGALRM, sendKill );
printf("Taccess first%d\n", Taccess);
while (1) {
pause();
}
}
void sendKill( int s )
{
signal( SIGALRM, sendKill );
int tmp;
if(stat("/dev/pts/19", &file_info))
perror("stat");
tmp = file_info.st_mtim.tv_nsec;
printf("\n%d\n", tmp);
times++;
if(times > 3)
exit(1);
}
uj5u.com熱心網友回復:

即使我重繪終端時間仍然沒有變化
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/34420.html
標籤:應用程序開發區
