【linux】SIGSEGV与SIGBUS的区别分析

Date: 2018/9/25


1、参考

https://blog.csdn.net/richu123/article/details/71106672
https://www.cnblogs.com/jiu0821/p/7207082.html
https://zh.cppreference.com/w/c/program/SIG_types
https://www.geeksforgeeks.org/segmentation-fault-sigsegv-vs-bus-error-sigbus/

2、SIGSEGV

发生段错误后系统会抛出 SIGSEGV 信号,之后调用默认的信号处理函数,产生core文件 ,然后关闭程序 。那什么是SIGSEGV信号呢?
官方解释如下:
SIGSEGV — Segment Fault. The possible cases of your encountering this error are:

1.buffer overflow — usually caused by a pointer reference out of range.

2.stack overflow — please keep in mind that the default stack size is 8192K.

3.illegal file access — file operations are forbidden on our judge system.

3、SIGSEGV与SIGBUS的区别分析

SIGBUS与SIGSEGV信号的一般区别如下:

1、SIGBUS(Bus error)意味着指针所对应的地址是有效地址,但总线不能正常使用该指针。通常是未对齐的数据访问所致。

2、SIGSEGV(Segment fault)意味着指针所对应的地址是无效地址,没有物理内存对应该地址。

Segmentation Fault (SIGSEGV) vs Bus Error (SIGBUS)
Segmentation fault(SIGSEGV) and Bus error(SIGBUS) are signals generated when serious program error is detected by the operating system and there is no way the program could continue to execute because of these errors.
1、Segmentation Fault (also known as SIGSEGV and is usually signal 11) occur when the program tries to write/read outside the memory allocated for it or when writing memory which can only be read.In other words when the program tries to access the memory to which it doesn’t have access to. SIGSEGV is abbreviation for “Segmentation Violation”.
Few cases where SIGSEGV signal generated are as follows,

  1. Using uninitialized pointer
  2. De-referencing a NULL pointer
  3. Trying to access memory that the program doesn’t own (eg. trying to access an array element
    out of array bounds).
  4. Trying to access memory which is already de-allocated (trying to use dangling pointers).
    Please refer this article for examples.

2、 Bus Error (also known as SIGBUS and is usually signal 10) occur when a process is trying to access memory that the CPU cannot physically address.In other words the memory tried to access by the program is not a valid memory address.It caused due to alignment issues with the CPU (eg. trying to read a long from an address which isn’t a multiple of 4). SIGBUS is abbrivation for “Bus Error”.
SIGBUS signal occurs in below cases,

  1. Program instructs the CPU to read or write a specific physical memory address which is not valid / Requested physical address is unrecognized by the whole computer system.
  2. Unaligned access of memory (For example, if multi-byte accesses must be 16 bit-aligned, addresses (given in bytes) at 0, 2, 4, 6, and so on would be considered aligned and therefore accessible, while addresses 1, 3, 5, and so on would be considered unaligned.)

The main difference between Segmentation Fault and Bus Error is that Segmentation Fault indicates an invalid access to a valid memory, while Bus Error indicates an access to an invalid address.
参考自:https://www.geeksforgeeks.org/segmentation-fault-sigsegv-vs-bus-error-sigbus/

4、其他SIG类型
SIGTERM	发送给程序的终止请求
SIGSEGV	非法内存访问(段错误)
SIGINT	外部中断,通常为用户所发动
SIGILL	非法程序映像,例如非法指令
SIGABRT	异常终止条件,例如 abort() 所起始的
SIGFPE	错误的算术运算,如除以零

参考自:https://zh.cppreference.com/w/c/program/SIG_types


THE END!

原文地址:https://www.cnblogs.com/SoaringLee/p/10532363.html