210 likes | 309 Views
Processes. Objectives to become familiar with Unix processes Contents processes & daemons Nice levels User limits System resources signals batch queues cron jobs Practicals to run and control several Unix processes Summary. The Unix Kernel. ksh chmod cron
E N D
Processes • Objectives • to become familiar with Unix processes • Contents • processes & daemons • Nice levels • User limits • System resources • signals • batch queues • cron jobs • Practicals • to run and control several Unix processes • Summary
The Unix Kernel ksh chmod cron ps ls who mail X11 123 man frame sysadm oracle pageout init memory multi-tasking management file devices system hardware kernel processes
Programs & Processes • A program is an executable file • for example: /bin/bash • a program may be executed by more than one process • A process is an instance of a program being executed • a process may execute more than one program during its lifetime • for example: mingetty® login® bash • SVR4 Unix supports three types of processes • real time processes (always run before all others) • system processes (run if no real time processes) • standard processes • each process category has its own priority levels
Daemons & Zombies • Daemon processes provide system functionality • detached from terminal • stdout redirected to console or log file • Zombies are completed processes • still holding onto resources • cleaned up when parent does a wait or exits
Key Attributes of a Process • Every process is identified by a unique number • its process id (pid) • Every process has an owner and a group • the effective UID and GID define the processes access to the system • The kernel allocates a control block for every process • there is a finite number of these process control blocks • an individual user can be restricted to just a few processes • Each process uses memory • code segment is read only and re-entrant • data and stack segments are writeable • The kernel uses a paged virtual memory system • writeable memory pages may be paged out to the swap file • code pages are discarded and reloaded from the executable program
Running Processes • A fork is Unix terminology for creating a child process • current process is duplicated to create the new child • child has a different PID otherwise identical • Exec replaces the current process with new program • usually done by a child process after a fork • there is an exec command in the shell • sometimes called "chaining" $ ps PID TTY TIME COMMAND 191 01 0:01 /bin/sh 207 01 0:00 ps $ exec ps PID TTY TIME COMMAND 191 01 0:01 ps Login: ps bash
Checking on Processes • Use the ps command to examine processes -f full listing -l long listing -e every process -a all interesting process (ignores daemons, login shells) -t terminal all processes attached to named terminal -u user all processes owned by named user • Key columns in output listings PID process ID PPID parent process ID UID owning user TTY controlling terminal CMD command used to run process S process state (-l listing only) R runnable, O on processor, S sleeping, Z zombie
Process priority • Nice-Levels From –19 to +20 -19 highest priority (system choking) +20 lowest priority (wait for all other processes to complete) • Use the nice command to set priority to application # nice –15 ls Will set nicelevel –15 to ls command • Use renice command to change priority to running process # renice 10 422 Will change nicelevel to 10 for process 422 • Getpriority & Setpriority • If you are programming C/C++ • Mother and Child • If mother is changed all childs will inherent nice levels
User resources control • ulimit (User resource limits) • shows a list of all current limits # ulimit –a core file size (blocks, -c) 0 data seg size (kbytes, -d) unlimited file size (blocks, -f) unlimited max locked memory (kbytes, -l) 32 max memory size (kbytes, -m) unlimited open files (-n) 1024 pipe size (512 bytes, -p) 8 stack size (kbytes, -s) unlimited cpu time (seconds, -t) unlimited max user processes (-u) 768 virtual memory (kbytes, -v) unlimited
Important Not all shells support ulimit directives. PAM (for instance, pam_limits) offers comprehensive adjustment possibilities if you depend on encompassing settings for these restrictions. Setting ulimits • Set ulimit • Systemwide is set in /etc/profile • On particular users in ~/.bashrc or ~/.bash_profile • Controlled by PAM modules /etc/security/limits.conf • PAM limits description in README.pam_limit Examples) # Limits of physical memory: ulimit -m 98304 # Limits of virtual memory: ulimit -v 98304
System resources control • sysctl files • Adjust all kinds of kernel parameters SuSE config file: /etc/sysconfig/sysctl Direct config file: /etc/sysctl.conf Memory filesystem: /proc/sys • reloading sysctl (the slow way) # SuSEconfig • show all parameters # sysctl –a • show all parameters, as a table! # sysctl –A • Load from /etc/sysctl.conf or other specified file # sysctl -p /etc/sysctl.conf • you can also do an echo # echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all Look in /proc/sys for parameters!
Exercise - Looking at Processes • Can you identify the daemon processes? • Can you follow the process tree for the ps command? $ ps -ef UID PID PPID STIME TTY TIME CMD root 1 0 09:22:31 ? 0:13 /sbin/init ... root 42 1 09:24:45 tty1 0:00 ttymon root 47 1 09:25:03 ? 0:02 cron hawkeye 81 1 10:02:04 console 0:03 -ksh hawkeye 121 81 10:22:43 console 0:01 vi datafile hawkeye 101 81 10:18:00 console 0:02 make myprog hawkeye 122 121 10:23:01 console 0:01 ksh hawkeye 125 122 10:24:31 console 0:01 ps -ef
Signals • Signals are used to communicate with a running process • processes define how to react to signals • most signals ignored by default • some cause process termination • signals defined by name (or number) • Hardware related program faults also cause signals • bus (memory) error • divide by zero • System administrators usually use signals to eliminate a process
Sending Signals • Use the kill command to send signals HUP (1) terminal hang up (powered off) INT (2) user pressed interrupt (^C) key QUIT (3) user pressed quit (^\) key KILL (9) kill immediately (process cannot ignore and dies immediately) TERM (15) terminate program (default) • Users can only signal processes they own # ps -fu trapper UID PID PPID STIME TTY TIME CMD trapper 92 1 10:02:04 tty1 0:03 -ksh trapper 97 92 10:18:00 tty1 0:02 myprog # kill 97 # ps -fu trapper | grep myprog trapper 97 92 10:18:00 tty1 0:02 myprog # kill -9 97
Intercepting Signals • The shell can respond to signals using the trap command • Traps are used mostly within programs and scripts trap 'echo Interrupt ignored' INT QUITwhen signal 2(INT)or signal 3 (QUIT) are received they will be ignored; instead the command in quotes, in this example the echo command, will be executed trap 'echo Terminating shell; exit 1' TERMthe action to be performed can include more that one command, program or indeed shell script trap '' 2 3 15if no action is required when intercepting signals, leave the quotes empy, but do include them;otherwise you will reset trapped signals (as below) trap INT QUIT TERMreset the three trapped signals (from now on, when received will be acted upon again)
The at Command • Execute commands at a specified time or run the commands on a batch queue • Syntax: attime [date] [increment] batch • Commands are read from stdin • When the job is run • stdout and stderr mailed to user • stdin is redirected to /dev/null • the job inherits current environment • commands are run using the Bourne shell $ at 8:00 tomorrow echo "Meeting at 9:30 today" ^D
Listing and Deleting at Jobs • Use options to at at -l [job...] list jobs at -r [job...] remove jobs • Newer SVR4 commands atq [user] list at queue atrm -a userremove all jobs for named user atrm -i userinteractive query on removal $ at -l 78653200.a at Thu Jul 27 08:00:00 1995 $ at -r 78653200.a
The crontab Command • Use crontab to run jobs at periodic intervals • List cron table with crontab -l[user] • Edit current cron table with crontab-e [user] • EDITOR or VISUAL variable must be defined • Commands specified in crontab include information to determine when to run the command • Space/tab separated columns • specify comma separated list or range of values • Fields in a cron table specify: minute hour day month weekday command
Exercise - Using cron • What programs are run on Thursday and what on Sunday? • Which programs generate mail messages? # "@(#) root 1.6 89/05/29 " 17 5 * * 0 /etc/cleanup > /dev/null 0 2 * * 0,4 /usr/lib/cron/logchecker 0 3 * * * /usr/lib/cleantmp > /dev/null 20 1 * * * /usr/bin/calendar - 0 8 * * 5 echo "Fill in timesheet today" 0 8 29 2 * echo "Happy Birthday" 11 1 * * 0 /home/system/bin/full.backup 11 1,13 * * 1-5 /home/system/bin/inc.backup
Administering at & crontab • Control files kept in /etc/ at.allow users allowed to use at at.deny users denied use of at (only used if no at.allow) cron.allow users allowed to use cron cron.deny users denied use of cron (only used if no cron.allow) (queuedefs limits jobs in at & batch queues) • SuSE / RedHat standard cron jobs /etc/ Add jobs to be executed hourly..monthly at will in: cron.d Yast update intervals cron.daily Daily cron jobs cron.hourly Hourly cron jobs cron.monthly monthly cron jobs cron.weekly weekly cron jobs crontab crontab maintanese • Awaiting jobs are kept in /var/spool / at*/ storage directory for at jobs cron/ storage directory for crontab files
Summary • The Unix kernel is responsible for the hardware interface and multi-tasking • All other functionality is performed by processes • Look at processes using ps • Daemon processes provide standard functionality such as scheduling using cron • Processes react to signals which can be sent with the kill command • Administrators use crontab to schedule regular jobs such as backups • Unix systems should be left running all the time to enable cron jobs to run overnight