Updated: April 19, 2023 |
Create a new session
#include <unistd.h> pid_t setsid( void );
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
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.
The new process group ID for the calling process, or -1 if an error occurred (errno is set).
/* * 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; }
Safety: | |
---|---|
Cancellation point | No |
Interrupt handler | No |
Signal handler | Yes |
Thread | Yes |