Ç°ÑÔ:ÕâÒ»ÕÂÎÒÃÇÌÖÂÛÒ»ÏÂLinuxϵÄÐźŴ¦Àíº¯Êý.
LinuxϵÄÐźŴ¦Àíº¯Êý:
1.ÐźŵIJúÉú
2.ÐźŵĴ¦Àí
3.ÆäËüÐźź¯Êý
--------------------------------------------------------------------------------
Ò»¸öʵÀý
1¡£ÐźŵIJúÉú
LinuxϵÄÐźſÉÒÔÀà±ÈÓÚDOSϵÄINT»òÕßÊÇWindowsϵÄʼþ.ÔÚÓÐÒ»¸öÐźŷ¢ÉúʱºòÏàÐŵÄÐźžͻᷢË͸øÏàÓ¦µÄ½ø³Ì.ÔÚLinuxϵÄÐźÅÓÐÒÔϼ¸¸ö. ÎÒÃÇʹÓà kill -l ÃüÁî¿ÉÒԵõ½ÒÔϵÄÊä³ö½á¹û:
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL
5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE
9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2
13) SIGPIPE 14) SIGALRM 15) SIGTERM 17) SIGCHLD
18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN
22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO
30) SIGPWR
¹ØÓÚÕâЩÐźŵÄÏêϸ½âÊÍÇë²é¿´man 7 signalµÄÊä³ö½á¹û. ÐźÅʼþµÄ·¢ÉúÓÐÁ½¸öÀ´Ô´:Ò»¸öÊÇÓ²¼þµÄÔÒò(±ÈÈçÎÒÃÇ°´ÏÂÁ˼üÅÌ),Ò»¸öÊÇÈí¼þµÄÔÒò(±ÈÈçÎÒÃÇʹÓÃϵͳº¯Êý»òÕßÊÇÃüÁî·¢³öÐźÅ). ×î³£ÓõÄËĸö·¢³öÐźŵÄϵͳº¯ÊýÊÇkill, raise, alarmºÍsetitimerº¯Êý. setitimerº¯ÊýÎÒÃÇÔÚ¼ÆʱÆ÷µÄʹÓà ÄÇÒ»ÕÂÔÙѧϰ.
#include
#include
#include
int kill(pid_t pid,int sig);
int raise(int sig);
unisigned int alarm(unsigned int seconds);
killϵͳµ÷ÓøºÔðÏò½ø³Ì·¢ËÍÐźÅsig.
Èç¹ûpidÊÇÕýÊý,ÄÇôÏòÐźÅsig±»·¢Ë͵½½ø³Ìpid.
Èç¹ûpidµÈÓÚ0,ÄÇôÐźÅsig±»·¢Ë͵½ËùÒÔºÍpid½ø³ÌÔÚͬһ¸ö½ø³Ì×éµÄ½ø³Ì
Èç¹ûpidµÈÓÚ-1,ÄÇôÐźŷ¢¸øËùÓеĽø³Ì±íÖеĽø³Ì,³ýÁË×î´óµÄÄĸö½ø³ÌºÅ.
Èç¹ûpidÓÉÓÚ-1,ºÍ0Ò»Ñù,Ö»ÊÇ·¢Ëͽø³Ì×éÊÇ-pid.
ÎÒÃÇÓÃ×î¶àµÄÊǵÚÒ»¸öÇé¿ö.»¹¼ÇµÃÎÒÃÇÔÚÊØ»¤½ø³ÌÄÇÒ»½ÚµÄÀý×ÓÂð?ÎÒÃÇÄǸöʱºòÓÃÕâ¸öº¯ÊýɱËÀÁ˸¸½ø³ÌÊØ»¤½ø³ÌµÄ´´½¨
raiseϵͳµ÷ÓÃÏò×Ô¼º·¢ËÍÒ»¸ösigÐźÅ.ÎÒÃÇ¿ÉÒÔÓÃÉÏÃæÄǸöº¯ÊýÀ´ÊµÏÖÕâ¸ö¹¦ÄܵÄ.
alarmº¯ÊýºÍʱ¼äÓеã¹ØϵÁË,Õâ¸öº¯Êý¿ÉÒÔÔÚsecondsÃëºóÏò×Ô¼º·¢ËÍÒ»¸öSIGALRMÐźÅ. ÏÂÃæÕâ¸öº¯Êý»áÓÐʲô½á¹ûÄØ?
#include
main()
{
unsigned int i;
alarm(1);
for(i=0;1;i++)
printf(""I=%d"",i);
}
SIGALRMµÄȱʡ²Ù×÷ÊǽáÊø½ø³Ì,ËùÒÔ³ÌÐòÔÚ1ÃëÖ®ºó½áÊø,Äã¿ÉÒÔ¿´¿´ÄãµÄ×îºóIֵΪ¶àÉÙ,À´±È½Ïһϴó¼ÒµÄϵͳÐÔÄܲîÒì(ÎÒµÄÊÇ2232).
2¡£ÐźŲÙ×÷ ÓÐʱºòÎÒÃÇÏ£Íû½ø³ÌÕýÈ·µÄÖ´ÐÐ,¶ø²»Ïë½ø³ÌÊܵ½ÐźŵÄÓ°Ïì,±ÈÈçÎÒÃÇÏ£ÍûÉÏÃæÄǸö³ÌÐòÔÚ1ÃëÖÓÖ®ºó²»½áÊø.Õâ¸öʱºòÎÒÃǾÍÒª½øÐÐÐźŵIJÙ×÷ÁË.
ÐźŲÙ×÷×î³£Óõķ½·¨ÊÇÐźÅÆÁ±Î.ÐźÅÆÁ±ÎÒªÓõ½ÏÂÃæµÄ¼¸¸öº¯Êý.
#include
int sigemptyset(sigset_t *set);
int sigfillset(sigset_t *set);
int sigaddset(sigset_t *set,int signo);
int sigdelset(sigset_t *set,int signo);
int sigismember(sigset_t *set,int signo);
int sigprocmask(int how,const sigset_t *set,sigset_t *oset);
sigemptysetº¯Êý³õʼ»¯Ðźż¯ºÏset,½«setÉèÖÃΪ¿Õ.sigfillsetÒ²³õʼ»¯Ðźż¯ºÏ,Ö»Êǽ«Ðźż¯ºÏÉèÖÃΪËùÓÐÐźŵļ¯ºÏ.sigaddset½«ÐźÅsigno¼ÓÈëµ½Ðźż¯ºÏÖ®ÖÐ,sigdelset½«ÐźŴÓÐźż¯ºÏÖÐɾ³ý.sigismember²éѯÐźÅÊÇ·ñÔÚÐźż¯ºÏÖ®ÖÐ.
sigprocmaskÊÇ×îΪ¹Ø¼üµÄÒ»¸öº¯Êý.ÔÚʹÓÃ֮ǰҪÏÈÉèÖúÃÐźż¯ºÏset.Õâ¸öº¯ÊýµÄ×÷ÓÃÊǽ«Ö¸¶¨µÄÐźż¯ºÏset¼ÓÈëµ½½ø³ÌµÄÐźÅ×èÈû¼¯ºÏÖ®ÖÐÈ¥,Èç¹ûÌṩÁËosetÄÇôµ±Ç°µÄ½ø³ÌÐźÅ×èÈû¼¯ºÏ½«»á±£´æÔÚosetÀïÃæ.²ÎÊýhow¾ö¶¨º¯ÊýµÄ²Ù×÷·½Ê½.
SIG_BLOCK:Ôö¼ÓÒ»¸öÐźż¯ºÏµ½µ±Ç°½ø³ÌµÄ×èÈû¼¯ºÏÖ®ÖÐ.
SIG_UNBLOCK:´Óµ±Ç°µÄ×èÈû¼¯ºÏÖ®ÖÐɾ³ýÒ»¸öÐźż¯ºÏ.
SIG_SETMASK:½«µ±Ç°µÄÐźż¯ºÏÉèÖÃΪÐźÅ×èÈû¼¯ºÏ.
ÒÔÒ»¸öʵÀýÀ´½âÊÍʹÓÃÕ⼸¸öº¯Êý.
#include
#include
#include
#include
int main(int argc,char **argv)
{
double y;
sigset_t intmask;
int i,repeat_factor;
if(argc!=2)
{
fprintf(stderr,""Usage:%s repeat_factor a"",argv[0]);
exit(1);
}
if((repeat_factor=atoi(argv[1]))<1)repeat_factor=10;
sigemptyset(&intmask); /* ½«Ðźż¯ºÏÉèÖÃΪ¿Õ */
sigaddset(&intmask,SIGINT); /* ¼ÓÈëÖÐ¶Ï Ctrl+C ÐźÅ*/
while(1)
{
/*×èÈûÐźÅ,ÎÒÃDz»Ï£Íû±£´æÔÀ´µÄ¼¯ºÏËùÒÔ²ÎÊýΪNULL*/
sigprocmask(SIG_BLOCK,&intmask,NULL);
fprintf(stderr,""SIGINT signal blocked "");
for(i=0;i fprintf(stderr,""Blocked calculation is finished "");
/* È¡Ïû×èÈû */
sigprocmask(SIG_UNBLOCK,&intmask,NULL);
fprintf(stderr,""SIGINT signal unblocked "");
for(i=0;i fprintf(stderr,""Unblocked calculation is finished "");
}
exit(0);
}
³ÌÐòÔÚÔËÐеÄʱºòÎÒÃÇҪʹÓÃCtrl+CÀ´½áÊø.Èç¹ûÎÒÃÇÔÚµÚÒ»¼ÆËãµÄʱºò·¢³öSIGINTÐźÅ,ÓÉÓÚÐźÅÒѾÆÁ±ÎÁË,ËùÒÔ³ÌÐòûÓз´Ó³.Ö»Óе½Ðźű»È¡Ïû×èÈûµÄʱºò³ÌÐò²Å»á½áÊø. ×¢ÒâÎÒÃÇÖ»Òª·¢³öÒ»´ÎSIGINTÐźžͿÉÒÔÁË,ÒòΪÐźÅÆÁ±ÎÖ»Êǽ«ÐźżÓÈëµ½ÐźÅ×èÈû¼¯ºÏÖ®ÖÐ,²¢Ã»ÓжªÆúÕâ¸öÐźÅ.Ò»µ©ÐźÅÆÁ±ÎÈ¡ÏûÁË,Õâ¸öÐźžͻᷢÉú×÷ÓÃ.
ÓÐʱºòÎÒÃÇÏ£Íû¶ÔÐźÅ×÷³ö¼°Ê±µÄ·´Ó³µÄ,±ÈÈçµ±Óµ»¤°´ÏÂCtrl+Cʱ,ÎÒÃDz»ÏëʲôÊÂÇéÒ²²»×ö,ÎÒÃÇÏë¸æËßÓû§ÄãµÄÕâ¸ö²Ù×÷²»ºÃ,Çë²»ÒªÖØÊÔ,¶ø²»ÊÇʲô·´Ó³Ò²Ã»ÓеÄ. Õâ¸öʱºòÎÒÃÇÒªÓõ½sigactionº¯Êý.
#include
int sigaction(int signo,const struct sigaction *act,
struct sigaction *oact);
struct sigaction {
void (*sa_handler)(int signo);
void (*sa_sigaction)(int siginfo_t *info,void *act);
sigset_t sa_mask;
int sa_flags;
void (*sa_restore)(void);
}
Õâ¸öº¯ÊýºÍ½á¹¹¿´ÆðÀ´ÊDz»ÊÇÓеã¿Ö²ÀÄØ.²»Òª±»Õâ¸öÏÅ×ÅÁË,ÆäʵÕâ¸öº¯ÊýµÄʹÓÃÏ൱¼òµ¥µÄ.ÎÒÃÇÏȽâÊÍһϸ÷¸ö²ÎÊýµÄº¬Òå. signoºÜ¼òµ¥¾ÍÊÇÎÒÃÇÒª´¦ÀíµÄÐźÅÁË,¿ÉÒÔÊÇÈκεĺϷ¨µÄÐźÅ.ÓÐÁ½¸öÐźŲ»Äܹ»Ê¹ÓÃ(SIGKILLºÍSIGSTOP). act°üº¬ÎÒÃÇÒª¶ÔÕâ¸öÐźŽøÐÐÈçºÎ´¦ÀíµÄÐÅÏ¢.oact¸ü¼òµ¥Á˾ÍÊÇÒÔÇ°¶ÔÕâ¸öº¯ÊýµÄ´¦ÀíÐÅÏ¢ÁË,Ö÷ÒªÓÃÀ´±£´æÐÅÏ¢µÄ,Ò»°ãÓÃNULL¾ÍOKÁË.
ÐźŽṹÓе㸴ÔÓ.²»Òª½ôÎÒÃÇÂýÂýµÄѧϰ.
sa_handlerÊÇÒ»¸öº¯ÊýÐÍÖ¸Õë,Õâ¸öÖ¸ÕëÖ¸ÏòÒ»¸öº¯Êý,Õâ¸öº¯ÊýÓÐÒ»¸ö²ÎÊý.Õâ¸öº¯Êý¾ÍÊÇÎÒÃÇÒª½øÐеÄÐźŲÙ×÷µÄº¯Êý. sa_sigaction,sa_restoreºÍsa_handler²î²»¶àµÄ,Ö»ÊDzÎÊý²»Í¬°ÕÁË.ÕâÁ½¸öÔªËØÎÒÃǺÜÉÙʹÓÃ,¾Í²»¹ÜÁË.
sa_flagsÓÃÀ´ÉèÖÃÐźŲÙ×÷µÄ¸÷¸öÇé¿ö.Ò»°ãÉèÖÃΪ0ºÃÁË.sa_maskÎÒÃÇÒѾѧϰ¹ýÁË
ÔÚʹÓõÄʱºòÎÒÃÇÓÃsa_handlerÖ¸ÏòÎÒÃǵÄÒ»¸öÐźŲÙ×÷º¯Êý,¾Í¿ÉÒÔÁË.sa_handlerÓÐÁ½¸öÌØÊâµÄÖµ:SIG_DELºÍSIG_IGN.SIG_DELÊÇʹÓÃȱʡµÄÐźŲÙ×÷º¯Êý,¶øSIG_IGNÊÇʹÓúöÂÔ¸ÃÐźŵIJÙ×÷º¯Êý.
Õâ¸öº¯Êý¸´ÔÓ,ÎÒÃÇʹÓÃÒ»¸öʵÀýÀ´ËµÃ÷.ÏÂÃæÕâ¸öº¯Êý¿ÉÒÔ²¶×½Óû§µÄCTRL+CÐźÅ.²¢Êä³öÒ»¸öÌáʾÓï¾ä.
#include
#include
#include
#include
#include
#define PROMPT ""ÄãÏëÖÕÖ¹³ÌÐòÂð?""
char *prompt=PROMPT;
void ctrl_c_op(int signo)
{
write(STDERR_FILENO,prompt,strlen(prompt));
}
int main()
{
struct sigaction act;
act.sa_handler=ctrl_c_op;
sigemptyset(&act.sa_mask);
act.sa_flags=0;
if(sigaction(SIGINT,&act,NULL)<0)
{
fprintf(stderr,""Install Signal Action Error:%s a"",strerror(errno));
exit(1);
}
while(1);
}
ÔÚÉÏÃæ³ÌÐòµÄÐźŲÙ×÷º¯ÊýÖ®ÖÐ,ÎÒÃÇʹÓÃÁËwriteº¯Êý¶øûÓÐʹÓÃfprintfº¯Êý.ÊÇÒòΪÎÒÃÇÒª¿¼Âǵ½ÏÂÃæÕâÖÖÇé¿ö.Èç¹ûÎÒÃÇÔÚÐźŲÙ×÷µÄʱºòÓÖÓÐÒ»¸öÐźŷ¢Éú,ÄÇô³ÌÐò¸ÃÈçºÎÔËÐÐÄØ? ΪÁË´¦ÀíÔÚÐźŴ¦Àíº¯ÊýÔËÐеÄʱºòÐźŵķ¢Éú,ÎÒÃÇÐèÒªÉèÖÃsa_mask³ÉÔ±. ÎÒÃǽ«ÎÒÃÇÒªÆÁ±ÎµÄÐźÅÌí¼Óµ½sa_mask½á¹¹µ±ÖÐÈ¥,ÕâÑùÕâЩº¯ÊýÔÚÐźŴ¦ÀíµÄʱºò¾Í»á±»ÆÁ±ÎµôµÄ.
3¡£ÆäËüÐźź¯Êý ÓÉÓÚÐźŵIJÙ×÷ºÍ´¦Àí±È½Ï¸´ÔÓ,ÎÒÃÇÔÙ½éÉܼ¸¸öÐźŲÙ×÷º¯Êý.
#include
#include
int pause(void);
int sigsuspend(const sigset_t *sigmask);
pauseº¯ÊýºÜ¼òµ¥,¾ÍÊǹÒÆð½ø³ÌÖ±µ½Ò»¸öÐźŷ¢ÉúÁË.¶øsigsuspendÒ²ÊǹÒÆð½ø³ÌÖ»ÊÇÔÚµ÷ÓõÄʱºòÓÃsigmaskÈ¡´úµ±Ç°µÄÐźÅ×èÈû¼¯ºÏ.
#include
int sigsetjmp(sigjmp_buf env,int val);
void siglongjmp(sigjmp_buf env,int val);
»¹¼ÇµÃgotoº¯Êý»òÕßÊÇsetjmpºÍlongjmpº¯ÊýÂð.ÕâÁ½¸öÐźÅÌøתº¯ÊýÒ²¿ÉÒÔʵÏÖ³ÌÐòµÄÌøתÈÃÎÒÃÇ¿ÉÒÔ´Óº¯ÊýÖ®ÖÐÌøתµ½ÎÒÃÇÐèÒªµÄµØ·½.
ÓÉÓÚÉÏÃ漸¸öº¯Êý,ÎÒÃǺÜÉÙÓöµ½,ËùÒÔÖ»ÊÇ˵Ã÷ÁËÒ»ÏÂ,ÏêϸÇé¿öÇë²é¿´Áª»ú°ïÖú.
4¡£Ò»¸öʵÀý »¹¼ÇµÃÎÒÃÇÔÚÊØ»¤½ø³Ì´´½¨µÄÄĸö³ÌÐòÂð?ÊØ»¤½ø³ÌÔÚÕâÀïÎÒÃÇ°ÑÄǸö³ÌÐò¼ÓÇ¿Ò»ÏÂ. ÏÂÃæÕâ¸ö³ÌÐò»áÔÚÒ²¿ÉÒÔ¼ì²éÓû§µÄÓʼþ.²»¹ýÌṩÁËÒ»¸ö¿ª¹Ø,Èç¹ûÓû§²»Ïë³ÌÐòÌáʾÓÐеÄÓʼþµ½À´,¿ÉÒÔÏò³ÌÐò·¢ËÍSIGUSR2ÐźÅ,Èç¹ûÏë³ÌÐòÌṩÌáʾ¿ÉÒÔ·¢ËÍSIGUSR1ÐźÅ.
#include
#include
#include
#include
#include
#include
#include
#include
#include
/* Linux µÄĬÈθöÈ˵ÄÓÊÏäµØÖ·ÊÇ /var/spool/mail/ */
#define MAIL_DIR ""/var/spool/mail/""
/* ˯Ãß10ÃëÖÓ */
#define SLEEP_TIME 10
#define MAX_FILENAME 255
unsigned char notifyflag=1;
long get_file_size(const char *filename)
{
struct stat buf;
if(stat(filename,&;buf)==-1)
{
if(errno==ENOENT)return 0;
else return -1;
}
return (long)buf.st_size;
}
void send_mail_notify(void)
{
fprintf(stderr,""New mail has arrived07 "");
}
void turn_on_notify(int signo)
{
notifyflag=1;
}
void turn_off_notify(int signo)
{
notifyflag=0;
}
int check_mail(const char *filename)
{
long old_mail_size,new_mail_size;
sigset_t blockset,emptyset;
sigemptyset(&;blockset);
sigemptyset(&;emptyset);
sigaddset(&;blockset,SIGUSR1);
sigaddset(&;blockset,SIGUSR2);
old_mail_size=get_file_size(filename);
if(old_mail_size<0)return 1;
if(old_mail_size>0) send_mail_notify();
sleep(SLEEP_TIME);
while(1)
{
if(sigprocmask(SIG_BLOCK,&;blockset,NULL)<0) return 1;
while(notifyflag==0)sigsuspend(&;emptyset);
if(sigprocmask(SIG_SETMASK,&;emptyset,NULL)<0) return 1;
new_mail_size=get_file_size(filename);
if(new_mail_size>old_mail_size)send_mail_notify;
old_mail_size=new_mail_size;
sleep(SLEEP_TIME);
}
}
int main(void)
{
char mailfile[MAX_FILENAME];
struct sigaction newact;
struct passwd *pw;
if((pw=getpwuid(getuid()))==NULL)
{
fprintf(stderr,""Get Login Name Error:%s a"",strerror(errno));
exit(1);
}
strcpy(mailfile,MAIL_DIR);
strcat(mailfile,pw->pw_name);
newact.sa_handler=turn_on_notify;
newact.sa_flags=0;
sigemptyset(&;newact.sa_mask);
sigaddset(&;newact.sa_mask,SIGUSR1);
sigaddset(&;newact.sa_mask,SIGUSR2);
if(sigaction(SIGUSR1,&;newact,NULL)<0)
fprintf(stderr,""Turn On Error:%s a"",strerror(errno));
newact.sa_handler=turn_off_notify;
if(sigaction(SIGUSR1,&;newact,NULL)<0)
fprintf(stderr,""Turn Off Error:%s a"",strerror(errno));
check_mail(mailfile);
exit(0);
}
ÐźŲÙ×÷ÊÇÒ»¼þ·Ç³£¸´ÔÓµÄÊÂÇé,±ÈÎÒÃÇÏëÏóÖ®Öеĸ´Ôӳ̶Ȼ¹Òª¸´ÔÓ,Èç¹ûÄãÏë³¹µ×µÄŪÇå³þÐźŲÙ×÷µÄ¸÷¸öÎÊÌâ,ÄÇô³ýÁË´óÁ¿µÄÁ·Ï°ÒÔÍ⻹Ҫ¶à¿´Áª»úÊÖ²á.²»¹ýÈç¹ûÎÒÃÇÖ»ÊÇÒ»°ãµÄʹÓõĻ°,ÓÐÁËÉÏÃæµÄ¼¸¸öº¯ÊýÒ²¾Í²î²»¶àÁË. ÎÒÃǾͽéÉܵ½ÕâÀïÁË.
tsh326 ÓÚ 2010-05-04 00:08:09·¢±í:
¶¥Æð
power1952 ÓÚ 2010-05-01 14:36:01·¢±í:
ÐźźÍÐźÅÁ¿Ã²ËƲ»ÊÇÒ»ÂëÊ£¡
yeqishi ÓÚ 2010-04-29 15:27:04·¢±í:
¶ÔÐźÅÁ¿ÓÐÒ»¶¨Ñ§Ï°£¬¶¥Â¥Ö÷
redapp ÓÚ 2005-12-03 19:10:32·¢±í:
Ì«ºÃÁË£¬Ö§³Ö