|
//创建守护进程(首先父进程创建子进程,然后子进程杀死父进程,子进程后台运行)
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
/* Linux 的默任个人的邮箱地址是 /var/spool/mail/用户的登录名 */
#define MAIL "/var/spool/mail/mymailfile"
/* 睡眠10秒钟 */
#define SLEEP_TIME 10
main(void)
{
pid_t child;
if((child=fork())==-1){
printf("Fork Error:%s\n",strerror(errno));
exit(1);
}
else if(child>0)
while(1);
if(kill(getppid(),SIGTERM)==-1){
printf("Kill Parent Error:%s\n",strerror(errno));
exit(1);
}
{
int mailfd;
while(1){
if((mailfd=open(MAIL,O_RDONLY))!=-1){
fprintf(stderr,"%s","\007");
//printf("%c", mailfd);
close(mailfd);
}
sleep(SLEEP_TIME);
printf("waitting~~~");
}
}
}
守护进程运行之后,本来应该在间隔10秒钟时间打印“waitting~~~”“
结果却是间隔一大段时间,把N个的waitting~~~同时打印出来 。。
谁知道这样做的原因呢??说说吧。。。(个人的认为是觉得应该由于cpu轮流占有导致的 ) |
|