CS21Lecture10

Download Report

Transcript CS21Lecture10

Introduction to Unix – CS 21
Lecture 10
Lecture Overview




Midterm questions
Jobs and processes description
The foreground and background
Controlling jobs
Midterm Results







Average: ~70, high 91.5
Problem 1
Problem 14
Problem 22
Problem 26
Problem 27
Problem 34
Sequential Execution Versus
Parallel Execution

Sequential


Happens one after another
Example


I must walk to my car and then I can drive home
Parallel


Happens at the same time
Example

I can watch TV and cook dinner at the same time
Multitasking Environment



Unix inherently allows parallel execution
You’ve seen this with multiple terminals
Multiple processes can be run from the
same terminal at the same time
Jobs = Processes = Programs

Process


Job



How the Operating System (Unix) refers to a
running program
How the shell views running programs started
from that shell
One job is one complete “Unix sentence” or
command line task
Both are simply different names for the
programs you run
Process ID (PID) and Control
Number

Process ID



The Unix operating system kernel assigns a
unique number to each running program
PID
Control Number


The shell assigns a unique number to each
command line run from that shell
Job ID
The Foreground

Foreground




Everything we’ve run so far has been in the
foreground
One command line at a time
The program that has control of the
terminal
No command line will appear until the task
is finished
Foreground Example
Before
After
The Background



Tasks that don’t need the terminal can
be run in the background
These tasks can run while you are
performing other tasks
The command line will pop up
immediately and you can do other tasks
Why Is This Useful?



Some tasks take a long time to finish
Doesn’t make sense to just sit around
and wait
Sometimes you only have one terminal
(i.e. you are remotely connected) and
you want to be able to do more than
one task
Running Jobs In The
Background


Place an & at the end of the command
line
Example:

sort testFile > sortedTestFile &
Background Example
Background Jobs And stdout




When something is run in the background, it
still has access to stdout
Programs can still write to the terminal
window
Could be confusing as it will intermingle with
other output
Suggestion:

Try to use output redirection when running jobs in
the background
Output Problem Example
Running Multiple Processes

Sequentially


ls ; more *.txt ; cd
Parallel

ls & cat testFile & grep “pattern” testFile
Examples
Sequential
Parallel
Seeing What Jobs Are
Running: The “jobs” command



Standard usage: jobs
With no arguments it simply prints out
a list of all jobs that are running in the
background of that shell
Gives the control number (job id) and
the command line running
Jobs Example
Process States




Runnable (running)
Suspended (stopped)
Waiting (Sleeping)
Zombie (defunct)
When Jobs In The Background
Finish
Suspending Jobs



If you run a program in the foreground,
you can reclaim the command line and
suspend that running task
Ctrl-z
The job will sit there and wait until you
start it again not doing anything
Suspending Jobs Example
Stopped Jobs

Some jobs are automatically stopped or
suspended if run in the background



Example: emacs
These are usually dependant on user input
and terminal control and can’t really do much
in the background
In order to get these programs running
again, they need to be switched to the
foreground
Switching Jobs To The
Foreground: “fg”

Usage: fg %NUM


The number specified here is the
control number, NOT the Process ID


Example: fg %2
The number listed by the jobs command
fg without any parameters takes the
most recent task (listed with a + in the
jobs command)
fg Example
Switching Suspended Jobs To
The Background: “bg”



Once a job has been suspended or
stopped, it will not do any work
If that job is switched to the
background, it can continue on its way
Usage: bg %NUM


Example: bg %1
Again, bg without any arguments moves
the most recent task into the background
bg Example
The ps Command



“Processor status”
Displays currently running processes
System V versus BSD


This command is probably the most
different basic command between the two
divisions of Unix
All the flags and all the output look
different
“ps” Example
Flag Combination To Know

ps aux

a


u


All with a controlling tty
User friendly format
x

All processes without a controlling tty
“ps aux” Example
The “top” Command


Provides up to the second information
on what is currently running
Only shows the processes that are
taking up the most CPU time


The processes that are working the hardest
The processes that are causing your
computer to run slowly…
Example Of “top”
Process Priority

Each process has a priority



Sense of urgency
Which process should get execution time
first
A number from –20 to 19

Just to be confusing, -20 is the highest
priority and 19 is the lowest
Adjusting Priority: The “nice”
Command




Usage: nice –n NUM COMMAND
Will run COMMAND with priority NUM
Allows you to specify which programs
are really important and which are
relatively unimportant
Our system will only allow you to
downgrade your priority
Piping Creates Many Processes



sort unsortedFile | uniq | wc
Creates three processes in parallel each
with a unique PID
Only creates one job
Piping Example
Terminating Rogue Jobs


Just suspending the job isn’t enough,
sometimes programs don’t do what you
want them to and need to be
terminated
When run in the foreground, Ctrl-c will
terminate a job, but what about the
background?
The kill Command

Usage: kill [OPTION] PID




OPTION is usually –NUM
Example: kill –11 1294
Will send a signal to a process running
By default, sends the termination signal
(the same as Ctrl-c)
Example Of kill
Signals And What They Mean



kill sends a signal to a process
A signal can be thought of as a
predetermined message
Each number is a different message
Table Of Signals
Signal Name
Number
Description
SIGHUP
1
Hang up
SIGINT
2
Interrupt
SIGQUIT
3
Quit
SIGILL
4
Illegal Instruction
SIGKILL
9
Kill Signal
SIGSEGV
11
Segmentation Fault
SIGPIPE
13
Broken Pipe
SIGTERM
15
Terminate
Handy Shortcut For Kill


You can use the %NUM notation that
we used with fg and bg to refer to
processes as jobs
Example:

kill %4
Extreme kill Usage


When all else fails, use the –9 option
Example:


kill –9 2364
-9 is the end all of signals

If this doesn’t end your program, nothing
will
The nohup Command



Usage: nohup COMMAND
Run COMMAND but ignore hang up
signals
With this command, you can continue
to run programs even after you log out
Next Time


Shell Programming
Homework Assignment #2