Опубликован: 06.08.2012 | Доступ: свободный | Студентов: 1179 / 25 | Оценка: 5.00 / 5.00 | Длительность: 53:41:00
Лекция 8:

Тaking control

< Лекция 7 || Лекция 8: 123456 || Лекция 9 >


A significant part of the work in a FreeBSD system is performed by daemons. A daemon is not just the BSD mascot described on page 21—it's also a process that goes around in the background and does routine work such as sending mail (sendmail), handling incoming Internet connections (inetd), or starting jobs at particular times (cron).

To quote the Oxford English Dictionary: Demon Also daemon. ME [In form and in sense I, a. L. dcemon (med. L. demon)...] 1a. In ancient Greek mythology (=\delta\alpha\iota\mu\omega\nu): A supernatural being of a nature intermediate between that of gods and men, an inferior divinity, spirit, genius (including the souls of deceased persons, esp deified heros). Often written dcemon for distinction.

You can recognize daemons in a ps waux listing by the fact that they don't have a controlling terminal—instead you see the characters ?? Each daemon has a man page that describes what it does.

Normally, daemons are started when the system is booted and run until the system is stopped. If you stop one by accident, you can usually restart them. One exception is init, which is responsible for starting other processes. If you kill it, you effectively kill the system. Unlike traditional UNIX systems, FreeBSD does not allow init to be killed.


One of the more useful daemons is cron named after Father Time.cron performs functions at specific times. For example, the system runs the script /etc/periodic/daily every day at 2:00 am, the script /etc/periodic/weekly every Saturday at 3:30 am, and the script /etc/periodic/monthly on the first day of every month at 5:30 am.

To tell cron to perform a function at a particular time, you need a file called a crontab. The system keeps the realcrontab where you can't get at it, but you can keep a copy. It's a good idea to call it crontab as well.

Let's look at the format of the default systemcrontab, located in /etc/crontab:

# /etc/crontab - root's crontab for FreeBSD
# $Id: crontab, v 1.10 1995/05/27 01:55:21 ache Exp $
# From: Id: crontab, v 1.6 1993/05/31 02:03:57 cgd Exp
PATH=/etc: /bin: /sbin: /usr/bin: /usr/sbin
# minute  hour  mday  month  wday  who   command
*/5       *     *     *      *     root  /usr/libexec/atrun
# rotate log files every hour, if necessary
#0        *     *     *      *     root  /usr/bin/newsyslog
#do daily/weekly/monthly maintenance
0         2     *     *      *     root  /etc/daily 2>&1
30        3     *     *      6     root  /etc/weekly 2>&1
30        5     1     *      *     root  /etc/monthly 2>&1
#time zone change adjustment for wall cmos clock,
#See adjkerntz (8) for details.
1, 31     0-4   *     *      *     root  /sbin/adjkerntz -a

As usual, lines starting with # are comments. The others have seven fields. The first five fields specify the minute, the hour, the day of the month, the month, and the day of the week on which an action should be performed. The character * means "every." Thus, 0 2*** (for /etc/daily) means "0 minutes, 2 o'clock (on the 24 hour clock), every day of the month, every month, every weekday."

Field number six is special: it only exists in /etc/crontab, not in private crontabs. It specifies the user for whom the operation should be performed. When you write your own crontab file, don't use this field.

The remaining fields define the operation to be performed cron doesn't read your shell initialization files. In particular, this can mean that it won't find programs you expect it to find. It's a good idea to put in explicit PATH definitions, or specify an absolute pathname for the program, as is done in this example. Cron mails the output to you, so you should check root's mail from time to time.

To install or list a crontab, use the crontab program:

$ crontab crontab              install a crontab
$ crontab –l                   list the contents of an installed crontab
#DO NOT EDIT THIS FILE - edit the master and reinstall.
#(crontab installed on Wed Jan   115:15:10 1997)
#(Cron version -- $Id: crontab.c,v 1.7 1996/12/17 00:55:12 pst Exp $)

Processes in FreeBSD Release 5

