当前位置: 技术问答>linux里面如何实现定时器的功能
iis7站长之家
Linux定时器问题,使用ITIMER_REAL实现系统调用gettimeofday()所实现的功能时出错,请教各位!
来源: 互联网 发布时间:2015-05-13
本文导语: 使用ITIMER_REAL实现系统调用gettimeofday()所实现的功能, 将它设置为每秒钟产生一个信号, 下面有程序代码,但有“段错误”,不知道改哪,请教各位, #include #include #include #include static long my_time = 0; static struct iti...
使用ITIMER_REAL实现系统调用gettimeofday()所实现的功能,
将它设置为每秒钟产生一个信号,
下面有程序代码,但有“段错误”,不知道改哪,请教各位,
#include
#include
#include
#include
static long my_time = 0;
static struct itimerval p_realt;
void p_realt_handler()
{
my_time++;
signal(SIGALRM,p_realt_handler);
}
long unsigned int fibonacci(unsigned int);
main()
{
struct timeval *s_time_start,*s_time_end;
long my_start_time = 0;
p_realt.it_interval.tv_sec = 1;
p_realt.it_interval.tv_usec = 0;
if(setitimer(ITIMER_REAL,&p_realt,(struct itimerval *)0)==-1)
printf("nSetitimer error!");
gettimeofday(s_time_start,(struct timezone *)0);
my_start_time = my_time = s_time_start->tv_sec;
signal(SIGALRM,p_realt_handler);
fibonacci(30);
gettimeofday(s_time_end,(struct timezone *)0);
printf("My result:%ldn",my_time-my_start_time);
printf("Sys result:%ldn",s_time_end->tv_sec-s_time_start->tv_sec);
return 0;
}
long unsigned int fibonacci(unsigned int n)
{
if(n==0)
return 0;
else
if(n==1 || n==2)
return 1;
else
return(fibonacci(n-1)+fibonacci(n-2));
}
调试结果:
[root@localhost timer]# gcc -o p2 -g p2.c
[root@localhost timer]# ./p2
My result:0
Sys result:0
段错误
[root@localhost timer]# gdb p2
GNU gdb Red Hat Linux (5.3post-0.20021129.18rh)
Copyright 2003 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu"...
(gdb) run
Starting program: /mnt/I/Linux/Practice/timer/p2
My result:0
Sys result:1
Program received signal SIGSEGV, Segmentation fault.
0x4000c6a0 in _dl_fini () from /lib/ld-linux.so.2
(gdb) where
#0 0x4000c6a0 in _dl_fini () from /lib/ld-linux.so.2
#1 0x42029c20 in exit () from /lib/tls/libc.so.6
#2 0x420155d8 in __libc_start_main () from /lib/tls/libc.so.6
(gdb) list
9 {
10 my_time++;
11 signal(SIGALRM,p_realt_handler);
12 }
13 long unsigned int fibonacci(unsigned int);
14
15 main()
16 {
17 struct timeval *s_time_start,*s_time_end;
18 long my_start_time = 0;
(gdb)
将它设置为每秒钟产生一个信号,
下面有程序代码,但有“段错误”,不知道改哪,请教各位,
#include
#include
#include
#include
static long my_time = 0;
static struct itimerval p_realt;
void p_realt_handler()
{
my_time++;
signal(SIGALRM,p_realt_handler);
}
long unsigned int fibonacci(unsigned int);
main()
{
struct timeval *s_time_start,*s_time_end;
long my_start_time = 0;
p_realt.it_interval.tv_sec = 1;
p_realt.it_interval.tv_usec = 0;
if(setitimer(ITIMER_REAL,&p_realt,(struct itimerval *)0)==-1)
printf("nSetitimer error!");
gettimeofday(s_time_start,(struct timezone *)0);
my_start_time = my_time = s_time_start->tv_sec;
signal(SIGALRM,p_realt_handler);
fibonacci(30);
gettimeofday(s_time_end,(struct timezone *)0);
printf("My result:%ldn",my_time-my_start_time);
printf("Sys result:%ldn",s_time_end->tv_sec-s_time_start->tv_sec);
return 0;
}
long unsigned int fibonacci(unsigned int n)
{
if(n==0)
return 0;
else
if(n==1 || n==2)
return 1;
else
return(fibonacci(n-1)+fibonacci(n-2));
}
调试结果:
[root@localhost timer]# gcc -o p2 -g p2.c
[root@localhost timer]# ./p2
My result:0
Sys result:0
段错误
[root@localhost timer]# gdb p2
GNU gdb Red Hat Linux (5.3post-0.20021129.18rh)
Copyright 2003 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu"...
(gdb) run
Starting program: /mnt/I/Linux/Practice/timer/p2
My result:0
Sys result:1
Program received signal SIGSEGV, Segmentation fault.
0x4000c6a0 in _dl_fini () from /lib/ld-linux.so.2
(gdb) where
#0 0x4000c6a0 in _dl_fini () from /lib/ld-linux.so.2
#1 0x42029c20 in exit () from /lib/tls/libc.so.6
#2 0x420155d8 in __libc_start_main () from /lib/tls/libc.so.6
(gdb) list
9 {
10 my_time++;
11 signal(SIGALRM,p_realt_handler);
12 }
13 long unsigned int fibonacci(unsigned int);
14
15 main()
16 {
17 struct timeval *s_time_start,*s_time_end;
18 long my_start_time = 0;
(gdb)
|
struct timeval *s_time_start,*s_time_end;
gettimeofday(s_time_start,(struct timezone *)0);
你没分配空间吧?
gettimeofday(s_time_start,(struct timezone *)0);
你没分配空间吧?
|
struct timeval *s_time_start,*s_time_end;
gettimeofday(s_time_start,(struct timezone *)0);
你没分配空间!!!!
建议
struct timeval s_time_start,s_time_end;
gettimeofday( &s_time_start,NULL );
gettimeofday(s_time_start,(struct timezone *)0);
你没分配空间!!!!
建议
struct timeval s_time_start,s_time_end;
gettimeofday( &s_time_start,NULL );