大佬們,我的子行程和守護行程都不能輸入信號怎么搞啊。
除了my_func里的不能執行,子行程和守護行程寫入日志的都可以寫入。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <syslog.h>
#include <signal.h>
void my_func(int sign_no)
{
if(sign_no==SIGINT)
{
printf("I have get SIGINT %d\n",sign_no);
exit(0);
}
else
{
char buf1[100];
if(sign_no==SIGQUIT)
{
int fd1;
if((fd1=open("/tmp/daemon.log",O_CREAT|O_WRONLY|O_APPEND, 0600))<0)
{
syslog(LOG_ERR,"open");
exit(1);
}
else
{
snprintf(buf1,sizeof(buf1),"%s""If you want to quit,please try SIGINT\n");
write(fd1,buf1,strlen(buf1));
}
}
int ret=alarm(15);
pause();
}
}
int main()
{
sigset_t set;
struct sigaction action, action1,action2;
pid_t pid,pid2;
int i, fd,x;
char buf[100];
openlog("daemon", LOG_PID, LOG_DAEMON);
pid = fork();
if (pid< 0)
{
printf("Error fork\n");
exit(1);
}
else if (pid> 0)
{
exit(0);
}
setsid();
chdir("/");
umask(0);
for(i = 0; i <getdtablesize() ; i++)
{
close(i);
}
if(pid==0)
{
pid2=fork();
if(pid2<0)
{
printf("Error fork\n");
}
else if(pid2==0)
{
syslog(LOG_INFO,"Child is running!");
sigemptyset(&action.sa_mask);
action.sa_handler = my_func;
action.sa_flags = 0;
sigaction(SIGINT, &action, NULL);
sigaction(SIGQUIT, &action,NULL);
pause();
exit(0);
}
if (sigemptyset(&set) < 0)
{
perror ("sigemptyset");
exit(1);
}
if (sigaddset(&set, SIGTSTP) < 0)
{
perror("sigaddset");
exit(1);
}
if (sigaddset(&set, SIGQUIT) < 0)
{
perror("sigaddset");
exit(1);
}
if (sigaddset(&set, SIGINT) < 0)
{
perror("sigaddset");
exit(1);
}
if (sigismember(&set, SIGINT))
{
sigemptyset(&action1.sa_mask);
action1.sa_handler = SIG_DFL;
action1.sa_flags = 0;
sigaction(SIGINT, &action1, NULL);
}
if (sigismember(&set, SIGQUIT))
{
sigemptyset(&action2.sa_mask);
action2.sa_handler = SIG_DFL;
action2.sa_flags = 0;
sigaction(SIGQUIT, &action2,NULL);
}
if(sigprocmask(SIG_BLOCK, &set, NULL) < 0)
{
perror("sigprocmask");
exit(1);
}
else
{
printf("Signal set was blocked, Press any key!");
}
waitpid(pid2,NULL,WNOHANG);
if(sigprocmask(SIG_UNBLOCK, &set, NULL) < 0)
{
perror("sigprocmask");
exit(1);
}
else
{
printf("Signal set is in unblock state\n");
x=1;
while(1)
{
if ((fd = open("/tmp/daemon.log",O_CREAT|O_WRONLY|O_APPEND, 0600))<0)
{
syslog(LOG_ERR, "open");
exit(1);
}
snprintf(buf,sizeof(buf),"%s%d%s","Daemon message",x,"\n");
x++;
write(fd, buf, strlen(buf));
close(fd);
sleep(1);
}
}
while(1);
exit(0);
}
closelog();
}
uj5u.com熱心網友回復:
你的代碼格式怎么這樣,看得頭疼。uj5u.com熱心網友回復:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <syslog.h>
#include <signal.h>
void my_func(int sign_no)
{
if(sign_no==SIGINT)
{
printf("I have get SIGINT %d\n",sign_no);
exit(0);
}
else
{
char buf1[100];
if(sign_no==SIGQUIT)
{
int fd1;
if((fd1=open("/tmp/daemon.log",O_CREAT|O_WRONLY|O_APPEND, 0600))<0)
{
syslog(LOG_ERR,"open");
exit(1);
}
else
{
snprintf(buf1,sizeof(buf1),"%s""If you want to quit,please try SIGINT\n");
write(fd1,buf1,strlen(buf1));
}
}
int ret=alarm(15);
pause();
}
}
int main()
{
sigset_t set;
struct sigaction action, action1,action2;
pid_t pid,pid2;
int i, fd,x;
char buf[100];
openlog("daemon", LOG_PID, LOG_DAEMON);
pid = fork();
if (pid< 0)
{
printf("Error fork\n");
exit(1);
}
else if (pid> 0)
{
exit(0);
}
setsid();
chdir("/");
umask(0);
for(i = 0; i <getdtablesize() ; i++)
{
close(i);
}
if(pid==0)
{
pid2=fork();
if(pid2<0)
{
printf("Error fork\n");
}
else if(pid2==0)
{
syslog(LOG_INFO,"Child is running!");
sigemptyset(&action.sa_mask);
action.sa_handler = my_func;
action.sa_flags = 0;
sigaction(SIGINT, &action, NULL);
sigaction(SIGQUIT, &action,NULL);
pause();
exit(0);
}
if (sigemptyset(&set) < 0)
{
perror ("sigemptyset");
exit(1);
}
if (sigaddset(&set, SIGTSTP) < 0)
{
perror("sigaddset");
exit(1);
}
if (sigaddset(&set, SIGQUIT) < 0)
{
perror("sigaddset");
exit(1);
}
if (sigaddset(&set, SIGINT) < 0)
{
perror("sigaddset");
exit(1);
}
if (sigismember(&set, SIGINT))
{
sigemptyset(&action1.sa_mask);
action1.sa_handler = SIG_DFL;
action1.sa_flags = 0;
sigaction(SIGINT, &action1, NULL);
}
if (sigismember(&set, SIGQUIT))
{
sigemptyset(&action2.sa_mask);
action2.sa_handler = SIG_DFL;
action2.sa_flags = 0;
sigaction(SIGQUIT, &action2,NULL);
}
if(sigprocmask(SIG_BLOCK, &set, NULL) < 0)
{
perror("sigprocmask");
exit(1);
}
else
{
printf("Signal set was blocked, Press any key!");
}
waitpid(pid2,NULL,WNOHANG);
if(sigprocmask(SIG_UNBLOCK, &set, NULL) < 0)
{
perror("sigprocmask");
exit(1);
}
else
{
printf("Signal set is in unblock state\n");
x=1;
while(1)
{
if ((fd = open("/tmp/daemon.log",O_CREAT|O_WRONLY|O_APPEND, 0600))<0)
{
syslog(LOG_ERR, "open");
exit(1);
}
snprintf(buf,sizeof(buf),"%s%d%s","Daemon message",x,"\n");
x++;
write(fd, buf, strlen(buf));
close(fd);
sleep(1);
}
}
while(1);
exit(0);
}
closelog();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/76159.html
標籤:應用程序開發區
上一篇:求解qemu加載問題
下一篇:救命啊,kali
