migrate main loop function call from clone to pthread

This commit is contained in:
Sergey Popov 2012-03-23 17:46:32 +04:00
parent 3d8d32cf1c
commit 80d7cb6e16

View File

@ -1,6 +1,7 @@
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>
#include <getopt.h>
#include <confuse.h>
@ -19,7 +20,7 @@ bool loop_locked=false; // flag for locking main loop while config re-reading
long failures_first=0; // count of failures while hosts checking, that triggers SMS sending
long failures_second=0; // count of failures while hosts checking, that triggers host's reset
long failures_third=0; // count of failures while hosts checking, that clean failure statistics
pid_t mainloop_clone_pid; // pid of clone process, that containt main loop
pthread_t mainloop_handle; // handle of main loop thread
void* allocate_memory(int bytes)
{
@ -179,12 +180,8 @@ void check_host(int num,host_decl* host)
host->helper_pid=pid;
}
int loop_function()
void* loop_function(void* param)
{
// set default signal handlers
signal(SIGTERM,SIG_DFL);
signal(SIGHUP,SIG_DFL);
//
for (int i=0;i<hosts_count;i++)
{
check_host(i,&hosts[i]);
@ -241,6 +238,7 @@ int loop_function()
continue;
}
}
return NULL;
}
void signal_handler(int signum)
@ -249,9 +247,8 @@ void signal_handler(int signum)
{
// lock Main Loop
loop_locked=true;
// kill Main Loop clone process
kill(mainloop_clone_pid,SIGTERM);
waitpid(mainloop_clone_pid,NULL,__WCLONE);
// kill Main Loop thread
pthread_cancel(mainloop_handle);
// Stop IPC loop and remove IPC queue
ipc_free();
// free config structure
@ -374,11 +371,13 @@ int main(int argc, char *argv[])
while (1) {
if (!loop_locked)
{
unsigned char child_stack[16384];
mainloop_clone_pid=clone(loop_function,child_stack+8192,CLONE_VM,NULL);
if(pthread_create(&mainloop_handle,NULL,loop_function, NULL) != 0)
{
log_event("Thread creation failed");
}
sleep(60); /* wait 60 seconds */
// wait for clone process to prevent zombie
waitpid(mainloop_clone_pid,NULL,__WCLONE);
// wait for the completion of thread
pthread_join(mainloop_handle,NULL);
// log_event("Exiting");
// exit(EXIT_SUCCESS);
}