exit (), _ exit (), and return

C language return, exit, break, continue. Status Code usually simultaneously bring the first two functions use the program exits, and the standard C has EXIT_SUCCESS EXIT_FAILURE two macros, located in /usr/include/stdlib.h.
It is defined:
#define EXIT_FAILURE. 1
#define of EXIT_SUCCESS 0

Its function difference with _exit () action of the exit function.

Definition has a library file shows: exit is a library function, exit (1) represents exit the program after an error occurs, exit (0) indicates a normal exit.
In the exit stdlib.h function is defined like this: void exit (int status);
This system call is used to terminate a process, no matter where in the program, as long as the implementation of exit, the process will be terminated from the process run.
Talked about the exit system call, we must mention another system call, _exit, _exit () function is located in unistd.h, compared to the exit (), _ exit () function is the function of the most simple and direct termination of running processes, frees the memory space they use and destruction of data structures in memory, and exit () that before the process exits to check the status of the file, the contents of the file buffer write the file.
Below we describe the case of binding in the above buffer function printf this operation by:

exit to terminate the process.

#include <stdlib.h>
#include <stdio.h>

main()
{
printf("output begin\n");
exit(0);
printf("output end\n");
}

Execute gcc -o exit1 exit1.c generate exit1, execution will only print out the output begin.

exit will be writing the buffer file.

Corresponds to each open file, in memory has a buffer, each time a file is read, the read will be more a number of records, so that the next can be read directly from the memory buffer when reading a file, each write the file, it is only the write buffer memory, and so on to meet certain conditions (up to a certain amount, are experiencing this specific characters (such as newline \ n and EOF EOF)), then the buffer the contents of the file write-once, we know that
void Exit (int status);
Exit () to perform the normal end of the current process, and the parameter status returns to the parent process, and the process all the data is automatically write-back buffer and close the unclosed files.

#include <stdlib.h>
#include <stdio.h>

main()
{
printf("output begin\n");
printf("content in buffer");
exit(0);
}

(GCC)
$ ./exit1
output begin
content in buffer
the printf () format can be converted according to the parameters and formats the data string, and then write the result to the standard output device, up until the end of the string appears ( '\ 0'). Visible, exit the newline statement does not appear to save the output file labeled.

_exit () does not perform cleanup I / O buffer operation.

_exit () is used to perform an immediate end to the current process, and the parameter status returns to the parent process, and close the unclosed files. After this function call does not return, and passed SIGCHLD signal to the parent process, the parent process can be achieved by the end of the function wait state child process.

#include <stdio.h>
#include <unistd.h>

main()
{
printf("output begin\n");
printf("content in buffer\n");
_exit(0);
}

(gcc)
$ ./exit2
output begin
is actually the second printf statement because certain conditions are not met, they just saved in the buffer, then we use _exit () function directly processes shut down, data in the buffer will be lost,

Comparison and exit, return primarily to provide function return values, continue, break statement is used in most cases the cycle. Many friends might confuse the three statements and exit, and the area of ​​these three statements could not tell.

the difference between the exit function return command:

  • Figuratively speaking, return from the city of A x y cell to cell, exit directly out of A City

  • exit at any time for the end of the program the program is running, the parameter is returned to the exit of the OS. Also implicitly calling the exit function at the end of the main function. Will first execute the exit function runtime functions by the atexit () function is registered, then will do some of their own clean-up work, and refresh all the output stream, close all open flow and closed by standard I / O function tmpfile () creates a temporary file. exit is the end of a process, it removes the memory space used by the process, while the parent process returns an error message.

    • exit can return any integer less than 256. Return different values ​​mainly to the caller for different treatments (separate process is returned to the operating system. If it is a multi-process, is returned to the parent process. The parent process which calls waitpid () and other functions to get the child process exits state to be treated differently according to the corresponding return value to allow the caller to make the appropriate treatment. Overall, exit () is the current process returns control to the calling program of the program, in brackets is the return value, the caller told the state run the program.)
    • exit (1) indicates that the process exits normally returns 1.;
    • exit (0) means that the process of non-normal exit. return 0.
  • Parameter atexit () function is a function pointer, the function pointer to a function of no arguments and returns no value. atexit () function prototype: int atexit (void (*) (void)); can atexit () register up to 32 processing functions in a program, called in reverse order sequence of these processing functions and its registration, i.e. the first registration of the final call, the first call to final registration

  • return is the language level, which represents a return call stack; and exit system call level, it represents the end of a process. return () function returns the current is, of course, if the main is the main function, naturally end the current process, if not, that is the one to call on the return. If sometimes detect whether the process exits normally. We must use the last process when multiple processes return values

  • In the function returns a value, the function return statement is to provide the return value of the function of the whole, and end the current function returns to the place that called it. You can also use the return statement in the function does not return a value, such as when an error checking to advance the implementation of the end of the current function and return. General program execution to the end of main () is complete, if you want to do something at the end of the program, you can try to use this function.
    Example:

#include <stdio.h>
void test1(void)
{
	printf("exit test1\n");
}

void test2(void)
{
	printf("exit test2\n");
}

int main()
{
	atexit(test1);
	atexit(test2);
	printf("exit main\n");
	return 0;
}

Process environment and process control (1): Start and terminate processes

  1. Process begins:

C program is started from the main function prototype is as follows:

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

The return value is usually main int type, correctly returns 0.

If the return value of main void or no, some compilers will give a warning, when the main return value is typically 0.

About main command-line parameters do not do too much to explain, in the following program shows this:

The following is the code fragment:

  #include <stdio.h>
  int main(int argc, char *argv[]) 
  { 
  int i; 
  for (i = 0; i < argc; i++) 
  printf("argv[%d]: %s\n", i, argv[i]); 
  return 0; 
  }

2. The process terminates:

C termination divided into two programs: normal termination and abnormal termination.

Terminates normally divided into: return, exit, _exit, _Exit, pthreade_exit

Abnormal middle finger is divided into: abort, SIGNAL, cancel threads response

Mainly talk about the first four, namely exit series function properly terminated.

The following is the code fragment:
#include
  void Exit (int Status);
  void the _Exit (int Status);
  #include
  void _exit (int Status);

The difference between the above three functions are:

Exit () (or return 0) I will call termination criteria handlers, and user space / O scavenger (e.g. fclose), _exit _Exit and do not call the kernel directly taken over by the clear

Management.

Thus, exit (0) in the main function is equivalent to return 0.

3. atexit termination handler:

ISO C provides a process most of the termination process 32 can register functions, these functions called by the sequence automatically registered by the opposite exit. If the same function registered repeatedly, will be

Called multiple times.

Prototype is as follows:

#include

int atexit(void (*func)(void));

Wherein the parameter is a function pointer termination function, the function does not return a reference value.

In the following program as an example:

The following is the code fragment:

 #include <stdio.h>
  static void myexit1() 
  { 
  printf("first exit handler\n"); 
  } 
  static void myexit2() 
  { 
  printf("second exit handler\n"); 
  } 
  int main() 
  { 
  if (atexit(my_exit2) != 0) 
  printf("can't register my_exit2\n"); 
  if (atexit(my_exit1) != 0) 
  printf("can't register my_exit1\n"); 
  if (atexit(my_exit1) != 0) 
  printf("can't register my_exit1\n"); 
  printf("main is done\n"); 
  return 0; 
  }

Operating results: (gcc)
$./ a.out
main is done
first exit handler
first exit handler
second exit handler

Released nine original articles · won praise 4 · Views 654

Guess you like

Origin blog.csdn.net/weixin_45494811/article/details/104042126