silicon valley university confidential 1 introduction to unix / linux - 8 dr. jerry shiao, silicon...

44
SILICON VALLEY UNIVERSITY CONFIDENTIAL 1 Introduction to UNIX / Linux - 8 Dr. Jerry Shiao, Silicon Valley University

Upload: caroline-sherman

Post on 04-Jan-2016

222 views

Category:

Documents


0 download

TRANSCRIPT

SILICON VALLEY UNIVERSITY CONFIDENTIAL 1

Introduction to UNIX / Linux - 8

Dr. Jerry Shiao, Silicon Valley University

SILICON VALLEY UNIVERSITY CONFIDENTIAL 2

Introduction UNIX/Linux Course

Section 8 Processes

What is a Process? UNIX creates a process when External Command or Shell

Script executes, removes a process when command or script terminates.

Process Hierarchy and CPU Scheduling. Time-Sharing System.

Concept of a Process, Multiple Process. Execution of External Command or Shell Script.

Process Attributes. Process and Job Control

Foreground and Background Processes. Sequential and Parallel Processes. Abnormal Termination of Commands and Processes

SILICON VALLEY UNIVERSITY CONFIDENTIAL 3Summer 2015

Introduction UNIX/Linux Course

Processes UNIX system creates process every time external

command executes and deletes process when external command completes.

Consists of: Executing program code. Process State

Registers: Fast memory. Ready/Running/Waiting/Swapped/Zombie

Set of resources (i.e. open files). Internal kernel data (i.e. struct task_struct). Address space. One or more threads of execution. Data section (i.e. global variables).

SILICON VALLEY UNIVERSITY CONFIDENTIAL 4Summer 2015

Introduction UNIX/Linux Course Processes Time-Sharing System (UNIX) – Multiple Processes execute

simultaneously. Scheduling Policy: Set of Rules to select Process for Execution.

Fast process response time, good throughput background jobs, avoid process starvation, reconcilitate low and high priority processes.

Time sharing principle based on timer interrupts. Each process has time slice or quantum. When quantum expires,

context switch occurs (process appear to run concurrently). Timer interrupts used by Linux to measure process quantum.

Quantum Linux 2.4: 210ms, Linux 2.6: 100ms. Linux dynamically changes process priority.

Priority Value = Threshold priority + Nice Value + (Recent CPU

usage / 2) Priority adjusted higher for releasing CPU before quantum (I/O-bound

process). Priority adjusted lower for using entire quantum (CPU-bound process).

SILICON VALLEY UNIVERSITY CONFIDENTIAL 5Summer 2015

Introduction UNIX/Linux Course

Processes Scheduling Algorithm

Epoch is division of CPU time or a period of time Every process has a maximum time quantum computed at beginning of each

epoch. When process waiting for I/O its quantum suspended and can be used again in

the epoch until its fully used. Epoch ends when all runnable processes have used all of their quantum

Process_3

Quantum_3

epoch

Process_2

Quantum_2Process_1

Quantum_1

Process_3

Quantum_3Process_1

Quantum_1

Process_1

Quantum_1

SILICON VALLEY UNIVERSITY CONFIDENTIAL 6

Copyright @2005 Pearson Addison-Wesley

Introduction UNIX/Linux Course

Processes UNIX Process States

Process can be in one of many states (Ready, Swapped, Running, Waiting, Zombie).

Moves from one state to another. Finishes execution (normally or abnormally) and exists system.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 7

Copyright @2005 Pearson Addison-Wesley

Introduction UNIX/Linux Course

Processes States

SILICON VALLEY UNIVERSITY CONFIDENTIAL 8

Copyright @2005 Pearson Addison-Wesley

Introduction UNIX/Linux Course

Processes Shell Command Execution

Internal (built-in) command has code is part of the shell (i.e. bash) process.

bg, cd, continue, echo, exec, export, fg, pwd, set, umask, wait External command has code in a file (i.e. binary or shell

script) found in $PATH. grep, more, cat, mkdir, rmdir, ls, sort, ftp, telnet, ps

fork System Call UNIX process can create another process by using the fork system call. Creates an exact memory map of the calling process. Parent process is the calling process (i.e. shell). Child process is the created process (i.e. command file).

