Return to the Project 1 Page

#include <stdio.h>
#include <signal.h>
#include <stddef.h>
#include <sys/wait.h>

#define tcsetpgrp(fd, pgrp)     ioctl ((fd), TIOCSPGRP, &(pgrp))

int main()
{
  int count;
  int status;
  int cpid;
  char buf[256];
  char title[256];
  sigset_t blocked;

  /*
   * This ignores a signal generated by job control shells like tcsh
   * preventing them from interfering with our shells
   */
  sigemptyset (&blocked);
  sigaddset (&blocked, SIGTTOU);
  sigaddset (&blocked, SIGTTIN);
  sigprocmask(SIG_BLOCK, &blocked, 0);

  for (count=0; count < 3; count++)
  {
    if (!(cpid=fork()))
    {
      setpgid (0,0);
      tcsetpgrp (0, getpid());
      sprintf (title, "%d", count);
      execl ("/usr/X/bin/xterm", "xterm", "-title", title, NULL);
      exit (-1);
    }
  
    if (cpid < 0)
      exit(-1);
  }

  wait (&status);

  while (1)
  {
    memset (buf, 0, 256);
    fgets (buf, 256, stdin);
    puts ("ECHO: ");
    puts (buf);
    puts ("\n");
  }
}