哪位老大用过SOLARIS8下的 setitimer ? (见内)

lixuyu 2002-07-11 03:01:14
哪位老大用过SOLARIS8下的 setitimer ? 用setitimer设置ITIMER_REALPROF之后为什么每个间隔来了三个SIGPROF信号???
...全文
42 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
lixuyu 2002-07-11
  • 打赏
  • 举报
回复
上面写错了:是ITIMER_REALPROF,不是ITIMER_REALPROC。
抱歉。
lixuyu 2002-07-11
  • 打赏
  • 举报
回复
这里例子我也见过,这个例子里使用的是interval timer中的一个——ITIMER_REAL。 类似的例子我也试过,没有问题。

但是我用的是另一个interval timer:ITIMER_REALPROC。因为需要在多线程中使用。

minifat(胖和尚) 试过ITIMER_REALPROC吗?
胖和尚minifat 2002-07-11
  • 打赏
  • 举报
回复
3.5 setitimer如何用

D: scz <scz@nsfocus.com>

为什么要学习使用setitimer(2),因为alarm(3)属于被淘汰的定时器技术。

A: 小四 <cloudsky@263.net>

下面是个x86/FreeBSD 4.3-RELEASE下的例子

--------------------------------------------------------------------------
/*
* File : timer_sample.c
* Author : Unknown (Don't ask me anything about this program)
* Complie : gcc -Wall -pipe -O3 -o timer_sample timer_sample.c
* Platform : x86/FreeBSD 4.3-RELEASE
* Date : 2001-09-18 15:18
*/

/************************************************************************
* *
* Head File *
* *
************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <signal.h>

/************************************************************************
* *
* Macro *
* *
************************************************************************/

typedef void Sigfunc ( int ); /* for signal handlers */

/************************************************************************
* *
* Function Prototype *
* *
************************************************************************/

static void Atexit ( void ( *func ) ( void ) );
static void init_signal ( void );
static void init_timer ( void );
static void on_alarm ( int signo );
static void on_terminate ( int signo );
static int Setitimer ( int which, const struct itimerval *value,
struct itimerval *ovalue );
Sigfunc * signal ( int signo, Sigfunc *func );
static Sigfunc * Signal ( int signo, Sigfunc *func );
static void terminate ( void );

/************************************************************************
* *
* Static Global Var *
* *
************************************************************************/

/************************************************************************/

static void Atexit ( void ( *func ) ( void ) )
{
if ( atexit( func ) != 0 )
{
perror( "atexit" );
exit( EXIT_FAILURE );
}
return;
} /* end of Atexit */

/*
* 初始化信号句柄
*/
static void init_signal ( void )
{
int i;

Atexit( terminate );
for ( i = 1; i < 9; i++ )
{
Signal( i, on_terminate );
}
Signal( SIGTERM, on_terminate );
Signal( SIGALRM, on_alarm );
return;
} /* end of init_signal */

static void init_timer ( void )
{
struct itimerval value;

value.it_value.tv_sec = 1;
value.it_value.tv_usec = 0;
value.it_interval = value.it_value;
Setitimer( ITIMER_REAL, &value, NULL );
} /* end of init_timer */

static void on_alarm ( int signo )
{
static int count = 0;

/*
* 演示用,这很危险
*/
fprintf( stderr, "count = %u\n", count++ );
return;
}

static void on_terminate ( int signo )
{
/*
* 这次我们使用atexit()函数
*/
exit( EXIT_SUCCESS );
} /* end of on_terminate */

static int Setitimer ( int which, const struct itimerval *value,
struct itimerval *ovalue )
{
int ret;

if ( ( ret = setitimer( which, value, ovalue ) ) < 0 )
{
perror( "setitimer" );
exit( EXIT_FAILURE );
}
return( ret );
} /* end of Setitimer */

Sigfunc * signal ( int signo, Sigfunc *func )
{
struct sigaction act, oact;

act.sa_handler = func;
sigemptyset( &act.sa_mask );
act.sa_flags = 0;
if ( signo == SIGALRM )
{
#ifdef SA_INTERRUPT
act.sa_flags |= SA_INTERRUPT; /* SunOS 4.x */
#endif
}
else
{
#ifdef SA_RESTART
act.sa_flags |= SA_RESTART; /* SVR4, 44BSD */
#endif
}
if ( sigaction( signo, &act, &oact ) < 0 )
{
return( SIG_ERR );
}
return( oact.sa_handler );
} /* end of signal */

static Sigfunc * Signal ( int signo, Sigfunc *func )
{
Sigfunc *sigfunc;

if ( ( sigfunc = signal( signo, func ) ) == SIG_ERR )
{
perror( "signal" );
exit( EXIT_FAILURE );
}
return( sigfunc );
} /* end of Signal */

static void terminate ( void )
{
fprintf( stderr, "\n" );
return;
} /* end of terminate */

int main ( int arg, char * argv[] )
{
init_signal();
init_timer();
while ( 1 )
{
/*
* 形成阻塞,降低CPU占用率
*/
getchar();
}
return( EXIT_SUCCESS );
} /* end of main */

/************************************************************************/

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