Jobs and Concurrency

commands

  • jobs

  • ps

  • fg bring background process to the foreground

  • bg resume a stopped background process if it is stopped

  • disown

  • kill

Running Tasks in the background

Tasks can be run concurrently in the background with the & operator.

# run 3 commands in a background jobs
tail -f /var/log/dmesg &
tail -f /var/log/syslog &
nano /tmp/file &

# Three jobs in the background

# list background jobs and their statuses and their PIDs
jobs -l

# list the PIDs only
jobs -p

# list jobs in the current process and its children
ps

UNIX Signals

Resources

  • man 7 signal

  • kill -L

SIGHUP

1

Term

Hangup or death of controlling terminal or process

SIGINT

2

Term

Interrupt from keyboard

SIGQUIT

3

Core

Quit from keyboard

SIGILL

4

Core

Illegal Instruction

SIGTRAP

5

Core

Trace/breakpoint trap

SIGABRT

6

Core

Abort signal from abort(3)

SIGBUS

7

Core

Bus error (bad memory access)

SIGFPE

8

Core

Floating point exception

SIGKILL

9

Term

Kill signal

SIGSEGV

11

Core

Invalid memory reference

SIGPIPE

13

Term

Broken pipe: write to pipe with no readers

SIGALRM

14

Term

Timer signal from alarm(2)

SIGTERM

15

Term

Termination signal

SIGUSR1

10

Term

User-defined signal 1

SIGUSR2

12

Term

User-defined signal 2

SIGCHLD

17

Ign

Child stopped or terminated

SIGCONT

18

Cont

Continue if stopped

SIGSTOP

19

Stop

Stop process

SIGTSTP

20

Stop

Stop typed at terminal

SIGTTIN

21

Stop

Terminal input for background process

SIGTTOU

22

Stop

Terminal output for background process

Stopping and Resuming

The main signals we are concerned about are:

  • kill -19 stop process

  • kill -18 continue if stopped

  • kill -15 soft termination

  • kill -9 hard termination

jobs can be selected by their order in which they were started with %1 being the first job, %2 being the second job and onwards.

tail -f /var/log/dmesg &
tail -f /var/log/syslog &
nano /tmp/file &

# stop the second job
kill -19 %2

# stop the first job
kill -19 %1

# resume the first job
kill -18 %1

# when we terminate the second job, jobs 3 onward maintain their order in the job list
kill -15 %2

Current and Child PID

# prints the current PID VS a child subshell PID
echo $BASHPID; ( echo $BASHPID )