CS计算机代考程序代写 Topic

Topic

5/10/2021

1

Topic
PROCESSES, JOBS, SIGNALS AND TRAPS

Linux Processes
Process – a piece of running code on your computer that’s executing some tasks

◦ Every process has a process ID (PID)
◦ System uses this number to keep track of the different processes and assign resources

◦ Every process has a parent process ID (PPID) because every process is started by a parent.

◦ init process is the only process that is not started by another process and has PID 1 (PPID 0)

We can take a look at the process table for the init process with the ps command: ps –f 1

UID PID PPID C STIME TTY STAT TIME CMD

root 1 0 0 Apr08 ? Ss 0:59 /sbin/init splash

TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS

5/10/2021

2

Linux Processes
What calls the init process?

◦ The Linux Kernel does.

A system is broken up into two pieces
◦ kernel space

◦ user space

Booting up your machine calls the Linux kernel -> does some stuff, once it’s done with
everything it does in kernel space, the only thing it does in user space is call the init process.

TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS

Linux Processes Life Cycle

TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS

5/10/2021

3

Foreground and background processes
• Foreground processes are started by typing a command. While a foreground

process is running the shell waits.
• Background processes are started by appending a & to the command

• Each process you start from the shell is known as a “job”, i.e., you have
maximally one foreground job and can have multiple background jobs.

• Background processes can be controlled:

• While a background process is running the shell continues.
• Output from the background process is mixed in.
• If a background process needs input, it gets suspended.

jobs lists background jobs and their process ID

kill %N terminates background job #N

kill PID terminates background job with process ID PID

fg %N brings job N to the foreground

TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS

Foreground and background processes
• Foreground processes can be suspended and brought to the background

• A background process will be suspended automatically if it needs to read data
from stdin. Some programs, e.g., perl check stdin when they start, so as
background processes they hang. The solution is to redirect stdin to come
from /dev/null (‘electronic sink’).

Ctrl Z suspends a foreground job

Ctrl C terminates foreground job

bg takes a suspended job to the background

fg %N brings job N to the foreground

bg %N takes suspended job N to the background

nohup

disown
can be used to leave jobs running when you logout (“hangup”).

TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS

5/10/2021

4

Sequences and commands
• Simple commands are commands
• Pipelines of simple commands are commands
• Sequences of simple commands with & are commands, and will

be executed concurrently.
• Sequences of simple commands with ; are commands, and will

be executed sequentially.
• Commands in ()s are simple commands.

(ls -l | wc) &

ls -l ; ls –a

ls –l & ls –&

lists files and directories and counts lines as one job in the background

lists files and directories consecutively

lists files and directories sequentially

TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS

TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS

Foreground/Background Processing
Performance Objectives:

1. Manipulate Processes and PIDs (ps, ^c, ^z, kill)

2. Run Commands in the Background (fg, bg, &)

3. Move a foreground command to background (^z, bg)

4. Check the status of background jobs (jobs)

5. Terminate a job (^c, ^d, kill)

6. Keep a job alive after logoff (nohup)

7. Suspend a running job

5/10/2021

5

TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS

Processes and PIDs – ps
Each command generates an independent process.

Each process has a unique process identification number (PID).

TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS

Processes and PIDs – ps
Use the ps command to see what processes are running.

$ ps

PID TT STAT TIME COMMAND

3835 p2 S 0:00 -csh (csh)

3838 p2 R 0:00 ps

◦ PID is the process identifier

◦ TT is the control terminal

◦ STAT is the status of the process

5/10/2021

6

TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS

Processes and PIDs – ps (Con’t)
STAT is the status of the process:

◦ R Runnable processes.

◦ T Stopped processes.

◦ P In page wait.

◦ D Non-interruptible wait (waiting for disk etc).

◦ S Sleeping for less than about 20 seconds.

◦ I Idle, sleeping longer than about 20 seconds.

◦ Z Terminated; waiting for parent (“zombie”).

(W) indicates process is swapped out.

TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS

Interrupting a Command

To “stop” a running interactive process use ^Z.

The message “stopped” is displayed.

A ps command will reveal the status:
$ ps

