-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11.c
More file actions
67 lines (58 loc) · 2.05 KB
/
11.c
File metadata and controls
67 lines (58 loc) · 2.05 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 : 11
Author : Piyush Singh
Description : Write a program to ignore a SIGINT signal then reset the default action of the SIGINT signal -
use sigaction system call.
Date: 19th Sep, 2025.
============================================================================
*/
#include <stdio.h> // For printf()
#include <signal.h> // For sigaction()
#include <unistd.h> // For sleep()
int main()
{
struct sigaction sa;
// Step 1: Ignore SIGINT
sa.sa_handler = SIG_IGN; // Ignore handler
sigemptyset(&sa.sa_mask); // No blocked signals
sa.sa_flags = 0; // No special flags
sigaction(SIGINT, &sa, NULL);
printf("🚫 SIGINT is now ignored for 10 seconds. Try pressing Ctrl+C...\n");
for (int i = 0; i < 10; i++)
{
printf("⌛ Still ignoring SIGINT... (%d)\n", i + 1);
sleep(1);
}
// Step 2: Reset SIGINT to default behavior
sa.sa_handler = SIG_DFL; // Default handler
sigaction(SIGINT, &sa, NULL);
printf("🔁 SIGINT reset to default. Ctrl+C will now terminate the program.\n");
// Step 3: Wait again so user can test Ctrl+C
while (1)
{
printf("🟢 Waiting... Press Ctrl+C to exit.\n");
sleep(2);
}
return 0;
}
// If you press Ctrl+C during the first 10 seconds, nothing happens.
// After that, it terminates the program as expected.
/*
Output:
╰─ ./11 ─╯
🚫 SIGINT is now ignored for 10 seconds. Try pressing Ctrl+C...
⌛ Still ignoring SIGINT... (1)
⌛ Still ignoring SIGINT... (2)
⌛ Still ignoring SIGINT... (3)
⌛ Still ignoring SIGINT... (4)
⌛ Still ignoring SIGINT... (5)
^C⌛ Still ignoring SIGINT... (6)
^C⌛ Still ignoring SIGINT... (7)
^C⌛ Still ignoring SIGINT... (8)
^C⌛ Still ignoring SIGINT... (9)
⌛ Still ignoring SIGINT... (10)
🔁 SIGINT reset to default. Ctrl+C will now terminate the program.
🟢 Waiting... Press Ctrl+C to exit.
^C
*/