Presentation on theme: "UNIX Processes. The UNIX Process A process is an instance of a program in execution. Created by another parent process as its child. One process can be."— Presentation transcript:
The UNIX Process A process is an instance of a program in execution. Created by another parent process as its child. One process can be parent of multiple children. The shell is the parent of the commands you run. The shell’s parent is the init process.
PID – The Process ID When born, logged in the Process Table with unique number Used to track and kill a process Shell’s PID is stored in variable $$ To see or show value of $$ use echo $$ PPID – The Process ID of the Parent Can be used to kill a child process Two Important Process Attributes
Other Process Attributes Inherited from Parent Real UID and GID Effective UID and GID Current directory File descriptors umask value Environment variables
$ ps Display a simple listing of your processes PID TT STAT TIME COMMAND 64199 p3 S 0:00.00 -ksh (ksh) 64218 p3 R+ 0:00.00 ps $ ps –a Display all the users’ processes $ ps –e Display all processes including system processes $ ps –f Display a full listing of your processes $ ps –l Display a long detailed listing of processes UID PID PPID CPU PRI NI VSZ RSS MWCHAN STAT TT TIME COMMAND 83641 75606 75605 0 20 0 1996 540 pause S p3 0:00.00 -ksh (ksh) 83641 75610 75606 0 96 0 5776 992 - R+ p3 0:00.00 ps –l Displaying Process Status with the ps Command
Daemons in UNIX Daemons are system processes that lack terminal control. Examples: lpsched mail, mailx cron Cannot read from terminal nor write to terminal ps –e shows all daemons running.
Exporting and Environment Variables A local variable is not visible in child processes: $ x=5 ; sh Create a child process $ echo $x No value in child! An exported variable is visible in all child processes: $ x=5 ; export x x is now environmental $ sh ; echo $x Create a child process 5 Changes made in child are not available in parent: $ x=7 Value set in child $ exit Child dies; back to parent $ echo $x 5 Original value in parent
The UNIX Process Cycle A parent process forks a child by replicating its own process image. The child process execs (overwrites) this image with that of another program. While the child is running, parent may wait for child to complete execution (foreground execution). continue with its other tasks (background execution). The process terminates and parent picks up exit status of child. Kernel removes entry for dead child from process table.
When the Child Dies Before the Parent When child dies, it leaves behind an exit status in process table. Parent process may pick up exit status; remove child entry in process table; child is now completely dead OR may not wait; child entry not removed from process table; child in zombie state. Zombies can’t be killed but are harmless (they take up space); shown as in ps output.
When the Parent Dies Before the Child Child becomes an orphan. Child adopted by init process. PPID of child changes to 1 (init process). Orphan processes continue to operate until killed or ends normally. When child dies, init picks up the exit status.
Signals Used by kernel to communicate with processes (e.g., notification of occurrence of an event, e.g. a keyboard press). Process may perform the default action OR ignore the signal OR catch the signal and invoke a signal-handling function (trap). Some important signals SIGINT (interrupt key)SIGQUIT (quit and core dump) SIGSTOP (suspend key)SIGKILL (a sure kill, but not a keyboard event)
No keyboard event generates a kill. Kill signals cannot be trapped. You must kill using this command. kill Example: $ kill 154339 (kills process 154339) Results are not reversible. Terminating Processes with the kill Command
Running a Job in the Background Job is run in background with &. Frees keyboard screen for other uses. Example: sort empl.lst > junk2 & If you logout before it’s finished, the job may be terminated. Using nohup (no hang up) and &, the job will run even after logging out. Example: nohup sort empl.lst > junk2 & Use nohup with every command in a pipeline. Example: nohup grep book | nohup sort > sortedfile &
Job Control Job control supported by most shells but not Bourne. Job accessed by job number usually shown in brackets e.g., . Apart from background execution, job control allows suspending a job ([Ctrl-z]). bringing a background job to foreground: fg %1 moving a suspended job to foreground or background: bg %find The jobs command lists all running/ suspended jobs. The kill command can also take a job id e.g., kill %3
Scheduling a Process to Run Periodically cron Kernel started daemon (UNIX’s chronograph) Wakes up each minute to execute what is in crontabs file crontab Takes as input a user edited file Used to insert jobs in crontabs file to be executed by cron daemon crontab –r (removes content from a user’s crontab file) crontab –l (lists content of user’s crontab file) crontab (adds content of user’s file to crontabs )
crontab File Format minutes hours days of month months days of week command Minutes: 00 to 59, can be expressed as a range e.g., 00-15, or 0,15,30,45 Hours : 0 to 23 Day in month: 0 to max # days in month Month: 1 to 12 Days of week: 0 to 6, where 0=Sunday Use asterisk (*) to indicate all Use comma (,) to indicate a set of values Use hyphen (-) to indicate a range of values
crontabs File Format Examples 00-10 17 10,20,30 * 1,3 find / -newer.last_time - print >bkup Execute the find command either 3 days of the month or Monday and Wednesday of each month. 55 17 * * 4 find / -newer.last_time -print > bkup Execute the find command every Thursday at 5:55 pm. 0,30 * * * * find / -newer.last_time -print >bkup Execute the find command every 30 minutes on the half hour.
at hh:mm or at Examples: at 15:08 at noon + 1 year at> flush.sh >rep.lst at> cleanup.sh > clean.lst ctl-d ctl-d batch - put in batch queue and run the job later runs when resource utilization levels are low. have lower priority than user foreground jobs. Example: batch < checkusers.sh System may limit use of these commands. Scheduling a Process to Run Later