How is RAM and CPU allocated if no manual changes are applied to an Ubuntu Server

RAM is first-come-first-serve. If userA runs 9 processes that allocate 10% of memory each, and then userB logs on, userB will see only 10% of memory left. In the event that memory is exhausted, Linux will start killing processes. The OOM killer is not tuned for multi-user, as far as I know, so it may be unfair in this scenario.

CPU time is generally allocated on a per-process basis, not per-user (but see below).

Any process which is ready to run (not sleeping, waiting on I/O, etc.) is considered for scheduling. (Processes which are not ready to run, are ignored, and so "don't count". (This is a slight oversimplification, but close enough.))

In the simplest model, if two users are running one process each, they each get roughly half of available CPU time. But if userA is running 10 processes, and userB is running 1 process, then userA gets 90% of CPU and userB gets 10% of CPU (all other things being equal).

However, the Linux scheduler can refine this by grouping processes together, and then allocating CPU time between those groupings.

Further, Linux has the capability to automatically group processes based on the session ID (typically associated with terminals, terminal windows, and/or X login sessions). This is called "autogrouping". The goal is that a single user running a heavy background task in one window, and an interactive task in another window, will still see responsive interactive performance.

Both of these capabilities are enabled by default on Ubuntu, as far as I can determine.

I cannot find information on how task groups and/or autogrouping behave in a multi-user workload. In theory, if the scheduler put each user in a separate task group, then users would always get balanced access to the CPU (50/50 for two users). However, I don't find anything that says this will happen automatically.

Further reading:

  • https://en.wikipedia.org/wiki/Completely_Fair_Scheduler
  • http://manpages.ubuntu.com/manpages/zesty/man7/sched.7.html
  • Nice level not working on linux

If you need to limit memory usage on the same server, your best bet will be to either

  1. Use two Virtual Machines, ideally KVM so that you can use the existing Ubuntu server to host the VMs. However, this will prevent you from easily sharing unused memory from one user with another.
  2. Use cgroups to limit resource usage
    2.1. https://askubuntu.com/questions/510913/how-to-set-a-memory-limit-for-a-specific-process 2.2 http://www.fernandoalmeida.net/blog/how-to-limit-cpu-and-memory-usage-with-cgroups-on-debian-ubuntu/

By default, users are unlimited memory-wise in Ubuntu, and in this case it's "first come, first serve". In other words, User A could use up all the memory and leave nothing for a second user.

Note though: If you configure limits, they will be always the same and not dependent on the number of current users, so you will restrict your users even if they are alone on the machine.

For the CPU, things are a bit better and the kernel scheduler will distribute CPU time between processes (not users!).