libhv教程04 編寫乙個完整的命令列程式

2021-10-19 06:31:31 字數 3215 閱讀 1974

首先,乙個完整的命令列程式應該包含哪些功能?

看看libhv是如何提供這些功能的,參考示例**見examples/hmain_test.cpp

編譯執行:

$ c++ -std=c++11 examples/hmain_test.cpp -o bin/hmain_test -i/usr/local/include/hv -lhv

$ bin/hmain_test -h

usage: hmain_test [hvc:ts:dp:]

options:

-h|--help print this information

-v|--version print version

-c|--confile set configure file, default etc/.conf

-t|--test test configure file and exit

-s|--signal send to process,

=[start,stop,restart,status,reload]

-d|--daemon daemonize

-p|--port set listen port

$ bin/hmain_test -v

hmain_test version 1.21.1.31

$ bin/hmain_test -c etc/hmain_test.conf -t

test confile [etc/hmain_test.conf] ok!

$ bin/hmain_test -d

$ bin/hmain_test -s restart -d

hmain_test stop/waiting

hmain_test start/running

$ bin/hmain_test -s status

hmain_test start/running, pid=27766

$ cat logs/hmain_test.pid

27776

$ cat logs/hmain_test*.log

2021-02-06 12:18:53.509 info hmain_test version: 1.21.1.31 [hmain_test.cpp:94:parse_confile]

2021-02-06 12:18:53.509 debug worker_processes=ncpu=2 [hmain_test.cpp:103:parse_confile]

2021-02-06 12:18:53.509 info parse_confile(

'/home/hw/github/libhv/etc/hmain_test.conf'

) ok [hmain_test.cpp:129:parse_confile]

2021-02-06 12:18:53.509 info create_pidfile(

'/home/hw/github/libhv/logs/hmain_test.pid'

) pid=27766 [hmain.cpp:317:create_pidfile]

2021-02-06 12:18:53.509 info workers[0] start/running, pid=27767 [hmain.cpp:611:master_workers_run]

2021-02-06 12:18:53.509 info workers[1] start/running, pid=27768 [hmain.cpp:611:master_workers_run]

2021-02-06 12:18:53.509 info master start/running, pid=27766 [hmain.cpp:614:master_workers_run]

2021-02-06 12:18:53.509 info worker_thread pid=27767 tid=27767 [hmain.cpp:539:worker_thread]

2021-02-06 12:18:53.510 info worker_thread pid=27768 tid=27768 [hmain.cpp:539:worker_thread]

2021-02-06 12:18:53.510 info worker_thread pid=27767 tid=27769 [hmain.cpp:539:worker_thread]

2021-02-06 12:18:53.510 info worker_thread pid=27768 tid=27770 [hmain.cpp:539:worker_thread]

$ ps aux |

grep hmain_test

hw 27776 0.0 0.0 18000 2084 ? ss 12:20 0:00 hmain_test: master process

hw 27777 0.0 0.0 91732 240 ? sl 12:20 0:00 hmain_test: worker process

hw 27778 0.0 0.0 91732 240 ? sl 12:20 0:00 hmain_test: worker process

$ sudo

kill -9 27778

$ ps aux |

grep hmain_test

hw 27776 0.0 0.0 18000 2084 ? ss 12:20 0:00 hmain_test: master process

hw 27777 0.0 0.0 91732 240 ? sl 12:20 0:00 hmain_test: worker process

hw 27796 0.0 0.0 91732 244 ? sl 12:27 0:00 hmain_test: worker process

可以看到,hmain_test提供了列印幫助資訊、列印版本資訊、測試配置檔案、後台執行、建立pid檔案、檢視程序狀態、開始|停止|重啟程序、master-workers多程序模式、崩潰自動重啟等功能。

流程圖:

libhv教程10 建立乙個簡單的HTTP服務端

http協議作為本世紀最通用的應用層協議,本文就不加以介紹了,不熟悉的自行閱讀awesome http 示例 參考examples http server test.cpp 編譯執行 bin http server test測試使用curl或瀏覽器輸入以下url curl v curl v curl...

libhv教程06 建立乙個簡單的TCP服務端

下文以tcp echo server為例,使用libhv建立tcp服務端。include hv hloop.h void on close hio t io void on recv hio t io,void buf,int readbytes void on accept hio t io in...

物件導向之編寫乙個完整的類

package com.work.cast public class people class people1else public string getname public void setage int a else public int getage int a public void se...