🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: es.comp.os.linux.programacion
Article #16488

Re: timers

#16488
From: J.A. Gutierrez
Date: Tue, 18 Apr 2000 00:00
146 lines
3580 bytes
Santiago Romero <compiler@iname.com> wrote:
:  os suena algo de settimer o algo asi?
:  

---------------------------------------------------------------------------
#include	<sys/time.h>
#include	<signal.h>
#include	<unistd.h>
#include	<stdio.h>


void handler(int sig)
{
	printf("signal %d\n",sig);
}

int main(int argc, char *argv[] )
{
	static struct itimerval ivalue;
	static struct itimerval ovalue;

	sigset(SIGALRM,handler);

	ivalue.it_value.tv_usec = 900;
	ivalue.it_value.tv_sec = 0;

	ivalue.it_interval.tv_usec = 0;
	ivalue.it_interval.tv_sec = 3;

	setitimer(ITIMER_REAL, &ivalue, &ovalue);

	while(1)
		sigpause(SIGALRM);
}
---------------------------------------------------------------------------
/*
 * Simple signal example
 * Define _BSD_ for SunOS
 * Define _HPUX_SOURCE for HPUX
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>

#ifdef _BSD_
#  include <sys/types.h>
extern char *sys_errlist[];
#  ifndef __USE_POSIX
#    define raise(s) kill(getpid(),(s)) /* not using glibc */
#  endif
#  define strerror(e) sys_errlist[(e)]
#endif

/* Prototypes */

void	rserv(int i);
void	rserv5(int i);
int main ( int	argc, char	*argv[]);

/* Globals */

#define MY_SIG	SIGINT
/*#define MY_SIG	SIGUSR1*/

char	*progname;
int		count=0;

void	rserv(int i)
{
	/* set MY_SIG again */

	if ( SIG_ERR == signal(MY_SIG, rserv))
		fprintf(stderr,"%s: Error: %s\n",progname,strerror(errno));

	printf("%s: Hola! (signal %d)\n", progname,  i);
	fflush(stdout);
	count ++;
	return;
}
void	rserv5(int i)
{
	printf("%s: Hola! 5 (signal %d)\n", progname,  i);
	fflush(stdout);
	sleep(5);
	count ++;
	return;
}

int main ( int	argc, char	*argv[])
{

	int	i;
	unsigned int s;

	void (*sys)(int);

	if (argc != 3)
	{
		printf ("Uso: %s nloops sleep\n", argv[0]);
		exit(1);
	}

	progname = argv[0];
	s=atoi(argv[2]);

	fprintf(stderr,"%s: %p\n", argv[0], sys);
	if ( SIG_ERR == (sys = signal(MY_SIG, rserv)))
		fprintf(stderr,"%s: Error: %s\n",argv[0],strerror(errno));
	else
		fprintf(stderr,"%s: %p\n", argv[0], sys);

	for (i = atoi(argv[1]); i > 0; i--)
	{
		sleep(s);
		
		raise(MY_SIG);
	}
	signal(MY_SIG, sys );

	sigset(MY_SIG, rserv5);
	sighold(MY_SIG);
	printf("b %d\n",count);
	sigpause(MY_SIG);
	printf("c %d\n",count);
	printf("in sleep\n");
	sighold(MY_SIG);
	sleep(5);
	printf("out sleep\n");
	sigpause(MY_SIG);
	printf("d %d\n",count);

	puts("bye");
	return 0;
}


---------------------------------------------------------------------------

-- 
PGP and other useless info at      \                 Oh, show us the way
http://www.cps.unizar.es/~spd/      \             to the next whisky bar
finger://daphne.cps.unizar.es/spd    \                      Weill/Bretch
es doloroso ser sabio y descubrir la sopa de ajo                   (Pla)
Technical Details
Message-ID: <8dhld4$e90$1@news.unizar.es>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <slrn8fm9gk.26r.compiler@compiler.linux.es>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f86c8,1e0a148764c36e8
X-Google-Attributes: gidf86c8,public
From: "J.A. Gutierrez" <spd@daphne.cps.unizar.es>
Subject: Re: timers
Date: 2000/04/18
Message-ID: <8dhld4$e90$1@news.unizar.es>
X-Deja-AN: 612662791
References: <slrn8fm9gk.26r.compiler@compiler.linux.es>
X-Complaints-To: usenet@news.unizar.es
X-Trace: news.unizar.es 956061924 14624 155.210.152.20 (18 Apr 2000 12:45:24 GMT)
Organization: Rank
User-Agent: tin/1.4.1-19991201 ("Polish") (UNIX) (IRIX/6.2 (IP22))
NNTP-Posting-Date: 18 Apr 2000 12:45:24 GMT
Newsgroups: es.comp.os.linux.programacion