2008年10月24日 星期五

timer_create

http://www.lslnet.com/linux/f/docs1/i38/big5278308.htm
每天 23:30 調用應用程序
每間隔1000小時執行一次應用程序


[code]
#Makefile

CC = cc
COPS = -g -D_DEBUG
RTLIB = -lrt

all: mytimer

mytimer: mytimer.c
$(CC) $(COPS) -o mytimer mytimer.c $(RTLIB)

clean:
rm -f core *.o mytimer
[/code]


[code]
/* mytimer.c
*
* Usage: mytimer time interval
* Example: mytimer 23:30:00 1000
*
*/

#include
#include
#include
#include
#include
#include
#include

#define SIGNUM SIGALRM
#define SIGINFO1 1
#define SIGINFO2 2
#define DAYINTERVAL 24*60*60
#define BILLION 1000000000L
#define TIMER_MSG1 "Program 1\t"
#define TIMER_MSG2 "Program 2\t"

static void handler(int signo, siginfo_t *info, void *context) {
int errsave;

errsave = errno;
switch (info->si_value.sival_int) {
case SIGINFO1:
write(STDOUT_FILENO, TIMER_MSG1, sizeof(TIMER_MSG1) - 1);
system("/usr/bin/date +%X");
break;
case SIGINFO2:
write(STDOUT_FILENO, TIMER_MSG2, sizeof(TIMER_MSG2) - 1);
system("/usr/bin/date +%X");
break;
default:
break;
}
errno = errsave;
}

static int setsighandler(void) {
struct sigaction act;

act.sa_flags = SA_SIGINFO;
act.sa_sigaction = handler;
if ((sigemptyset(&act.sa_mask) == -1) ||
(sigaction(SIGNUM, &act, NULL) == -1))
return -1;
return 0;
}

static int settime(long val, long interval, int evt) {
timer_t timerid;
struct itimerspec value;
struct sigevent evp;

evp.sigev_notify = SIGEV_SIGNAL;
evp.sigev_signo = SIGNUM;
switch (evt) {
case SIGINFO1:
evp.sigev_value.sival_int = SIGINFO1;
break;
case SIGINFO2:
evp.sigev_value.sival_int = SIGINFO2;
break;
default:
return -1;
}


if (timer_create(CLOCK_REALTIME, &evp, &timerid) == -1) {
perror("Failed to create a timer");
return -1;
}

value.it_interval.tv_sec = (long)interval;
value.it_interval.tv_nsec = (interval - value.it_interval.tv_sec)*BILLION;
if (value.it_interval.tv_nsec >= BILLION) {
value.it_interval.tv_sec++;
value.it_interval.tv_nsec -= BILLION;
}

value.it_value.tv_sec = (long)val;
value.it_value.tv_nsec = (val - value.it_value.tv_sec)*BILLION;
if (value.it_value.tv_nsec >= BILLION) {
value.it_value.tv_sec++;
value.it_value.tv_nsec -= BILLION;
}

return timer_settime(timerid, 0, &value, NULL);
}

int string2time(const char *str, struct tm **time) {
const char *delimiters = ": \t";
char *buf;
char *bufp;
buf = (char *) malloc(strlen(str) + 1);
strcpy(buf, str);
*time = (struct tm *) malloc(sizeof(struct tm));

if ((bufp = strtok(buf, delimiters)) != NULL) {
(*time)->tm_hour = atoi(bufp);
if ((bufp = strtok(NULL, delimiters)) != NULL) {
(*time)->tm_min = atoi(bufp);
if ((bufp = strtok(NULL, delimiters)) != NULL)
(*time)->tm_sec = atoi(bufp);
else
(*time)->tm_sec = 0;
} else {
fprintf(stderr, "Time value(%s) is invalid\n", str);
free(buf);
return -1;
}
} else {
fprintf(stderr, "Time value(%s) is invalid\n", str);
free(buf);
return -1;
}

free(buf);
return 0;

}

