introduction to unix – cs 21 lecture 10. lecture overview midterm questions jobs and processes...

47
Introduction to Unix – CS 21 Lecture 10

Upload: chloe-holland

Post on 17-Dec-2015

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

“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

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

“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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Next Time Shell Programming Homework Assignment #2