-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8c.c
More file actions
58 lines (49 loc) · 1.55 KB
/
8c.c
File metadata and controls
58 lines (49 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
============================================================================
Name : 8c
Author : Piyush Singh
Description : Write a separate program using signal system call to catch the following signals.
a. SIGSEGV
b. SIGINT
c. SIGFPE
d. SIGALRM (use alarm system call)
e. SIGALRM (use setitimer system call)
f. SIGVTALRM (use setitimer system call)
g. SIGPROF (use setitimer system call)
Date: 19th Sep, 2025.
============================================================================
*/
// **********************Flag**********************
/*
On macOS, integer division by zero doesn’t reliably raise SIGFPE.
Instead, the program either crashes silently or prints garbage like 0.
I tried using volatile to prevent compiler optimization, but it still didn’t work.
macOS doesn’t support feenableexcept() either, so I couldn’t enable floating-point traps.
Unlike Linux, macOS handles these errors differently and doesn’t expose low-level control
over floating-point exceptions. So to test the signal handler, I had to simulate it using
raise(SIGFPE) — just to prove the handler works.
*/
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
// #include <fenv.h> // For feenableexcept()
void handler(int signum)
{
printf("➗ Caught SIGFPE: Arithmetic error (divide by zero)\n");
exit(1);
}
int main()
{
signal(SIGFPE, handler);
int x = 1;
int y = 0;
int z = x / y;
// Manually raise SIGFPE to test handler
raise(SIGFPE);
return 0;
}
/*
Output :
./8c
➗ Caught SIGFPE: Arithmetic error (divide by zero)
*/