setsid()

Create a new session

Synopsis:

#include <unistd.h>

pid_t setsid( void );

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The setsid() function creates a new session with the calling process becoming the process group leader with no controlling terminal. The process group ID of the calling process is set to the process ID of the calling process. The calling process is the only process in the new process group, and is also the only process in the new session.

If the calling process is already a process group leader, a new session isn't created and an error is returned.

Returns:

The new process group ID for the calling process, or -1 if an error occurred (errno is set).

Errors:

EPERM
The calling process is already a process group leader, or the process group ID of a process other than the calling process matches the process ID of the calling process.

Examples:

/*
 * You can only become a session leader if you are not
 * a process group leader that is the default for a
 * command run from the shell.
 */

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>

int main( void )
  {

    if( fork() )
      {
      if( setsid() == -1 )
        perror( "parent: setsid" );
      else
        printf( "parent: I am a session leader\n" );
      }
    else
      {
      if( setsid() == -1 )
        perror( "child: setsid" );
      else
        printf( "child: I am a session leader\n" );
      }
    return EXIT_SUCCESS;
  }

Classification:

POSIX 1003.1

Safety:
Cancellation point No
Interrupt handler No
Signal handler Yes
Thread Yes

See also:

errno, getsid(), setpgid()