Linux environment programming - perror, exit,

  
 

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

Defining Functions

void perror(const char *s); perror ("open_port");

Function Description

perror ( ) is used to output the reason for the previous function error to standard error (stderr)
. The string pointed to by the parameter s will be printed first, followed by the error reason string. The cause of this error is the string to be output according to the value of the global variable error. There is an error variable in the library function, and each error value corresponds to the type of error represented by a string. When you call the "some" function error, the function has reset the value of error. The perror function simply outputs some of the information you entered with the error corresponding to the current error. Exit:(#include <stdlib.h>) In the C main function we usually use return (0); this way returns a value. But this is limited to non-void situations, that is, non-void main(). Exit() is usually used in the subroutine to terminate the program. After use, the program automatically ends and jumps out of OS
. Exit(0) means the program exits normally, exit(1)/exit(-1) means the program exits abnormally. Exit() Ends the current process/current program/, and ends in the entire program as soon as exit is called. But if you use exit in main, regardless of whether main is defined as void, the value returned is valid, and exit does not need to consider the type, exit(1) is equivalent to return (1). For example: #include<stdlib.h> int main() { exit (1);//equivalent to return (1); } exit() is a function that ends a process that will delete the memory space used by the process. At the same time, the error message is returned to the parent process, and the wait system call will receive the return message in the parent process.

The process is just like human life. We create a process through the fork() function, then how do we stop the process?

Process Exit

1. Let any process exit in Linux

The process exits indicating that the process is about to end. In Linux, process exit is divided into normal exit and abnormal exit.

1> Normal exit

a. Perform return in the main() function.


b. Call the exit() function #include <stdlib.h> void exit(int status)

c.call the _exit() function #include < ;unistd.h> void _exit(int status)

status is an integer parameter that can be used to pass the state at the end of the process. In general, 0 means normal end; other values ​​indicate an error and the process ends abnormally. In the actual programming, you can use the wait system call to receive the return value of the child process, so different processing for different situations

2> Exception exit

a. Call the about function

b. The process receives a signal that causes the program to terminate.

Tiger-John Description: Regardless of the exit method, the system will eventually execute the same code in the kernel. This code is used to close the open file descriptor used by the process, freeing up the memory and other resources it occupies.



3>Compare the differences between the above exit methods

(1)The difference between exit and return:

A.exit is a function with parameters. After exit is executed, control is given to the system.

b.return is the return after the function is executed. After the renturn is executed, the control is given to the calling function.

(2) The difference between exit and abort:

a.exit is the normal termination process

b.about is abnormal termination.



Now we focus on the exit() and _exit() functions


2.exit() and _ The exit() learning

1>exit and _exit functions are used to terminate the process.

When the program is executed to exit or _exit, the system unconditionally stops all operations, clears various data structures including the PCB, and terminates the operation of the process.

2>exit is declared in the header file stdlib.h, and the _exit() declaration is declared in the header file unistd.h. The parameter exit_code in exit is 0 to indicate that the process terminates normally. If other values ​​indicate that an error occurred during program execution.

3> The difference between exit() and _exit():

a._exit() returns to the kernel immediately after execution, and exit() performs some cleanup first, then Control is given to the kernel.

b. When the _exit function is called, it will close all file descriptors of the process, clean up memory and other kernel cleanup functions, but will not refresh the stream (stdin, stdout, stderr ...). exit A function is a wrapper around the _exit function that calls _exit and flushes the stream before calling it.

Tiger-John Description:

The biggest difference between the exit() function and the _exit() function is that the exit() function checks the file open before calling the exit system, buffering the file. The contents of the area are written back to the file. Because of the standard library of Linux, there is an operation called "buffering I/O", which is characterized by a buffer in memory for each open file. Each time a file is read, several records are read continuously, so that the next time the file is read, it can be read directly from the buffer of the memory; likewise, each time the file is written, it is only a buffer written into the memory. Wait for a certain condition (such as reaching a certain number or encountering a specific character, etc.), and then write the contents of the buffer to the file at one time. This technique greatly increases the speed of file reading and writing, but it also brings a little trouble to programming. For example, if there is some data, it is considered that the file has been written. In fact, because the specific conditions are not met, they are only stored in the buffer. In this case, the process is directly closed by the _exit() function, and the data of the buffer is lost. . Therefore, to ensure the integrity of your data, you must use the exit() function.

c . Take a look at the differences between them by a function instance:

Function instance 1: exit.c

1 #include<stdio.h> 2 # Include<stdlib.h> 3 4 int main() 5 { 6 printf("using exit----\ "); 7 printf("This is the content in buffer\ "); 8 exit(0 );9 }

The function has been debugged

$ gcc exit.c -o exit$ ./exit

The result of the execution is:

using exit ----This is the content in buffer

Function instance 2:_exit.c

1 #include<stdio.h> 2 #include<unistd.h> 3 4 int main( Void) 5 { 6 printf("using _exit--\ "); 7 printf("This is the content in buffer"); 8 _exit(0); 9 } After the function has been debugged

$ gcc _exit.c -o _exit$ ./_exit

Execution results are:

using _exit--

Tiger-John Description:

1 The .printf function is a way to use buffered I/O, which automatically reads the record from the buffer when it encounters a newline character. So exit() will exit after the buffer data has been written, and the _exit() function will exit directly.

2. You can also change printf("This is the content in buffer"); in function instance 2 to printf("This is the content in buffer\ ") (ie in printf In the end, add a \ to see what the result is, why is this happening?)



After a process calls exit, the process does not disappear completely immediately. It is a data structure called a zombie process. The zombie process is a very special process. It has almost abandoned all memory space, has no executable code, can't be scheduled, just keeps a position in the process list, records the exit status of the process and other information for other processes. Collect, in addition, the zombie process no longer occupies any memory space. The

wait wait function is used to block the parent process (that is, the process that called wait) until a child process ends or the process receives a specified signal. If the parent process has no child processes or its child processes have ended, wait will return immediately. #include <sys/types.h>#include <sys/wait.h>pid_t wait(int *status)waitpid waitpid works the same as wait, but it doesn't have to wait for the first terminated child process. It also has several options, such as a non-blocking version of the wait function, as well as job control. In fact, the wait function is just a special case of the waitpid function. The waitpid function is called directly when the wait function is implemented inside Linux. #include <sys/types.h>#include <sys/wait.h>pid_t waitpid(pid_t pid, int *status, int options)

Copyright © Windows knowledge All Rights Reserved