在Java中,<< 5/0 QUOT;声明我的Linux机器上不火SIGFPE信号,为什么呢? [英] In java, &quot;5/0&quot; statement doesn&#39;t fire SIGFPE signal on my Linux machine, why?

查看:236
本文介绍了在Java中,<< 5/0 QUOT;声明我的Linux机器上不火SIGFPE信号,为什么呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个非常简单的C程序:

 #包括LT&;&stdio.h中GT;诠释主(){
    int类型的= 2;
    INT B = 0;
    的printf(%d个\\ N,A / B);
}

和与strace的运行:使用strace ./a.out并得到下面的输出(仅粘贴尾部)

  ......
则mprotect(0x600000,4096,PROT_READ)= 0
则mprotect(0x7f04c7fb8000,4096,PROT_READ)= 0
则munmap(0x7f04c7f96000,127640)= 0
--- SIGFPE(浮点异常)@ 0(0)---
+++通过SIGFPE杀害+++
浮点异常

输出我的期望相符,因为它是由SIGFPE信号杀害。

但是,用Java编写的同一个程序,没有得到SIGFPE信号,没有任何人知道如何Java进程被零除异常?

 公共类主要{  公共静态无效的主要(字串[] args){
    int类型的= 2;
    INT B = 0;
    的System.out.println(A / B);
  }
}

strace的java的-Xcomp主要

  ......
则mprotect(0xf6949000,8171520,PROT_READ | PROT_WRITE)= 0
则mprotect(0xf6949000,8171520,PROT_READ | PROT_EXEC)= 0
则munmap(0xf774f000,5727)= 0
mmap2(NULL,331776,PROT_READ | PROT_WRITE | PROT_EXEC,MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,-1,0)= 0xfffffffff68d0000
则mprotect(0xf68d0000,4096,PROT_NONE)= 0
克隆(child_stack = 0xf6920494, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID, parent_tidptr = 0xf6920bd8,TLS = 0xf6920bd8,child_tidptr = 0xff9c5520)= 958
futex的(0xf6920bd8,FUTEX_WAIT,958,NULL)= 0
exit_group(0)


解决方案

下面,它提出了一个SIGFPE。

您忘了告诉 strace的跟随孩子。在 -f 选项添加到 strace的,你应该会看到类似的东西:

  [PID 2304]阅读(3,\\ 312 \\ 376 \\ 272 \\ 276 \\ 0 \\ 0 \\ 0001 \\ 0N \\ n \\ 0 \\ 23 \\ 0I \\ t \\ 0 \\ 3 \\ 0J \\ 7 \\ 0K \\ n \\ 0L \\ 0M \\ n \\ 0N \\ 0......,2369)= 2369
[PID 2304] --- SIGFPE(浮点异常)@ 0(0)---
[PID 2304] rt_sigreturn(0x1c50800)= 5
[PID 2304]写(2,异常螺纹\\主\\,27Exception线程main)= 27
[PID 2304]写(2,java.lang.ArithmeticException:/......,40java.lang.ArithmeticException:/零)= 40
[PID 2304]写(2,\\ n,1

I wrote a very simple c program:

#include<stdio.h>

int main(){
    int a=2;
    int b=0;
    printf("%d\n", a/b);
}

and run it with strace: strace ./a.out and get below output (only paste tail part)

... ...
mprotect(0x600000, 4096, PROT_READ)     = 0
mprotect(0x7f04c7fb8000, 4096, PROT_READ) = 0
munmap(0x7f04c7f96000, 127640)          = 0
--- SIGFPE (Floating point exception) @ 0 (0) ---
+++ killed by SIGFPE +++
Floating point exception

The output matches my expectation, as it was killed by SIGFPE signal.

However, the same program written in Java, doesn't get SIGFPE signal, does anybody know how java processes "divide by zero" exception?

public class Main {

  public static void main(String[] args) {
    int a = 2;
    int b = 0;
    System.out.println(a / b);
  }
}

strace java -Xcomp Main

... ...
mprotect(0xf6949000, 8171520, PROT_READ|PROT_WRITE) = 0
mprotect(0xf6949000, 8171520, PROT_READ|PROT_EXEC) = 0
munmap(0xf774f000, 5727)                = 0
mmap2(NULL, 331776, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANONYMOUS|MAP_STACK, -1, 0) = 0xfffffffff68d0000
mprotect(0xf68d0000, 4096, PROT_NONE)   = 0
clone(child_stack=0xf6920494, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID, parent_tidptr=0xf6920bd8, tls=0xf6920bd8, child_tidptr=0xff9c5520) = 958
futex(0xf6920bd8, FUTEX_WAIT, 958, NULL) = 0
exit_group(0)  

解决方案

Here, it raises a SIGFPE.

You forgot to tell strace to follow children. Add the -f option to strace and you should see something similar to:

[pid  2304] read(3, "\312\376\272\276\0\0\0001\0n\n\0\23\0I\t\0\3\0J\7\0K\n\0L\0M\n\0N\0"..., 2369) = 2369
[pid  2304] --- SIGFPE (Floating point exception) @ 0 (0) ---
[pid  2304] rt_sigreturn(0x1c50800)     = 5
[pid  2304] write(2, "Exception in thread \"main\" ", 27Exception in thread "main" ) = 27
[pid  2304] write(2, "java.lang.ArithmeticException: /"..., 40java.lang.ArithmeticException: / by zero) = 40
[pid  2304] write(2, "\n", 1

这篇关于在Java中,<< 5/0 QUOT;声明我的Linux机器上不火SIGFPE信号,为什么呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