FastNetMon

Friday 30 November 2012

Подсистема форка CGI процессов в PHP в пятидесяти строках


#include
#include
# include

//struct sigaction act, old_term, old_quit, old_int;
static int parent = 1;
static int children = 4;
int status = 0;
/*
void fastcgi_cleanup(int signal) {
sigaction(SIGTERM, &old_term, 0);
    printf("Signal handler\n");
    exit(0);
}
*/
int main() {
    int running = 0;
    /*
    act.sa_flags = 0;
    act.sa_handler = fastcgi_cleanup;
    if (sigaction(SIGTERM, &act, &old_term) ||
        sigaction(SIGINT,  &act, &old_int)  ||
        sigaction(SIGQUIT, &act, &old_quit)
    ) {  
        printf("Can't set signals");
        exit(1);
    }
    */
    while (parent) {
        do {
            int pid = fork();

            switch (pid) {
                case 0:
                    printf("Start child: %d\n",  getpid());
                /* Child */
    /*    
          sigaction(SIGTERM, &old_term, 0);
                sigaction(SIGQUIT, &old_quit, 0);
                sigaction(SIGINT,  &old_int,  0);
*/
                    parent = 0;
                    break;
                default:
                    printf ("Master: %d\n", getpid());
                     running++;
                    break;
            }
            } while (parent && (running < children));
            if (parent) {
   
                while (1) {
                    if (wait(&status) >= 0) {
                        printf("Master wait loop\n");
                        running--;
                       
                        printf("Master: child killed\n");
                        break;
                    }  
                }  

            }
    }

    if (!parent) {
               printf("Child start to work: %d\n", getpid());
   
                int counter = 0;
                while (1) {
                    counter++;
                    sleep(1);
                    if (counter > 5) {
                        break;
                    }  
                }  
   
                printf("stop %d process\n", getpid());
                exit(0);
    }
}

Код вычленен из оригинального: sapi/cgi/cgi_main.c

No comments :

Post a Comment

Note: only a member of this blog may post a comment.