exec System Call Process overwrite itself with executable code for another command. Child process uses exec system call to execute shell external

command.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 9

Copyright @2005 Pearson Addison-Wesley

Introduction UNIX/Linux Course

Processes Shell Command Execution

SILICON VALLEY UNIVERSITY CONFIDENTIAL 10

Copyright @2005 Pearson Addison-Wesley

Introduction UNIX/Linux Course

Processes

[student1]$ exec 4<>test_r4[student1]$ cat >&4Hello test_r4Hello test_r4Hello exit[student1]$ ls -lt test_r4-rw-rw-r-- 1 student1 student1 39 2013-10-17 15:21 test_r4[student1]$ cat test_r4Hello test_r4Hello test_r4Hello exit[student1]$

exec: Execute <command> in place of the current shell (instead of creating a new process).

SILICON VALLEY UNIVERSITY CONFIDENTIAL 11Summer 2015

Introduction UNIX/Linux Course Processes

int main()

{

pid_t pID = fork();

If (pID == 0) {

sIdent = “Child Process: “;

} else if (pID < 0) {

/* Failed to fork. */

exit(1);

} else {

sident = “Parent Process:”;

. . .

wait (&status);

}

}

fork:

Spawn a new child process which is an identical process to the parent, except that it has a new system process ID. The process is copied in memory from the parent.

- Copy-on-write memory pages created.

- Duplicate parent’s page tables.

- Create unique task_struct for the child.

- Return value = 0 indicates child’s process.

child PID in the parent’s process.

-1 error, no child process created.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 12

Copyright @2005 Pearson Addison-Wesley

Introduction UNIX/Linux Course

Processes Shell Command Execution Shell script: a series of shell commands in a file Execution of a shell script is different from execution of an

external binary command Execution of a shell script:

The current shell creates a child shell and lets the child shell execute commands in the shell script, one by one.

The child shell creates a child for every command it executes While the child shell is executing commands in the script file,

the parent shell waits for the child to terminate, after which it comes out of waiting state and resumes execution

Only purpose of child shell is to execute commands and eof means no more commands

The child shell is same as parent, unless specified in first line: # ! /bin/csh

SILICON VALLEY UNIVERSITY CONFIDENTIAL 13

Copyright @2005 Pearson Addison-Wesley

Introduction UNIX/Linux Course

Processes Shell Command Execution

Running another shell:#!/bin/csh

SILICON VALLEY UNIVERSITY CONFIDENTIAL 14

Copyright @2005 Pearson Addison-Wesley

Introduction UNIX/Linux Course

Processes Shell Command Execution Subshells ( from current Shell )

Execute commands in another shell. Command line, run the shell (shell becomes the child of the

current shell). sh : Bourne shell csh : C shell ksh : Korn shell bash : Bourne again shell

Commands in Shell Script Child shell has the type of the parent shell. Default, child shell executed by “copy” of parent shell. Change child shell for shell script with “# ! shell-path-name”.

First line of the shell script. # ! /bin/csh

SILICON VALLEY UNIVERSITY CONFIDENTIAL 15

Copyright @2005 Pearson Addison-Wesley

Introduction UNIX/Linux Course

Section 8 Processes Shell Command Execution

ps

SILICON VALLEY UNIVERSITY CONFIDENTIAL 16

Copyright @2005 Pearson Addison-Wesley

Introduction UNIX/Linux Course

Processes Shell Command Execution[student1~]$ cat shell_exe/bin/shecho Bourne Shell

[student1 ~]$ ./shell_exesh-3.2$

< Another SSH session >[student1~]$ ps alF UID PID PPID PRI NI VSZ RSS WCHAN STAT TTY TIME COMMAND. . .0 501 9510 9509 15 0 4824 1540 wait Ss pts/4 0:00 -bash1 501 9542 9510 17 0 4824 740 wait S pts/4 0:00 -bash0 501 9543 9542 17 0 4816 1352 - S+ pts/4 0:00 /bin/sh

[student1~]$ ps axl | grep 9509

F UID PID PPID PRI NI VSZ RSS WCHAN STAT TTY TIME COMMAND5 501 9509 9507 21 0 8240 1500 - S ? 0:00 sshd: student1@pts/40 501 9510 9509 15 0 4824 1540 wait Ss pts/4 0:00 –bash

