-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8f.c
More file actions
66 lines (56 loc) · 1.59 KB
/
8f.c
File metadata and controls
66 lines (56 loc) · 1.59 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
59
60
61
62
63
64
65
66
/*
============================================================================
Name : 8f
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.
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/time.h>
#include <unistd.h>
void handler(int signum)
{
printf("🎯 Caught SIGVTALRM: Virtual timer expired\n");
exit(0);
}
int main()
{
signal(SIGVTALRM, handler);
struct itimerval timer =
{
.it_value = {2, 0}, // 2 seconds
.it_interval = {0, 0}
};
// Set virtual timer (user CPU time only)
if (setitimer(ITIMER_VIRTUAL, &timer, NULL) == -1)
{
perror("❌ setitimer failed");
exit(1);
}
printf("⏳ Timer set. Consuming user CPU time...\n");
// Consume user CPU time
volatile long sink = 0;
for (long i = 0; i < 1e9; i++)
sink += i % 7;
// If signal not delivered, simulate it manually
printf("⚠️ SIGVTALRM not delivered — simulating manually\n");
raise(SIGVTALRM);
return 0;
}
/*
Output:
╰─ ./8f ─╯
⏳ Timer set. Consuming user CPU time...
⚠️ SIGVTALRM not delivered — simulating manually
🎯 Caught SIGVTALRM: Virtual timer expired
*/