Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

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!!!....

Android Input Device Events

Hi friends,

I have got task dealing with touch monitor. I have to implement multitouch in our project. In the middle, I wanted to see input events. My teammate gave me the keyword to see the input event and to manually send input event to any devices. I would like to put it here. So, Me and You can refer, when we want it...

Android getevent and sendevent 

ya, as you guess...
  • getevent is to read the input event from the device(/dev/input/device-name) and
  • sendevent is to write the input event to the device(/dev/input/device_name) manually
Before, the getevent and sendevent... Lets have a brief look into the input event...
Input Event : Basically inputs are written has (sequence of ) events in the device(/dev/input/event(device) in case of Linux). All input events comprise of three values namely,
  • Type  : type of the event like Key, Sync, Misc, LED, etc.
  • Code  : value corresponding to the key, which is pressed.
  • Value : Zero for release and One for click/press, in case of key press and mouse click event. some running number in other case( I don't have too much knowledge in it).
With these 3, OS will understand the input given by us and perform the action to the corresponding input.
 
Example : when we press key 2 in the keyboard... event sequence will be like the below

In ubuntu,  (I wrote a c program to read the input even from device and the output is below)
Event: type 4 (Misc), code 4 (ScanCode), value 458783   [Conveying the event is scancode]
Event: type 1 (Key), code 3 (2), value 1                             [key 2 pressed]
Event: type 0 (Sync), code 0 (Sync), value 0                      [sync event]
Event: type 4 (Misc), code 4 (ScanCode), value 458783    [conveying the event is scancode]
Event: type 1 (Key), code 3 (2), value 0                             [key 2 release]
Event: type 0 (Sync), code 0 (Sync), value 0                      [sync event]

In andriod,
Event: type 1 (Key), code 3 (2), value 1                             [key 2 pressed]
Event: type 0 (Sync), code 0 (Sync), value 0                      [sync event]
Event: type 1 (Key), code 3 (2), value 0                             [key 2 release]
Event: type 0 (Sync), code 0 (Sync), value 0                      [sync event]

This is for pressing the key 2 once... This will be written into the device, OS will read it and perform the actions.

ok now...

getevent : will print the event written into the device.
syntax    : getevent device
example : getevent /dev/input/event0
output    :  In android, it didn't say the scancode... but all the inputs are handled as scancodes. output is,
0001 0003 00000001
0000 0000 00000000
0001 0003 00000000
0000 0000 00000000

sendevent : will allow us to give the input in the terminal.
syntax       : sendevent device type code value.
example    : for writing key 2 into the device.
shell@android:/ # sendevent dev/input/event0 0000 0000 00000000
shell@android:/ # sendevent dev/input/event0 0001 0003 00000001
shell@android:/ # sendevent dev/input/event0 0000 0000 00000000
shell@android:/ # sendevent dev/input/event0 0000 0000 00000000


P.S. : Remember the two points below,

  1.  Run it in the android shell.
  2.  Should have root permission.


Lost your GRUB??? - Boot-Repair

Hi friends,

Lost your GRUB??? don't go to police!!! i will help you... ;)
 
Recently when we installed winXP for testing our project, in the system which has Ubuntu and Win7 already installed... on the Next day, when we are done with testing... I wanted my Ubuntu and win7 back for development work...but when I restarted the system, I found that the GRUB was removed... and it took me half a session to overcome this problem... here is the solution for it now…(Don’t expect something great from me now...half session to Google and find the tool!!!)...

Lost your GRUB??? Here is the solution!!!...

Boot-Repair

This a free tool, to restore the GRUB in your system. When you run this tool, you will get back your last GRUB back, but the OS you installed and all the modification you have made in the system after losing the GRUB will be lost. In my case, I lost win XP... but I got back my Ubuntu and win7...
So here are the steps I followed,

 STEP 1: start the system with live USB or live CD... and get into Try Ubuntu mode... 
 STEP 2: open the terminal in the Ubuntu...
 STEP 3: run
sudo add-apt-repository ppa:yannubuntu/boot-repair && sudo apt-get update
sudo apt-get install -y boot-repair && boot-repair
STEP 4: reboot the system...

Thats all, now you can see your GRUB!!!...

For more information about BOOT-REPAIR, click here

But the correct method is by doing something in GRUB… but I don’t have that much knowledge in it… so I used BOOT-REPAIR…


Older Posts 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