Signal in LINUX

Hi friends,

Currently, I'm working in wake-up issue in my project. I wanna inform android when my host OS windows wakes up. My project leader gave me an idea to send a signal from windows to android in the shell and use that signal to wake android when windows wakes up, like in exam days I keep alarm by 4am, my bad wakes up and give me a kick ;). But I'm zero at LINUX signal concept, as usual i studied it. Now, I'm typing here for my future ref. and other beginner's use.

SIGNAL IN LINUX

Now, we use CTRL + C to terminate something in LINUX. But, we don't wanna let someone to terminate our code with CTRL + C. So, what we have to do now. We have to monitor the user input and we should not let the system to perform any operation when user gives CTRL +C.

But, Instead what we can do in LINUX is, CTRL + C will generate a signal SIGINT in LINUX, so we can register a function to this signal in our code. So that when we user press CTRL + C, it will call our function, so our code won't be terminated.

What is signal in LINUX ? 
 LINUX has a set of signals. One nice example is, kill -9 PID, which we use to kill a process. Here, we are sending KILL signal to a process. Similarly, few other signals are available in LINUX. (SRY, i don't know to explain it, pls ask prof.google :( )

How can we register a function to a signal ?
here is the syntax,

signal(SIGINT, sig_handler)
SIGINT - signal generated on pressing CTRL + C. It can be any LINUX signal.
sig_handler - our function, which will be executed on receiving SIGINT signal.

What should be the sig_handler signature? 
here it is,
void sig_handler(int signo) {}
signo - signal number crossponding to the register signal.
Inside the function, we can write our own story.

Ok, How the final code will be? 
here is the simple one,
#include <stdio.h>
#include
<signal.h>
#include <unistd.h>

void sig_handler(int signo)
{
  if (signo == SIGINT)
    printf("received SIGINT\n");
}

int main(void)
{
  if (signal(SIGINT, sig_handler) == SIG_ERR)
  printf("\ncan't catch SIGINT\n");
  // A long long wait so that we can easily issue a signal to this process
  while(1)
    sleep(1);
  return 0;
}

Now, run the code and press CTRL + C. It will print "^Creceived SIGINT".

But, LINUX has alot of signals, is it possible to do for all signal of LINUX?
 Expect SIGKILL and SIGSTOP, we can use any signal in the place of SIGINT. So, change signal you would like register, change the body of sig_handler function as you like. Your coding will be done.

In my case, I have to generate a signal on my own... I choose to send user signal(SIGUSR1)...
So, the code will be?
here it is,

#include <stdio.h>
#include
<signal.h>
#include <unistd.h>

void sig_handler(int signo)
{
  if (signo == SIGUSR1)
    printf("received SIGUSR1\n");
}

int main(void)
{
  if (signal(SIGUSR1, sig_handler) == SIG_ERR)
  printf("\ncan't catch SIGUSR1\n");
  // A long long wait so that we can easily issue a signal to this process
  while(1)
    sleep(1);
  return 0;
}

Thats all, compile and run it :)... But how to send user signal? The following line will do that...
KILL -USR1 PID
 Got the signal!!!....

0 comments:

Newer Post Older Post Home

About Me

My photo
Hi everyone,myself Alagappan...electronic and communication engg. student... living in madurai... interested in everything... want to achieve something great in my lifetime...

Followers


Recent Comments