PID TT STAT TIME COMMAND

3835 p2 S 0:00 -csh (csh)

3839 p2 T 0:00 cat

3840 p2 R 0:00 ps

5/10/2021

7

TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS

Foreground Processing – fg

Type fg to continue running the stopped job in the foreground.

Use jobs to list job numbers and status.
$ jobs

[1] – Stopped cat > file4

[2] +Running vi file.c

TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS

Background Processing – bg

To continue the job in the background use:
$ bg %n

◦ Where n is the job number.

To initiate a command in the background use the ampersand:
◦ $ command & (ex. $ ./myscript.sh &)

5/10/2021

8

TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS

Terminating Jobs
Terminate a stopped command with kill.

$ kill pid

Use -1 or -9 option to forcefully terminate.
$ kill -9 1234

Terminate a running job with ^C

Terminate a program waiting for input with a ^d

TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS

Keeping a Job Active – nohup
On logout, the shell removes child processes running in background.

You can terminate the shell before it sends signal 1.

Children continue to run and you will be logged off because your login shell has terminated.

5/10/2021

9

TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS

Keeping a Job After Logout
Start a foreground process and then logoff.

Allow the process to continue to run.
◦ Enter to stop the foreground process,

◦ Execute bg to place process in background,

◦ Run ps -u userid to obtain the pid of your login shell,

◦ Enter kill -9 shellpid to kill the login shell and log off; but leave the process running detached from
the terminal.

The process must not do any terminal I/O.

TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS

Keeping a Job Active – nohup
Include the command nohup (no arguments) in your .logout file.

No need to use kill, logout normally.

Executed on logout, nohup directs the login shell to not stop child processes.

Processes survive because no signal is sent.

$ nohup command-name &

$ exit

Or

$nohup /path/to/command-name arg1 arg2 > myoutput.log &

$exit

5/10/2021

10

TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS

Monitoring for Unwanted Jobs
Unwanted processes can consume valuable computer time.

Consider including the following command in the .login and .logout files to spot runaway or
unwanted processes.

◦ ps -u userid

Signals on Linux
(base) $ kill -l

1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP

6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1

11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM

16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP

21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ

26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR

31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3

38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8

43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13

48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12

53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7

58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2

63) SIGRTMAX-1 64) SIGRTMAX

CTRL+C is 2) SIGINT

TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS

5/10/2021

11

Handling signals
Unix allows you to send a signal to any process – you can use the number identifier
for each signal or the abbreviation (ex. kill -1 1234 or kill –HUP 1234)

-1 = hangup kill -HUP 1234

-2 = interrupt with ^C kill -2 1235

no argument = terminate kill 1235

-9 = kill kill -9 1236
◦ -9 cannot be blocked

list your processes with
ps -u userid

TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS

Handling signals
Default action for most signals is to end process

◦ term: signal handler

Bash allows to install custom signal handler

Syntax:
trap ‘handler commands’ signals

Example:
trap ‘echo do not hangup’ 1 2

TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS

5/10/2021

12

Example: trap hangup
#! /bin/bash

# kill -1 won’t kill this process

# kill -2 will

trap ‘echo dont hang up’ 1

while true

do

echo “try to hang up”

sleep 1

done

TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS

Example: trap multiple signals
#! /bin/sh

# plain kill or kill -9 will kill this

trap ‘echo 1’ 1

trap ‘echo 2’ 2

while true; do

echo -n .

sleep 1

done

TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS

5/10/2021

13

Example: removing temp files
#! /bin/bash

trap ‘cleanup; exit’ 2

cleanup () {

/bin/rm -f /tmp/tempfile.$$.?

}

for i in 1 2 3 4 5 6 7 8

do

echo “$i.iteration”

touch /tmp/tempfile.$$.$i

sleep 1

done

cleanup

TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS

Restoring default handlers
trap without a command list will remove a signal handler

Use this to run a signal handler once only

#! /bin/sh

trap ‘justonce’ 2

justonce() {

echo “not yet”

trap 2 # now reset it

}

while true; do

echo -n “.”

sleep 1

done

TOPIC 10 – PROCESSES, JOBS, SIGNALS AND TRAPS