Now, I am sending a signal(Ctrl+C) from Child to Parent, using 'kill' command. 'Kill' needs pid of the process & Signal to be sent.
Steps can be defined as:
When we execute the program
Child waits for Ctrl+C, as Parent is sleeping. Within moments, the rest of the program executes
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 | #include <stdio.h> #include <unistd.h> #include <signal.h> #include <sys/wait.h> // Function telling what to do when we catch CTRL+C void catch_signal (int sig_num) { printf("SIGNAL FUNCTION>> Caught the signal %d by process with PID=%d\n",sig_num,getpid()); fflush(stdout); } int main() { pid_t child_pid; int status; child_pid=fork(); if (child_pid == 0) { //This is Child Section //Registering the Signal Handler signal(SIGINT,catch_signal); printf("CHILD>>pid=%d Ppid=%d Waiting for Ctrl+C\n",getpid(),getppid()); fflush(stdout); pause(); printf("CHILD>>Caught the signal\n"); } else { //This is Parent Section printf("PARENT>>Waiting couple of seconds to make sure, child waits for SIGINT\n"); sleep(2); kill(child_pid,SIGINT); printf("PARENT>>Sent the SIGINT signal to child with pid=%d\n",child_pid); wait(&status); } return 0; } |
Steps can be defined as:
- Process is forked
- Child registers signal handler
- Child waits for Ctrl+C
- Parent sleeps.
- Parent sends Ctrl+C using Kill command.
- Parent waits for Child to complete
When we execute the program
root@kali:/media/root/persistence# ./a.out PARENT>>Waiting couple of seconds to make sure, child waits for SIGINT CHILD>>pid=3722 Ppid=3721 Waiting for Ctrl+C
Child waits for Ctrl+C, as Parent is sleeping. Within moments, the rest of the program executes
root@kali:/media/root/persistence# ./a.out PARENT>>Waiting couple of seconds to make sure, child waits for SIGINT CHILD>>pid=3722 Ppid=3721 Waiting for Ctrl+C SIGNAL FUNCTION>> Caught the signal 2 by process with PID=3722 PARENT>>Sent the SIGINT signal to child with pid=3722 CHILD>>Caught the signal
No comments:
Post a Comment