linux - SIGALRM 以负数给出秒的常数值

标签 linux

achul@achul:~/chennai/linux/SM_practise$ cat doubt.c
#include<stdio.h>
#include<signal.h>
main()
{
    int ret;
    printf("\nhello x : if i'm give -ve alarm second then what it will return \n");
    ret=alarm(-5);
    printf("ret=%d\n",ret);//
    ret=alarm(8);
    printf("ret=%d\n",ret);// why exactly 2G
    while(1);
} 

因为警报第一个参数是无符号整数,所以可能是将 -5 转换为无符号值。如果我在第二个警报中给出任何负值警报,它只返回 2147483647 吗?为什么会这样……有什么理论吗?

最佳答案

原因是您为延迟指定了 4+ 十亿 (4,294,967,296 - 5) 秒——大约 136 年。

这就是将 signed int 提升为 unsigned int 时发生的情况。 alarm() 的声明是:

unsigned int alarm (unsigned int seconds);

-5 作为 32 位值在 twos-complement 中为 1111 1111 1111 1111 1111 1111 1111 1011 (这是所有现代 CPU 使用的)二进制。

将该位模式视为一个 unsigned int 使其值为 4,294,967,291。

关于linux - SIGALRM 以负数给出秒的常数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33839135/

相关文章:

linux - 用于检查进程是否已在运行并在是时退出的 Shell 脚本

c++ - 使用 C++ 在 32 位 Linux 中查找进程可用的剩余内存

linux - Linux/Ubuntu (gui) 的 WSDL 资源管理器/查看器

linux - PostgreSQL 无法启动 : "server.key" has group or world access

c - RDMA - ibv_post_send 如何知道 CQ?

linux - 需要使用批处理文件或脚本从 Windows 计算机创建 Linux 用户。我可以通过 ssh 访问服务器。

c - 在 linux 中测量 c 程序的运行时间

c - 如何向c中的线程发送信号?

css - 自动换行不适用于 Linux

java - 使用 ffmpeg android 在视频上添加自定义日期