About Error handling in C

本文介绍了C语言中标准库和Unix/Linux系统调用提供的错误处理机制。通过使用errno、strerror和perror等函数,可以有效地捕获并处理程序运行过程中出现的错误。

C Programming language itself doesn't provide any good mechanism for error
handling. But standard library and Unix/Linux system calls do provide a
convinient way to handle errors. External variable errno, function strerror and
function perror from library <errno.h>, <string.h> and <stdio.h> are all the
things you need to make your programs more robust.
The general way to use errno to handle error is like this:
     
if you want to print error messages onto standard output(screen), you can use
  perror as an alternative.
  perror( const char *msg );
  perror( msg ); is equal to printf( "%s: %s/n", msg, strerror( errno ) );
There is a little difference between perror and strerror, the former is more
elegant and easier to use, but it can print messages on standard output only and
its format is predefined which cannot be changed. strerror provides much more
freedom. You can write the message to anywhere and use any format you want.

We know something goes wrong according to the return value of some functions
like fopen etc. Then we can retrieve the detail information of the error through
errno and strerror and output the info to user or do some recovery operations.
But many times, we cannot know abnormal things happened through return value.
fgets, for instance, would return NULL if either EOF reached or error occured.
For these situations, we need to check errno to see whehter error has occured.
Before you do that you have to know two rules about errno: First, its value
is never cleared by a routine if an error does not occur. Therefore, we should
examine its value only when the return value from a function indicates that an
error occurred. Second, the value of errno is never set to 0 by any of the
functions, and none of the constants defined in <errno.h> has a value of 0.
As a result, if you want to know whether there is an error through errno, you'd
better initialize it, then see if it remains its initial value. Like this:
       

In addition, if you write some error handling routines for system calls, you'd
better save the original errno. Because, an error could occur during your
handling routine and errno could be overwritten. Take err_doit in comhdr.h for
example:
     
Here, vsprintf might fail and modify errno, if so, the following strerror(errno)
will no longer be the original error. Therefore, we have to save a copy of errno
in errno_save, just in case.

### PCIe Logical Idle Error Explanation In systems utilizing PCI Express (PCIe), encountering a logical idle error signifies an issue within the communication protocol or hardware configuration. This type of error occurs when there is a discrepancy between expected and actual states during periods where no transactions are taking place on the link, indicating potential problems with how devices enter or maintain low-power states[^1]. For understanding this further from another context related to system architecture but not directly about PCIe errors, insights into graphics pipeline stages can offer some background knowledge regarding state management and transitions which might be analogous in certain aspects though specific details would differ significantly as described elsewhere[^2]. However, direct correlation may not apply here. ### Solutions for PCIe Logical Idle Error Addressing such issues involves several approaches: #### Verification of Configuration Settings Ensure that all relevant parameters concerning power management features like ASPM (Active State Power Management) settings are correctly configured both at BIOS/UEFI level and operating system side. Incorrect configurations could lead to improper handling of idle conditions causing these types of errors. #### Firmware Updates Applying latest firmware updates available for involved components including motherboard chipsets, network interface cards, GPUs etc., ensures compatibility fixes addressing known bugs potentially leading to similar anomalies have been implemented by manufacturers. #### Driver Adjustments Updating drivers associated with affected peripherals often resolves conflicts arising due to outdated software stacks interacting improperly under new operational scenarios introduced either through changes made within U-boot environment modifications or other factors impacting early boot processes before full OS control takes over operations completely. ```bash # Example command sequence for updating driver in Linux sudo apt-get update sudo apt-get install --only-upgrade <driver_package_name> ``` #### Diagnostic Tools Utilization Employ diagnostic utilities provided by hardware vendors specifically designed to test and analyze PCIe links performance along with identifying any misconfigurations present across connected elements forming complex interconnect topologies inside modern computing platforms.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值