PID of /bin/sh.PPID is default shell when shell script executed.

PID of shell when process started from ssh Daemon.

PID of ssh Daemon.

Same ( the login bash shell).

Child shell created when executing shell script.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 17

Copyright @2005 Pearson Addison-Wesley

Introduction UNIX/Linux Course

Processes Shell Command Execution

[student1 ~]$ ps lF UID PID PPID PRI NI VSZ RSS WCHAN STAT TTY TIME COMMAND0 501 9510 9509 15 0 4824 1540 wait Ss pts/4 0:00 -bash0 501 9838 9510 17 0 4280 840 - R+ pts/4 0:00 ps l

[student1 ~]$ /bin/shsh-3.2$ ps lF UID PID PPID PRI NI VSZ RSS WCHAN STAT TTY TIME COMMAND0 501 9510 9509 15 0 4824 1540 wait Ss pts/4 0:00 -bash0 501 9839 9510 15 0 4816 1404 wait S pts/4 0:00 /bin/sh0 501 9841 9839 17 0 4280 860 - R+ pts/4 0:00 ps lsh-3.2$ ps axl | grep 95095 501 9509 9507 15 0 8240 1504 - S ? 0:00 sshd: student1@pts/40 501 9510 9509 15 0 4824 1540 wait Ss pts/4 0:00 -bash0 501 9848 9839 18 0 4004 712 pipe_w S+ pts/4 0:00 grep 9509sh-3.2$

Bash shell is waiting for the child process that was created to terminate.

Child process uses “exec” command to become the command to be executed.

When running shell script, current shell creates child shell to execute commands.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 18

Copyright @2005 Pearson Addison-Wesley

Introduction UNIX/Linux Course

Processes Attributes :

UID, Process Name, PID, Process State, PPID, Process Execution Time.

ps [ options ] Report the process status (attributes of process on system). - a : Display information about processes on terminal session, except local session (i.e. session leader). - e : Display information about all the processes running on the system. - x : List all processes owned. Used with “-a” option, list all

processes. - l : Display long list of status report. - u uidlist : Display information about processes belonging to the users with UIDs in the “uidlist” (separated by commas).

SILICON VALLEY UNIVERSITY CONFIDENTIAL 19

Copyright @2005 Pearson Addison-Wesley

Introduction UNIX/Linux Course

Processes Attributes :

$ psPID TTY TIME CMD19440 pts/1 0:02 -ksh18786 pts/1 0:00 ps20668 pts/1 0:10 vi$

$ ps –aPID TTY TIME CMD20668 pts/ 1 0:10 vi21534 pts/1 0:00 ps$

$ ps -u 147UID PID TTY TIME CMD147 18802 pts/1 0:00 ps147 19440 pts/1 0:02 ksh147 20668 pts/1 0:10 vi$

$ ps –lF S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD200001 A 147 18630 19440 13 66 20 fedf 288 pts/1 0:00 ps240001 A 147 19440 19496 1 60 20 9e93 476 pts/1 0:02 ksh

ps command (pid=18630) was fork from ksh (pid=19440).Shows process from same pseudo-terminal or ksh process.

Display information about processes on terminal session, except local session (i.e. session leader).

Display information about UID 147.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 20

Copyright @2005 Pearson Addison-Wesley

Introduction UNIX/Linux Course

Processes Attributes : ps - l

SILICON VALLEY UNIVERSITY CONFIDENTIAL 21

Copyright @2005 Pearson Addison-Wesley

Introduction UNIX/Linux Course

Processes top : Monitor CPU Activity in Real Time Top version 3.5beta12, Copyright © 1984 through 1996, William LeFebvre

A top users display for UNIXThese single-character commands are available:^L —redraw screenq —quith or ? —help; show this textd —change number of displays to showe —list errors generated by last “kill” or “renice” commandi —toggle the displaying of idle processesI —same as ‘i’k —kill processes; send a signal to a list of processesn or # —change number of processes to displayo —specify sort order (size, res, cpu, time)r —renice a processs —change number of seconds to delay between updatesu —display processes for only one user (+ selects all users)

Displays status of first 15 of most CPU-Intensive tasks on the system, CPU activity, and manipulate processes interactively.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 22

