Presentation on theme: "June 1, 1999Foreground/Background Processing1 Introduction to UNIX H. Foreground/Background Processing."— Presentation transcript:
June 1, 1999Foreground/Background Processing1 Introduction to UNIX H. Foreground/Background Processing
June 1, 1999Foreground/Background Processing2 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
June 1, 1999Foreground/Background Processing3 Processes and PIDs - ps Each command generates an independent process. Each process has a unique process identification number (PID).
June 1, 1999Foreground/Background Processing4 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
June 1, 1999Foreground/Background Processing5 Processes and PIDs - ps (Con’t) STAT is the status of the process: n R Runnable processes. n T Stopped processes. n P In page wait. n D Non-interruptible wait (waiting for disk etc). n S Sleeping for less than about 20 seconds. n I Idle, sleeping longer than about 20 seconds. n Z Terminated; waiting for parent ("zombie"). (W) indicates process is swapped out.
June 1, 1999Foreground/Background Processing6 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
June 1, 1999Foreground/Background Processing7 Foreground Processing - fg Type fg to continue running the stopped job in the foreground. Use jobs to list job numbers and status. host% jobs  - Stopped cat > file4  +Running vi file.c
June 1, 1999Foreground/Background Processing8 Background Processing - bg To continue the job in the background use: host% bg %n n Where n is the job number. To initiate a command in the background us the ampersand: è host% CC file.c &
June 1, 1999Foreground/Background Processing9 Terminating Jobs Terminate a stopped command with kill. host% kill pid Use -1 or -9 option to forcefully terminate. host% kill Terminate a running job with ^C Terminate a program waiting for input with a ^d
June 1, 1999Foreground/Background Processing10 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.
June 1, 1999Foreground/Background Processing11 Keeping a Job After Logout Start a foreground process and then logoff. Allow the process to continue to run. è Enter 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.
June 1, 1999Foreground/Background Processing12 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.
June 1, 1999Foreground/Background Processing13 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
June 1, 1999Foreground/Background Processing14 End of Module Complete Foreground/Background Processing Exercises