CS 497C – Introduction to UNIX Lecture 27: - The Process Chin-Chih Chang
nice: Job Execution with Low Priority Processes are usually executed with equal priority. The nice command is used with the & operator to reduce the priority of jobs. To run a job with a low priority, the command name is prefixed with nice: nice wc -l manual & nice values are system-dependent and range from -20 to 19 in Linux. A higher nice value implies a lower priority. nice –5 wc –l manual &
Signals A signal is an interrupt generated by the shell or other process in response to some error condition. We can send a signal to the active process with a specific request of termination. After the signal is communicated with the process, the process does one of three things: –Do nothing. –Ignore the signal.
Signals –Trap the signal. The process catch the signal and decides to take some action on the signal. A signal is represented by an integer that presents a particular event: –1 (SIGHUP): Hang up –2 (SIGINT): Terminal interrupt –3 (SIGQUIT): Quit from terminal –9 (SIGKILL): Surest kill –15 (SIGTERM): Default termination signal –24 (SIGTSTP): Suspends process [Ctrl-z]
Signals The complete list of signals applicable to your machine can be found in the file /usr/include/sys/signal.h. In the Linux systems in our department, it is defined in /usr/include/bits/signum.h. SIGQUIT directs a process to produce a core dump (a file named core in the current directory). The SIGKILL signal will surely terminate the process.
kill: Premature Termination of a Process You can terminate a process with the kill command. The kill command uses one or more PIDs as its arguments, and by default uses the SIGTERM signal (15). If the default signal number 15 doesn’t work, then signal 9 will ( kill -9 ). The last background job can be killed with kill $! on most shells.
Job Control If you are using the C shell, Korn shell, or bash, you can use their job control facilities to manipulate jobs. Job control in these shells includes: –Relegate a job to the background (bg). –Bring it back to the foreground (fg). –List the active jobs (jobs). –Suspend a foreground job ([Ctrl-z]). –Kill a job (kill). fg %2 brings the second job to the foreground.
at and batch: Execute later You can schedule a job for one-time execution with the at command, or run it when the system load permits with the batch command. at takes as its argument the time the job is to be executed. $ at 14:08 empawk2.sh [Ctrl-d]
at and batch: Execute later You can also use the -f (file) option to take commands from a file. To mail the job completion to the user, use the -m (mail) option. Jobs can be listed with the at -l (list) command and removed with at -r (remove). The batch command uses an internal algorithm to determine the execution time. batch < empawk2.sh
cron: Running Jobs Periodically cron lets you schedule jobs so that they run periodically. It takes input from a user’s crontab file which is located in /var/spool/cron/crontabs. The file contains six fields separated by whitespace : –The first field specifies the minutes after the hour. –The second field indicates the hour. –The third field controls the day of the month.
cron: Running Jobs Periodically –The fourth field specifies the month. –The fifth field indicates the days of the week. –The last field is the command to be executed * 3,6,9,12 5 find / -size print The find command will executes every minute in the first 10 minutes after 5 p.m., every Friday of the months March, June, September, and December. A * indicates the command is executed every period on the field.
cron: Running Jobs Periodically The crontab command is used to make entries in the crontab file. You can see the contents of your crontab file with crontab -l and remove them with crontab -r. Not every user is able to use the cron facilities. Access to cron services are also controlled by cron.allow and cron.deny in /etc/cron.d or /usr/lib/cron.
time: Timing Processes The time command accepts a command to be timed as its argument and does this work. It executes the program and also displays the time usage on the terminal. This enables programmers to tune their programs to keep CPU usage at an optimum level. time sort –o list invoice Generally, three time information is shown: –The real time shown is the clock elapsed from the command invocation till its end.
time: Timing Processes –The user time shows the time spent by the program in executing itself. –The system time indicates the time used by the UNIX system in doing work. The sum of the user time and the system time actually represents the CPU time.