Copyright @2005 Pearson Addison-Wesley

Introduction UNIX/Linux Course

Processes top : Monitor CPU Activity in Real Time $ toptop - 02:56:55 up 16:17, 3 users, load average: 1.32, 1.92, 2.06Tasks: 129 total, 2 running, 127 sleeping, 0 stopped, 0 zombieCpu(s): 2.7%us, 5.4%sy, 0.0%ni, 60.7%id, 30.5%wa, 0.0%hi, 0.7%si, 0.0%stMem: 1018688k total, 659596k used, 359092k free, 9376k buffersSwap: 2104476k total, 72160k used, 2032316k free, 303896k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1602 root 20 0 70588 29m 3056 S 4.3 2.9 5:14.52 Xorg 4258 sau 20 0 248m 3752 2780 S 1.3 0.4 8:17.84 knotify4 . . . u <enter>top - 03:01:07 up 16:21, 3 users, load average: 0.07, 0.85, 1.57Tasks: 128 total, 1 running, 127 sleeping, 0 stopped, 0 zombieCpu(s): 4.7%us, 2.0%sy, 0.0%ni, 92.9%id, 0.0%wa, 0.3%hi, 0.0%si, 0.0%stMem: 1018688k total, 667048k used, 351640k free, 10144k buffersSwap: 2104476k total, 72156k used, 2032320k free, 314384k cachedWhich user (blank for all): PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1602 root 20 0 70860 29m 3056 S 3.3 2.9 5:20.79 Xorg 29005 sau 20 0 105m 11m 6576 S 1.3 1.2 0:08.27 konsole . . .

When top execute, it will take over console until “q” is entered.To change the number of users to display,Enter “u” and top will prompt for users.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 23

Copyright @2005 Pearson Addison-Wesley

Introduction UNIX/Linux Course

UNIX Responsible for Process and Job Management

Process Creation. Process Termination. Running Processes in Foreground.

Process currently controlling Keyboard and Console. Running Processes in Background. Suspending Processes. Switching Processes from Foreground to

Background. Switching Processes from Background to

Foreground.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 24

Copyright @2005 Pearson Addison-Wesley

Introduction UNIX/Linux Course

Process and Job Control Foreground Process:

Syntax: command Executing process keeps control of the keyboard and display

screen, until process completes. Background Process

Syntax: command & “&” End of command places command in background. Command takes long time to complete (i.e. find, sort

commands). Executing releases the keyboard and display screen to the

shell. Local Shell Session can execute multiple background

processes in parallel.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 25

Copyright @2005 Pearson Addison-Wesley

Introduction UNIX/Linux Course

Process and Job Control Foreground fg [ %jobid ]

Purpose: Resume execution of the process with job number “jobid” in the foreground or move background processes to the foreground.

“%jobid”: % or %+ (Current job). % - : Previous job. %N : Job number N. %Name : Job beginning with “Name”. %?Name: Command containing “Name”

$ find / -name foo -print > foo.paths 2> /dev/null

Command has keyboard and console. Command has prompt. .

SILICON VALLEY UNIVERSITY CONFIDENTIAL 26

Copyright @2005 Pearson Addison-Wesley

Introduction UNIX/Linux Course

Process and Job Control Background bg [ %jobid-list ]

Purpose: Resume execution of suspended processes/jobs with job numbers in “jobid-list” in the background.

“%jobid”: % or % + : Current job. % - : Previous job. %N : Job Number N. %Name : Job beginning with “Name”. %?Name : Command containing “Name”.

$ find / -name foo -print > foo.paths 2> /dev/null &[1] 23467

$

[1] = Job Number.23467 = Process ID (PID).Shell has prompt back to enter next commands.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 27

Copyright @2005 Pearson Addison-Wesley

Introduction UNIX/Linux Course

Process and Job Control jobs [ option ] [ %jobid-list ]

Purpose: Display the status of the suspended and background processes in “jobid-list”. If “jobid-list” not defined, display the status of the current job.

“%jobid”: List specified job numbers.

- l : Display Process ID (PID) of jobs.

