1 day 10 process control. 2 running a program in the background here’s a trick: you can actually...

13
1 Day 10 Process Control

Upload: nicholas-briggs

Post on 02-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Day 10 Process Control. 2 Running a program in the background Here’s a trick: You can actually log onto a UNIX server as many times as you want Try

1

Day 10

Process Control

Page 2: 1 Day 10 Process Control. 2 Running a program in the background Here’s a trick: You can actually log onto a UNIX server as many times as you want Try

2

Running a program in the background• Here’s a trick:

• You can actually log onto a UNIX server as many times as you want

• Try double-clicking on PuTTY a few times and log in every window

• You can run different things in different windows

Page 3: 1 Day 10 Process Control. 2 Running a program in the background Here’s a trick: You can actually log onto a UNIX server as many times as you want Try

3

Background• For instance, you could run hangman in one

window, elm in another, and have vi in another

• All appear to be running at the same time with no slowdown (usually)

Page 4: 1 Day 10 Process Control. 2 Running a program in the background Here’s a trick: You can actually log onto a UNIX server as many times as you want Try

4

More on Background• Try running this command:

• primes 0 100

• It will print out all prime numbers between 0 and 100

• Now keep running primes over and over and add zeros to the 100 each time

• primes 0 10000

Page 5: 1 Day 10 Process Control. 2 Running a program in the background Here’s a trick: You can actually log onto a UNIX server as many times as you want Try

5

Running primes• Eventually you will see that running primes takes

longer and longer

• Try running this one:– primes 0 100000000 > /dev/null

• This will take quite a while to finish

• You cannot type at the shell while this is running

Page 6: 1 Day 10 Process Control. 2 Running a program in the background Here’s a trick: You can actually log onto a UNIX server as many times as you want Try

6

Running primes in the background• One solution: run primes in one window and do

something else in another

• Or….

• Run primes in the background of just one window!

Page 7: 1 Day 10 Process Control. 2 Running a program in the background Here’s a trick: You can actually log onto a UNIX server as many times as you want Try

7

Background processes• You can start a command in the background (you

maintain control of the shell and can run new commands)

• Special character: &

• Example:– primes 0 100000000 > /dev/null &

Page 8: 1 Day 10 Process Control. 2 Running a program in the background Here’s a trick: You can actually log onto a UNIX server as many times as you want Try

8

Another way...• You can also stop a process

• Command: CONTROL-Z

• You are given a job number:– [1]+ Stopped hangman

• The job number is in brackets

Page 9: 1 Day 10 Process Control. 2 Running a program in the background Here’s a trick: You can actually log onto a UNIX server as many times as you want Try

9

Stopped Jobs• Jobs that are stopped can be brought back into the

foreground

• Command: fg %<job number>

• Example: fg %1

• Or they can begin running in the background

• Command: bg %<job number>

• Example: bg %1

Page 10: 1 Day 10 Process Control. 2 Running a program in the background Here’s a trick: You can actually log onto a UNIX server as many times as you want Try

10

You can also kill jobs• CONTROL-C can only be used in the foreground

• Must kill background jobs

• Command: kill %<job number>

• Example: kill %1

Page 11: 1 Day 10 Process Control. 2 Running a program in the background Here’s a trick: You can actually log onto a UNIX server as many times as you want Try

11

Forget a job number?• Command: jobs

• This will tell you job numbers for all running and paused jobs

Page 12: 1 Day 10 Process Control. 2 Running a program in the background Here’s a trick: You can actually log onto a UNIX server as many times as you want Try

12

Another Way• You can also use a job’s pid (process ID) to kill it

• You can get the pid with: ps

• Then you can use kill: kill <pid_number>

• Example: kill 12994

Page 13: 1 Day 10 Process Control. 2 Running a program in the background Here’s a trick: You can actually log onto a UNIX server as many times as you want Try

13

Stubborn Processes• You can force a process to die

• Command: kill -9 <pid_val>

• Example: kill -9 12994