1 / 32

5 UNIX Processes

5 UNIX Processes. Introduction. Processes How to list them How to terminate them Process priorities Scheduling jobs Signals. Processes. Every time you run a command under UNIX it creates a process A process is an instance of a running program

declan
Download Presentation

5 UNIX Processes

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

  2. Introduction • Processes • How to list them • How to terminate them • Process priorities • Scheduling jobs • Signals

  3. Processes • Every time you run a command under UNIX it creates a process • A process is an instance of a running program • A program and process are different things. wc is a program; each time you run the program wc, that creates a new process. • A process runs until it has finished or is killed

  4. Processes (2) • Every process has a number assigned to it called a process–ID or PID. • All the processes running on a UNIX machine are scheduled a certain amount of time on the CPU. The more that are running, the longer it will take for any one to finish

  5. Listing Processes • ps PID TTY TIME CMD 4073 pts/23 00:00:00 bash 16007 pts/23 00:00:00 ps • ps can be given different options to, e.g. list all processes on system, show the user that owns the process, etc.

  6. Child and Parent Processes • Any process can create another process • That process is then a parent process of the newly created child process • Use the pstree command to view relationships between parent and child processes (the process tree)

  7. pstree [zlizmj@unnc-cslinux ~]$ pstree -p zuczpd gconfd-2(10782) kdeinit(10733)-+-artsd(10752) |-emacs(10779)---aspell(27099) |-kdeinit(10738) |-kdeinit(10771)---bash(10783)---emacs(23700)-+-aspell(32542) | `-emacsserver(23754) |-kdeinit(10773) |-kdeinit(10774)---bash(10802) |-pam-panel-icon(10768)---pam_timestamp_c(10770) `-xload(10778)

  8. Running Job 1 $ ls $ $ ls & $ $ pwd & $ Prompt $ pwd Running Job 2 Child and Parent Processes (2) • A parent process forks to create a child process • This allows multiprocessing: Child Process Parent Process Child Process

  9. Child and Parent Processes (3) • Children run separately and simultaneously with each other and with their parents (although the parent might choose to wait for its child to finish) • Children inherit from their parents • When a process spawns to make a child process, the child is initially an exact copy of the parent, except for some differences (see next)

  10. Differences between Child and Parent Processes • They have different Process IDs (PIDs) • The have different PPIDs (parent PIDs) • Accounting information is reset for the child • For details of all the differences look up the manual page on the system call fork • All other things are initially the same, but each process has its own copy (they can modify their own copy)

  11. Being “nice” to other people! • If you are running a program on a shared machine and you know it will take some time - “nice” it • nice will run a program at a lower priority so that it doesn’t clog up the CPU • Priorities range from 19 to -20 • -20 is the highest priority, but you are not allowed to set priorities below 0 unless you are root $ nice –n 20 find . -name unix.ps –print

  12. Job Control: top • A program which shows you information about the top CPU processes • Updates this information at regular intervals • Type q to quit • Type k to kill a process (to kill means to send a signal) • Type u (return) followed by a username (return) to just see the processes belonging to that user

  13. Killing Processes • Use top • Alternatively, use kill • kill <signal number> <PID> • E.g. kill -15 25718 • “-15” is the signal number – here, it means “stop the process cleanly” (i.e. close any files it is using) • More about signals later… • “-9” means “kill the process whatever” • Useful if all else fails! • killall <signal number> <process name> will send the signal to every process with that name.

  14. Process States • Processes can have one of a number of states: • 0 - running on a processor • S - sleeping (waiting for an event to complete) • R - runnable (process is on run queue) • Z – zombie

  15. Running a Process in the Background • Some commands may take a while to complete • Some may run until the user Exits (e.g. emacs) • You may want to use your command line in the meantime: • & puts a process in the background (detaches it from the terminal) • E.g. emacs &

  16. Suspending Processes • Processes can be temporarily suspended • Use Ctrl-Z • To restart a process type one of: • fg (puts process back in the foreground) • bg (will restart process, but in the background) • Suspended processes can also be killed: • Do a process listing • Use the kill command with the process ID

  17. Job Control • The jobs command produces a numbered list of background and suspended processes • You can use these job numbers to access your jobs: • Note that the job number is not the process ID! [zlizmj@unnc-cslinux ~]$ jobs [1] - Running xclock -d [2] + Suspended more temp.txt [zlizmj@unnc-cslinux ~]$ kill %2 [zlizmj@unnc-cslinux ~]$ jobs [1] - Running xclock -d [3] Terminated more temp.txt

  18. Job Control (2) • jobs allows you to: • Bring a job to the foreground • fg %<job number> • Run a job in the background • bg %<job number> • Suspend a job • stop %<job number> • Terminate a job • kill %<job number>

  19. Control Key Sequences for Processes • Some control sequences affect processes: • Ctrl-C - kill a process • Ctrl-D - exit a shell (send EOF) • Ctrl-S - suspend or pause the display of output • Ctrl-Q - resume or continue output from Ctrl-S

  20. Daemons • UNIX daemons are processes which lie dormant until they are needed for a particular service (services in windows-speak) • Commonly, their names end with a ‘d’ • Examples are: • Printer daemons (lpd) • Web server daemons (httpd) • Scheduling daemons (atd, crond)

  21. Scheduling Processes - cron • Processes can be scheduled to run at a periodic intervals: • Use the cron daemon • With this, users can schedule processes to run periodically, or at specified times • Create a text file called crontab.cron which contains lines with a date/time and command line

  22. Scheduling Processes - cron (2) • Cron jobs are allowed or denied by system administrators using the cron.allow and cron.deny files in either /var/spool/cron or /etc/crond.d • You have to register your crontab using the command crontab crontab.cron in order for the cron daemon to activate your crontab

  23. Scheduling Processes - cron (3) • Each line in crontab.cron has five fields: • Minute - (0-59) • Hour - (0-23) • Day of the month - (1-31) • Month of the year - (1-12) • Day of the week - (0-6) (Sunday is 0) • Command line - the command to be executed

  24. Using cron • Edit your crontab.cron file to contain what you want it to do: • This cron job will record the date it was run every 30 minutes from Monday to Friday, in the file datelog • Register your crontab: 0,30 * * * 1-5 date >> datelog $ crontab crontab.cron

  25. Scheduling Processes - at • You can schedule something to happen once using at • at TIME will execute at given TIME the commands given in STDIN. • It’s often more comfortable to use at TIME < filename at TIME –f filename

  26. Scheduling Processes - at (2) $ at now + 1 min $ at> who | logged $ at> ls myDir | listing.txt $ at> <EOT> job 1171280502.a at Mon Feb 12 11:41:42 2007 $ at 3am < commands job 8 at 2007-03-21 03:00

  27. Scheduling Processes - batch • The batch command can be used to queue up jobs: • These jobs will be run as soon as the system has the resources to do so $ batch at> ls myDir > listing.txt at> <EOT> $

  28. UNIX Signals • Signals are a UNIX mechanism for controlling processes • A signal is a message to a process that requires immediate attention • Signals are generated by exceptions, e.g.: • Attempts to use illegal instructions • The user pressing an interrupt key • Window resize events • A child process calling exit or terminating abnormally

  29. Signal Numbers • Each signal has a default action associated with it • Most signals can be caught from within a program. A programmer can then: • Ignore signal • Perform the default action • Execute a program specified function • The default action can be • Term Terminate the process. • Ign Ignore the signal. • Core Terminate the process and dump core. • Stop Stop the process.

  30. Signal Numbers For a complete reference see the section 7 of the manual on signal $ man 7 signal

  31. Summary • UNIX processes • How to list them • How to prioritise them • How to schedule them • Parent and Child processes • Signals • What are they for? • Types of signal

  32. Next Lecture UNIX Network utilities

More Related