[sau@localhost cs206]$ find / -inum 23456 -print > pathnames 2> /dev/null &[1] 2496[sau@localhost cs206]$ find / -inum 11111 -print > pathnames 2> /dev/null &[2] 2497[sau@localhost cs206]$ find / -inum 22222 -print > pathnames 2> /dev/null &[3] 2498[sau@localhost cs206]$ jobs -l[1] 2496 Running find / -inum 23456 -print > pathnames 2> /dev/null &[2]- 2497 Running find / -inum 11111 -print > pathnames 2> /dev/null &[3]+ 2498 Running find / -inum 22222 -print > pathnames 2> /dev/null &

SILICON VALLEY UNIVERSITY CONFIDENTIAL 28

Copyright @2005 Pearson Addison-Wesley

Introduction UNIX/Linux Course

Process and Job Control $ jobs[1] Running find / -inum 23456 -print > pathnames 2> /dev/null &[2]- Running find / -inum 23456 -print > pathnames_2 2> /dev/null &[3]+ Running find / -inum 23456 -print > pathnames_3 2> /dev/null & $ jobs %?pathnames_2[2]- Running find / -inum 23456 -print > pathnames_2 2> /dev/null & $ jobs -l[1] 28337 Running find / -inum 23456 -print > pathnames 2> /dev/null &[2]- 28483 Running find / -inum 23456 -print > pathnames_2 2> /dev/null &[3]+ 28509 Running find / -inum 23456 -print > pathnames_3 2> /dev/null & $ fg %1find / -inum 23456 -print > pathnames 2> /dev/null^C $ jobs[2]- Running find / -inum 23456 -print > pathnames_2 2> /dev/null &[3]+ Running find / -inum 23456 -print > pathnames_3 2> /dev/null &

“Jobs” shows background jobs running.“+” is current job.“-” is the previous job.

Bring Job 1 to foregound. Once in foreground, locks the keyboard.<Ctrl> <C> terminates process..<Ctrl> <Z> suspends process.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 29

Copyright @2005 Pearson Addison-Wesley

Introduction UNIX/Linux Course

Process and Job Control $ fg %2find / -inum 23456 -print > pathnames_2 2> /dev/null^Z $ jobs[2]+ Stopped find / -inum 23456 -print > pathnames_2 2> /dev/null[3]- Running find / -inum 23456 -print > pathnames_3 2> /dev/null & $ fg %3find / -inum 23456 -print > pathnames_3 2> /dev/null^Z[3]+ Stopped find / -inum 23456 -print > pathnames_3 2> /dev/null $ jobs[2]- Stopped find / -inum 23456 -print > pathnames_2 2> /dev/null[3]+ Stopped find / -inum 23456 -print > pathnames_3 2> /dev/null

Bring Job 2 to foreground. Once inforeground, locks the keyboard.<Ctrl> <Z> suspends process.

Job 2 suspended.Bring Job 3 to foreground. <Ctrl> <Z> suspends process.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 30

Copyright @2005 Pearson Addison-Wesley

Introduction UNIX/Linux Course

Process and Job Control Daemons

System Process running in the background. Starts during system initialization or starts when needed and

terminates. Offers services to users and handle system administration tasks.

Printer Daemon, lpd: Print spooling system. finger Daemon, fingerd: Information on logged-in users of system. ssh Daemon, sshd: Secure remote login server. udev Daemon, udevd: Dynamic device naming server. Internet Daemon, xinetd:Server starts other servers providing services over the

network (i.e. tftpd).

[root@localhost cs206]# ps aux | grep "d$“. . .root 491 0.0 0.0 2800 1072 ? S<s 03:39 0:00 /sbin/udevd -droot 1432 0.0 0.0 6576 1064 ? Ss 03:39 0:00 /usr/sbin/sshdroot 2903 0.0 0.0 4072 856 ? Ss 05:36 0:00 xinetd -stayalive -pidfile

/var/run/xinetd.pid

SILICON VALLEY UNIVERSITY CONFIDENTIAL 31

Copyright @2005 Pearson Addison-Wesley

Introduction UNIX/Linux Course

Process and Job Control Sequential Execution of Commands cmd1; cmd2; …; cmdN

Purpose: Execute the “cmd1”, “cmd2”, …, “cmdN” commands sequentially.

“;” is command separator. $ date; echo Hello World; uname; whoWed Nov 7 03:18:13 PST 2012Hello WorldLinuxsau :0 2012-11-05 10:40 (console)sau pts/0 2012-11-05 10:41sau pts/6 2012-11-06 22:59

