160 likes | 447 Views
Introduction to UNIX. H. Foreground/Background Processing. Foreground/Background Processing. Performance Objectives: 1. Manipulate Processes and PIDs (ps, ^c, ^z, kill) 2. Run Commands in the Background (fg, bg, &) 3. Move a foreground command to background (^z, bg)
E N D
Introduction to UNIX H. Foreground/Background Processing Foreground/Background Processing
Foreground/Background Processing Performance Objectives: 1.Manipulate Processes and PIDs (ps, ^c, ^z, kill) 2. Run Commands in the Background (fg, bg, &) 3. Move a foreground command to background (^z, bg) 4. Check the status of background jobs (jobs) 5. Terminate a job (^c, ^d, kill) 6. Keep a job alive after logoff (nohup) 7. Suspend a running job Foreground/Background Processing
Processes and PIDs - ps • Each command generates an independent process. • Each process has a unique process identification number (PID). Foreground/Background Processing
Processes and PIDs - ps • Use the ps command to see what processes are running. host% ps PID TT STAT TIME COMMAND 3835 p2 S 0:00 -csh (csh) 3838 p2 R 0:00 ps • PID is the process identifier • TT is the control terminal • STAT is the status of the process Foreground/Background Processing
Processes and PIDs - ps (Con’t) • STAT is the status of the process: • R Runnable processes. • T Stopped processes. • P In page wait. • D Non-interruptible wait (waiting for disk etc). • S Sleeping for less than about 20 seconds. • I Idle, sleeping longer than about 20 seconds. • Z Terminated; waiting for parent ("zombie"). • (W) indicates process is swapped out. Foreground/Background Processing
Interrupting a Command • To "stop" a running interactive process use ^Z. • The message “stopped” is displayed. • A ps command will reveal the status: host% ps PID TT STAT TIME COMMAND 3835 p2 S 0:00 -csh (csh) 3839 p2 T 0:00 cat 3840 p2 R 0:00 ps Foreground/Background Processing
Foreground Processing - fg • Type fg to continue running the stopped job in the foreground. • Use jobs to list job numbers and status. host% jobs [1] - Stopped cat > file4 [2] +Running vi file.c Foreground/Background Processing
Background Processing - bg • To continue the job in the background use: host% bg %n • Where n is the job number. • To initiate a command in the background us the ampersand: • host% CC file.c & Foreground/Background Processing
Terminating Jobs • Terminate a stopped command with kill. host% killpid • Use -1 or -9 option to forcefully terminate. host% kill -9 1234 • Terminate a running job with ^C • Terminate a program waiting for input with a ^d Foreground/Background Processing
Keeping a Job Active - nohup • On logout, the C-shell removes child processes running in background. • You can terminate the shell before it sends signal 1. • Children continue to run and you will be logged off because your login shell has terminated. Foreground/Background Processing
Keeping a Job After Logout • Start a foreground process and then logoff. • Allow the process to continue to run. • Enter <CTRL-z> to stop the foreground process, • Execute bg to place process in background, • Run ps -u userid to obtain the pid of your login shell, • Enter kill -9 shellpid to kill the login shell and log off; but leave the process running detached from the terminal. • The process must not do any terminal I/O. Foreground/Background Processing
Keeping a Job Active - nohup • Include the command nohup (no arguments) in your .logout file. • No need to use kill, logout normally. • Executed on logout, nohup directs the login shell to not stop child processes. • Processes survive because no signal is sent. Foreground/Background Processing
Monitoring for Unwanted Jobs • Unwanted processes can consume valuable computer time. • Consider including the following command in the .login and .logout files to spot runaway or unwanted processes. • ps -u userid Foreground/Background Processing
End of Module Complete Foreground/Background Processing Exercises Foreground/Background Processing