Linux Process Learning notes-process exit/Terminate process

Source: Internet
Author: User
Tags exit in flushes

<!--[if!supportlists]-->ÿ<!--[endif]--> Exit/terminating a processvoid_exit (intStatus) andvoidExitintstatus) Both functions let the process exit, the parameter status indicates what state the process will exit in,<stdlib.h>Some states are pre-defined, such as exit_success (a value of 0) that exits with a success state, and Exit_failure (a value of 1) indicates exiting in a failed state.   When the _exit function is called, it closes all the file descriptors of the process, cleans up memory and some other kernel cleanup functions, but does not flush the stream (stdin, stdout, stderr ...). The Exit function is a wrapper over the _exit function, which invokes _exit and flushes the stream before the call. Refer to the following code: #include<stdio.h>//For printf (const char *)#include <unistd.h>//For Fork ()#include <sys/wait.h>//For Wait (int *)#include <stdlib.h>//For exit_success intMain () {printf ("app start...\n"); if(fork () = =0) {printf ("Do something in child process ... \ n");                 Exit (exit_success); printf ("This is not been executed\n"); }        intstatus; Wait (&status); printf ("app end\n"); return 0;} The above code either uses exit or the _exit output is as follows: App start ... DoSomethinginchChild process ... app end this is because the stdout buffer is buffered by rows and refreshes the current buffer when a newline character is encountered, so even if _exit does not refresh before the process exits,"Do somethign in child process"the sentence is still being printed on the screen. Now we will use printf without a newline character, and do not call functions such as fflush, and try using _exit: #include<stdio.h>//For printf (const char *)#include <unistd.h>//For Fork ()#include <sys/wait.h>//For Wait (int *)#include <stdlib.h>//For exit_success intMain () {printf ("app start...\n"); if(fork () = =0) {printf ("Do something in child process ...");                 _exit (exit_success); printf ("This is not been executed\n"); }        intstatus; Wait (&status); printf ("app end\n"); return 0;} The output is: app Start...app End If you switch to exit then the result is: App start ... DoSomethinginchChild process ... app endvoidabort () quits the process abnormally. It generates a sigabort signal and then stops the process, which means it won't clean up, but it flushes the buffer. #include<stdio.h>//For printf ()#include <unistd.h>//For Fork ()#include <sys/wait.h>//For wait ()#include <stdlib.h>//For exit_success intMain () {printf ("app start...\n"); if(fork () = =0) {printf ("Do something in child process ...");                Abort (); printf ("This is not been executed\n"); }        intstatus; Wait (&status); printf ("app end\n"); return 0;} The output is: App start ... DoSomethinginchChild process ... app endvoidAtexitvoid(*f) ()) at the exiting moment. You can call this function if you want to do a bit of customization before the process ends normally. It simply executes a function callback using the function pointer you passed in. It is important to note that the callback function is executed only when the Exit function ends the process or when the process finishes executing all the code, i.e. if the process is terminated by _exit or abort, then the atexit function is invalid # include<stdio.h>//For printf ()#include <unistd.h>//For Fork ()#include <sys/wait.h>//For wait ()#include <stdlib.h>//For exit_success voidBefore_exit () {printf ("exit!\n");} intMain () {printf ("app start...\n"); if(fork () = =0) {printf ("Do something in child process ... \ n"); void(*f) () =Before_exit;                Atexit (f);                Exit (exit_success); printf ("This is not been executed\n"); }        intstatus; Wait (&status); printf ("app end\n"); return 0;} The output is: App start ... DoSomethinginchChild Process ...1,2,3exit!app End

Linux Process Learning notes-process exit/Terminate process

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.