8

If I start a really heavy (memory/cpu intensive) running really long running process will the OS terminate it after a period of time arbitrarily?
I.e. are there any limits beyond which the OS would stop a background process from running?
Are these limits configurable? Where are they defined?

1
  • 1
    I don't know the rest but if your process consumes more memory than available, then it gets killed by the kernel with SIGKILL and dmesg shows out of memory message. I think CPU consumption should not be any problem, if you write a program with enough infinite loops on threads like while(1){}, it consumes the CPU quite heavily but I never experienced that the process was killed.
    – Angs
    Commented Aug 9, 2013 at 20:02

2 Answers 2

5

Yes, there are such limits. They are called resource limits (rlimits). There are more then dozen different limits. For CPU time, for memory, for number of opened files etc.

Kernel sends different signals to process, when process exceeds different limits. If process does not react properly, it will get killed.

For each rlimit there are two values. First one is current limit. Second one is maximal limit (which is maximal limit you can set). Only root (more precisely user with CAP_SYS_RESOURCE capability) allowed to increase maximal limit or set current limit greater than maximal limit.

There are getrlimit() and setrlimit() system calls to manipulate limits.

By default, most resource limits contain huge value, meaning no limits. Of course, real limits exist due to kernel design restrictions, available RAM, available space on disk etc.

4
  • 3
    It should be clarified that the "limits" regarding CPU time and memory usage are not by default defined -- i.e., if you haven't set them, then there are no such limits.
    – goldilocks
    Commented Aug 9, 2013 at 20:25
  • All the limits have default values. For most of them, including CPU and memory, these values are very huge numbers. Commented Aug 10, 2013 at 8:23
  • That "very large" default value is just the biggest number representable with a rlim_t, which is an (32-bit) integer type in the struct rlimit used by getrlimit. If you actually read the man page: The value RLIM_INFINITY denotes no limit on a resource. RLIM_INFINITY is -1 signed or 4294967295 unsigned -- all bits set. That's the "very large" value you get for RLIMIT_CPU and memory stats like RLIMIT_AS, RLIMIT_DATA, and RLIMIT_RSS. I'll flesh this out and add it to my answer so you can try it yourself ;)
    – goldilocks
    Commented Aug 10, 2013 at 12:07
  • Actually they are 64-bit types here, although that may vary system to system.
    – goldilocks
    Commented Aug 10, 2013 at 13:00
4

There are various ways to implement such a policy, but there is a fairly obvious reason why it does not exist by default in the first place: because the system is intended to be used to its maximum potential. By analogy: if you bought a car that could do 200 kph and it had a 100 litre tank, you probably would not want it controlled by software that limited the speed to 100 kph and stopped the car when the tank was 2/3 empty, even if you'd been driving "for a long time".

Note there is no universal definition of "long-running". Is 40 minutes long running? How about 8 hours? 24 hours? 3 days? A week, a month, or a year? None of those are that unusual.

With regard to the resource limits mentioned by rasen, this is a primary means by which you can "implement such a policy". You did not actually ask how to do that, so I will not waste time going into depth, however, you did ask about the limits. With regard to CPU and memory, there aren't any. This is from the getrlimit/setrlimit system call man page:

The getrlimit() and setrlimit() system calls get and set resource limits respectively. Each resource has an associated soft and hard limit, as defined by the rlimit structure [...] The value RLIM_INFINITY denotes no limit on a resource (both in the structure returned by getrlimit() and in the structure passed to setrlimit()).

So it is relatively easy to check this programmatically:

#include <stdio.h>
#include <sys/resource.h>
#include <errno.h>
#include <string.h>
#include <stdint.h>

int main (void) {
    struct rlimit rl;
    int resources[] = {
        RLIMIT_CPU,
        RLIMIT_DATA,
        RLIMIT_AS,
        RLIMIT_RSS
    };
    char labels[][16] = { "CPU", "Data", "Virtual", "Resident" };
    int rlen = sizeof(resources) / sizeof(int);
    printf("RLIM_INFINITY is %lu\n", (uint64_t)RLIM_INFINITY);
    for (int i = 0; i < rlen; i++) {
        if (getrlimit(resources[i], &rl) != 0) {
            fprintf(stderr,"!!%s\n", strerror(errno));
            return -1;
        }
        printf("%8s soft: %lu hard: %lu\n",
            labels[i],
            (uint64_t)rl.rlim_cur,
            (uint64_t)rl.rlim_max
        );
    }
    return 0;
}                            

You can compile that gcc -std=gnu99 and try it yourself. This is the most definitive source of information, since it queries the kernel directly. Unless someone has intentionally configured limits on your system, you'll get something like:

RLIM_INFINITY is 18446744073709551615
     CPU soft: 18446744073709551615 hard: 18446744073709551615
    Data soft: 18446744073709551615 hard: 18446744073709551615
 Virtual soft: 18446744073709551615 hard: 18446744073709551615
Resident soft: 18446744073709551615 hard: 18446744073709551615

To clarify for non-programmers: the "RLIM_INFINITY" value is a value intended to indicate "infinity" or no limit (since computers cannot represent infinity as a number). The RLIMIT fields are explained in the man page, to summarize:

RLIMIT_CPU: CPU time limit in seconds.

RLIMIT_DATA: The maximum size of the process's data segment (initialized data, uninitialized data, and heap).

RLIMIT_AS: The maximum size of the process's virtual memory (address space) in bytes.

RLIMIT_RSS: Specifies the limit (in pages) of the process's resident set (the number of virtual pages resident in RAM).

They correspond to actual usage values you can see in top (CPU, DATA, VIRT, and RSS).

Again: on a normal linux system, there is no per process limit on cpu or memory, but you can set such.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .