It's probably killed by kernel's oom killer. dmesg should contain information about it. Sorry, but you may need to redesign your algorithm.
Hey guys, I'm running an ArchLinux on a Raspberry Pi and a BeagleBoard. I compiled a C program that does some calculations with various datatype. I had no problem with int and long so far, but with long long I encounter a problem:
The program is stopped automatically after some (longer) time and I get the output "Killed" on the terminal. Is there an auto-kill script running per default or something like this? What could've happend here?
Please help me (and ask more questions if you need them). Thanks
Edit: The solution:
You can change a value and disable the over-allocation of memory which was the problem in my case (at elast it works):
echo "2" > /proc/sys/vom/overcommit_memory
Source
linux - Program in C executed and the terminal shows: Finished (killed) - Stack Overflow
linux - Killing a process with C program - Stack Overflow
Coding Games and Programming Challenges to Code Better
c++ - My program was "Killed" - Stack Overflow
You almost certainly lack a swap partition, or you have some other utility monitoring the amount of available free memory, and therefore your program is killed because it consumes too much memory, up to the point that there is no free ram left.
You can use htop or conky to have a clear view of the issue.
If instead you had a swap partition then your system would attempt to swap the memory from/to the disk, and this would most likely freeze it. This is a much worse scenario, because in most cases the only action that you can take to get the system back working is to reboot it: in my experience, when the system freezes then it no longer responds to any keyboard input.
On my system it takes 2m26s for your executable to fill over 50% of the available 8GB of RAM, not counting the additional 32% that is occupied by the other running programs.
You might want to use a memory leak profiling tool (e.g. valgrind) to check for potential leaks, or simply inspect the source code manually. If you can't reduce its memory footprint, then your only option is to look for a much more powerful machine with a larger pool of RAM.
Compile and link with debugging information using -g, then run the program under control of a debugger such as gdb and watch where it stops. You should see which line of source code causes the problem that gets your program killed by the system.
$ gcc -O2 -g -o my_program my_program.c
$ gdb my_program
gdb> run
If your program needs command line arguments, type them on the run command
gdb> run arg1 arg2
In C++, a float is a single (32 bit) floating point number: http://en.wikipedia.org/wiki/Single-precision_floating-point_format
which means that you are allocating (without overhead) 3 840 000 000 bytes of data.
or roughly 3,57627869 gigabytes..
Lets safely assume that the header of the vector is nothing compared to the data, and continue with this number..
This is a huge amount of data to build up, Linux may assume that this is just a memoryleak, and protect it self by killing the application:
https://unix.stackexchange.com/questions/136291/will-linux-start-killing-my-processes-without-asking-me-if-memory-gets-short
I don't think this is an overcommit problem, since you are actually utillizing nearly half the memory in a single application.
but perhaps.. consider this just for fun.. are you building an 32bit application? you are getting close to the 2^32 (4Gb) memory space that can be addresssed by your program if it's a 32 bit build..
So in case you have another large vector allocated... bum bum bum
First install the signal handler for example
static bool installSignalHandler(int sigNumber, void (*handler)(int) = signal_handler)
{
struct sigaction action;
memset(&action, 0, sizeof(action));
action.sa_flags = SA_SIGINFO;
action.sa_sigaction = signal_handler_action;
return !sigaction(sigNumber, &action, NULL);
}
Call it:
installSignalHandler(SIGINT);
installSignalHandler(SIGTERM);
And the next code will be executed:
static void signal_handler_action(int sig, siginfo_t *siginfo, void* content)
{
switch(sig) {
case SIGHUP:
break;
case SIGUSR1:
break;
case SIGTERM:
break;
case SIGINT:
break;
case SIGPIPE:
return;
}
}
Take a look at the siginfo_t structure for the data you want
printf("Continue. Signo: %d - code: %d - value: %d - errno: %d - pid: %ld - uid: %ld - addr %p - status %d - band %d",
siginfo->si_signo, siginfo->si_code, siginfo->si_value, siginfo->si_errno, siginfo->si_pid, siginfo->si_uid, siginfo->si_addr,
siginfo->si_status, siginfo->si_band);
Every year we see one or two languages come out that are branded as hot new C++ replacements that will make it obsolete. Was C++ branded the same in relation to C? Did C++ users have the same attitude towards the language ancestor as rust users to cpp now?
This post isn't about one language being better than the other, I'm just interested in, say, social history of them.
kill(2) is the system call to send a signal to a process there isn't an equivilent to the killall utility.
An easy way to do this in C would be to invoke killall from your C program, using the system(3) library function or possible popen(3).
Alternatively you could read the manual page about the /proc pseudo file system and search for the command names and find the pids yourself.
man 2 kill
man 3 system
man 3 popen
man 5 proc
To answer the question in the title, no there is no C library that does this matching. Not even libprocps does this.
killall is a simple program that:
- works out what you want to match against
- effectively does ls on /proc looking for directories with only numbers in their name and matches against files under those directories
Due to it being generic (e.g. it doesn't know what match criteria a user will use beforehand) it has lots of matches. You should know what you want to match already. Your question doesn't really say but it sounds like the name or the command line.
I really caution this entire approach. Processes should be really sure about what other processes they are touching. Name is a terrible match as I can trivially fake that. Also consider you may have two users or two systems using the same name, which process should be killed?
PID files or some other method that records the PID on program commencement is much better because you know exactly which process you are talking about (unless they fork)
As Richard points out above, the killall code is GPL2+ so you can reuse it with the same license. The project moved to gitlab though and is at https://gitlab.com/psmisc/psmisc