我试图将守护进程(没有领导者的守护进程组)与标题中的主进程连接,问题是我必须将每个守护进程(支持SIGUSR1信号)的语句发送到主进程,但我不知道甚至不知道怎么做,在我的代码中我使用了mkfifo,但它根本不工作..
这是主要的流程来源:
int main(int argc, char* argv[])
{
pid_t pid;
int i;
int n = atoi(argv[1]);
char c, message[255];
if(argc!=2){
printf("please insert one parametr\n");
return -1;
}
int fd = open("pipe", O_RDONLY);
if (fd == -1) {
perror("Failed open fifo to read");
return EXIT_FAILURE;
}
for( i = 0; i < n; i++) {
pid=fork();
if (pid==0){
printf("daemon created..\n");
}
else{
execl("daemons", "daemons", argv[1], NULL);
while(1){
sleep(2);
read(fd, message, c);
printf("P received: %s\n", message);
close(fd);
//read(fd[0], message, sizeof(message));
}
}
}
return 0;
}
以下是我创建守护进程的一些源代码:
int fd = open("pipe", O_WRONLY);
if (fd < 0){
perror("cannot open fifo: ");
return EXIT_FAILURE;
}
if ( getppid() == 1 )
return 0;
/* Creating daemon */
pid[n] = fork();
if (pid[n] < 0)
exit(EXIT_FAILURE);
if (pid[n] > 0)
exit(EXIT_SUCCESS);
/* Setting leader of session */
sid = setsid();
if (sid < 0){
exit(EXIT_FAILURE);
}
/* fork one more time to make children
to have an opportunity to destroy
session leader */
for ( i = 0; i < n; i++){
pid[i] = fork();
if(pid[i] < 0){
printf("filed to fork...\n");
exit(EXIT_FAILURE);
}
if(pid[i]==0){
while(1){
sleep(2);
printf("Demon[%d]: My ID in pipe.%d\n", i+1, getpid());
signal(SIGUSR1, sigHandler);
write(fd, "Hi\n", strlen("Hi\n"));
close(fd);
}
}
chdir(".");
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
file = fopen("log_file.txt", "w+");
fclose(file);
umask(027);
}
至少我不确定我是否正在以良好的方式创建守护进程.. 我应该把信号放在哪里,以后可以执行?
你有什么建议吗?