title: Why MUST detach from tty when writing a linux daemon?
body: <p>When i tried to write a daemon under linux using C, i was told i should add following code after <strong>fork</strong> code block:</p>
  
  <pre><code>/* Preparations */
  ...
  
  /* Fork a new process */
  pid_t cpid = fork();
  if (cpid == -1){perror("fork");exit(1);}
  if (cpid &gt; 0){exit(0);}
  
  /* WHY detach from tty ? */
  int fd = open("/dev/tty", O_RDWR);
  ioctl(fd, TIOCNOTTY, NULL);
  
  /* Why set PGID as current PID ? */
  setpgid(getpid(), 0);
  </code></pre>
  
  <p>My question is: 
  Is there a must to do the above operations?</p>