Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs."— Presentation transcript:

1 Introduction to Unix – CS 21 Lecture 10

2 Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs

3 Midterm Results Average: ~70, high 91.5 Problem 1 Problem 14 Problem 22 Problem 26 Problem 27 Problem 34

4 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

5 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

6 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

7 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

8 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

9 Foreground Example Before After

10 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

11 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

12 Running Jobs In The Background Place an & at the end of the command line Example: sort testFile > sortedTestFile &

13 Background Example

14 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

15 Output Problem Example

16 Running Multiple Processes Sequentially ls ; more *.txt ; cd Parallel ls & cat testFile & grep “pattern” testFile

17 Examples Sequential Parallel

18 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

19 Jobs Example

20 Process States Runnable (running) Suspended (stopped) Waiting (Sleeping) Zombie (defunct)

21 When Jobs In The Background Finish

22 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

23 Suspending Jobs Example

24 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

25 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)

26 fg Example

27 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

28 bg Example

29 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

30 “ps” Example

31 Flag Combination To Know ps aux a All with a controlling tty u User friendly format x All processes without a controlling tty

32 “ps aux” Example

33 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…

34 Example Of “top”

35 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

36 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

37 Piping Creates Many Processes sort unsortedFile | uniq | wc Creates three processes in parallel each with a unique PID Only creates one job

38 Piping Example

39 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?

40 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)

41 Example Of kill

42 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

43 Table Of Signals Signal NameNumberDescription SIGHUP1Hang up SIGINT2Interrupt SIGQUIT3Quit SIGILL4Illegal Instruction SIGKILL9Kill Signal SIGSEGV11Segmentation Fault SIGPIPE13Broken Pipe SIGTERM15Terminate

44 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

45 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

46 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

47 Next Time Shell Programming Homework Assignment #2


Download ppt "Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs."

Similar presentations


Ads by Google