Please HELP with setpgid()

Please HELP with setpgid()

Post by WILLIAM WEST » Sun, 15 Oct 1995 04:00:00



Hello,   I wish to brazenly cadge some time and expertise of a Un*x guru
'cos I can't work out why setpgid() is not working for me.

The problem is the line:
if (argc>1 && (setpgid(0,atoi(argv[1]))!=0))
in the following program, which is an attempt to meter signals from a process
in Linux 1.2.3. ...

#include <sys/time.h>           // timeval
#include <unistd.h>             // for select?  
#include <errno.h>             // for select?  
#include <stdio.h>              // for gets
#include <stdlib.h>             // for atoi
#include <iostream.h>           // for <<>>
#include <sys/signal.h>    

/* NOTE 1: gettimeofday() loses!  It won't resolve below a second. */
/* NOTE 2: I think select() doesn't auto restart on handler return */
/*           so I can meter signals as well with this code.        */

void handler(int);

struct sigaction act,oact;
int interrupted = 1;

void handler(int)
{
  interrupted++;
  sigaction(SIGUSR1,&act,&oact);
  return;

Quote:}

int main(int argc, char** argv)
{
  char str[100];
  int count = 0 , retval=0 , min_count=0 , max_count=1000000 ;
  timeval T={0,0} , S=T;
  fd_set in;
  long int t = 1000;

  /* set up signal handler */
  act.sa_handler = handler;
  act.sa_flags = SA_RESTART;
  sigaction(SIGUSR1,&act,&oact);
  sigemptyset(&act.sa_mask);                   // full mask

  if (argc>1 && !strcmp(argv[1],"--help"))
    {
      cout <<
    "Usage: meter [pgid] [period {1000 ms}] [min count {0}] [max count {\\infty}]\n";
      exit(0);
    }
#ifdef _POSIX_JOB_CONTROL
  cout << "YEAH !!!!!!!!!  we got job control!!! " <<endl;
#endif
  if (argc>1 && (setpgid(0,atoi(argv[1]))!=0))
    {
      cout << "Set a valid pgid to listen to as the first argument,\n"
           << "or put a zero in the first argument space to listen to the current group,\n"
           << "or use no arguments at all.\n";
      cout <<
    "\nUsage: meter [pgid] [period {1000 ms}] [min count {0}] [max count {\\infty}]\n";
      exit(0);
    }

  if (argc>2)  t = atol(argv[2]);
  T.tv_sec = t/1000;
  T.tv_usec = (t%1000)*1000;
  if (argc>3)  min_count = atoi(argv[3]);
  if (argc>4)  max_count = atoi(argv[4]);

  while (1)
    {
      for( S=T , count = 0 ; S.tv_usec || S.tv_sec ; count++ )
        {
          FD_ZERO(&in);
          FD_SET(STDIN_FILENO, &in);
          retval= select(2,&in,0,0,&S);
          if (retval && !interrupted) gets(str);
          interrupted = 0;
        }
      count--;                             // seem to count one too many

      cout << 1000000*count / t << endl;   // output in millihertz

      /* ad hoc stretch/contraction of time window if too few/many counts occur */
      if (count < min_count)
        {
          t=t * min_count / (count?:1) + 1;  
          T.tv_sec = t/1000;
          T.tv_usec = (t%1000)*1000;
        }

     if (count > max_count)
        {
          t=t * count / max_count - 1;    // I just invent these formulae!
          T.tv_sec = t/1000;              //  so YMMV
          T.tv_usec = (t%1000)*1000;
        }
    }
  return 0;

Quote:}

You see the idea?  Tune in to  a process group using setpgid() and then
listen out for signals using the non-auto-restarting behaviour of
select().   But the first bit does not work.

TIA
Bill