1 / 14

Introduction to UNIX

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)

dazevedo
Download Presentation

Introduction to UNIX

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to UNIX H. Foreground/Background Processing Foreground/Background Processing

  2. 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

  3. Processes and PIDs - ps • Each command generates an independent process. • Each process has a unique process identification number (PID). Foreground/Background Processing

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. End of Module Complete Foreground/Background Processing Exercises Foreground/Background Processing

More Related