Some of the processes in the example above are specific to FreeBSD Release 5:

  • FreeBSD Release 5 has an idle process to use up the excess processor time and perform certain activities needed when no process is active. This example machine has two processors, so there are two of them:
    root  12 95.7  0.0  0  12  ??  RL  1Jan70 1406:43.85  (idle: cpu0)
    root  11 95.1  0.0  0  12  ??  RL  1Jan70 1406:44.64  (idle: cpu1)
  • A number of the processes have names starting with irq or swi:
    root  12  0.0  0.0  0  12  ??  WL  1Jan70 15:04.95  (swi1: net)
    root  13  0.0  0.0  0  12  ??  WL  1Jan70 21:30.29  (swi6: tty:s
    root  18  0.0  0.0  0  12  ??  WL  1Jan70  0:00.00  (swi3: cambi
    root  20  0.0  0.0  0  12  ??  WL  1Jan70  0:00.00  (irq11: ahc0
    root  21  0.0  0.0  0  12  ??  WL  1Jan70 39:00.32  (irq5: rl0)
    root  22  0.0  0.0  0  12  ??  WL  1Jan70  7:12.92  (irq14: ata0)
    root  23  0.0  0.0  0  12  ??  WL  1Jan70  0:47.99  (irq15: ata1)

    These processes handle hardware interrupts (irq) or software interrupts (swi). The text which follows gives an idea of which devices or software services they support.


Another tool for investigating system performance is top, which shows a number of performance criteria, including the status of the processes are using the most resources. Start it with the number of processes you want displayed. Example 8-1 gives an example.

$ top -S 10
last pid:    3992; load averages:    0.59, 0.17, 0.06 up 0+23:54:49 17:25:13
87 processes:    3running, 73 sleeping, 8 waiting, 3 lock
CPU states: 10.2% user, 0.0% nice, 18.8% system, 1.7% interrupt, 69.4% idle
Mem: 43M Active, 36M Inact, 31M Wired, 7460K Cache, 22M Buf, 2996K Free
Swap: 512M Total, 512M Free

  12  root  -16     0      0K    12K  RUN     0  23.7H  55.32%  55.32%  idle: cpu0
  11  root  -16     0      0K    12K  CPU1    1  23.7H  54.49%  54.49%  idle: cpu1
2854  grog   97     0   4940K  3932K  *Giant  1   0:04   3.88%   3.86%  xterm
  20  root  -64  -183      0K    12K  WAIT    1   0:08   0.83%   0.83%  irq14: ata0
2925  root   96     0    712K   608K  select  1   0:01   0.15%   0.15%  make
3193  grog   96     0   2220K  1304K  CPU0    0   0:01   0.15%   0.15%  top
3783  root   96     0    520K   416K  select  1   0:00   0.10%   0.05%  make
 167  root   96     0  13876K  2112K  select  0   1:02   0.00%   0.00%  xcpustate
  25  root  -68  -187      0K    12K  WAIT    0   0:28   0.00%   0.00%  irq9: xl0
 110  root   96     0   1528K   956K  select  1   0:26   0.00%   0.00%  ntpd

Figure8-2: top display

By default, the display is updated every two seconds and contains a lot of information about the system state:

  • The first line gives information about the last PID allocated (you can use this to follow the number of processes being created) and the load average, which gives information about how many processes are waiting to be scheduled.
  • The next line gives an overview of process statistics, and in what state they are. A process waits for external events to complete; it waits on a lock if some other process has a kernel resource which it wants.
  • The third line shows the percentage of time used in user mode, in system (kernel) mode and by interrupts.
  • The fourth line shows memory usage.
  • The fifth line shows swap statistics. When swapping activity occurs, it also appears on this line.
  • The remaining lines show the ten most active processes (because the parameter 10 was specified on the command line). The -S option tells top to include system processes, such as the idle and the interrupt processes. The state can be:
  • RUN, when the process is waiting for a processor to run on.
  • CPU0 or CPU1, when the process is actively executing.
  • *lock, where lock is the name of a kernel lock. In this example, the xterm is waiting on the lock Giant.
  • A wait string, which indicates an event on which the process is waiting. See the man page top (1) for more details.
< Лекция 7 || Лекция 8: 123456 || Лекция 9 >