Simplified nginx multi-process model demo and nginxdemo

Source: Internet
Author: User

Simplified nginx multi-process model demo and nginxdemo

[test@vm c]$ cat mynginx.c
#include <stdio.h>#include <string.h>#include <unistd.h>char **os_argv;char *os_argv_last; init_setproctitle(void){ char *p; size_t size; int i; size = 0; os_argv_last = os_argv[0]; for (i = 0; os_argv[i]; i++) { if (os_argv_last == os_argv[i]) { os_argv_last = os_argv[i] + strlen(os_argv[i]) + 1; } } os_argv_last += strlen(os_argv_last);}void setproctitle(char *title){ char *p; os_argv[1] = NULL; p = strncpy((char *) os_argv[0], (char *) title, strlen(title)); p += strlen(title); if (os_argv_last - (char *) p > 0) { memset(p, ' ', os_argv_last - (char *) p); }}void start_woker_processes(){ setproctitle("mynginx:worker process"); for(;;) { sleep(1); printf("worker pid=%d\n",getpid()); }}void start_dispatcher_process(){ setproctitle("mynginx:dispatcher process"); for(;;) { sleep(1); printf("\tdispatcher pid=%d\n",getpid()); }}void save_argv(int argc, char *const *argv){ os_argv = (char **) argv;}int main(int argc, char **argv){ int fd; int i; pid_t pid; save_argv(argc, argv); init_setproctitle(); printf("father pid1=%d\n",getpid()); for (i = 0; i <3; i++) { pid = fork(); if (pid == 0) { start_woker_processes(); } } pid = fork(); if (pid == 0) { start_dispatcher_process(); } printf("father pid2=%d\n",getpid()); while(1) sleep(1); return 0;}

 

 

Make mynginx

 

./Mynginx

 

Ps-ef | grep mynginx.

 

Test 20553 20463 0 00:00:00 pts/0./mynginx
Test 20554 20553 0 00:00:00 pts/0 mynginx: worker process
Test 20555 20553 0 00:00:00 pts/0 mynginx: worker process
Test 20556 20553 0 00:00:00 pts/0 mynginx: worker process
Test 20557 20553 0 00:00:00 pts/0 mynginx: dispatcher process
Root 20574 20560 0 00:00:00 pts/2 grep mynginx

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.