In C programming language, exit function is used to terminate the process. Exit function will be called automatically when the program is terminates. exit() cleans up the user-mode data related to library. exit() function internally calls _exit() function which cleans up kernel related data before terminating the process.
exit() flushes the IO related buffer before exiting the process and calls the _exit() function. Where as _exit() function just terminates the process without cleaning up or flushing user data. It is not advisable to call the _exit() function in your programming until unless you are very clear. There is another function called _Exit() function which also works same as _exit() functionally. We need to use strlid.h for exit() and unistd.h for _exit() functions.
Sample code with exit function:
Explanation:
Result we got as expected and there is no \n at the end, so on the same line prompt came.
Sample code with _exit function:
It prints nothing.
Explanation:
This is due to ,we called _exit() function directly, so IO related data is not flushed, so printf data is not flushed, because of this, it has printed nothing.
Sample code with _exit function with \n:
Explanation:
Using GDB, we can see functions which are called when the process terminates. giving below for your info for simple c program
you just compile your program using -g option for gcc, without this option, we cant use GDB. after compilation, launch GDB debugger using gdb with a.out or your program binary file. put a break point at main function and forward using next or n gdb command. You can find the exit and _exit functions calling in the above gdb process in red colour.
exit() flushes the IO related buffer before exiting the process and calls the _exit() function. Where as _exit() function just terminates the process without cleaning up or flushing user data. It is not advisable to call the _exit() function in your programming until unless you are very clear. There is another function called _Exit() function which also works same as _exit() functionally. We need to use strlid.h for exit() and unistd.h for _exit() functions.
Sample code with exit function:
#include<stdio.h> #include<unistd.h> #include<stdlib.h> int main() { printf("Hello"); exit(0); }Output:
programs$ ./a.out
Helloprograms$
Explanation:
Result we got as expected and there is no \n at the end, so on the same line prompt came.
Sample code with _exit function:
#include<stdio.h> #include<unistd.h> #include<stdlib.h> int main() { printf("Hello"); _exit(0); }OutPut:
It prints nothing.
Explanation:
This is due to ,we called _exit() function directly, so IO related data is not flushed, so printf data is not flushed, because of this, it has printed nothing.
Sample code with _exit function with \n:
#include<stdio.h> #include<unistd.h> #include<stdlib.h> int main() { printf("Hello\n"); _exit(0); }OutPut:
programs$ ./a.out
Hello
programs$
Explanation:
We got the output Hello, this is due to we are forcefully flushing the data using '\n'. Infact printf() function wont print or flush the data until buffer completes or end of the character is \n. printf internally maintains some buffer.
Using GDB, we can see functions which are called when the process terminates. giving below for your info for simple c program
int main() { printf("Hello"); }
programs$ gdb a.out GNU gdb 6.3.50-20050815 (Apple version gdb-1824) (Wed Feb 6 22:51:23 UTC 2013) Copyright 2004 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "x86_64-apple-darwin"...Reading symbols for shared libraries .. done (gdb) br main Breakpoint 1 at 0x100000f14: file _exit.c, line 5. (gdb) r Starting program: /Users/kh1279/Desktop/practice/Blog/programs/a.out Reading symbols for shared libraries +............................. done Breakpoint 1, main () at _exit.c:5 5 printf("Hello"); (gdb) (gdb) n 6 } (gdb) 0x00007fff933ab7e1 in start () (gdb) Single stepping until exit from function start, which has no line number information. 0x00007fff933ab808 in dyld_stub_exit () (gdb) Single stepping until exit from function dyld_stub_exit, which has no line number information. 0x00007fff8b4a4f74 in exit () (gdb) Single stepping until exit from function exit, which has no line number information. Hello0x00007fff8b4eb576 in dyld_stub___exit () (gdb) Single stepping until exit from function dyld_stub___exit, which has no line number information. 0x00007fff8efa3ae0 in _exit () (gdb) Single stepping until exit from function _exit, which has no line number information. Program exited with code 0377. (gdb) The program is not being run. (gdb)Explanation:
you just compile your program using -g option for gcc, without this option, we cant use GDB. after compilation, launch GDB debugger using gdb with a.out or your program binary file. put a break point at main function and forward using next or n gdb command. You can find the exit and _exit functions calling in the above gdb process in red colour.
No comments:
Post a Comment