2

I was recently asked an interview question whose answer I am curious to know, although I could not answer during the interview itself: Given user A and user B are running their tasks on a ubuntu 12.04 server as non-root user. User A is running mongodb with 52.2% memory usage. Is there a program in python which user B can run such that user A's program terminates itself or kills itself.

My Attempt: I tried to read a very large file into memory as user B. However, on doing so user B's program itself gets terminated or killed by the operating system. Is there someway of killing another user's memory intensive program as non-root user?

The interviewer gave me hint..he said if your process (user B) is more memory intensive than user A, then user A's process will automatically get killed in sometime. But I am not able to understand as to how can I design such a memory intensive program

5
  • Kind of duplicate superuser.com/questions/137207/… Commented May 30, 2016 at 13:08
  • @PeterNimroot I read this question and answer before posting..however it does not suit my needs :) Commented May 30, 2016 at 13:10
  • Well, yeah, but there isn't much you could do. Knowing that mongodb is hosted at localhost you could try to DoS it or something alike, however you cannot send POSIX signals to other user's process if you are not root or sudoer. And DoSing service at localhost makes no sense, so this interview question seems nonsense to me. Commented May 30, 2016 at 13:13
  • @PeterNimroot The interviewer gave me hint..he said if your process (user B) is more memory intensive than user A, then user A's process will automatically get killed in sometime. But I am not able to understand as to how can I design such a memory intensive program Commented May 30, 2016 at 13:16
  • You should look into this question: unix.stackexchange.com/questions/153585/… Commented May 30, 2016 at 13:23

1 Answer 1

2

Most (all?) modern operating systems employ memory protection. They do not allow one process to access the memory of another process, generally. This prevents a memory heavy application from using memory allocated other processes. The memory heavy application's data will get written to the swap file once the physical memory has ran out. Once the swap file is also full, the operating system will kill that process.

So in order for UserB to kill UserA's process, it would have to use up the rest of the available memory and swap. As soon as UserA's process requires more memory, the OS will kill it, to protect UserB's process.

Designing this process is easy for UserB. Just generate and open a big enough file. The program used to open the file, like the Python interpreter, will load up the contents in memory.

kill_list = []
try:
    while True:
        kill_list.append(1)
except MemoryError:
    pass

That ought to do it. It will take up as much memory as it can without having the OS kill it. Now to wait...

Sign up to request clarification or add additional context in comments.

8 Comments

Yes and no. "The OOM Killer has to select the “best” process to kill. “Best” here refers to that process which will free up maximum memory upon killing and is also least important to the system.", so it also depends on priority and nice of the process.
yeah, and UserA's process is using up 52% of the memory
Unfortunately we have no idea what is the priority of mongodb process, so it might be the reason why such approach did not work for the OP (his process got killed by OOM)
The priority of User A and User B processes are the same :) However, I tried writing such a program..unfortunately I dont know why but it is getting killed
I might be wrong here, but I think OP's process got killed because it made a call to malloc. In what I am saying, Mongo will make that call. Process priority might play a part in general though
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.