[Previous]
[Contents]
[Next]

setsid()

create a new session

Synopsis:

#include <unistd.h>
pid_t setsid( void );

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 it's also the only process in the new session.

If the calling process is already a process group leader, a new session will not be created and an error will be returned.

Returns:

The new process group ID for the calling process. On error, -1 is returned and errno is set accordingly.

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>

void main()
  {

    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" );
      }
  }

Classification:

POSIX 1003.1

Safety:
Interrupt handler No
Signal handler Yes, but modifies errno
Thread Yes

See also:

errno, getsid(), setpgid()


[Previous]
[Contents]
[Next]