a small cpu hog

rs at mirror.UUCP rs at mirror.UUCP
Tue May 7 06:38:00 AEST 1985



What am I doing wrong?  I have a little reminder that I want to
be able to set -- sort of like leave.  I want it small and unobtrusive,
and I want to be able to say "alarm 10 'ten minutes are up'"  I wrote
the obvious program (see below), but it ends up hogging all available
CPU time.  If I take out the fork() call (#ifdef CPU_HOG) it doesn't
take up all the time?  What's going on?

This only happens on BSD4.2; on Sys5, everything is ok.

Why am I taking up all the cycles?

#include <stdio.h>
#include <signal.h>


/* Linked in later. */
#ifndef	lint
exit(X) 	{ _exit(X); }
#endif	lint

/* Sure-fire guaranteed file descriptors. */
#define STDIN		0
#define STDOUT		1
#define STDERR		2

/* Global variables. */
static char	 Usage[] = "usage:  alarm delay 'message...'\n";
static char	 Buzzer[] = "\007\n\r";


main(ac, av)
    int			 ac;
    register char	*av[];
{
    register int	 Temp;

    /* Parse JCL. */
    if (ac < 3 || (Temp = atoi(av[1])) <= 0)
    {
	(void)write(STDERR, Usage, sizeof(Usage));
	return(1);
    }

    (void)close(STDIN);
    (void)close(STDOUT);
#ifdef	CPU_HOG	
    if (fork() == 0)
#endif	CPU_HOG	
    {
	av += 2;
	/* Now I lay me down to sleep... */
	(void)signal(SIGINT, SIG_IGN);
	(void)sleep((unsigned)Temp * 60);

	(void)write(STDERR, Buzzer, sizeof(Buzzer));
	(void)write(STDERR, *av, strlen(*av));
	(void)write(STDERR, Buzzer, sizeof(Buzzer));
    }

    return(0);
}

thanks.
--------
	Rich $alz
	Mirror Systems
	2067 Massachusetts Ave.  Cambridge, MA  02150
	617/661-0777
	{decvax!genrad!mit-eddie, ihnp4!inmet, wjh12} !mirror!rs



More information about the Comp.unix.wizards mailing list