1 / 24

Unix Comp-145

Unix Comp-145. Lecture 5: UNIX Processes Text: S. Das, “Your Unix: The ultimate Guide”, 2 nd Edition, McGraw Hill, 2006. Unix Operating System. The UNIX process structure Process Initialization Sequence Process Control Commands Running Shell Scripts in Background

trynt
Télécharger la présentation

Unix Comp-145

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. Unix Comp-145 Lecture 5: UNIX Processes Text: S. Das, “Your Unix: The ultimate Guide”, 2nd Edition, McGraw Hill, 2006 BROOKDALE COMMUNITY COLLEGE

  2. Unix Operating System The UNIX process structure Process Initialization Sequence Process Control Commands Running Shell Scripts in Background Process Signaling and Exit Structure Execute &Terminate Processes Schedule a Process to Run Automatically BROOKDALE COMMUNITY COLLEGE

  3. The Process An INSTANCE of a program in execution. Identified by a unique PID (Process-id). Created by another process as its child One process can be parent of multiple children Can be killed or stopped by sending it a SIGNAL BROOKDALE COMMUNITY COLLEGE

  4. The Process Cycle Parent FORKS a child by 1st replicating its own process image and logging the child in the process table. Child executes (overwrites) this image with that of another program. While child is running, parent may Wait for child to complete execution (If a child executing in foreground) Continue with its other tasks (If a child executing in foreground) Process terminates and parent picks up exit status of child. Until Parent picks up exit status of child, process called a ZOMBIE, because process not removed Kernel removes entry for dead child from process table. BROOKDALE COMMUNITY COLLEGE

  5. The Process Cycle (contd) • If Parent process dies before a child process • Child process called an ORPHAN • Process init becomes parent of child,as it is for most daemons • Orphan processes continue to operate until itskilled or ends normally. BROOKDALE COMMUNITY COLLEGE

  6. More on the Process • Built in Shell commands do not fork separate a process • For example: cdorpwd • Shell scripts use sub-shells to run commands in script • SIGNALS: Used by Kernel to communicate with a process • kill -s, or • generated through your keyboard BROOKDALE COMMUNITY COLLEGE

  7. Process Attributes Inherited by Child • Real User ID (UID) and Group (GID) • Effective UID and GID • Current directory • File descriptors • umask value • Environment variables BROOKDALE COMMUNITY COLLEGE

  8. PID Process ID – referred to as “pid” When born, logged in Process Table with unique number Used to track and kill a process Stored in variable $$ To see or show value of $$use echo $$ PPID Parent PID, referred to as “p-pid” Can be used to kill a child process but only be used if 1 child spawned $ ps PID TT STAT TIME COMMAND 64199 p3 S 0:00.00 -ksh (ksh) 64218 p3 R+ 0:00.00 ps $ Process Attributes Not Inherited by Child BROOKDALE COMMUNITY COLLEGE

  9. Process Control Commands • ps (program status) • kill (terminate process) BROOKDALE COMMUNITY COLLEGE

  10. Program Status (ps) • Display environment • Specifies a list of directories to search. • $ ps –e • PID TT STAT TIME COMMAND • 64199 p3 S 0:00.00 -ksh (ksh) • 64304 p3 R+ 0:00.00 ps –e BROOKDALE COMMUNITY COLLEGE

  11. Program Status (ps) (contd) • Display command-line and environment info about swapped out processes. UID of use must be 0. • $ ps –f • PID TT STAT TIME COMMAND • 64199 p3 S 0:00.00 -ksh (ksh) • 64305 p3 R+ 0:00.00 ps –f BROOKDALE COMMUNITY COLLEGE

  12. Program Status (ps) (contd) • Display detailed process listing • $ ps –l • 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 BROOKDALE COMMUNITY COLLEGE

  13. Running jobs in the Background • Append&(ampersand) to the end of a shell or command • Shell operator to run in background • Frees keyboard and screen for use. • $ sort empl.lst > junk2 & BROOKDALE COMMUNITY COLLEGE

  14. Running jobs in the Background (contd) • Prependnohup (no hang up) in front of a shell or command • Shell operator to not terminate the command running in background even if the session ends • $ nohup sort empl.lst > junk2 & • Unike& , usenohupwith each command in a pipeline, e.g. • $ nohupgrep directory | nohup sort > empl_directors_sorted& BROOKDALE COMMUNITY COLLEGE

  15. Process Signaling and Exit Structure • Job control uses <job_id>as it relates to PGID • Move jobs between foreground and background • Commands • fg • bg • suspend(output to terminal) • jobs(lists currently active jobs) • kill(terminates jobs) • ctl-z(suspends current foreground job) BROOKDALE COMMUNITY COLLEGE

  16. Execute & Terminate Processes • Job control commands • Start a command in the background • $ find / -name a.out –print > files_to_remove 2>dev/null & • [1] 1287 • $ ls -lR / > system_list 2>dev/null & • [2] 1288 • $ du -s /usrs1/* > disk_usage & • [3] 1289 • Stop output to the terminal + suspend job • Suspends job when tries to write to terminal • $ sttytostop BROOKDALE COMMUNITY COLLEGE

  17. Execute & Terminate Processes (contd) • Use of job control functions • Using job number move to foreground • $ fg %1 • Suspends only a foreground job • $ [ctl-z] • Using job number return job 1 to background • $ bg %1 • When job ends normally get • $ [2]- Exit 1 ls -lR / > system_list 2>dev/null • When job needs to be killed manually use <job_id> • $ kill %2 • [1] + Terminated ls -lR / > system_list 2>dev/null BROOKDALE COMMUNITY COLLEGE

  18. Terminate Processes • kill <pid> or<job_id> • Pidspecifies the process that the OS is to kill • The result is not reversible. • $ kill 154339 • $ kill %1 BROOKDALE COMMUNITY COLLEGE

  19. Daemonin UNIX • Scripts that lack terminal control • $ lpsched • $ mail, mailx, cron • ps –eshowscrondaemon running. • Programs invoked by launching a shell or sub-shell • Shell Scripts • Sub-shell reads & executes each statement in script BROOKDALE COMMUNITY COLLEGE

  20. Schedule a Process to Run Automatically • cronvscrontab • cron • Kernel started daemon • Started each minute to execute what is in a user’s crontabfile • crontab • Takesas input a user edited file • Used to insert jobs to be executed by crondaemon • crontab –r(removes content from a user’scrontabfile) • crontab –l(lists content of user’s crontabfile) • crontab<file>(lists content of user’s crontabfile) BROOKDALE COMMUNITY COLLEGE

  21. Schedule a Process to Run Automatically(contd) • CRONTAB file’s entry syntax: • minutes, hours, days of month, month, 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, # of hours • Day in month: 0 to max # days in month • Month: 1 to 12 • Days of week: 0 to 6, where0=Sunday • Use asterisk (*)to indicate all • Use comma (,) to indicate a set of values • Use hyphen (-) to indicate a range of values BROOKDALE COMMUNITY COLLEGE

  22. Schedule a Process to Run Automatically(contd) • Examples of entries in a crontabfile • minutes, hours, days of month, month, days of week, command • 00-10 17 10,20,30 * 1,3 find / -newer .last_time -print >backuplist • (Execute the command either 3 days of the month or Monday and Wednesday of each month) • 55 17 * * 4 find / -newer .last_time -print >backuplist • (Execute the command every Thursday at 5:55 pm) • 0,30 * * * * find / -newer .last_time -print >backuplist • (Execute the command every 3o minutes on the half hour) BROOKDALE COMMUNITY COLLEGE

  23. Schedule a Process to Run Automatically(contd) • Utility for one time execution • at <time> - same date, formathhmmorhh:mm • at –t <time2> - scheduled, format[[CC]YY]MMDDhhmm[.SS] • $ at 15:08 • at> empawk2.sh > rep.lst • at –l - lists items in “at”queue • at -r <job_id> - removes item from “at”queue • at –m <time> • Execute as a batch job in “at”queue • Batch jobs are run when resource utilization levels are low, • Have lower priority than user foreground jobs • Submits job to run in batch queue • batch < empawk22.sh BROOKDALE COMMUNITY COLLEGE

  24. Summary • Process Cycle • What parent or child can do while other is running • Program status command • Running a process • Background • Foreground • Terminate a processes • CRON and related commands BROOKDALE COMMUNITY COLLEGE

More Related