Sunday, September 28, 2014

Unix Prog: Sessions

1. Introduction:

The session is the collection of process groups


2. setsid, getsid

System Definition:
session id: the session leader process id, which is also the session leader's process group id. In other words, getsid return the process group id of calling process's session leader. In some systems, if the argument pid doesn't belong to the same session of the caller process, it will return error.
 ubuntu@ip-172-31-23-227:~$ less /usr/include/unistd.h  
 ......  
 /* Create a new session with the calling process as its leader.  
   The process group IDs of the session and the calling process  
   are set to the process ID of the calling process, which is returned. */  
 extern __pid_t setsid (void) __THROW;  
   
 #if defined __USE_XOPEN_EXTENDED || defined __USE_XOPEN2K8  
 /* Return the session ID of the given process. */  
 extern __pid_t getsid (__pid_t __pid) __THROW;  
 ......  

What happens after calling setsid():
1) The process becomes the session leader of this new session. And the process is the only process in the new session
2) The process becomes the process group leader of a new process group.
3) The process has no controlling terminal. If it had one, the association will be broken.

The function returns an error if the caller is already a process group leader, to ensure it is not, the usual practice is to call fork and have the parent terminate and let child continue. Since child process belong to parent process's group, it child process is not the leader.

Example:
session.c:
 #include<stdio.h>  
 #include<stdlib.h>  
 #include<unistd.h>  
   
 int main(int argc, char* argv[])  
 {  
  pid_t sid;  
   
  // Retrieve the session id of given process, if argument is 0  
  // getsid will use current process id  
  if((sid = getsid(0)) < 0) {  
   printf("getsid error!\n");  
   exit(1);  
  }  
   
  printf("current process session id: %d\n", sid);  
  printf("current process id: %d\n", getpid());  
  printf("current process group id: %d\n", getpgrp());  
   
  // It would be an error here, since current process is the  
  // process group lead  
  if((sid = setsid()) < 0) {  
   printf("setsid error!\n");  
   exit(2);  
  }  
   
  exit(0);  
 }  

shell:
Since process is the leader of the process group, it can't start a new session.
 ubuntu@ip-172-31-23-227:~$ ./session.out  
 current process session id: 12050  
 current process id: 12306  
 current process group id: 12306  
 setsid error!  

==================================================================
session.c:
 #include<stdio.h>  
 #include<stdlib.h>  
 #include<unistd.h>  
   
 int main(int argc, char* argv[])  
 {  
  pid_t sid;  
  pid_t pid;  
   
  // Retrieve the session id of given process, if argument is 0  
  // getsid will use current process id  
  if((sid = getsid(0)) < 0) {  
   printf("getsid error!\n");  
   exit(1);  
  }  
   
  printf("parent process session id: %d\n", sid);  
  printf("parent process id: %d\n", getpid());  
  printf("parent process group id: %d\n", getpgrp());  
   
  if((pid = fork()) < 0) {  
   printf("fork error!\n");  
   exit(2);  
  }  
  else if (pid != 0) { // parent process  
   exit(0);  
  }  
   
  printf("before setting up the session: \n");  
  printf("child process id: %d\n", getpid());  
  printf("child process group id: %d\n", getpgrp());  
  printf("child process session id: %d\n", sid);  
   
  if((sid = setsid()) < 0) {  
   printf("setsid error!\n");  
   exit(3);  
  }  
   
  printf("after setting up the session: \n");  
  printf("child process id: %d\n", getpid());  
  printf("child process group id: %d\n", getpgrp());  
  printf("child process session id: %d\n", sid);  
  exit(0);  
 }  

shell:
The parent process session id: 12050, process id: 12339, process group id: 12339. After fork a child process, the child process id: 12340, process group id: 12339, session id: 12050. Child process inherits the process group id and session id from the parent process.
After setting up the new session inside the child process, the group id becomes: 12340, session id becomes 12340.
 ubuntu@ip-172-31-23-227:~$ ./session.out  
 parent process session id: 12050  
 parent process id: 12339  
 parent process group id: 12339  
 ubuntu@ip-172-31-23-227:~$ before setting up the session:  
 child process id: 12340  
 child process group id: 12339  
 child process session id: 12050  
 after setting up the session:  
 child process id: 12340  
 child process group id: 12340  
 child process session id: 12340  

No comments:

Post a Comment