processes objectives –to become familiar with unix processes contents –processes & daemons...

21
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

Upload: william-dawson

Post on 29-Dec-2015

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Processes Objectives –to become familiar with Unix processes Contents –processes & daemons –Nice levels –User limits –System resources –signals –batch

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

Page 2: Processes Objectives –to become familiar with Unix processes Contents –processes & daemons –Nice levels –User limits –System resources –signals –batch

kshchmod cron

ps ls

who mail

X11 123

man frame

sysadm oracle

pageout init

The Unix Kernel

memory multi-taskingmanagement

file devices system

hardware

kernel

processes

Page 3: Processes Objectives –to become familiar with Unix processes Contents –processes & daemons –Nice levels –User limits –System resources –signals –batch

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

Page 4: Processes Objectives –to become familiar with Unix processes Contents –processes & daemons –Nice levels –User limits –System resources –signals –batch

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

Page 5: Processes Objectives –to become familiar with Unix processes Contents –processes & daemons –Nice levels –User limits –System resources –signals –batch

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

Page 6: Processes Objectives –to become familiar with Unix processes Contents –processes & daemons –Nice levels –User limits –System resources –signals –batch

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

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:

bash

ps

Page 7: Processes Objectives –to become familiar with Unix processes Contents –processes & daemons –Nice levels –User limits –System resources –signals –batch

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 listingsPID 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

Page 8: Processes Objectives –to become familiar with Unix processes Contents –processes & daemons –Nice levels –User limits –System resources –signals –batch

Process priority

• Nice-LevelsFrom –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 new childs will inherent nice levels

Page 9: Processes Objectives –to become familiar with Unix processes Contents –processes & daemons –Nice levels –User limits –System resources –signals –batch

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

Page 10: Processes Objectives –to become familiar with Unix processes Contents –processes & daemons –Nice levels –User limits –System resources –signals –batch

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

     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.

Page 11: Processes Objectives –to become familiar with Unix processes Contents –processes & daemons –Nice levels –User limits –System resources –signals –batch

System resources control

• sysctl files, sysctl command runs when server boots– Adjust all kinds of kernel parameters

Direct config file: /etc/sysctl.conf

Memory filesystem: /proc/sys

– reloading sysctl (the slow way)

# reboot

- 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

- For example, enabling IP forward:

net.ipv4.ip_forward = 1

- you can also do an echo strait to memory cell setting

# echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all Look in /proc/sys for parameters!

Page 12: Processes Objectives –to become familiar with Unix processes Contents –processes & daemons –Nice levels –User limits –System resources –signals –batch

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

$ 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

Page 13: Processes Objectives –to become familiar with Unix processes Contents –processes & daemons –Nice levels –User limits –System resources –signals –batch

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

Page 14: Processes Objectives –to become familiar with Unix processes Contents –processes & daemons –Nice levels –User limits –System resources –signals –batch

Sending Signals

• Use the kill command to send signalsHUP (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

# 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

Page 15: Processes Objectives –to become familiar with Unix processes Contents –processes & daemons –Nice levels –User limits –System resources –signals –batch

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 15 if 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)

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 15 if 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)

Page 16: Processes Objectives –to become familiar with Unix processes Contents –processes & daemons –Nice levels –User limits –System resources –signals –batch

The at Command

• Execute commands at a specified time or run the commands on a batch queue

• Syntax:at time [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

$ at 8:00 tomorrow

echo "Meeting at 9:30 today"

^D

Page 17: Processes Objectives –to become familiar with Unix processes Contents –processes & daemons –Nice levels –User limits –System resources –signals –batch

Listing and Deleting at Jobs

• Use options to at at -l [job...] list jobs

at -r [job...] remove jobs

• Newer SVR4 commandsatq [user] list at queue

atrm -a user remove all jobs for named user

atrm -i user interactive query on removal

$ at -l

78653200.a at Thu Jul 27 08:00:00 1995

$ at -r 78653200.a

$ at -l

78653200.a at Thu Jul 27 08:00:00 1995

$ at -r 78653200.a

Page 18: Processes Objectives –to become familiar with Unix processes Contents –processes & daemons –Nice levels –User limits –System resources –signals –batch

The crontab Command

• Use crontab to run jobs at periodic intervals

• List cron table withcrontab -l [user]

• Edit current cron table withcrontab -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

Page 19: Processes Objectives –to become familiar with Unix processes Contents –processes & daemons –Nice levels –User limits –System resources –signals –batch

Exercise - Using cron

# "@(#) root 1.6 89/05/29 "17 5 * * 0 /etc/cleanup > /dev/null0 2 * * 0,4 /usr/lib/cron/logchecker0 3 * * * /usr/lib/cleantmp > /dev/null20 1 * * * /usr/bin/calendar -

# "@(#) root 1.6 89/05/29 "17 5 * * 0 /etc/cleanup > /dev/null0 2 * * 0,4 /usr/lib/cron/logchecker0 3 * * * /usr/lib/cleantmp > /dev/null20 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.backup11 1,13 * * 1-5 /home/system/bin/inc.backup

0 8 * * 5 echo "Fill in timesheet today"0 8 29 2 * echo "Happy Birthday"11 1 * * 0 /home/system/bin/full.backup11 1,13 * * 1-5 /home/system/bin/inc.backup

• What programs are run on Thursday and what on Sunday?

• Which programs generate mail messages?

Page 20: Processes Objectives –to become familiar with Unix processes Contents –processes & daemons –Nice levels –User limits –System resources –signals –batch

Administering at & crontab• Control files kept in /etc/

at.allow users allowed to use atat.deny users denied use of at (only used if no at.allow)

cron.allow users allowed to use croncron.deny users denied use of cron (only used if no cron.allow)(queuedefs limits jobs in at & batch queues)

• All Linux standard cron jobs /etc/Add jobs to be executed hourly..monthly at will in:cron.d Every minute cron jobscron.daily Daily cron jobscron.hourly Hourly cron jobscron.monthly monthly cron jobscron.weekly weekly cron jobscrontab crontab maintanese

• Awaiting jobs are kept in /var/spool /at*/ storage directory for at jobscron/ storage directory for crontab files

Page 21: Processes Objectives –to become familiar with Unix processes Contents –processes & daemons –Nice levels –User limits –System resources –signals –batch

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