-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1b.c
More file actions
67 lines (53 loc) · 1.81 KB
/
1b.c
File metadata and controls
67 lines (53 loc) · 1.81 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
67
/*
============================================================================
Name : 1b
Author : Piyush Singh
Description : Write a separate program (for each time domain) to set a interval timer in 10sec and
10micro second
a. ITIMER_REAL
b. ITIMER_VIRTUAL
c. ITIMER_PROF
Date: 18th Sep, 2025.
============================================================================
*/
#include <stdio.h> // For printf(), perror()
#include <stdlib.h> // For exit()
#include <sys/time.h> // For setitimer(), struct itimerval
#include <unistd.h> // For pause(), getppid()
#include <signal.h> // For signal(), SIGVTALRM
// Signal handler for SIGVTALRM
void virtual_handler(int signum)
{ printf("🎯 ITIMER_VIRTUAL triggered: SIGVTALRM received\n"); }
int main()
{
// Register signal handler for SIGVTALRM
signal(SIGVTALRM, virtual_handler);
// Define timer structure
struct itimerval timer;
// Set initial expiration to 10 seconds and 10 microseconds
timer.it_value.tv_sec = 10;
timer.it_value.tv_usec = 10;
// Set interval to 0 (one-shot timer)
timer.it_interval.tv_sec = 0;
timer.it_interval.tv_usec = 0;
// Start the virtual timer
if (setitimer(ITIMER_VIRTUAL, &timer, NULL) == -1)
{
perror("❌ setitimer failed");
exit(1);
}
printf("🟢 ITIMER_VIRTUAL set for 10s + 10μs\n");
// Consume user CPU time to trigger the timer
// Use a volatile variable to prevent compiler optimization
volatile long sink = 0;
for (long i = 0; i < 1e9; i++)
sink += i; // Keeps CPU busy
// Optional: sleep to allow signal to print before exit
sleep(1);
return 0;
}
/*
============================================================================
Output :
============================================================================
*/