Did you ever wish your program had a pause button? I did so regularly.
I regularly have to do lengthy calculations, for example for evaluating some heuristics for an optimization problem such as
Maximum Independent Set or
Matchings.
A typical program could look like this:
for all benchmark graphs G:
for all algorithm variants A:
for k in [2, 4, 8, ..., 128]:
Execute A on G with the current value for k
Now if this computation takes 20 hours and I'm running this on my laptop, one of the CPU cores is hogged and the fan goes berserk. So what happens if I want to take my computer somewhere else or view a movie?
As it turns out, I can simply suspend the machine and after waking up the program will continue to run. This already solves the first problem and shows that the operating system can suspend my program out of the box.
The bash shell (and I'm sure other shells, too) helps me with the second problem: It is able to do basic job management. Consider the following command sequence.
This lists my temporary directory and pastes it through less. less will wait for my input.
$ ls /tmp | less
Now, I press
Ctrl+Z to suspend less:
[1]+ Stopped ls /tmp | less
Neat! How do we get back the listing. With
fg %<job number>:
$ fg %1
We can also have multiple jobs:
$ ls /tmp | less
Ctrl+Z
[1]+ Stopped ls /tmp | less
$ ls /usr | less
Ctrl+Z
[2]+ Stopped ls /usr | less
The command
job gives us a listing of the current jobs:
$ jobs
[1]- Stopped ls /tmp | less
[2]+ Stopped ls /usr | less
There is one gotcha: If you use the bash command
time, it will display the passed time when you suspend your job and not print the remaining spent time after you restart it again:
$ time ls /tmp | less
Ctrl+Z
[1]+ Stopped ls /tmp | less
real 0m1.139s
user 0m0.001s
sys 0m0.003s
manuel@coxorange ~
$ fg %1
ls /tmp | less
Ctrl+Z
[1]+ Stopped ls /tmp | less
In this case, use
time on
fg %1:
$ time ls /tmp | less
Ctrl+Z
[1]+ Stopped ls /tmp | less
real 0m0.723s
user 0m0.001s
sys 0m0.003s
manuel@coxorange ~
$ time fg
ls /tmp | less
Ctrl+Z
[1]+ Stopped ls /tmp | less
real 0m0.913s
user 0m0.000s
sys 0m0.000s