void freetimepointer(struct tm **time) {
if (time == NULL)
return;
if (*time != NULL)
free(*time);
free(time);
}
int runtime(char *timep) {
time_t current;
time_t end;
struct tm *tcurrent;
struct tm *tend;

if (setsighandler() == -1) {
perror("Failed to setup signal handler");
return -1;
}

string2time(timep, &tend);
end = tend->tm_hour*60*60 + tend->tm_min*60 + tend->tm_sec;
current = time(NULL);
tcurrent = localtime(&current);
current = tcurrent->tm_hour*60*60 + tcurrent->tm_min*60 + tcurrent->tm_sec;

if (current > end)
end += 24*60*60 - end;
else
end -= current;

#ifdef _DEBUG
if (settime(end, 2, SIGINFO1) == -1)
return -1;
#else
if (settime(end, DAYINTERVAL, SIGINFO1)== -1)
return -1;
#endif

freetimepointer(&tend);
return 0;

}

int runinterval(int interval) {
time_t secs;

if((secs = interval*60*60) <= 0) {
fprintf(stderr, "Time value(%d) is invalid\n", interval);
return -1;
}

if (setsighandler() == -1) {
perror("Failed to setup signal handler");
return -1;
}

#ifdef _DEBUG
secs = 6;
#endif

if (settime(1, secs, SIGINFO2) == -1)
return -1;

return 0;
}

int main(int argc, char **argv)
{
if (argc != 3) {
printf("Usage: %s time interval\n", argv[0]);
printf("Example: %s 23:30:00 1000\n", argv[0]);
return 1;
}

if (runtime(argv[1]) == -1)
return 1;

if (runinterval(atoi(argv[2])) == -1)
return 1;

for ( ; ; )
pause();

return 0;
}
[/code]

2008年10月18日 星期六

for loop in bash script

for loop in bash script

for var in 1111 222 3333 444
do
echo ${var}
done


Result
linux@linux-desktop:~$ ./test.sh
1111
222
3333
444
linux@linux-desktop:~$

MAKE file for @%^?

hello.c
#include
//#include
#include "func.h"
#include "my.h"

int main(void){
char * say ="hello, world\n";
printf("%s \n" ,say);
func();

//printf("%f\n",log(1000));

return 0;

}

func.c
#include

int func (void){
printf("come from func");
return 0;
}

func.h
#include
int func (void);


my.h
#define NUMBER 1024


Makefile

CC=gcc
all: hello

func.o: func.c func.h
$(CC) $(OPT) -c func.c
echo "1=$@ 2=$< 3="$?" 4="$^" 1="$@" 2="$<" 3="$?" 4="$^" 1="$@" 2="$<" 3="$?" 4="$^" 1="$@" 2="$<" 3="$?" 4="$^">

2008年10月17日 星期五

Makefile variables=, :=, ?=, +=

Makefile
Recursively expanded variables=
ex:
OPT=O2
var2=$(var1)
var1=$(OPT)

all:
echo "$(OPT) $(var1) $(var2)"

OPT, var1, var2 will be the same.
It will work fine.


Simply expanded variables:=

ex:
OPT=O2
var3:=(var2)
var2=$(var1)
var1=$(OPT)


all:
echo "$(OPT) $(var1) $(var2) $(var3)"

OPT, var1, var2 will be the same.
var3 will be empty
.

Use the environment variable ?=

ex:
OPT=O2
var4?=DEBUG
var3:=(var2)
var2=$(var1)
var1=$(OPT)


all:
echo "$(OPT) $(var1) $(var2) $(var3) $(var4)"
OPT, var1, var2 will be the same.
var3 will be empty.
var4 will use the environment variable

孩子 3C 使用條款

昨天看到有人說別讓孩子用 youtube,因為上面的確是有一些不太合適的內容。 其實我們家都是把 youtube 投影到電視上面跟孩子一起看,但比較大的孩子可能需要在房間裡面準備一台電腦。 我自己寧可開放孩子使用電腦也不想讓他迷上手機。平板是我可以接受的最低限度,但也僅限於在外...