Showing posts with label exception. Show all posts
Showing posts with label exception. 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!!!....

Out of Memory - dalvikvm-heap error

Hi friend,

Recently in our project SystemUI was crashing, saying that “Unfortunately, SystemUI has stopped”. I was supposed to fix it, so went through logcat and for found error as follow…

D/dalvikvm( 2120): GC_FOR_ALLOC freed 10K, 7% free 14024K/14983K, paused 10ms
I/dalvikvm-heap( 2120): Forcing collection of SoftReferences for 1587212-byte allocation
D/dalvikvm( 2120): GC_BEFORE_OOM freed 14K, 7% free 14009K/14983K, paused 0ms
E/dalvikvm-heap( 2120): Out of memory on a 1587212-byte allocation.
I/dalvikvm( 2120): "main" prio=5 tid=1 RUNNABLE
………………………………………………………………………………………..……………………………………………..
D/AndroidRuntime( 2120): Shutting down VM
W/dalvikvm( 2120): threadid=1: thread exiting with uncaught exception (group=0xb598b180)
E/AndroidRuntime( 2120): FATAL EXCEPTION: main
E/AndroidRuntime( 2120): java.lang.OutOfMemoryError
…………………………………………………………………………………………………………………………………………
E/AndroidRuntime( 2120):        at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager( 1682):   Force finishing activity 

SOLUTION

The error is the default heap size was not enough, so we need to increase the heap size or decrease the data in the heap to overcome this problem. We planned to increase the heap size. So we mounted the system image and edited the build.prop file in it, and then the problem was solved.

The solution is to increase the dalvik heap size by entering the one line in the build.prop script inside the system folder.

Step 1: use a file explore and open the build.prop file inside the system folder.
Step 2: enter the dalvik.vm.heapsize=32m. (I needed 32 MB of heap, so I specified it as 32… you can modify it as per your need)
Step 3: Restart your android.
The problem solved :)

If you are working with android source code…if you are customizing the android… and you want to solve in the source code… follow the following steps…

Step 1: open the main.mk in the folder android/build/core.
Step 2: enter the line ADDITIONAL_BUILD_PROPERTIES += dalvik.vm.heapsize=32m (I needed 32 MB of heap, so I specified it as 32… you can modify it as per your need) after the line ADDITIONAL_BUILD_PROPERTIES += dalvik.vm.stack-trace-file=/data/anr/traces.txt.
Step 3: save it and rebuilt it… 

The problem solved :)

From that time, I’m following the same, whenever I get out of memory problem ;)

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