Thread View: cn.bbs.comp.program
1 messages
1 total messages
Started by ���ܵ�Сʨ
Thu, 07 May 1998 00:00
Daemon���������
Author: ���ܵ�Сʨ
Date: Thu, 07 May 1998 00:00
Date: Thu, 07 May 1998 00:00
175 lines
13370 bytes
13370 bytes
����֪ʶ
�����ǵ�¼����UNIX����ϵͳʱ,ϵͳΪ���Dz���һ����¼shell
, ��¼shell�ı����롢����ʹ������ָ��һ���ն��ļ����ڵ�
¼shell��,�������Ǽ�����������:
$proc1 | proc2 &$proc3 | proc4 | proc5�������������Ľ�
����͵�¼shell��һ�����ļ��ϳ���һ���Ự(session),��¼she
ll�������Ự�ĻỰͷ(session leader),���¼shell�������ն���
�������������ն�(control terminal)��һ���Ự�����һ��������
��,Ҳ����û�п����նˡ��п����ն˵ĻỰ�ĻỰͷ���������ƽ�
��(control proces) ��һ���Ự�еĽ����ɷ�Ϊǰ̨�����ͺ�
̨�����,���Ự�п����ն�ʱ,������һ��ǰ̨�����,�Ự�е���
�����鶼�Ǻ�̨����顣ֻ��ǰ̨�������Խ����ն�����,����
�Ǽ����ն˵Ŀ��Ƽ�ʱ,���жϼ�(һ��Ϊcontrol-c)�����˳���(һ
��Ϊcontrol-\),��������ж��ź�SIGINT�����˳��ź�SIGQUIT����
��ǰ̨������е����н�̡����ն˹���ʱ,IGH UP�źŷ��͵��Ự
�е�ÿһ�����,������ǰ̨�����,���Ǻ�̨�����(��һ��ϵ����
ͼ��ʾ)��
ͼ1
һ����̿��Ե���setsid������һ���µĻỰ,�����ý��
���ǽ����ͷ,����������һ���µĻỰ,ͬʱ�������������:
�������̱�Ϊ�»Ự�ĻỰͷ,�����ǻỰ�е�Ψһ��̡�
�������̱�Ϊ�½����Ľ����ͷ,�½��ID�ǵ��ý�̵�ID
��
�����û�п����ն�,������setsid֮ǰ���ӵ�п����ն�,��
�ú���ӵ�С�
�����ý���ǽ����ͷ,��ú������ʧ�ܡ�
ʲô��Daemon
daemon����˼�Ǿ�����ħ,��UNIX��һ�㷭��Ϊ�ػ���פ����
�̡���������������Ϊ��Ԥ,daemonһ���ڲ���ϵͳ����ʱ����,��
��ʱ����daemonͨ����Ϊ�ͻ� /������ģʽ�еķ�������̡�UN
IX����ϵͳ���д�����daemon�ո�һ�յ�Ϊ�ͻ���������Ҫ�ص���
:daemon��һ�ֺ�̨���,û�п����ն�,��˲����ն˿��Ƽ��Ӱ��,
Ҳ���������жϹ�����˳����������ǽ����ͷ�ͻỰͷ,�ǽ����
�ͻỰ��Ψһ�Ľ�̡�
daemon�����ԭ������:
(1)����fork���ҽ����̡����������Դﵽ����Ŀ�ġ�����,
���daemon����shel������������,�����̻�ʹshell��Ϊ������
������ڶ�,�ӽ�̼̳��˸���̵Ľ����IDͬʱ�õ�һ���µĽ�
��ID,�������ǿ��Ա�֤�ӽ�̲��ǽ����ͷ,���ǵ���setsid�����
�Ⱦ�������
(2)����setsid����һ���µĻỰ����̱�Ϊ�»Ự�ĻỰͷ��
�½����Ľ����ͷ,ͬʱ���û�п����նˡ�������Щ������daemo
n���ص㡣Ҳ����˵,����Ѿ����Ϊdaemo��̡����ǻ�������һ
Щ����Ҫ����
(3)�ر����д��ļ������֡�
(4)�ı䵱ǰ�Ĺ���Ŀ¼��
(5)�����ļ���ȡ�����������롣
(6)����SIGCLD�źš���Ϊserver,��daemon���յ�client������
ʱ,һ��forkһ���ӽ��Ϊclient�����ӽ���˳�ʱ,���daemon
δ����wait�ȴ��ӽ�̽���,���ӽ�̳�Ϊo mbie���;��ÿһ��zomb
ie���,�ں˻ᱣ��һЩ�йص���Ϣ,����ID,�Ա㸸��̵���wit
������д���client�������,�ؽ�����ܶ�zombie���ռ��ϵͳ��
��,����SIGCLD�ź���ɱ���zombie��̵Ļ��ۡ�
������ʹ��ǰ��ı���ԭ���д�ļ�daemon��̡�
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
int daemon_init(void){
pid_t pid;
if ( (pid = fork()) < 0)
return(-1);
else if (pid != 0)
exit(0); /* ������˳� */
/* �ӽ�̼��� */
setsid(); /* ��Ϊ�Ựͷ */
chdir("/"); /* �ı乤��Ŀ¼ */
umask(0); /* �����ļ���ȡ������������ */
signal(SIGCLD,SIG_IGN); /* ����SIGCLD�ź� */
return(0);
}
Daemon��־
�ػ����û�п����ն�,���Ե�daemonϣ���������Ϣʱ,����
����ʹ��printf�������ն�д������Ϣ����Ȼ,daemon���Դ�һ��
�ļ�,�ڴ��ļ��м�¼������Ϣ��������ļ�Ӧ������ӷ�ʽ��,��
��ǰ�ߵ���Ϣ������ߵ���Ϣ��ǡ���ʹ�����ַ�������һЩ����
:������־��Ϣ������,ϵͳ����Ա���벻�ϵضԸ��ļ����м��,ʹ
֮������һ�����ȷ�Χ��,���ϵͳ����Ա��һ���Ƚ��鷳�����顣
����,���������ʹ�������ļ���¼��־ʱ,����������daemonԴ��
�������,��UNIXϵͳ���ṩ��syslogd daemonר�����ڼ�¼��־��
Ϣ,ʹ��syslogd daemon����ʹ����������־ʹ�õ��ļ���
syslogd daemon������ʱ�������ļ�/etc/syslog.conf������
ͬ����Ϣ�ֱ������ĸ��ļ���,����,����ָ��������Ϣֱ�������
��̨,��һ��ĸ澯�¼���¼��ijһ�ļ��С�
ʹ��syslogd�ı�̽ӿ���:
#include<syslog.h>
void openlog(char *ident,int option,int facility);
void syslog(int priority,char *format,����);
void closelog(void);
����Щϵͳ������,���ǿ���ָ����Ϣ����Դ����Ϣ����Ҫ��
��,��������Ϣ�ϸ���������Ϣ����Щ�ӿڵľ���ʹ�÷�����������
��syslog.conf�ĸ�ʽ,�ɲο�ϵͳ�ṩ���ֲᡣ
Daemon��������
daemon������ʱ���ȶ������ļ���daemon��ʹ�õIJ����������
����������ı�ijЩ����ʱ,���ǿ��Ա༭�����ļ�����Ȼ,�༭����
���ļ��������̸ı���aemon�еIJ���,d aemon�������¶�����
�ļ���һ�ַ�������������ϵͳ,��Ȼ���ַ�����һ����ķ�����
��һ��ķ�����ʹ��ps�����ҳ�daemon���ID,����ý��Ȼ������
����demon�����ְ취��ȱ����:���ijһ���������daemonͨ��ʱ,
��ʹ�ý�̸о�������õİ취��:daemon�յ�ij������Լ�����ź�
ʱ,���¶������ļ�,����Ͳ����ж�daemon�����С�
�����daemon����������ʾ:
void read_config_file()
{
int fd;
fd=open("config_file",O_RDONLY);
/*�������ļ������ò���*/
close(fd);
signal(SIGHUP,read_config_file);
}
main()
{
read_config_file();
/*�������ѭ��*/
while(1)
{
}
}
��Ҫע�����:��read_config_file��,��ò�Ҫʹ��fopen,frea
d�Ⱥ���,��Ϊ�����Dz������뺯��(nonreentarnt function),�����
�ܲ�����Ԥ��Ľ��
������������������syslogd daemon����,�༭��syslog.conf��
����,���������µ�����,��syslogd��������:
$ps -ae | grep syslogd /* ���syslogd daemon��ID */$kil
l -HUP processID* ǿ��daemon���¶������ļ������� */
Daemon������
һ�������,daemon��ϵͳ����ʱ�Զ�����,Ҫʹdaemon��ϵͳ��
����������,��������Ӧ���������нű��м���һЩ���ݡ���UNXϵͳ
����ʱ,�ں˲���һ��init���,��Ľ��ID Ϊ1,��init���ִ��dae
mon�����ű���
��BSD��,init���ִ��/etc/rc.boot��/etc/rc��/etc/rc.local
�ű���rc.local�ű��Ǵ�����б���daemon����ĵط�����Щ������
��B shell����������daemonֻ����rc.local�м������и�d
aemon������ɡ�
��SVR4������Ƚϸ��ӡ�init���֧�ֶ�����м���,��0��6��s
(���û�)����ν���м�����Կ�����ϵͳ��������á�ÿ�����м���
ѡ�����в�ͬ�Ľ�̡����м���ͬʱ,ini���ȡ�Ķ���Ҳ��ͬ,��
�������ļ��Ͷ��shell�ű��ļ����ơ����������ļ���/etc/initab
�ļ��������Ǹ��ļ���һ������:
init:2:initdefault:
stty::sysinit:stty 9600 clocal icanon echo opost onlcr i
enqak ixon icrnl in par </dev/systty
link::wait:/bin/sh -c "rm -f /dev/syscon; ln /dev/systty
/dev/syscon" >/dev /console 2>&1
rc ::wait:/etc/rc </dev/console >/dev/console 2>&1 # sy
stem initializatio
halt:6:wait:/usr/lib/X11/ignition/shutdown.ksh\
# NOTE: runlevel 6 is reserved for system shutdown.
vue :234:respawn:/etc/vuerc #VUE validation and invocat
ion
inittab�ļ��ĸ�ʽ��:
���::���м���:����:���ÿ�еĽ�β�ɻس������#���Ƕ�
���е�ע�͡�ÿ���еĸ�����ð�Ÿ���,���ε���������:
�����:���ڱ����һ�С�
�����м���:ÿ��������м�������,���init�����м�����
1,���������1��һ�����ݡ����öο�ʱ,���κμ��������и�
�����
������:����init�����daemonʱӦ�ò�ȡ�IJ��ԡ�����ͨ�IJ�
����wait,����daemonֻ��ϵͳ����ʱ������,�����Ժ��Ƿ��жϡ�re
spawn����˼��:ϵͳ����daemon������״��,�����ж�ʱ�Զ�������
������������IJ���ֵ����Ͳ�һһ�����ˡ�
�����:��ִ�е�B shell�ű���shell���
���ǿ���ֱ����inittab�ļ���д��daemon��������,����д��B
shell�ű���
--
________ _____ _____ ______________ _____
___ __ \___(_)________ ___(_) __ ___/___ /_ ___(_)
__ /_/ /__ / ___ __ \__ / _____ \ __ __ \__ /
_ ____/ _ / __ /_/ /_ / ____/ / _ / / /_ /
/_/ /_/ _ .___/ /_/ /____/ /_/ /_/ /_/
/_/
[m[1;36m�� ��Դ:���ϴ�С�ٺ���Ϣ����վ bbs.nju.edu.cn��[FROM: hsia.nju.edu.cn][m
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads