stop the interrupts and termination in linux using C

The tutorial is how to stop the interrupts and termination in linux using C.
The communication with the the program using the Linux kernel can be done by using the mechanism called signal





Often we use the Ctrl  +  C interrupt to stop any running C program in terminal.
But we can easily stop it.
Firstly, let's see an example of Ctrl  +  C


#include<stdio.h>
void main()
{
while(1)
printf("Stop me using the Ctrl + C");
}
Output is
stop the interrupts and termination in linux using C

Now this program is infinite loop and can be stopped by using the Ctrl  +  C interrupt.
But we can easily disable the Ctrl  +  C interrupt.
See the next C program.

#include<stdio.h>
#include<signal.h>   //for signal ()
void disable_Ctrlc(int signum)
{
printf("Sorry even Ctrl + C is disabled");
}
void main()
{
signal(SIGINT,disable_Ctrlc);   //it is library function
while(1)
printf("Ctrl + C also can't stop me");

}
stop the interrupts and termination in linux using C

As you can see from above screenshot that this program will not be stopped even after using the Ctrl + C interrupt.
  1. signal() is the library function which registers a signal handler for various signals.
  2. First parameter of signal specifies the ID of the signal that we wish to register.Here we are using the signal SIGINT(INT->  interrupt).
  3. Second parameter is the address of the function that should be called when signal is received
  4. So when we press Ctrl + C, the registered handler, which we named disable_Ctrlc, is gonna be called. As we haven't done anything with it, it'll not stop the running program.

The question is, How to stop this program?
well, we can use the ps command to see the PID of this process.
And then stop that process using kill command.
Now the twist is that we can also stop kill command to stop our program.
See the below see C code

#include<stdio.h>
#include<signal.h>   //for signal ()
void disable_Ctrlc(int signum)
{
printf("Sorry even Ctrl + C is disabled");
}
void disable_kill(int signum)
{
printf("Sorry even kill is disabled");
}
void main()
{
signal(SIGINT,disable_Ctrlc);   //to register signal handler fo SIGINT
signal(SIGTERM,disable_kill);   //to register signal handler fo SIGTERM
while(1)
printf("Ctrl + C and kill also can't stop me");

}

  1. So when we press Ctrl + C, the registered handler, which we named disable_Ctrlc, is gonna be called. As we haven't done anything with it, it'll not stop the running program.
  2. And when we use kill PID the registered handler, which we named disable_kill, is gonna be called. As we also haven't done anything with it, it'll also not be able stop the running program.

Now if we want to stop our unstoppable program, we have to use kill -SIGKILL PID command where PID is the PID of our program. As SIGKILL signal is uncatchable signal it will forcible terminate our program.

So guys, it's all about how to stop the interrupts and termination in linux using C.
You can ask anytime if you wanna know more. Keep learning.

2 comments:

  1. I remember my experience with hackers i contacted online, i almost lost hope in finding out if my husband was loyal to me and has been loyal in our 20 years marriage,I lost a lot of funds in paying these so called hackers so i made up my mind that if i get scammed again i'll just forget about finding out what God doesn't want me to know, Then i saw reviews about ( HACKINTECHNOLOGY@GMAIL.COM ) and decided contact him and if and if i hear any stupid excuse won't contact anybody and just accept that God doesn't want me to then he proved to me that he isn't a cheat by working a sample job for me but to my greatest surprise he did the job and sent results to me fast, i was so happy but sad part was i divorced my husband, and i will drop an advise for people who wants any kind of hacking services stopped being scammed by people who impersonate being hackers, (ETHICALHACKERS009@GMAIL.COM) for a fast and clean job.Here is his cell number +1 (669) 225 2253 , Thank me later.

    ReplyDelete
  2. Hello guys, I just want to share my joyful experience about HACK VANISH CREDIT SPECIALIST. Before the coronavirus pandemic, I had criminal Judgments, few evictions, and late payment which negatively impacted my credit score down to 487 and left me in difficult financial situation, as a single mum of 3, I needed a loan to keep the family running after I lost my husband as a result of COVID-19, all hope were lost, until Mrs. Olivia referred me to HACK VANISH, a credit genius who was very helpful in fixing my credit 6 days after I reached out to him via Email: hackvanish (At) gmail (Dot) com and Phone: +1(747) 293-8514, throughout the process, I felt so involved because an open line of communication was maintained till I got my FICO score restored to 796, currently my loan application has been approved. If you need to hire a credible hacker, HACK VANISH is highly recommended.

    ReplyDelete