-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1c.c
More file actions
67 lines (55 loc) · 1.83 KB
/
1c.c
File metadata and controls
67 lines (55 loc) · 1.83 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 : 1c
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 getppid()
#include <signal.h> // For signal(), SIGPROF
// Signal handler for SIGPROF
void prof_handler(int signum)
{
printf("📊 ITIMER_PROF triggered: SIGPROF received\n");
exit(0); // Exit program after signal
}
int main()
{
// Register signal handler for SIGPROF
signal(SIGPROF, prof_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 profiling timer
if (setitimer(ITIMER_PROF, &timer, NULL) == -1)
{
perror("❌ setitimer failed");
exit(1);
}
printf("🟢 ITIMER_PROF set for 10s + 10μs\n");
// Consume user + system time to trigger the timer
for (long i = 0; i < 1e9; i++) getppid(); // Syscall + CPU
return 0;
}
/*
============================================================================
Output :
gcc -O0 -o 1c 1c.c ─╯
./1c
🟢 ITIMER_PROF set for 10s + 10μs
📊 ITIMER_PROF triggered: SIGPROF received
============================================================================
*/