Error handling mechanism of Linux system programming

Source: Internet
Author: User

Let's take a look at the code before explaining the LIUNX error handling mechanism:

1#include <sys/types.h>2#include <sys/stat.h>3#include <fcntl.h>4#include <stdio.h>5#include <stdlib.h>6 7 intMainvoid)8 {9     intFD;TenFd=open ("ABC", o_wronly); One     if(fd<0){ Aprintf"error:fd=%d\n", FD); -          -     }    the  -     return 0; -}

This section of code opens a file named ABC with the function open, and the Help document for the Open function is as follows:

Open () Return the new file descriptor, or-1 If an error occurred (in which case, errno is set appropriately).

The open () function returns a new file descriptor and returns 1 if an error occurs (in the case of an error, the errno is set accordingly)

The code above only determines if the open has an error, and what error cannot be judged. Because there are many reasons why the above code can cause an open function error, for example, the file ABC does not exist, or the file ABC exists, but there is no write permission. This can be an error. So how to determine exactly what caused the open function at the wrong?

From the help document above we know that when the open function goes wrong, not only does it return a-1, the function sets the value of errno. So what kind of errno is it? Let's take a look at errno's statement or definition.

In the file/usr/include/errno.h there is the following code

#ifndef errno

extern int errno;
#endif

From here we can see that the errno is an integral type and is a global integer variable.

In fact, errno is an error number, when the error occurs, each a different error has a number, the value of this number will be stored in the errno, according to the numbering system can determine what error occurred, since the system can determine what error occurred, then you can print out the wrong information. The function that prints the error message is perror (), and of course there are some other functions that are printed incorrectly, and we don't have an example here, and if you want to see it, you can view the help document through the Man 2 perror. Here we list some of the wrong definitions, the definition of the error number is placed in the/usr/include/asm-generic/directory of errno-base.h and errno.h two header files, the following are the error numbers defined in these files, but not listed.

#defineEperm 1/* Operation not permitted */#defineENOENT 2/* No such file or directory */#defineEsrch 3/* No such process */#defineEINTR 4/* Interrupted system call */#defineEIO 5 */I/O error */#defineEnxio 6/* No such device or address */#defineE2big 7/* Argument list too long */#defineEnoexec 8/* Exec format Error */#defineEBADF 9/* Bad file number */#defineEchild/* No child processes */#defineEagain/* Try again */#defineENOMEM/* Out of memory */#defineEacces/* Permission denied */#defineEfault/* Bad address */#defineENOTBLK/* Block Device required */#defineEbusy/* Device or resource busy */#defineEexist/* File exists */#defineExdev */* cross-device link */#defineEnodev/* No such device */#defineEnotdir/* Not a directory */#defineEisdir/* is a directory */#defineEINVAL */* Invalid argument */#defineEnfile/* File table overflow */#defineEmfile/* Too many open files */#defineEnotty/* Not a typewriter */#defineEtxtbsy/* Text file Busy */#defineEfbig/* File too large */#defineENOSPC/* No space left on device */#defineEspipe/* Illegal seek * *#defineErofs/* read-only File System */1,1Top

Then we can rewrite the test program.

1#include <sys/types.h>2#include <sys/stat.h>3#include <fcntl.h>4#include <stdio.h>5#include <stdlib.h>6 7 intMainvoid)8 {9     intFD;TenFd=open ("ABC", o_wronly); One     if(fd<0){ Aprintf"error:fd=%d\n", FD); -Perror ("Open File ABC");//This line prints out the wrong information according to the system's number -     }    the  -     return 0; - } -~

Error handling mechanism of Linux system programming

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.