#if defined(sgi) || defined(linux)
#include <stdio.h>

#include <sys/types.h>
#include <sys/time.h>
#include <sys/prctl.h>
#include <sys/schedctl.h>
#include <signal.h>
#include <sys/sysmp.h>

int procPid;
volatile int foo = 0;

int set;
       
onSigusr1()
{

}

procfxn()
{
	volatile int spin;


	if (schedctl(NDPRI, 0, NDPHIMIN) < 0)
	 { perror("schedctl:");
	   exit (-1);
	 }

	if (sysmp (MP_MUSTRUN, 1) < 0)
	 { perror ("sysmp");
	 }

/*	blockproc(getpid()); */

	while (1)
	 { 

	   set = 0;
/*	   kill (getppid(), SIGUSR1); */
/*	   nap (0.1); */
	   printf ("**\n");
	   foo = 1;

	   for (spin=0; spin<10000; spin++)
	      ;
	   foo = 0;
	   nap (0.01);
	 }

}

main()
{
	int cnt, proc;
	int nprocs;

	signal (SIGUSR1, onSigusr1);
#if 1
	nprocs = sysmp (MP_NPROCS);
	printf ("nprocs=%d\n", nprocs);
	if (sysmp (MP_MUSTRUN, 1) < 0)
	 { perror ("sysmp");
	 }
	nap (0.01);
#endif
	if ((proc = sproc(procfxn, PR_SADDR|PR_SID|PR_SFDS, 0)) < 0)
	 { perror("control task -- sproc:");
	   exit (-1);
	 }
#if 0
	if (schedctl (SETMASTER, proc, 0) < 0)
	 { perror ("schedctl");
	 }
	unblockproc(proc);
	printf ("he is master\n");
#endif
	while (1)
	 { if (foo == 1 && foo == 1)
	    { printf ("GAG!\n");
	    }
#if 1
	   if (cnt++ > 100000)
	    { printf ("hi\n");
	      cnt = 0;
	    }
#endif
	 }
	
}

nap(sec)
float sec;
{
 struct timeval tv;
 struct timeval now;
 struct timeval then;
 int fds;
 int n;
 extern int errno;

 gettimeofday(&now,0);
 tv.tv_sec = (int) sec;
 tv.tv_usec = (int) (1000000 * (sec - tv.tv_sec));
 then.tv_sec = now.tv_sec + tv.tv_sec;
 then.tv_usec = now.tv_usec + tv.tv_usec;
 while (then.tv_usec >= 1000000)
  { then.tv_usec -= 1000000;
    then.tv_sec ++;
  }
 while (1)
  { fds = 0;
    errno = 0;
    n = select(32,&fds,0,0,&tv);
    gettimeofday(&now,0);
    tv.tv_sec = then.tv_sec - now.tv_sec;
    tv.tv_usec = then.tv_usec - now.tv_usec;
    while (tv.tv_usec < 0)
     { tv.tv_usec += 1000000;
       tv.tv_sec --;
     }
    if (tv.tv_sec < 0)
     { return;
     }
  }
}
#endif

