create a new session
#include <unistd.h> pid_t setsid( void );
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.
The new process group ID for the calling process. On error, -1 is returned and errno is set accordingly.
/* * 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" ); } }
POSIX 1003.1
Safety: | |
---|---|
Interrupt handler | No |
Signal handler | Yes, but modifies errno |
Thread | Yes |