Processes and Job Control - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

Processes and Job Control

Description:

SIMPLicity engine at Penn State, which was developed at Stanford ... Clears the command line [ctrl-S] Freezes the process [ctrl-Q] Unfreezes the process ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 30
Provided by: mer95
Category:

less

Transcript and Presenter's Notes

Title: Processes and Job Control


1
Processes and Job Control
  • CSCI 2467 Spring 2007
  • Instructor Michael Ruth
  • Computer Science Department
  • University of New Orleans
  • mruth_at_cs.uno.edu

2
Any Questions Before We Move On?
  • The two topics of today
  • Ken Thompson was born in New Orleans, LA
  • Not surprisingly currently at Google
  • We spoke earlier of a Content-based Image
    Retrieval
  • SIMPLicity engine at Penn State, which was
    developed at Stanford
  • http//wang14.ist.psu.edu/cgi-bin/zwang/regionsear
    ch_show.cgi
  • I can never seem to remember all of your
    questions please send them to mevia email
  • Any other questions before we move on?

3
Topics
  • Quoting metacharacters
  • Control codes
  • The Unix Process
  • Process Definitions
  • Process environment
  • Process execution
  • Process Management
  • Other Useful Commands
  • Touch
  • Aliases
  • History
  • Grep

4
Quoting Metacharacters
  • Sometimes you may need to quote a metacharacter
  • For example, suppose we wish to find all files
    that begin with .
  • . and .. are metacharacters?
  • We can quote a single metacharacter using a \
    just before it
  • Ex ls a \.
  • Suppose we need to quote a sequence of
    metacharcters
  • We quote using ltmetacharactersgt
  • Ex ls a

5
Control Codes
  • Control codes are a way to send signals of
    certain types to the system
  • The following summarizes some of the most
    commonly used control codes
  • ctrl-D
  • Signals the end of a file you are entering from
    the terminal if typed at the beginning of a line
    or if typed twice elsewhere in a line
  • ctrl-Z
  • Suspends a process or job but does not terminate
    it
  • Well discuss later why this is important
  • ctrl-C
  • Cancels a command or interrupts a running program
  • ctrl-U
  • Clears the command line
  • ctrl-S
  • Freezes the process
  • ctrl-Q
  • Unfreezes the process

6
The Unix Process Model
  • A program is an executable file residing on disk
    in a directory
  • A process is an instance of running a program
  • Every time you issue a command, Unix starts a new
    process, and suspends the current process (the
    shell) until the new process completes
  • Each process is uniquely identified on each
    system with a process identification number or
    PID
  • Every process has an environment in which it
    operates in

7
Process Environment
  • There is a notion of each process having its own
    memory space
  • There is also a notion of timesharing
  • However, at this juncture, well focus on
    environment variables and setting them
  • Environment Variables
  • Name value pairs which are used for possibly
    application specific reasons

8
Manipulating the Environment
  • For a list of all name-value pairs
  • env
  • For a specific environment value
  • echo ltnamegt
  • To set an environment value
  • export ltnamegtltvaluegt

9
Permanence and Env Vars
  • Using export saves the variables temporarily
  • When you log off they vanish
  • We need to be able to set them so that they
    persist even after logging out
  • We do this by putting the export statement in one
    of the following files
  • .cshrc
  • Called every time a process is created/started
  • Do NOT insert text outputting commands
  • Too much can slow execution of all command
  • .login
  • Run just after cshrc upon login (once only)
  • Put aliases or environment value stuff here
  • Activate changes in either by either logging out
    then back in or
  • Use source command like so
  • Ex source .login

10
Process Execution
  • A process may be in the foreground, in the
    background, or be suspended
  • In general the shell does not return the UNIX
    prompt until the current process has finished
    executing
  • Some processes take a long time to run and hold
    up the terminal

11
The background
  • Backgrounding a long process has the effect that
    the UNIX prompt is returned immediately, and
    other tasks can be carried out while the original
    process continues executing
  • To perform this
  • ltcommand linegt
  • The result of which looks something like
  • num-one num-two
  • num-one is the job id
  • num-two is the process id

12
Bringing it back
  • Suppose we want to bring the background process
    back into the foreground
  • We can get a list of jobs by running
  • jobs
  • This will give a list of items such as
  • ltjob idgt ltJob statusgt ltthe commandlinegt
  • To bring something back into the foreground
  • fg ltjob idgt
  • If there is no job id, then the last backgrounded
    job is brought to the foreground

13
Suspending processes
  • Why?
  • We can suspend processes in the foreground by
    using ctrl-Z
  • To suspend a background process, we need its job
    number
  • stop ltjob idgt

14
Resuming Processes
  • The bg command is used to resume a process in the
    background
  • bg ltjob idgt
  • The fg command resumes a suspended process in the
    foreground
  • fg ltjob idgt

