1
0

hmain.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. #include "hmain.h"
  2. #include "hbase.h"
  3. #include "hlog.h"
  4. #include "herr.h"
  5. #include "htime.h"
  6. #include "hthread.h"
  7. #ifdef OS_DARWIN
  8. #include <crt_externs.h>
  9. #define environ (*_NSGetEnviron())
  10. #endif
  11. main_ctx_t g_main_ctx;
  12. int g_worker_processes_num = 0;
  13. int g_worker_threads_num = 0;
  14. proc_ctx_t* g_worker_processes = NULL;
  15. procedure_t g_worker_fn = NULL;
  16. void* g_worker_userdata = NULL;
  17. int main_ctx_init(int argc, char** argv) {
  18. if (argc == 0 || argv == NULL) {
  19. argc = 1;
  20. argv = (char**)malloc(2*sizeof(char*));
  21. argv[0] = (char*)malloc(MAX_PATH);
  22. argv[1] = NULL;
  23. get_executable_path(argv[0], MAX_PATH);
  24. }
  25. get_run_dir(g_main_ctx.run_dir, sizeof(g_main_ctx.run_dir));
  26. //printf("run_dir=%s\n", g_main_ctx.run_dir);
  27. strncpy(g_main_ctx.program_name, hv_basename(argv[0]), sizeof(g_main_ctx.program_name));
  28. #ifdef OS_WIN
  29. if (strcmp(g_main_ctx.program_name+strlen(g_main_ctx.program_name)-4, ".exe") == 0) {
  30. *(g_main_ctx.program_name+strlen(g_main_ctx.program_name)-4) = '\0';
  31. }
  32. #endif
  33. //printf("program_name=%s\n", g_main_ctx.program_name);
  34. char logpath[MAX_PATH] = {0};
  35. snprintf(logpath, sizeof(logpath), "%s/logs", g_main_ctx.run_dir);
  36. hv_mkdir(logpath);;
  37. snprintf(g_main_ctx.confile, sizeof(g_main_ctx.confile), "%s/etc/%s.conf", g_main_ctx.run_dir, g_main_ctx.program_name);
  38. snprintf(g_main_ctx.pidfile, sizeof(g_main_ctx.pidfile), "%s/logs/%s.pid", g_main_ctx.run_dir, g_main_ctx.program_name);
  39. snprintf(g_main_ctx.logfile, sizeof(g_main_ctx.confile), "%s/logs/%s.log", g_main_ctx.run_dir, g_main_ctx.program_name);
  40. hlog_set_file(g_main_ctx.logfile);
  41. g_main_ctx.pid = getpid();
  42. g_main_ctx.oldpid = getpid_from_pidfile();
  43. #ifdef OS_UNIX
  44. if (kill(g_main_ctx.oldpid, 0) == -1 && errno == ESRCH) {
  45. g_main_ctx.oldpid = -1;
  46. }
  47. #else
  48. HANDLE hproc = OpenProcess(PROCESS_TERMINATE, FALSE, g_main_ctx.oldpid);
  49. if (hproc == NULL) {
  50. g_main_ctx.oldpid = -1;
  51. }
  52. else {
  53. CloseHandle(hproc);
  54. }
  55. #endif
  56. // save arg
  57. int i = 0;
  58. g_main_ctx.os_argv = argv;
  59. g_main_ctx.argc = 0;
  60. g_main_ctx.arg_len = 0;
  61. for (i = 0; argv[i]; ++i) {
  62. g_main_ctx.arg_len += strlen(argv[i]) + 1;
  63. }
  64. g_main_ctx.argc = i;
  65. char* argp = (char*)malloc(g_main_ctx.arg_len);
  66. memset(argp, 0, g_main_ctx.arg_len);
  67. g_main_ctx.save_argv = (char**)malloc((g_main_ctx.argc+1) * sizeof(char*));
  68. char* cmdline = (char*)malloc(g_main_ctx.arg_len);
  69. g_main_ctx.cmdline = cmdline;
  70. for (i = 0; argv[i]; ++i) {
  71. g_main_ctx.save_argv[i] = argp;
  72. strcpy(g_main_ctx.save_argv[i], argv[i]);
  73. argp += strlen(argv[i]) + 1;
  74. strcpy(cmdline, argv[i]);
  75. cmdline += strlen(argv[i]);
  76. *cmdline = ' ';
  77. ++cmdline;
  78. }
  79. g_main_ctx.save_argv[g_main_ctx.argc] = NULL;
  80. g_main_ctx.cmdline[g_main_ctx.arg_len-1] = '\0';
  81. #if defined(OS_WIN) || defined(OS_LINUX) || defined(OS_DARWIN)
  82. // save env
  83. g_main_ctx.os_envp = environ;
  84. g_main_ctx.envc = 0;
  85. g_main_ctx.env_len = 0;
  86. for (i = 0; environ[i]; ++i) {
  87. g_main_ctx.env_len += strlen(environ[i]) + 1;
  88. }
  89. g_main_ctx.envc = i;
  90. char* envp = (char*)malloc(g_main_ctx.env_len);
  91. memset(envp, 0, g_main_ctx.env_len);
  92. g_main_ctx.save_envp = (char**)malloc((g_main_ctx.envc+1) * sizeof(char*));
  93. for (i = 0; environ[i]; ++i) {
  94. g_main_ctx.save_envp[i] = envp;
  95. strcpy(g_main_ctx.save_envp[i], environ[i]);
  96. envp += strlen(environ[i]) + 1;
  97. }
  98. g_main_ctx.save_envp[g_main_ctx.envc] = NULL;
  99. // parse env
  100. for (i = 0; environ[i]; ++i) {
  101. char* b = environ[i];
  102. char* delim = strchr(b, '=');
  103. if (delim == NULL) {
  104. continue;
  105. }
  106. g_main_ctx.env_kv[std::string(b, delim-b)] = std::string(delim+1);
  107. }
  108. #endif
  109. return 0;
  110. }
  111. #define UNDEFINED_OPTION -1
  112. static int get_arg_type(int short_opt, const char* options) {
  113. if (options == NULL) return UNDEFINED_OPTION;
  114. const char* p = options;
  115. while (*p && *p != short_opt) ++p;
  116. if (*p == '\0') return UNDEFINED_OPTION;
  117. if (*(p+1) == ':') return REQUIRED_ARGUMENT;
  118. return NO_ARGUMENT;
  119. }
  120. int parse_opt(int argc, char** argv, const char* options) {
  121. for (int i = 1; argv[i]; ++i) {
  122. char* p = argv[i];
  123. if (*p != '-') {
  124. g_main_ctx.arg_list.push_back(argv[i]);
  125. continue;
  126. }
  127. while (*++p) {
  128. int arg_type = get_arg_type(*p, options);
  129. if (arg_type == UNDEFINED_OPTION) {
  130. printf("Invalid option '%c'\n", *p);
  131. return -20;
  132. } else if (arg_type == NO_ARGUMENT) {
  133. g_main_ctx.arg_kv[std::string(p, 1)] = OPTION_ENABLE;
  134. continue;
  135. } else if (arg_type == REQUIRED_ARGUMENT) {
  136. if (*(p+1) != '\0') {
  137. g_main_ctx.arg_kv[std::string(p, 1)] = p+1;
  138. break;
  139. } else if (argv[i+1] != NULL) {
  140. g_main_ctx.arg_kv[std::string(p, 1)] = argv[++i];
  141. break;
  142. } else {
  143. printf("Option '%c' requires param\n", *p);
  144. return -30;
  145. }
  146. }
  147. }
  148. }
  149. return 0;
  150. }
  151. static const option_t* get_option(const char* opt, const option_t* long_options, int size) {
  152. if (opt == NULL || long_options == NULL) return NULL;
  153. int len = strlen(opt);
  154. if (len == 0) return NULL;
  155. if (len == 1) {
  156. for (int i = 0; i < size; ++i) {
  157. if (long_options[i].short_opt == *opt) {
  158. return &long_options[i];
  159. }
  160. }
  161. } else {
  162. for (int i = 0; i < size; ++i) {
  163. if (strcmp(long_options[i].long_opt, opt) == 0) {
  164. return &long_options[i];
  165. }
  166. }
  167. }
  168. return NULL;
  169. }
  170. #define MAX_OPTION 32
  171. // opt type
  172. #define NOPREFIX_OPTION 0
  173. #define SHORT_OPTION -1
  174. #define LONG_OPTION -2
  175. int parse_opt_long(int argc, char** argv, const option_t* long_options, int size) {
  176. char opt[MAX_OPTION+1] = {0};
  177. for (int i = 1; argv[i]; ++i) {
  178. char* arg = argv[i];
  179. int opt_type = NOPREFIX_OPTION;
  180. // prefix
  181. if (*arg == OPTION_PREFIX) {
  182. ++arg;
  183. opt_type = SHORT_OPTION;
  184. if (*arg == OPTION_PREFIX) {
  185. ++arg;
  186. opt_type = LONG_OPTION;
  187. }
  188. }
  189. int arg_len = strlen(arg);
  190. // delim
  191. char* delim = strchr(arg, OPTION_DELIM);
  192. if (delim == arg || delim == arg+arg_len-1 || delim-arg > MAX_OPTION) {
  193. printf("Invalid option '%s'\n", argv[i]);
  194. return -10;
  195. }
  196. if (delim) {
  197. memcpy(opt, arg, delim-arg);
  198. opt[delim-arg] = '\0';
  199. } else {
  200. if (opt_type == SHORT_OPTION) {
  201. *opt = *arg;
  202. opt[1] = '\0';
  203. } else {
  204. strncpy(opt, arg, MAX_OPTION);
  205. }
  206. }
  207. // get_option
  208. const option_t* pOption = get_option(opt, long_options, size);
  209. if (pOption == NULL) {
  210. if (delim == NULL && opt_type == NOPREFIX_OPTION) {
  211. g_main_ctx.arg_list.push_back(arg);
  212. continue;
  213. } else {
  214. printf("Invalid option: '%s'\n", argv[i]);
  215. return -10;
  216. }
  217. }
  218. const char* value = NULL;
  219. if (pOption->arg_type == NO_ARGUMENT) {
  220. // -h
  221. value = OPTION_ENABLE;
  222. } else if (pOption->arg_type == REQUIRED_ARGUMENT) {
  223. if (delim) {
  224. // --port=80
  225. value = delim+1;
  226. } else {
  227. if (opt_type == SHORT_OPTION && *(arg+1) != '\0') {
  228. // p80
  229. value = arg+1;
  230. } else if (argv[i+1] != NULL) {
  231. // --port 80
  232. value = argv[++i];
  233. } else {
  234. printf("Option '%s' requires parament\n", opt);
  235. return -20;
  236. }
  237. }
  238. }
  239. // preferred to use short_opt as key
  240. if (pOption->short_opt > 0) {
  241. g_main_ctx.arg_kv[std::string(1, pOption->short_opt)] = value;
  242. } else if (pOption->long_opt) {
  243. g_main_ctx.arg_kv[pOption->long_opt] = value;
  244. }
  245. }
  246. return 0;
  247. }
  248. const char* get_arg(const char* key) {
  249. auto iter = g_main_ctx.arg_kv.find(key);
  250. if (iter == g_main_ctx.arg_kv.end()) {
  251. return NULL;
  252. }
  253. return iter->second.c_str();
  254. }
  255. const char* get_env(const char* key) {
  256. auto iter = g_main_ctx.env_kv.find(key);
  257. if (iter == g_main_ctx.env_kv.end()) {
  258. return NULL;
  259. }
  260. return iter->second.c_str();
  261. }
  262. #ifdef OS_UNIX
  263. /*
  264. * memory layout
  265. * argv[0]\0argv[1]\0argv[n]\0env[0]\0env[1]\0env[n]\0
  266. */
  267. void setproctitle(const char* title) {
  268. //printf("proctitle=%s\n", title);
  269. int len = g_main_ctx.arg_len + g_main_ctx.env_len;
  270. if (g_main_ctx.os_argv && len) {
  271. strncpy(g_main_ctx.os_argv[0], title, len-1);
  272. }
  273. }
  274. #endif
  275. int create_pidfile() {
  276. FILE* fp = fopen(g_main_ctx.pidfile, "w");
  277. if (fp == NULL) {
  278. hloge("fopen('%s') error: %d", g_main_ctx.pidfile, errno);
  279. return -1;
  280. }
  281. char pid[16] = {0};
  282. snprintf(pid, sizeof(pid), "%d\n", g_main_ctx.pid);
  283. fwrite(pid, 1, strlen(pid), fp);
  284. fclose(fp);
  285. hlogi("create_pidfile('%s') pid=%d", g_main_ctx.pidfile, g_main_ctx.pid);
  286. atexit(delete_pidfile);
  287. return 0;
  288. }
  289. void delete_pidfile() {
  290. hlogi("delete_pidfile('%s') pid=%d", g_main_ctx.pidfile, g_main_ctx.pid);
  291. remove(g_main_ctx.pidfile);
  292. }
  293. pid_t getpid_from_pidfile() {
  294. FILE* fp = fopen(g_main_ctx.pidfile, "r");
  295. if (fp == NULL) {
  296. // hloge("fopen('%s') error: %d", g_main_ctx.pidfile, errno);
  297. return -1;
  298. }
  299. char pid[64];
  300. int readbytes = fread(pid, 1, sizeof(pid), fp);
  301. fclose(fp);
  302. return readbytes <= 0 ? -1 : atoi(pid);
  303. }
  304. static procedure_t s_reload_fn = NULL;
  305. static void* s_reload_userdata = NULL;
  306. #ifdef OS_UNIX
  307. // unix use signal
  308. #include <sys/wait.h>
  309. void signal_handler(int signo) {
  310. hlogi("pid=%d recv signo=%d", getpid(), signo);
  311. switch (signo) {
  312. case SIGINT:
  313. case SIGNAL_TERMINATE:
  314. hlogi("killall processes");
  315. signal(SIGCHLD, SIG_IGN);
  316. // master send SIGKILL => workers
  317. for (int i = 0; i < g_worker_processes_num; ++i) {
  318. if (g_worker_processes[i].pid <= 0) break;
  319. kill(g_worker_processes[i].pid, SIGKILL);
  320. g_worker_processes[i].pid = -1;
  321. }
  322. exit(0);
  323. break;
  324. case SIGNAL_RELOAD:
  325. if (s_reload_fn) {
  326. s_reload_fn(s_reload_userdata);
  327. if (getpid_from_pidfile() == getpid()) {
  328. // master send SIGNAL_RELOAD => workers
  329. for (int i = 0; i < g_worker_processes_num; ++i) {
  330. if (g_worker_processes[i].pid <= 0) break;
  331. kill(g_worker_processes[i].pid, SIGNAL_RELOAD);
  332. }
  333. }
  334. }
  335. break;
  336. case SIGCHLD:
  337. {
  338. pid_t pid = 0;
  339. int status = 0;
  340. while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
  341. hlogw("proc stop/waiting, pid=%d status=%d", pid, status);
  342. for (int i = 0; i < g_worker_processes_num; ++i) {
  343. if (g_worker_processes[i].pid == pid) {
  344. g_worker_processes[i].pid = -1;
  345. hproc_spawn(&g_worker_processes[i]);
  346. break;
  347. }
  348. }
  349. }
  350. }
  351. break;
  352. default:
  353. break;
  354. }
  355. }
  356. int signal_init(procedure_t reload_fn, void* reload_userdata) {
  357. s_reload_fn = reload_fn;
  358. s_reload_userdata = reload_userdata;
  359. signal(SIGINT, signal_handler);
  360. signal(SIGCHLD, signal_handler);
  361. signal(SIGNAL_TERMINATE, signal_handler);
  362. signal(SIGNAL_RELOAD, signal_handler);
  363. return 0;
  364. }
  365. #elif defined(OS_WIN)
  366. #include <mmsystem.h> // for timeSetEvent
  367. // win32 use Event
  368. //static HANDLE s_hEventTerm = NULL;
  369. static HANDLE s_hEventReload = NULL;
  370. void WINAPI on_timer(UINT uTimerID, UINT uMsg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2) {
  371. DWORD ret;
  372. /*
  373. ret = WaitForSingleObject(s_hEventTerm, 0);
  374. if (ret == WAIT_OBJECT_0) {
  375. hlogi("pid=%d recv event [TERM]", getpid());
  376. if (getpid_from_pidfile() == getpid()) {
  377. timeKillEvent(uTimerID);
  378. exit(0);
  379. }
  380. }
  381. */
  382. ret = WaitForSingleObject(s_hEventReload, 0);
  383. if (ret == WAIT_OBJECT_0) {
  384. hlogi("pid=%d recv event [RELOAD]", getpid());
  385. if (s_reload_fn) {
  386. s_reload_fn(s_reload_userdata);
  387. }
  388. }
  389. }
  390. void signal_cleanup() {
  391. //CloseHandle(s_hEventTerm);
  392. //s_hEventTerm = NULL;
  393. CloseHandle(s_hEventReload);
  394. s_hEventReload = NULL;
  395. }
  396. int signal_init(procedure_t reload_fn, void* reload_userdata) {
  397. s_reload_fn = reload_fn;
  398. s_reload_userdata = reload_userdata;
  399. char eventname[MAX_PATH] = {0};
  400. //snprintf(eventname, sizeof(eventname), "%s_term_event", g_main_ctx.program_name);
  401. //s_hEventTerm = CreateEvent(NULL, FALSE, FALSE, eventname);
  402. //s_hEventTerm = OpenEvent(EVENT_ALL_ACCESS, FALSE, eventname);
  403. snprintf(eventname, sizeof(eventname), "%s_reload_event", g_main_ctx.program_name);
  404. s_hEventReload = CreateEvent(NULL, FALSE, FALSE, eventname);
  405. timeSetEvent(1000, 1000, on_timer, 0, TIME_PERIODIC);
  406. atexit(signal_cleanup);
  407. return 0;
  408. }
  409. #endif
  410. static void kill_proc(int pid) {
  411. #ifdef OS_UNIX
  412. kill(pid, SIGNAL_TERMINATE);
  413. #else
  414. //SetEvent(s_hEventTerm);
  415. //sleep(1);
  416. HANDLE hproc = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
  417. if (hproc) {
  418. TerminateProcess(hproc, 0);
  419. CloseHandle(hproc);
  420. }
  421. #endif
  422. }
  423. void signal_handle(const char* signal) {
  424. if (strcmp(signal, "start") == 0) {
  425. if (g_main_ctx.oldpid > 0) {
  426. printf("%s is already running, pid=%d\n", g_main_ctx.program_name, g_main_ctx.oldpid);
  427. exit(0);
  428. }
  429. } else if (strcmp(signal, "stop") == 0) {
  430. if (g_main_ctx.oldpid > 0) {
  431. kill_proc(g_main_ctx.oldpid);
  432. printf("%s stop/waiting\n", g_main_ctx.program_name);
  433. } else {
  434. printf("%s is already stopped\n", g_main_ctx.program_name);
  435. }
  436. exit(0);
  437. } else if (strcmp(signal, "restart") == 0) {
  438. if (g_main_ctx.oldpid > 0) {
  439. kill_proc(g_main_ctx.oldpid);
  440. printf("%s stop/waiting\n", g_main_ctx.program_name);
  441. msleep(1000);
  442. }
  443. } else if (strcmp(signal, "status") == 0) {
  444. if (g_main_ctx.oldpid > 0) {
  445. printf("%s start/running, pid=%d\n", g_main_ctx.program_name, g_main_ctx.oldpid);
  446. } else {
  447. printf("%s stop/waiting\n", g_main_ctx.program_name);
  448. }
  449. exit(0);
  450. } else if (strcmp(signal, "reload") == 0) {
  451. if (g_main_ctx.oldpid > 0) {
  452. printf("reload confile [%s]\n", g_main_ctx.confile);
  453. #ifdef OS_UNIX
  454. kill(g_main_ctx.oldpid, SIGNAL_RELOAD);
  455. #else
  456. SetEvent(s_hEventReload);
  457. #endif
  458. }
  459. sleep(1);
  460. exit(0);
  461. } else {
  462. printf("Invalid signal: '%s'\n", signal);
  463. exit(0);
  464. }
  465. printf("%s start/running\n", g_main_ctx.program_name);
  466. }
  467. // master-workers processes
  468. static HTHREAD_ROUTINE(worker_thread) {
  469. hlogi("worker_thread pid=%ld tid=%ld", hv_getpid(), hv_gettid());
  470. if (g_worker_fn) {
  471. g_worker_fn(g_worker_userdata);
  472. }
  473. return 0;
  474. }
  475. static void worker_init(void* userdata) {
  476. #ifdef OS_UNIX
  477. char proctitle[256] = {0};
  478. snprintf(proctitle, sizeof(proctitle), "%s: worker process", g_main_ctx.program_name);
  479. setproctitle(proctitle);
  480. signal(SIGNAL_RELOAD, signal_handler);
  481. #endif
  482. }
  483. static void worker_proc(void* userdata) {
  484. for (int i = 1; i < g_worker_threads_num; ++i) {
  485. hthread_create(worker_thread, NULL);
  486. }
  487. worker_thread(NULL);
  488. }
  489. int master_workers_run(procedure_t worker_fn, void* worker_userdata,
  490. int worker_processes, int worker_threads, bool wait) {
  491. #ifdef OS_WIN
  492. // NOTE: Windows not provide MultiProcesses
  493. if (worker_threads == 0) {
  494. // MultiProcesses => MultiThreads
  495. worker_threads = worker_processes;
  496. }
  497. worker_processes = 0;
  498. #endif
  499. if (worker_threads == 0) worker_threads = 1;
  500. g_worker_threads_num = worker_threads;
  501. g_worker_fn = worker_fn;
  502. g_worker_userdata = worker_userdata;
  503. if (worker_processes == 0) {
  504. // single process
  505. if (wait) {
  506. for (int i = 1; i < worker_threads; ++i) {
  507. hthread_create(worker_thread, NULL);
  508. }
  509. worker_thread(NULL);
  510. }
  511. else {
  512. for (int i = 0; i < worker_threads; ++i) {
  513. hthread_create(worker_thread, NULL);
  514. }
  515. }
  516. }
  517. else {
  518. if (g_worker_processes_num != 0) {
  519. return ERR_OVER_LIMIT;
  520. }
  521. // master-workers processes
  522. #ifdef OS_UNIX
  523. char proctitle[256] = {0};
  524. snprintf(proctitle, sizeof(proctitle), "%s: master process", g_main_ctx.program_name);
  525. setproctitle(proctitle);
  526. signal(SIGNAL_RELOAD, signal_handler);
  527. #endif
  528. g_worker_processes_num = worker_processes;
  529. int bytes = g_worker_processes_num * sizeof(proc_ctx_t);
  530. g_worker_processes = (proc_ctx_t*)malloc(bytes);
  531. memset(g_worker_processes, 0, bytes);
  532. proc_ctx_t* ctx = g_worker_processes;
  533. for (int i = 0; i < g_worker_processes_num; ++i, ++ctx) {
  534. ctx->init = worker_init;
  535. ctx->proc = worker_proc;
  536. hproc_spawn(ctx);
  537. hlogi("workers[%d] start/running, pid=%d", i, ctx->pid);
  538. }
  539. g_main_ctx.pid = getpid();
  540. hlogi("master start/running, pid=%d", g_main_ctx.pid);
  541. if (wait) {
  542. while (1) sleep (1);
  543. }
  544. }
  545. return 0;;
  546. }