Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes...
-
Upload
chloe-holland -
Category
Documents
-
view
219 -
download
0
Transcript of Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes...
![Page 1: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/1.jpg)
Introduction to Unix – CS 21
Lecture 10
![Page 2: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/2.jpg)
Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs
![Page 3: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/3.jpg)
Midterm Results Average: ~70, high 91.5 Problem 1 Problem 14 Problem 22 Problem 26 Problem 27 Problem 34
![Page 4: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/4.jpg)
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
![Page 5: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/5.jpg)
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
![Page 6: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/6.jpg)
Jobs = Processes = Programs Process
How the Operating System (Unix) refers to a running program
Job 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
![Page 7: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/7.jpg)
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
![Page 8: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/8.jpg)
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
![Page 9: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/9.jpg)
Foreground Example
Before
After
![Page 10: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/10.jpg)
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
![Page 11: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/11.jpg)
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
![Page 12: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/12.jpg)
Running Jobs In The Background Place an & at the end of the
command line Example:
sort testFile > sortedTestFile &
![Page 13: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/13.jpg)
Background Example
![Page 14: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/14.jpg)
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
![Page 15: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/15.jpg)
Output Problem Example
![Page 16: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/16.jpg)
Running Multiple Processes Sequentially
ls ; more *.txt ; cd Parallel
ls & cat testFile & grep “pattern” testFile
![Page 17: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/17.jpg)
Examples
Sequential
Parallel
![Page 18: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/18.jpg)
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
![Page 19: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/19.jpg)
Jobs Example
![Page 20: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/20.jpg)
Process States Runnable (running) Suspended (stopped) Waiting (Sleeping) Zombie (defunct)
![Page 21: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/21.jpg)
When Jobs In The Background Finish
![Page 22: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/22.jpg)
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
![Page 23: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/23.jpg)
Suspending Jobs Example
![Page 24: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/24.jpg)
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
![Page 25: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/25.jpg)
Switching Jobs To The Foreground: “fg” Usage: fg %NUM
Example: fg %2 The number specified here is the
control number, NOT the Process ID The number listed by the jobs command
fg without any parameters takes the most recent task (listed with a + in the jobs command)
![Page 26: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/26.jpg)
fg Example
![Page 27: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/27.jpg)
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
![Page 28: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/28.jpg)
bg Example
![Page 29: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/29.jpg)
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
![Page 30: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/30.jpg)
“ps” Example
![Page 31: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/31.jpg)
Flag Combination To Know ps aux
a All with a controlling tty
u User friendly format
x All processes without a controlling tty
![Page 32: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/32.jpg)
“ps aux” Example
![Page 33: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/33.jpg)
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…
![Page 34: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/34.jpg)
Example Of “top”
![Page 35: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/35.jpg)
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
![Page 36: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/36.jpg)
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
![Page 37: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/37.jpg)
Piping Creates Many Processes sort unsortedFile | uniq | wc Creates three processes in parallel
each with a unique PID Only creates one job
![Page 38: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/38.jpg)
Piping Example
![Page 39: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/39.jpg)
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?
![Page 40: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/40.jpg)
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)
![Page 41: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/41.jpg)
Example Of kill
![Page 42: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/42.jpg)
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
![Page 43: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/43.jpg)
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
![Page 44: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/44.jpg)
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
![Page 45: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/45.jpg)
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
![Page 46: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/46.jpg)
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
![Page 47: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d005503460f949d2d76/html5/thumbnails/47.jpg)
Next Time Shell Programming Homework Assignment #2