My opinion of errno

Source: Internet
Author: User

Errno can help us find the error message of the system function. For example, the open function, if returned normally, its return value is a non-negative integer. The exception will return-1, and the system function will set the value of errno, so that we can understand the cause of the error.

The correct use of errno is to first clarify the system function call exception (such as return 1 or null), and then to determine the value of the errno. We cannot directly judge the value of errno to determine whether a system function call has occurred. Because the errno value is not zeroed, it only assigns the corresponding value when an exception occurs. And of course we don't have to clear it ourselves.

In addition, errno is thread-safe. It can be understood that each thread has a errno value that does not interfere with each other.

There are two system functions that can make the error message more readable, as follows:

#include <string.h>

char *strerror (int errnum);

Returns:pointer to message string

#include <stdio.h>

void perror (const char *msg);

by judging the value of errno, we can make our system more robust. One example is that some operating systems will not continue to block in the previous system function when the signal is processed at slow systems call, while setting the errno value to eintr. Another example is when the network connection is abnormal, we can slow down for a period of time and then connect without having to exit directly.

My opinion of errno

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.