Sunday, September 7, 2014

Unix Prog: Process Termination

1. main function

int main(int argc, char* argv[])

argc is the number of command-line arguments, and argv is the array of pointers to the arguments.

When a c program is executed by the kernel, a special start-up routine is called before the main function is called. It takes values from the kernel, the command-line arguments and the environments and set things up for the main function to run.

2. Process termination
Normal termination of following ways:

1) return from the main function
2) calling exit
3) calling _exit or _Exit
4) return the last thread from its start routine
5) call the pthread_exit from the last thread

Abnormal termination:
1) Calling abort
2) Receipt of a signal
3) Response of the last thread to a cancellation request

3. Exit functions

exit performs a clean shut down of the standard I/O library: fclose is called for each open stream, and also it will function handlers registered with "atexit" and "on_exit"

_Exit, _exit terminate the process without doing anything.

Definition:
 ubuntu@ip-172-31-23-227:~$ less /usr/include/stdlib.h  
 ......  
 /* Call all functions registered with `atexit' and `on_exit',  
   in the reverse of the order in which they were registered,  
   perform stdio cleanup, and terminate program execution with STATUS. */  
 extern void exit (int __status) __THROW __attribute__ ((__noreturn__));  
 ......  
 /* Terminate the program with STATUS without calling any of the  
   functions registered with `atexit' or `on_exit'. */  
 extern void _Exit (int __status) __THROW __attribute__ ((__noreturn__));  
 ......  

 ubuntu@ip-172-31-23-227:~$ less /usr/include/unistd.h  
 ......  
 /* Terminate program execution with the low-order 8 bits of STATUS. */  
 extern void _exit (int __status) __attribute__ ((__noreturn__));  
 ......  

4. Default termination

proc.c:
 #include<stdio.h>  
 #include<stdlib.h>  
   
 int main(int argc, char* argv[])  
 {  
  printf("Hello world!\n");  
 }  

shell:
Since we don't specify the return value in main function, then returning status is undefined.
 ubuntu@ip-172-31-23-227:~$ ./a.out  
 Hello world!  
 ubuntu@ip-172-31-23-227:~$ echo $?  
 13  
=============================================================
proc.c:
 #include<stdio.h>  
 #include<stdlib.h>  
   
 void main(int argc, char* argv[])  
 {  
  printf("Hello world!\n");  
 }  

shell:
Although we changed the returning type of main function to "void", it still return value 13 by default.
 ubuntu@ip-172-31-23-227:~$ ./a.out  
 Hello world!  
 ubuntu@ip-172-31-23-227:~$ echo $?  
 13  
==============================================================
proc.c:
 #include<stdio.h>  
 #include<stdlib.h>  
   
 int main(int argc, char* argv[])  
 {  
  printf("Hello world!\n");  
  return 0;  
 }  

shell:
 ubuntu@ip-172-31-23-227:~$ ./a.out  
 Hello world!  
 ubuntu@ip-172-31-23-227:~$ echo $?  
 0  

5. atexit

We can use atexit system call to register up to 32 function handlers.

Definition:
 ubuntu@ip-172-31-23-227:~$ less /usr/include/stdlib.h  
 ......  
 /* Register a function to be called when `exit' is called. */  
 extern int atexit (void (*__func) (void)) __THROW __nonnull ((1));  
 ......  

By ISO C and POSIX.1 standard, exit() system call will firstly call the exit handlers and then close all open streams.
_exit and _Exit will just terminate process without doing anything.

proc.c:
 #include<stdio.h>  
 #include<stdlib.h>  
   
 void myexit_1()  
 {  
  static int i = 0;  
  i++;  
  printf("myexit_1 is called! Time: %d\n", i);  
 }  
   
 void myexit_2()  
 {  
  printf("myexit_2 is called!\n");  
 }  
   
 int main(int argc, char* argv[])  
 {  
  if(atexit(myexit_1) != 0) {  
   printf("atexit error!\n");  
   exit(1);  
  }  
   
  if(atexit(myexit_1) != 0) {  
   printf("atexit error!\n");  
   exit(1);  
  }  
   
  if(atexit(myexit_2) != 0) {  
   printf("atexit error!\n");  
   exit(1);  
  }  
   
  return 0;  
 }  

shell:
atexit handlers will also be called if we use "return" exit main function.
The calling order of handlers are in reverse of the registration order.
 ubuntu@ip-172-31-23-227:~$ ./a.out  
 myexit_2 is called!  
 myexit_1 is called! Time: 1  
 myexit_1 is called! Time: 2  

6. Command Line and Environment List

proc.c:
 #include<stdio.h>  
 #include<stdlib.h>  
 #include<unistd.h>  
   
 int main(int argc, char* argv[])  
 {  
  int i = 0;  
  while(argv[i] != NULL) {  
   printf("argv[%d]: %s\n", i, argv[i]);  
   i++;  
  }  
   
  i = 0;  
  while(__environ[i] != NULL) {  
   printf("environ[%d]: %s\n", i, argv[i]);  
   i++;  
  }  
   
  exit(0);  
 }  

shell:
Run the program to print out argv string array and environ array.
In current implementations, environ array includes information of the argv array.
 ubuntu@ip-172-31-23-227:~$ ./a.out Hello world!  
 argv[0]: ./a.out  
 argv[1]: Hello  
 argv[2]: world!  
 environ[0]: ./a.out  
 environ[1]: Hello  
 environ[2]: world!  
 environ[3]: (null)  
 environ[4]: XDG_SESSION_ID=51  
 environ[5]: TERM=xterm  
 environ[6]: SHELL=/bin/bash  
 environ[7]: SSH_CLIENT=98.14.66.46 64135 22  
 environ[8]: SSH_TTY=/dev/pts/2  
 environ[9]: USER=ubuntu  
 environ[10]: LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:  
 environ[11]: MAIL=/var/mail/ubuntu  
 environ[12]: PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games  
 environ[13]: PWD=/home/ubuntu  
 environ[14]: LANG=en_US.UTF-8  
 environ[15]: SHLVL=1  
 environ[16]: HOME=/home/ubuntu  
 environ[17]: LOGNAME=ubuntu  
 environ[18]: SSH_CONNECTION=98.14.66.46 64135 172.31.23.227 22  

Normally, we should use system calls getenv putenv to get environment information.

No comments:

Post a Comment