setitimer 定时器

#include <unistd.h>
#include <time.h>
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <curses.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/wait.h>

WINDOW *wtime,*wnumb;
void showtime(int s){
	time_t tt;
	struct tm *t;
	if(s == SIGALRM){
		tt = time(0);
		t = localtime(&tt);
		mvwprintw(wtime,1,1,"%02d:%02d:%02d",t->tm_hour,t->tm_min,t->tm_sec);
		refresh();
		wrefresh(wnumb);
		wrefresh(wtime);
	}
}


int main(){
	struct itimerval val;
	initscr();
	curs_set(0);  //close cursor
	signal(SIGALRM,showtime);
	bzero(&val,sizeof(struct itimerval));
	val.it_value.tv_sec = 0;
	val.it_value.tv_usec = 1;
	val.it_interval.tv_sec = 1;
	wnumb=derwin(stdscr,3,11,(LINES-3)/2,(COLS-11)/2);
	wtime=derwin(stdscr,3,10,0,COLS-10);
	box(wnumb,0,0);
	box(wtime,0,0);
	setitimer(ITIMER_REAL,&val,0);
	refresh();
	wrefresh(wnumb);
	wrefresh(wtime);
	while(1){
		int sum = 0,i;
		for(i = 0;i < 7;i++)
			sum = sum*10 + random()%10;
		mvwprintw(wnumb,1,2,"%07d",sum);
		refresh();
		wrefresh(wnumb);
		wrefresh(wtime);
		usleep(1000000);
	}
	endwin();
	return 1;
}

#include <unistd.h>
#include <time.h>
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <curses.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/wait.h>

WINDOW *wnumb;

int isstop = 0;
void handle(int s){
	if(s == SIGUSR1){
		if(isstop==0)
			isstop = 1;
		else
			isstop = 0;
	}
}
int main(){

	initscr();
	curs_set(0);  //close cursor
	wnumb=derwin(stdscr,3,11,(LINES-3)/2,(COLS-11)/2);
	keypad(wnumb,TRUE);
	keypad(stdscr,TRUE);
	noecho();

	box(wnumb,0,0);
	refresh();
	wrefresh(wnumb);
	if(fork()){
		signal(SIGUSR1,handle);
		while(1){
			if(isstop==1)
			pause();
			int sum = 0,i;
			for(i = 0;i < 7;i++)
				sum = sum*10 + random()%10;
			mvwprintw(wnumb,1,2,"%07d",sum);
			refresh();
			wrefresh(wnumb);
			usleep(1000);
		}
	}else{
		while(1){
			int ch = getch();
			kill(getppid(),SIGUSR1);
		}

	}
	
	endwin();
	return 1;
}


你可能感兴趣的:(setitimer 定时器)