-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
91d239d
commit 7c1c213
Showing
4 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Create a process and kill it by using kill() system call. | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
#include <sys/types.h> | ||
#include <sys/wait.h> | ||
#include <signal.h> | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
pid_t pid; | ||
int status; | ||
|
||
// kill(getpid(), SIGKILL); // kills itself | ||
|
||
// parent process created first, then child process | ||
// after 5 second delay, parent process being killed | ||
// so the child process never gets a chance to terminate | ||
|
||
pid = fork(); | ||
if (pid == 0) | ||
{ | ||
printf("Child process is created. PID: %d\n", getpid()); | ||
sleep(10); | ||
printf("Child process is terminated.\n"); | ||
} | ||
else if (pid > 0) | ||
{ | ||
printf("Parent process is created. PID: %d\n", getpid()); | ||
sleep(5); | ||
kill(pid, SIGKILL); | ||
wait(&status); | ||
printf("Parent process is terminated.\n"); | ||
// print pid | ||
} | ||
else | ||
{ | ||
perror("fork"); | ||
exit(1); | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <signal.h> | ||
#include <unistd.h> | ||
|
||
void sigint_handler(int sig) | ||
{ | ||
printf("Received SIGINT signal.\n"); | ||
if (signal(SIGINT, SIG_DFL) == SIG_ERR) | ||
{ | ||
printf("Error setting signal handler for SIGINT.\n"); | ||
exit(1); | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
// Install the signal handler for SIGINT | ||
signal(SIGINT, sigint_handler); | ||
|
||
printf("Press Ctrl+C to send a SIGINT signal.\n"); | ||
|
||
// Loop forever | ||
while (1) | ||
{ | ||
// Do some work | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <signal.h> | ||
#include <unistd.h> | ||
|
||
// First signals will be handled by the signal handlers | ||
// after that, the default signal handlers will be called | ||
|
||
void sigint_handler(int sig) | ||
{ | ||
printf("Received SIGINT signal.\n"); | ||
if (signal(SIGINT, SIG_DFL) == SIG_ERR) | ||
{ | ||
printf("Error setting signal handler for SIGINT.\n"); | ||
exit(1); | ||
} | ||
} | ||
|
||
void sigterm_handler(int sig) | ||
{ | ||
printf("Received SIGTERM signal.\n"); | ||
if (signal(SIGTERM, SIG_DFL) == SIG_ERR) | ||
{ | ||
printf("Error setting signal handler for SIGTERM.\n"); | ||
exit(1); | ||
} | ||
} | ||
|
||
void sigkill_handler(int sig) | ||
{ | ||
// Handling SIGKILL is not possible, so this function will never be called | ||
printf("Received SIGKILL signal.\n"); | ||
if (signal(SIGKILL, SIG_DFL) == SIG_ERR) | ||
{ | ||
printf("Error setting signal handler for SIGKILL.\n"); | ||
exit(1); | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
// Install the signal handlers | ||
signal(SIGINT, sigint_handler); | ||
signal(SIGTERM, sigterm_handler); | ||
signal(SIGKILL, sigkill_handler); // SIGKILL cannot be caught or ignored | ||
|
||
printf("Press Ctrl+C to send a SIGINT signal.\n"); | ||
printf("To kill this process by SIGTERM, use the following command: kill -SIGTERM %d\n", getpid()); | ||
printf("To kill this process by SIGKILL, use the following command: kill -9 %d\n", getpid()); | ||
|
||
// Loop forever | ||
while (1) | ||
{ | ||
// Do some work | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <signal.h> | ||
#include <unistd.h> | ||
|
||
void critical_operation() | ||
{ | ||
printf("Performing critical operation... ('enter' to continue)\n"); | ||
// Do some work | ||
while (1) | ||
{ | ||
if (getchar() == '\n') | ||
{ | ||
break; | ||
} | ||
} | ||
printf("Critical operation complete.\n"); | ||
} | ||
|
||
int main() | ||
{ | ||
sigset_t mask; // create a signal set | ||
sigemptyset(&mask); // initialize the signal set | ||
sigaddset(&mask, SIGINT); // add SIGINT to the signal set | ||
sigaddset(&mask, SIGTSTP); // add SIGTSTP to the signal set | ||
sigprocmask(SIG_BLOCK, &mask, NULL); // block the signals in the signal set | ||
|
||
// Pressing Ctrl+C will not interrupt the program | ||
printf("Press Ctrl+C to interrupt the program...\n"); | ||
|
||
// Pressing Ctrl+Z will not interrupt the program | ||
printf("Press Ctrl+Z to send a SIGTSTP signal...\n"); | ||
|
||
// to SIGKILL the program, use the following command: kill -9 <pid> from another terminal | ||
printf("To kill this process by SIGKILL, use the following command: kill -9 %d\n", getpid()); | ||
|
||
// Perform a critical operation that must not be interrupted | ||
critical_operation(); | ||
// Unblock the SIGINT signal | ||
sigprocmask(SIG_UNBLOCK, &mask, NULL); | ||
|
||
printf("Program complete.\n"); | ||
|
||
return 0; | ||
} |