unameHello Worlddate

who

SILICON VALLEY UNIVERSITY CONFIDENTIAL 32

Copyright @2005 Pearson Addison-Wesley

Introduction UNIX/Linux Course

Process and Job Control Parallel Execution of Commands cmd1& cmd2& … cmdN&

Purpose: Execute the “cmd1”, “cmd2”, …, “cmdN” in parallel as separate processes.

$ date& echo Hello, World& uname; who[4] 5924[5] 5925Hello, WorldWed Nov 7 03:05:42 PST 2012Linux[4] Done date[5] Done echo Hello, Worldsau :0 2012-11-05 10:40 (console)sau pts/0 2012-11-05 10:41sau pts/6 2012-11-06 22:59

who

unamedateHello World

SILICON VALLEY UNIVERSITY CONFIDENTIAL 33

Copyright @2005 Pearson Addison-Wesley

Introduction UNIX/Linux Course

Process and Job Control Sequential and Parallel Execution of Commands

$ date& who; whoami; uname; echo Hello World&[4] 7124sau :0 2012-11-05 10:40 (console)sau pts/0 2012-11-05 10:41sau pts/6 2012-11-06 22:59Wed Nov 7 03:32:20 PST 2012sau[4] Done dateLinux[4] 7128Hello World[4] Done echo Hello World

who

datewhoami

unameHello World

Two processes separated by “&”.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 34

Copyright @2005 Pearson Addison-Wesley

Introduction UNIX/Linux Course

Process and Job Control Group Commands ( cmd1; cmd2; …; cmdN )

Purpose: Execute commands “cmd1”, “cmd2”, … , “cmdN” sequentially, but as one process.

$ (date; echo Hello, World); uname; whoWed Nov 7 03:57:53 PST 2012Hello, WorldLinuxsau :0 2012-11-05 10:40 (console)sau pts/0 2012-11-05 10:41sau pts/6 2012-11-06 22:59

unameHello Worlddate

who

“date” and “echo” executes sequentially, but as one process.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 35

Copyright @2005 Pearson Addison-Wesley

Introduction UNIX/Linux Course

Process and Job Control Group Commands

$ (date; echo Hello, World)& (who; uname)& whoami[4] 8544[5] 8545sau :0 2012-11-05 10:40 (console)sau pts/0 2012-11-05 10:41sau pts/6 2012-11-06 22:59sauLinuxWed Nov 7 04:03:36 PST 2012Hello, World[4] Done ( date; echo Hello, World )[5] Done ( who; uname )$

Hello, Worlddateuname

who

“date” and “echo” executes sequentially, but as one process.“who” and “uname” executes sequentially, but as one process.

whoami

SILICON VALLEY UNIVERSITY CONFIDENTIAL 36

Copyright @2005 Pearson Addison-Wesley

Introduction UNIX/Linux Course

Process Termination Normal Successful Process Termination Abnormal Process Termination:

Foreground Process <Ctrl-C>.

Background Process Use “kill” command. Use “fg” to bring process into foreground and then use <Ctrl-C>.

Primary purpose of “kill” command is to send a signal, a software interrupt, to a process.

Process actions upon receiving a signal: Accept the default action (terminate the process). Ignore the signal. Intercept the signal and take an user-defined action.

Internal signal ( trap) caused by event internal to the process. External signal caused by event external to the process.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 37

Copyright @2005 Pearson Addison-Wesley

Introduction UNIX/Linux Course

Process Termination Abnormal Process Termination:

Internal signal ( trap): Process executes invalid instruction. Process access invalid memory address (i.e. NULL dereference, memory

reference outside user space). External signal:

<Ctrl-C> Logging off. “kill” command.

kill [ - signal_number ] proc-list Send the signal for “signal-number” to processes with PIDs in “proc-list”. - 1 : Hangup or Log out. - 2 : Interrupt <Ctrl-C> - 3 : Quit (<Ctrl-\) - 9 : Sure kill. - 15 : Software signal (default signal number, i.e. kill <PID>).

SILICON VALLEY UNIVERSITY CONFIDENTIAL 38

Copyright @2005 Pearson Addison-Wesley

