Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Infinite recursion turns into a D mode process

Consider this program:

int w(int x, int y){
    if(x != y) {
        w(x, y);
    }else return x+y;
}

Call it this way:

w(0, 5);

For distinct values (like above), it generates an infinite recursion. The problem is, once the program started this way, the specific process turns into a D mode (waiting for an internal I/O, therefore untouchable).

top see untitled1

Take a normal loop

while(1){
    if(0 != 5){
        //foo
    }else{
        //bar
    }
}

Generates R state mode - perfectly capable of getting SIGKILL.

enter image description here

Although normal loops beat recursions performance-wise; the system should still be capable of killing the process.

Why is this happening? And how to prevent it remotely?

The code will be compiled an executed by a program that returns its output to the webclient via sockets. So there is no control over what the user attempts to compile.

Edit:

Ubiquitous process of compiling and running the code:

$ g++ main.cpp -o m
$ ./m

Edit2:

$ g++ --version
g++ (GCC) 4.9.2 20150204 (prerelease)
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ df --print-type
Filesystem     Type     1K-blocks      Used Available Use% Mounted on
/dev/sda2      ext4     957174124 168965980 739563400  19% /
dev            devtmpfs   4087124         0   4087124   0% /dev
run            tmpfs      4089872       528   4089344   1% /run
tmpfs          tmpfs      4089872        76   4089796   1% /dev/shm
tmpfs          tmpfs      4089872         0   4089872   0% /sys/fs/cgroup
tmpfs          tmpfs      4089872      1052   4088820   1% /tmp
tmpfs          tmpfs       817976        12    817964   1% /run/user/1000

The tests were done on Arch Linux x64 (with latest gcc).

Edit3:

The same problem occurs even after reinstalling the OS. The image has been used on another pc as well, where this problem does not occur.

like image 230
Gabe Avatar asked Feb 26 '15 13:02

Gabe


1 Answers

I'm not sure to have undertood:

Launch your command in this way:

./m & echo $! > mpid

So to kill process:

kill $(cat mpid)
like image 62
Luca Davanzo Avatar answered Oct 30 '22 12:10

Luca Davanzo