15
Scheduling Processes
  • Additionally, we can schedule processes to run at
    a specific time in the future
  • at command does this
  • at job TIME
  • This is useful when we need to run things at a
    specific time (think of concurrency issues)

16
Attaining Process Status
  • To see information about your processes, with
    their associated PID and status
  • ps
  • Some useful options
  • -e
  • List all processes currently running on this
    machine
  • -f
  • Generate a full listing
  • -l
  • Generate a long listing

17
Example of running ps
PID TTY TIME CMD 3880 pts/10
000 ksh 3883 pts/10 000 ps
18
Example of running ps -e
PID TTY TIME CMD 0 ?
055 sched 1 ? 000 init 2 ?
000 pageout 3 ? 1925
fsflush 121 ? 000 sysevent 7 ?
004 svc.star 9 ? 013
svc.conf 125 ? 011 picld 103 ?
006 ipmon 204 ? 000
ttymon 96 ? 000 pfild 221 ?
000 rpc.nisd 712 ? 000
sendmail 852 ? 000 rpc.ttdb 130
? 010 nscd
PID TTY TIME CMD 195 ?
000 sac 134 ? 001 kcfd 189 ?
002 rpcbind 241 ? 000
nfs4cbd 196 ? 003 statd 205
console 000 sh 220 ? 001
ypserv 202 ? 000 lockd 206 ?
000 utmpd 381 ? 000
in.rarpd 227 ? 000 ypbind 719 ?
000 dtlogin 249 ? 006
nfsmapid 450 ? 000 vold 256 ?
000 cron
19
Example of running ps -f
UID PID PPID C STIME TTY TIME
CMD mruth 3938 3935 0 030854 pts/8
000 ps -f mruth 3935 3934 0 030849
pts/8 000 sh -i
20
Example of running ps -l
F S UID PID PPID C PRI NI ADDR
SZ WCHAN TTY TIME CMD 0 O 11743
3946 3943 0 50 20 ? 163
pts/8 000 ps 0 S 11743 3943 3942
0 50 20 ? 176 ? pts/8
000 ksh
21
Sometimes you want to place nice
  • You can set the priority of a process by using
    the nice command
  • Yes, I know everyone should be nice right?
  • This command allows us to improve/degrade the
    priority of a process
  • Unless you are a superuser you cannot improve the
    priority of a process
  • The syntax is as follows
  • nice -n ltcommand linegt
  • Where n is a number
  • positive degrades the priority, negative numbers
    improve the priority
  • Range goes from -20 (highest priority) to 19
    (lowest).

22
When its time not to be nice
  • There may be many reasons why we want to kill a
    process
  • To kill a job when it is in the foreground
  • ctrl-C
  • To kill a job when it is in the background
  • kill ltprocess idgt
  • kill -9 ltprocess idgt

23
Other useful commands
  • There are a number of commands that are highly
    useful
  • There are many, many more. We will cover what we
    need to cover as we need to cover it
  • However, there are a few which cannot be skipped
  • Touch
  • Touch resets the access and modification times of
    the file we touch
  • Alternatively creates the file if it doesnt
    exist
  • stty
  • Set terminal options
  • -a outputs all the settings for the terminal
  • stty option value
  • EX stty erase 'H'

24
AKA (aliases)
  • Aliases are used to rename commands, and
    sometimes to include options
  • Syntax alias cmdnew command
  • This allows us to enforce rules upon ourselves
  • EX alias rm"rm -i"
  • However, this is a double edge sword we could
    things make our own life worse

25
Those that dont study History
  • In Unix, we cant simply use the up button to
    get the command we just typed in
  • Of course, theres a much more complex system in
    place
  • Using the history command
  • The syntax history
  • Lists all recent commands
  • fc s ltcommand numbergt
  • Reruns the command given by the number
  • Can we save more commands in the list?
  • Yes, but you shouldnt need to do this man fc/env

26
grep
  • Finds lines in a file matching a character
    pattern
  • Syntax grep - icvn pattern file
  • Options of interest
  • -i Ignores case
  • -c Lists count of lines that contain pattern
  • -v Lists all lines except those with pattern
  • -n Lists line number for each found pattern
  • The pattern is a regular expression and the file
    is the file you are interested in
  • This command will certainly help each of you
    understand Regular expressions b/c its quick.

27
script
  • What if you wanted to log your usage at the
    shell?
  • Use the script command
  • script ltfilenamegt
  • It waits until the EOF is encountered (ctrl-d)

28
The future
  • Shell Programming Regular Expressions
  • Homework 1 is coming next week!!!
  • Krewe du Vieux is this weekend! (2/3 _at_ 7pm)
  • Introduction to C Programming

29
Questions?
Write a Comment
User Comments (0)
About PowerShow.com