Introduction UNIX/Linux Course

Process Termination Abnormal Process Termination:

kill [ - signal_number ] proc-list List all signals and the signal names. $ kill –l

$ kill -l 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR111) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+338) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+843) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+1348) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-

1253) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-758) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-263) SIGRTMAX-1 64) SIGRTMAX

SILICON VALLEY UNIVERSITY CONFIDENTIAL 39

Copyright @2005 Pearson Addison-Wesley

Introduction UNIX/Linux Course

Process Termination Abnormal Process Termination $ psPID TTY TIME CMD13495 pts/0 0:03 -ksh13583 pts/0 9:11 find / -inum 23456 -print13586 pts/0 5:03 find / -name foo -print13587 pts/0 23:11 find / -name foobar -print20577 pts/0 31:07 a.out20581 pts/0 12:53 sort bigfile > bigfile.sorted$ kill 13583 20577[1] + Killed find / -inum 23456 -print &$ kill -2 13587 20577[3] + Killed find / -name foobar -print &$ kill -9 20577[4] - Killed a.out &$

Default Signal 15 sent to PID 13583.

Signal 2 sent to PID 13587.

Signal 9 sent to PID 20577. PID 20577 ignores Signals 15 and 2.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 40

Copyright @2005 Pearson Addison-Wesley

Introduction UNIX/Linux Course

Process Termination Logout all processes running in your session get a

Hangup Signal and terminates (Default Action). nohup command [ args ]

Purpose: Run “command” and ignore hangup signal (1). Used when a process executes for a long time and

do not want to log out (Signal 1) to terminate the process.

nohup process should run in background. Logout: Process will ignore the Hangup Signal. Run multiple nohup processes.

$ nohup find / -name foo –print 1> foo.paths 2> /dev/null &[1] 15928 $ nohup GenData > employees ; sort employees > sorted &[2] 15931

SILICON VALLEY UNIVERSITY CONFIDENTIAL 41

Copyright @2005 Pearson Addison-Wesley

Introduction UNIX/Linux Course

Process Hierarchy in UNIX init Process: Initial process which starts other processes

from the Linux configuration file. Has a PID of 1. Starts up Getty Process for every terminal.

Getty Process: Starts up on active terminal and starts Login Process.

Login Process: Child process fork from the Getty Process and checks validity of the login name and password.

Swapper Process: Starts up the Init Process, scheduler function, and becomes the Idle Process.

UNIX ptree command displays the process tree of the currently running processes on the system showing parent-child relationship.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 42

Copyright @2005 Pearson Addison-Wesley

Introduction UNIX/Linux Course

Process Hierarchy in UNIX

Init Process exists during lifetime of the system.

Getty Process exists as long as the terminal is attached to the system.

Shell Process exists as long as user is logged in.

Command Process exists as long as command executes.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 43

Copyright @2005 Pearson Addison-Wesley

Introduction UNIX/Linux Course

Process Hierarchy in UNIX Ptree displays the process tree of currently system,

showing parent-child relationship.

Shell process is bash shell.

Init process granddaddy of all processes.

Process tree of current system.

Telnet session opened.

SILICON VALLEY UNIVERSITY CONFIDENTIAL 44

Copyright @2005 Pearson Addison-Wesley

Introduction UNIX/Linux Course

Process Hierarchy in LINUX View process tree using ps command and “f” option (forest). [student1 ~]$ ps axlfF UID PID PPID PRI NI VSZ RSS WCHAN STAT TTY TIME COMMAND. . .5 0 2096 1 18 0 5380 844 - Ss ? 0:00 /usr/sbin/sshd4 0 2993 2096 17 0 8236 2748 - Ss ? 0:00 \_ sshd: student1 [priv]5 501 2995 2993 15 0 8236 1512 - S ? 0:00 \_ sshd: student1@pts/30 501 2996 2995 16 0 4824 1544 wait Ss pts/3 0:00 \_ -bash0 501 3028 2996 18 0 4244 852 - R+ pts/3 0:00 \_ ps axlf

. . . View process tree using pstree. Displays the process tree of currently

system, showing parent-child relationship. [student1 ~]$ pstreeinit─┬─. . .

├─sshd───sshd───sshd───bash───pstree. . .