[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Fork, session leader and printing to stdout



  What will happen when a daemon process with no controlling terminal
tries to print to its stdout?
  More specifically, consider the following pseudo code:

int main(void)
{
  pid_t pid;

  /* Assume that at this point stdin, stdout and stderr are open
   * because the process was created by the shell.
   */
  if( (pid = fork()) < 0 )
    return FORK_FAILED;
  else {                    /* Child Process - Continue Execution */
    fclose(stdin);
    fclose(stderr);
    setsid();               /* become session leader */
    chdir("/");             /* change working directory */
    umask(0);               /* clear the file mode creation mask */

    printf("No controlling terminal. What will happen here?")

    return OK;
   } else {
                            /* Parent Process - Exit & Return Control */
     exit(0);
}


  No, this is no homework. Just trying to get a complete answer,
hopefully with a bit of wider overview, without falling to some corner
case when I am trying to get the answer by myself.



Reply to: