搭建与电脑将棋软件对局的服务器 <第六部分>

樱花VPS的试用期为两周,但我还无法将程序常驻。

で、さっさと最優先で プログラムの常駐 が可能かどうか 調べた方がいいと思うんだが、
その他の可能性を潰していくのに 記事<その1>~<その5> までかかった。

上一篇文章:http://qiita.com/muzudho1/items/8f940f8b4c679c23a174

我想做的事情 (wǒ zuò de

第二计划

    • コンピュータ将棋プログラム を常駐させる

 

    • ブラウザでのページ・アクセスの度にPHPは 指し手ブローカー三河屋 を起動して、キュー待ち させ、コンピューター将棋プログラムに 指し手のお伺いを立てる

 

    PHPは、指し手ブローカー三河屋 が持ってきた指し手を PHPで出力する

计划一

    • コンピュータ将棋プログラム を常駐させる

 

    • ゲームサーバー を常駐させる

 

    • ゲームサーバーは コンピューター将棋プログラム と局面検討ができるものとする

 

    ブラウザでのページ・アクセスの度にPHPは 無限ループに入り ゲームサーバーにプロセス間通信、メッセージキューに指し手が入っていれば 無限ループを出て 指し手を PHPで出力する

で、プラン1は それができないという話しが えんえんと 記事<その1>~<その3> ぐらいに書いてあったのだった。

残ってるのは プラン2。

調べてみる。

计算机将无法常驻将棋程序。

PuTTYで遠隔ログインし、PHPで無限ループを書いて バックグランド実行しても ログアウトすると ジョブは消えてしまう。

そこで Cron を使って 無限ループPHP を実行するんだが、すぐ S (停止)している。

Apache は常駐している。
PuTTY でセッション・ログアウトしても ブラウザで http://★.★.★.★/index.php とでも打てば画面は出てくる。

常駐はできるようだ。
どうやって常駐するのか。

Windows 10 だと タスクバーの「何でも聞いてください」と書いてあるテキストボックスに 「コンピューターの管理」と打ち込んで [サービスとアプリケーション] – [サービス] と進んでいけば、常駐しているプログラムの一覧が出てくる。

Ubuntu16.04 だと

ps aux

と打ち込めば プロセスの一覧が見れるんだが、これはアプリケーションの一覧ではない。

# ps aux | grep apache

输入”と”的话,例如

www-data  2776  0.0  0.2 352548  2496 ?        S    Mar09   0:00 /usr/sbin/apache2 -k start
root     11959  0.0  0.0 351884    76 ?        Ss   Mar07   0:34 /usr/sbin/apache2 -k start
www-data 11965  0.0  0.0 352296   148 ?        S    Mar07   0:14 /usr/sbin/apache2 -k start
root     19418  0.0  0.0  12936   980 pts/2    S+   03:16   0:00 grep --color=auto apache
www-data 29283  0.0  0.1 352164  2004 ?        S    Mar09   0:00 /usr/sbin/apache2 -k start
www-data 29284  0.0  0.0 352164   556 ?        S    Mar09   0:00 /usr/sbin/apache2 -k start
www-data 29285  0.0  0.1 352164  1944 ?        S    Mar09   0:00 /usr/sbin/apache2 -k start
www-data 29286  0.0  0.0 352164     0 ?        S    Mar09   0:00 /usr/sbin/apache2 -k start
www-data 29287  0.0  0.1 352164  1776 ?        S    Mar09   0:00 /usr/sbin/apache2 -k start
www-data 29318  0.0  0.2 352164  2508 ?        S    Mar09   0:00 /usr/sbin/apache2 -k start
www-data 31812  0.0  0.1 352164  1116 ?        S    Mar09   0:00 /usr/sbin/apache2 -k start

と出てきて、このうち「color=auto apache」というのは apacheの文字色を赤色にしましょう、という今叩いたばかりのコマンドだから無視するとして、

启动Apache2的命令是/usr/sbin/apache2 -k start。

通过此命令可以确定正在运行的进程。

然而仔细看,大家都是S型的呢。

www-data 15221  197 27.6 712928 281056 pts/3   Rsl+ Mar09 458:47 ./release

これなんか、ブラウザでアクセスして 叩き起こした コンピュータ将棋ソフトの 技巧 のプログラムなんじゃないか?

Rsl+ 是什么情况?

「ps命令摘要」(Qiita)
http://qiita.com/shell/items/68ed71a7f018e5688f73

「S」不是停止而是睡眠嗎……。

# kill 15221

那就是说。

# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.1  37448  1872 ?        Ss   Mar04   0:56 /lib/systemd/systemd --system --deserialize 19
root         2  0.0  0.0      0     0 ?        S    Mar04   0:00 [kthreadd]
root         3  0.0  0.0      0     0 ?        S    Mar04   0:03 [ksoftirqd/0]
root         5  0.0  0.0      0     0 ?        S<   Mar04   0:00 [kworker/0:0H]
root         7  0.0  0.0      0     0 ?        S    Mar04   1:52 [rcu_sched]
root         8  0.0  0.0      0     0 ?        S    Mar04   2:16 [rcuos/0]
root         9  0.0  0.0      0     0 ?        S    Mar04   0:18 [rcuos/1]
root        10  0.0  0.0      0     0 ?        S    Mar04   0:00 [rcu_bh]
root        11  0.0  0.0      0     0 ?        S    Mar04   0:00 [rcuob/0]
root        12  0.0  0.0      0     0 ?        S    Mar04   0:00 [rcuob/1]
root        13  0.0  0.0      0     0 ?        S    Mar04   0:05 [migration/0]
root        14  0.0  0.0      0     0 ?        S    Mar04   0:10 [watchdog/0]
root        15  0.0  0.0      0     0 ?        S    Mar04   0:07 [watchdog/1]
root        16  0.0  0.0      0     0 ?        S    Mar04   0:01 [migration/1]
root        17  0.0  0.0      0     0 ?        S    Mar04   0:01 [ksoftirqd/1]
root        19  0.0  0.0      0     0 ?        S<   Mar04   0:00 [kworker/1:0H]
root        20  0.0  0.0      0     0 ?        S<   Mar04   0:00 [khelper]
root        21  0.0  0.0      0     0 ?        S    Mar04   0:00 [kdevtmpfs]
root        22  0.0  0.0      0     0 ?        S<   Mar04   0:00 [netns]
root        23  0.0  0.0      0     0 ?        S<   Mar04   0:00 [writeback]
root        24  0.0  0.0      0     0 ?        S<   Mar04   0:00 [kintegrityd]
root        25  0.0  0.0      0     0 ?        S<   Mar04   0:00 [bioset]
root        27  0.0  0.0      0     0 ?        S<   Mar04   0:00 [kblockd]
root        28  0.0  0.0      0     0 ?        S<   Mar04   0:00 [ata_sff]
root        29  0.0  0.0      0     0 ?        S    Mar04   0:00 [khubd]
root        30  0.0  0.0      0     0 ?        S<   Mar04   0:00 [md]
root        31  0.0  0.0      0     0 ?        S<   Mar04   0:00 [devfreq_wq]
root        35  0.0  0.0      0     0 ?        S    Mar04   0:02 [khungtaskd]
root        36  0.0  0.0      0     0 ?        S    Mar04   3:27 [kswapd0]
root        37  0.0  0.0      0     0 ?        SN   Mar04   0:00 [ksmd]
root        38  0.0  0.0      0     0 ?        SN   Mar04   0:11 [khugepaged]
root        39  0.0  0.0      0     0 ?        S    Mar04   0:00 [fsnotify_mark]
root        40  0.0  0.0      0     0 ?        S    Mar04   0:00 [ecryptfs-kthrea]
root        41  0.0  0.0      0     0 ?        S<   Mar04   0:00 [crypto]
root        53  0.0  0.0      0     0 ?        S<   Mar04   0:00 [kthrotld]
root        55  0.0  0.0      0     0 ?        S    Mar04   0:00 [vballoon]
root        56  0.0  0.0      0     0 ?        S    Mar04   0:00 [scsi_eh_0]
root        57  0.0  0.0      0     0 ?        S    Mar04   0:00 [scsi_eh_1]
root        77  0.0  0.0      0     0 ?        S<   Mar04   0:00 [deferwq]
root        78  0.0  0.0      0     0 ?        S<   Mar04   0:00 [charger_manager]
root       115  0.0  0.0      0     0 ?        S<   Mar04   0:00 [kpsmoused]
root       141  0.0  0.0      0     0 ?        S    Mar04   0:36 [jbd2/vda1-8]
root       142  0.0  0.0      0     0 ?        S<   Mar04   0:00 [ext4-rsv-conver]
root       185  0.0  0.0  29480  1012 ?        Ss   Mar04   0:47 /lib/systemd/systemd-journald
root       196  0.0  0.0      0     0 ?        S    Mar04   0:00 [kauditd]
root       234  0.0  0.0  43808     8 ?        Ss   Mar04   0:03 /lib/systemd/systemd-udevd
systemd+   417  0.0  0.0 100316    60 ?        Ssl  Mar04   0:03 /lib/systemd/systemd-timesyncd
syslog     443  0.0  0.0 256388   376 ?        Ssl  Mar04   0:39 /usr/sbin/rsyslogd -n
root       453  0.0  0.0  29872     4 ?        Ss   Mar04   0:00 /sbin/cgmanager -m name=systemd
root       469  0.0  0.0 232308   236 ?        S    Mar09   0:02 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root       488  0.0  0.0      0     0 ?        S<   Mar04   0:00 [kvm-irqfd-clean]
root       490  0.0  0.1 274560  1356 ?        Ssl  Mar04   1:03 /usr/lib/accountsservice/accounts-daemon
root       494  0.0  0.0  28540   880 ?        Ss   Mar04   0:13 /lib/systemd/systemd-logind
message+   515  0.0  0.0  43020   636 ?        Ss   Mar04   0:22 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-a
root       551  0.0  0.0  19592   304 ?        Ss   Mar04   0:56 /usr/sbin/irqbalance --pid=/var/run/irqbalance.pid
root       709  0.0  0.0  65512   332 ?        Ss   Mar04   0:04 /usr/sbin/sshd -D
memcache   714  0.0  0.0 340888   540 ?        Ssl  Mar04   1:32 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 160.16.83.155
mysql      715  0.1  0.0 1302112    0 ?        Ssl  Mar04  11:35 /usr/sbin/mysqld
root       757  0.0  0.0  14468     0 ttyS0    Ss+  Mar04   0:00 /sbin/agetty --keep-baud 115200 38400 9600 ttyS0 vt220
root       758  0.0  0.0  14652     0 tty1     Ss+  Mar04   0:00 /sbin/agetty --noclear tty1 linux
root      1878  0.0  0.0 232308   108 ?        S    Mar09   0:03 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
www-data  2776  0.0  0.2 352548  2496 ?        S    Mar09   0:00 /usr/sbin/apache2 -k start
root      2866  0.0  0.0      0     0 ?        S    Mar07   0:00 [kworker/0:2]
root      2957  0.0  0.0 232308    40 ?        S    Mar09   0:02 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root      4784  0.0  0.0 232308    92 ?        S    Mar09   0:03 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root      4945  0.0  0.0 232308   104 ?        S    Mar08   0:09 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root      5693  0.0  0.0      0     0 ?        S    Mar07   0:49 [kworker/0:0]
root      5916  0.0  0.0 232308   172 ?        S    Mar09   0:02 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root      6091  0.0  0.0 311040   312 ?        Ss   Mar07   0:49 php-fpm: master process (/etc/php/7.0/fpm/php-fpm.conf)
www-data  6094  0.0  0.0 311040     0 ?        S    Mar07   0:00 php-fpm: pool www
www-data  6095  0.0  0.0 311040     0 ?        S    Mar07   0:00 php-fpm: pool www
root      6301  0.0  0.0 232308   260 ?        S    Mar08   0:10 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root      6856  0.0  0.0 232308   112 ?        S    Mar09   0:02 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root      7280  0.0  0.0      0     0 ?        S    Mar07   0:18 [kworker/1:1]
root      7795  0.0  0.0 232308   112 ?        S    Mar09   0:01 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root      8303  0.0  0.0 232308   132 ?        S    Mar08   0:09 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root      8732  0.0  0.0 232308   132 ?        S    Mar09   0:01 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root      9246  0.0  0.0 232308    80 ?        S    Mar08   0:09 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root      9664  0.0  0.0 232308   272 ?        S    Mar09   0:01 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     10278  0.0  0.0 232308    52 ?        S    Mar08   0:08 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     10632  0.0  0.0 232308   124 ?        S    Mar09   0:01 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     11364  0.0  0.0 232308   276 ?        S    Mar08   0:08 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     11567  0.0  0.0 232308    72 ?        S    Mar09   0:01 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     11959  0.0  0.0 351884    76 ?        Ss   Mar07   0:34 /usr/sbin/apache2 -k start
www-data 11965  0.0  0.0 352296   148 ?        S    Mar07   0:14 /usr/sbin/apache2 -k start
root     12122  0.0  0.0   4500     0 ?        S    Mar07   0:00 /bin/sh /usr/sbin/rabbitmq-server
root     12129  0.0  0.0  55128     0 ?        S    Mar07   0:00 su rabbitmq -s /bin/sh -c /usr/lib/rabbitmq/bin/rabbitmq-server
rabbitmq 12130  0.0  0.0   4500     8 ?        Ss   Mar07   0:00 sh -c /usr/lib/rabbitmq/bin/rabbitmq-server
rabbitmq 12131  0.0  0.0   4500     0 ?        S    Mar07   0:00 /bin/sh -e /usr/lib/rabbitmq/bin/rabbitmq-server
rabbitmq 12237  0.6  1.8 2812192 19272 ?       Sl   Mar07  25:13 /usr/lib/erlang/erts-8.1/bin/beam.smp -W w -A 64 -P 1048576 -t 5000000 -stbt db -
rabbitmq 12333  0.0  0.0   4356    72 ?        Ss   Mar07   0:16 erl_child_setup 1024
rabbitmq 12343  0.0  0.0  13676    36 ?        Ss   Mar07   0:00 inet_gethost 4
rabbitmq 12344  0.0  0.0  15796   228 ?        S    Mar07   0:01 inet_gethost 4
root     12376  0.0  0.0 232308    76 ?        S    Mar08   0:08 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     12582  0.0  0.0 232308   164 ?        S    Mar09   0:01 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     12714  0.0  0.0  99616   332 ?        Ss   Mar09   0:00 sshd: ★user [priv]
★user    12738  0.0  0.0  99616   524 ?        S    Mar09   0:01 sshd: ★user@pts/2
★user    12742  0.0  0.0  21484     0 pts/2    Ss   Mar09   0:00 -bash
root     12760  0.0  0.0  55780     4 pts/2    S    Mar09   0:00 sudo su -
root     12761  0.0  0.0  55128     0 pts/2    S    Mar09   0:00 su -
root     12762  0.0  0.1  21508  1348 pts/2    S    Mar09   0:00 -su
root     13304  0.0  0.0 232308   180 ?        S    Mar08   0:07 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     13713  0.0  0.0 232308    72 ?        S    Mar09   0:00 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     14237  0.0  0.0 232308   100 ?        S    Mar08   0:07 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     14600  0.0  0.0  99528   164 ?        Ss   Mar09   0:00 sshd: ★user [priv]
★user    14616  0.0  0.0  99528   584 ?        S    Mar09   0:00 sshd: ★user@notty
★user    14617  0.0  0.0  12876   256 ?        Ss   Mar09   0:00 /usr/lib/openssh/sftp-server
root     14706  0.0  0.0 232308   136 ?        S    Mar09   0:00 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     15183  0.0  0.0 232308   204 ?        S    Mar08   0:07 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     15608  0.0  0.0      0     0 ?        S    Mar09   0:00 [kworker/u4:2]
root     15780  0.0  0.0      0     0 ?        S    00:04   0:00 [kworker/u4:0]
root     15791  0.0  0.0 232308    80 ?        S    00:05   0:00 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     16115  0.0  0.0 232308    80 ?        S    Mar08   0:07 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     16778  0.0  0.0 232308    60 ?        S    01:05   0:00 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     17059  0.0  0.0 232308    76 ?        S    Mar08   0:07 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     17726  0.0  0.0      0     0 ?        S<   02:00   0:00 [kworker/u5:2]
root     17967  0.0  0.0 232308   256 ?        S    02:05   0:00 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     17993  0.0  0.0 232308   252 ?        S    Mar08   0:06 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     18025  0.0  0.0      0     0 ?        S<   02:07   0:00 [kworker/u5:0]
root     18398  0.0  0.0      0     0 ?        S    02:24   0:00 [kworker/1:0]
root     19047  0.0  0.0 232308    76 ?        S    Mar08   0:06 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     19194  0.0  1.3 232308 14048 ?        S    03:05   0:00 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     19518  0.0  0.0   6004   632 ?        S    03:23   0:00 sleep 1m
root     19529  0.0  0.1  36076  1700 pts/2    R+   03:24   0:00 ps aux
root     20097  0.0  0.0 232308   212 ?        S    Mar08   0:06 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     21104  0.0  0.0 232308   224 ?        S    Mar08   0:05 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     22197  0.0  0.0 232308   264 ?        S    Mar09   0:05 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     22468  0.0  0.0  11332   556 ?        S    Mar07   0:04 /bin/bash ./loop8.sh
★user    22666  0.0  0.0  45240     0 ?        Ss   Mar06   0:00 /lib/systemd/systemd --user
★user    22667  0.0  0.0  63256     4 ?        S    Mar06   0:00 (sd-pam)
root     23284  0.0  0.0 232308    96 ?        S    Mar09   0:05 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     24410  0.0  0.0 232308   192 ?        S    Mar09   0:04 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     25393  0.0  0.0 232308   196 ?        S    Mar09   0:04 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     26650  0.0  0.0 232308   232 ?        S    Mar09   0:04 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
rabbitmq 27670  0.0  0.0  13848    64 ?        S    Mar06   0:11 /usr/lib/erlang/erts-8.1/bin/epmd -daemon
root     27756  0.0  0.0 232308   228 ?        S    Mar09   0:04 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     28734  0.0  0.0 232308   128 ?        S    Mar09   0:03 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
www-data 29283  0.0  0.1 352164  2004 ?        S    Mar09   0:00 /usr/sbin/apache2 -k start
www-data 29284  0.0  0.1 352164  1300 ?        S    Mar09   0:00 /usr/sbin/apache2 -k start
www-data 29285  0.0  0.1 352164  1944 ?        S    Mar09   0:00 /usr/sbin/apache2 -k start
www-data 29286  0.0  0.1 352556  1956 ?        S    Mar09   0:00 /usr/sbin/apache2 -k start
www-data 29287  0.0  0.1 352164  1776 ?        S    Mar09   0:00 /usr/sbin/apache2 -k start
www-data 29318  0.0  0.2 352164  2508 ?        S    Mar09   0:00 /usr/sbin/apache2 -k start
root     29957  0.0  0.0 232308    84 ?        S    Mar09   0:04 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     30931  0.0  0.0 232308    40 ?        S    Mar09   0:04 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     31317  0.0  0.0  27720   300 ?        Ss   Mar08   0:02 /usr/sbin/cron -f
www-data 31812  0.0  0.1 352164  1116 ?        S    Mar09   0:00 /usr/sbin/apache2 -k start
root     31947  0.0  0.0 232308   180 ?        S    Mar09   0:02 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php

よく見ると dequeueBroker.php が たくさん 常駐している。

唔,要不要尝试一次将 dequeueBroker.php 全部_kill掉。

# ps aux | grep dequeueBroker.php
root       469  0.0  0.0 232308   236 ?        S    Mar09   0:02 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root      1878  0.0  0.0 232308   108 ?        S    Mar09   0:03 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root      2957  0.0  0.0 232308    40 ?        S    Mar09   0:02 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root      4784  0.0  0.0 232308    92 ?        S    Mar09   0:03 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root      4945  0.0  0.0 232308   104 ?        S    Mar08   0:09 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root      5916  0.0  0.0 232308   172 ?        S    Mar09   0:02 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root      6301  0.0  0.0 232308   260 ?        S    Mar08   0:10 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root      6856  0.0  0.0 232308   112 ?        S    Mar09   0:02 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root      7795  0.0  0.0 232308   112 ?        S    Mar09   0:01 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root      8303  0.0  0.0 232308   132 ?        S    Mar08   0:09 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root      8732  0.0  0.0 232308   132 ?        S    Mar09   0:01 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root      9246  0.0  0.0 232308    80 ?        S    Mar08   0:09 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root      9664  0.0  0.0 232308   272 ?        S    Mar09   0:01 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     10278  0.0  0.0 232308    52 ?        S    Mar08   0:08 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     10632  0.0  0.0 232308   124 ?        S    Mar09   0:01 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     11364  0.0  0.0 232308   276 ?        S    Mar08   0:08 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     11567  0.0  0.0 232308    72 ?        S    Mar09   0:01 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     12376  0.0  0.0 232308    76 ?        S    Mar08   0:08 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     12582  0.0  0.0 232308   164 ?        S    Mar09   0:01 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     13304  0.0  0.0 232308   180 ?        S    Mar08   0:07 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     13713  0.0  0.0 232308    72 ?        S    Mar09   0:00 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     14237  0.0  0.0 232308   100 ?        S    Mar08   0:07 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     14706  0.0  0.0 232308   136 ?        S    Mar09   0:00 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     15183  0.0  0.0 232308   204 ?        S    Mar08   0:07 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     15791  0.0  0.0 232308    80 ?        S    00:05   0:00 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     16115  0.0  0.0 232308    80 ?        S    Mar08   0:07 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     16778  0.0  0.0 232308    60 ?        S    01:05   0:00 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     17059  0.0  0.0 232308    76 ?        S    Mar08   0:07 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     17967  0.0  0.0 232308   256 ?        S    02:05   0:00 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     17993  0.0  0.0 232308   252 ?        S    Mar08   0:06 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     19047  0.0  0.0 232308    76 ?        S    Mar08   0:06 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     19194  0.0  1.3 232308 14048 ?        S    03:05   0:00 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     19588  0.0  0.0  13068   984 pts/2    S+   03:29   0:00 grep --color=auto dequeueBroker.php
root     20097  0.0  0.0 232308   212 ?        S    Mar08   0:06 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     21104  0.0  0.0 232308   224 ?        S    Mar08   0:05 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     22197  0.0  0.0 232308   264 ?        S    Mar09   0:05 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     23284  0.0  0.0 232308    96 ?        S    Mar09   0:05 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     24410  0.0  0.0 232308   192 ?        S    Mar09   0:04 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     25393  0.0  0.0 232308   196 ?        S    Mar09   0:04 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     26650  0.0  0.0 232308   232 ?        S    Mar09   0:04 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     27756  0.0  0.0 232308   228 ?        S    Mar09   0:04 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     28734  0.0  0.0 232308   128 ?        S    Mar09   0:03 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     29957  0.0  0.0 232308    84 ?        S    Mar09   0:04 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     30931  0.0  0.0 232308    40 ?        S    Mar09   0:04 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     31947  0.0  0.0 232308   180 ?        S    Mar09   0:02 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php

我记得有一个命令可以提取特定列,但由于我不记得了,所以要去搜索一下。

「利用Linux命令灵活操作文本数据」(棠果风味)

ええっ! cat と cut があるの!?

cat でフィールド抽出する方法なんか無いよな、とか思ってたんだが。
じゃあ、こうしたらどうか?

# ps aux | grep dequeueBroker.php | cut -f 2

不行,出局了。什么都没有。

使用cut命令时,可以将一个或多个连续的空格或制表符作为分隔符进行处理。

这类博客文章,如果在IT类公司工作就无法从安全性角度来阅读,而在非IT类公司工作就可以阅读。不知道什么是IT类……通过切割每个风险来实现最完善的安全,但正是IT技术的一次丑闻广泛传播造就了当前的IT行业。

那么,这样行吗?

# ps aux | grep dequeueBroker.php | sed 's/[\t ]\+/\t/g' | cut -f 2
469
1878
2957
4784
4945
5916
6301
6856
7795
8303
8732
9246
9664
10278
10632
11364
11567
12376
12582
13304
13713
14237
14706
15183
15791
16115
16778
17059
17967
17993
19047
19194
19795
20097
21104
22197
23284
24410
25393
26650
27756
28734
29957
30931
31947

太棒了!

那你不能用for循环来执行kill嘛?

将多个进程集中杀死。

所以,如果我理解得正确的话,阅读这个的意思是杀死对方。对吗?

kill `ps aux | grep dequeueBroker.php | sed 's/[\t ]\+/\t/g' | cut -f 2`
# kill `ps aux | grep dequeueBroker.php | sed 's/[\t ]\+/\t/g' | cut -f 2`
-su: kill: (19870) - No such process

什么? ?)

# ps aux | grep dequeueBroker.php
root     19889  0.0  0.0  12936   984 pts/2    S+   03:45   0:00 grep --color=auto dequeueBroker.php

唉,难道不是消失了吗?

那么,让我们试着在后台启动浮かむ瀬。

# cd /home/★user/shogi/ukamuse_sdt4/bin
# ./apery &
[1] 20023
# ps aux | grep apery
root     20023  0.8 14.0 1325848 142852 pts/2  Tl   03:54   0:00 ./apery
root     20033  0.0  0.0  12936   984 pts/2    S+   03:55   0:00 grep --color=auto apery

[1]+  Stopped                 ./apery

我不懂这是什么意思。写着”Stopped”。

# ps aux | grep apery
root     20023  0.1 14.0 1325848 142852 pts/2  Tl   03:54   0:00 ./apery
root     20055  0.0  0.0  12936   988 pts/2    S+   03:56   0:00 grep --color=auto apery

再看一次,似乎还有尚未结束的/apery进程。TL正在停止中,可能是正在生成新的进程吧。这是什么情况?

让我们从超级用户和一般登录账户中注销以尝试。

# logout
There are stopped jobs.
# logout
# logout

嗯?

重新登录

进行验证,然后可以访问系统。

$ sudo su -

從…出發

# ps aux | grep apery
root     20182  0.0  0.0  12936   988 pts/2    S+   04:02   0:00 grep --color=auto apery

./apery 不见了。如果退出登录,任务会停止吗?
那接下来的。

「【nohup】即使注销也能继续执行命令」(ITPro)

来源:http://itpro.nikkeibp.co.jp/article/COLUMN/20060227/230850/

# cd /home/★user/shogi/ukamuse_sdt4/bin
# nohup ./apery &
[1] 20237
root@★:/home/★user/shogi/ukamuse_sdt4/bin# nohup: ignoring input and appending output to 'nohup.out'

这是什么呢?

如果输入被忽略的话,那不是会很困扰吗?

nohup php ./apery > apery.out.log 2> apery.err.log < /dev/null &

標準入力は 何にしたらいいんだ?
コンピューター将棋ソフトは 標準入力から コマンドを受け付けるので、それを閉じてしまうと 役に立たない。

それは あとで考えるとして、 nohup で実行したコマンドが、ログアウト後も残っているのか調べてみたい。

# ps aux | grep apery      root     20436  0.0  0.0  12936   984 pts/2    S+   04:15   0:00 grep --color=auto apery
[1]+  Done                    nohup ./apery

Done 是什么意思呢?nohup 是不是 Done 的原因呢?

# ps aux | grep apery
root     20447  0.0  0.0  12936   988 pts/2    S+   04:16   0:00 grep --color=auto apery

难道是因为没有执行”./apery”命令,所以在注销期间消失了。

那么,使用nohup可以在后台运行什么呢?

「PHP的while循环和无限循环」(零编程博客-从零开始的编程学习)

PHP while文と無限ループ

loop28.php

<?php
while(true){
  sleep(1);
}
# php loop28.php &
[1] 20615
# ps aux | grep php
root      6091  0.0  0.0 311040   312 ?        Ss   Mar07   0:50 php-fpm: master process (/etc/php/7.0/fpm/php-fpm.conf)
www-data  6094  0.0  0.0 311040     0 ?        S    Mar07   0:00 php-fpm: pool www
www-data  6095  0.0  0.0 311040     0 ?        S    Mar07   0:00 php-fpm: pool www
root     20224  0.0  1.3 232308 14048 ?        S    04:05   0:00 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     20615  0.1  1.3 230188 13304 pts/2    S    04:27   0:00 php loop28.php
root     20622  0.0  0.0  12936   988 pts/2    S+   04:28   0:00 grep --color=auto php

那么,我试着注销一下账户。

# logout
# logout

刚才是三次,而这次只有两次。不知道有什么区别呢。

# ps aux | grep php
root      6091  0.0  0.0 311040   312 ?        Ss   Mar07   0:50 php-fpm: master process (/etc/php/7.0/fpm/php-fpm.conf)
www-data  6094  0.0  0.0 311040     0 ?        S    Mar07   0:00 php-fpm: pool www
www-data  6095  0.0  0.0 311040     0 ?        S    Mar07   0:00 php-fpm: pool www
root     20224  0.0  1.3 232308 14048 ?        S    04:05   0:00 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     20615  0.0  1.3 230188 13304 ?        S    04:27   0:00 php loop28.php
root     20743  0.0  0.0  12936   984 pts/3    S+   04:33   0:00 grep --color=auto php

听说这次需要 loop28.php。

如果PHP和C#、C++能够进行进程间通信的话,那事情就简单了。但是,正如我在文章《第1篇》至《第3篇》中所写道,PHP和C#之间的程序间通信却不能正常工作。

C++ 的无限循环能够常驻吗?

「时间·时刻处理(4)」(LinuxC)

不行,不能在Windows上运行。

「毫秒级的睡眠」(来自 Stack Overflow)
http://stackoverflow.com/questions/4184468/毫秒级的睡眠

好吧,这样吧。

// Cpp_Tamesi28.cpp : Defines the entry point for the console application.
//

#include <chrono>
#include <thread>

#if WINDOWS
#include "stdafx.h"
#endif

int main(void)
{
    for (;;) {
        std::this_thread::sleep_for(std::chrono::seconds(1));
    }
    return 0;
}

如何在 Visual Studio 2015 C++ 中将预处理器定义作为命令行参数附加?

「/D(预处理器定义)」(MSDN)

从主菜单的[项目]选项中,打开项目的属性页面,然后依次点击[配置属性] – [C/C++] – [预处理器] – [预处理器定义]。

_DEBUG;_CONSOLE;%(PreprocessorDefinitions)

在末尾加上WINDOWS的标志。

_DEBUG;_CONSOLE;%(PreprocessorDefinitions);WINDOWS

不行。这样不行。

_DEBUG;_CONSOLE;WINDOWS;%(PreprocessorDefinitions)

这也不行。

啊!变成了x86了。要适应x64吧。
好的,没问题。

stdafx.h -> 标准附加头文件.h .h)

// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>


// TODO: reference additional headers your program requires here

很抱歉,但无法对这个片段进行适当的中文转述。可以给出更多上下文以获得更好的答案吗?

// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#pragma once

#if WINDOWS
#include "targetver.h"

#include <stdio.h>
#include <tchar.h>
#endif


// TODO: reference additional headers your program requires here

母语中国,只需一个选项,请改写以下内容:
“内容”

#if WINDOWS
#endif

打开一个不接受输入的命令提示窗口。

把源代码带到Ubuntu上,它能运行吗?
但是我不知道如何编写Makefile。

# cd /home/★user/shogi
# mkdir cpp_service
# mkdir tamesi28.d
# nano tamesi28.cpp
#include <chrono>
#include <thread>

int main(void)
{
        for (;;) {
                std::this_thread::sleep_for(std::chrono::seconds(1));
        }
        return 0;
}

这样可以吗?
要用g++进行编译吗?

「g++」(C++)是一个C++编译器。
可以在以下链接找到相关信息:http://kaworu.jpn.org/cpp/g++

只需要给它命名一个文件名吗?

# g++ tamesi28.cpp
In file included from /usr/include/c++/5/chrono:35:0,
                 from tamesi28.cpp:1:
/usr/include/c++/5/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
 #error This file requires compiler and library support \
  ^
tamesi28.cpp: In function ‘int main()’:
tamesi28.cpp:7:8: error: ‘std::this_thread’ has not been declared
   std::this_thread::sleep_for(std::chrono::seconds(1));
        ^
tamesi28.cpp:7:36: error: ‘std::chrono’ has not been declared
   std::this_thread::sleep_for(std::chrono::seconds(1));
                                    ^

连 std::this_thread 和 std::chrono 都不能使用吗?

浮现在代码中的 usi.cpp。

            std::this_thread::sleep_for(std::chrono::seconds(5)); // 指定秒だけ待機し、進捗を表示する。

在我使用的环境中,我觉得能够成功编译使用g++。不知道是否有一个Makefile机制存在?

在Makefile开头的前三行

COMPILER = g++
#COMPILER = mpicxx
CFLAGS   = -std=c++11 -fno-exceptions -fno-rtti -Wextra -Ofast -MMD -MP -fopenmp

这附近可能有关系吧?

# g++ -std=c++11 tamesi28.cpp

似乎已经成功执行了。

# ls
a.out  tamesi28.cpp
# ./a.out

好的,看起来进入了无限循环。

按住[Ctrl]键,然后按下[C]键。

可以出去了。

# ./a.out &
[1] 21556

糟糕了,文件名很难搜索到。

# ps aux | grep a.out
root     21556  0.0  0.0   4216   364 pts/3    S    05:22   0:00 ./a.out
root     21569  0.0  0.0  12936   988 pts/3    S+   05:22   0:00 grep --color=auto a.out

看起来在动。
试着登出账号。

# logout
$ logout

重新登录。

# ps aux | grep a.out
root     21556  0.0  0.0   4216   364 ?        S    05:22   0:00 ./a.out
root     21658  0.0  0.0  12936   984 pts/4    S+   05:25   0:00 grep --color=auto a.out

平常都在这里呢……。

那么,我们来研究一下,C++是否可以进行进程间通信。

「RabbitMQ教程」(RabbitMQ)
http://www.rabbitmq.com/getstarted.html

嗯?RabbitMQ没有关于C++的说明吗?

「客户端和开发者工具」(RabbitMQ)
https://www.rabbitmq.com/devtools.html

似乎有四个。

「AMQP-CPP」是由CopernicaMarketingSoftware开发的一个库,用于在C++中实现AMQP(高级消息队列协议)。您可以在https://github.com/CopernicaMarketingSoftware/AMQP-CPP找到更多关于该库的信息。

你想调查一下这个写有示例的文件吗?

在FileZilla中将AMQP-CPP-master复制。这是AMQP而不是RabbitMQ真正的AMQP,它是RabbitMQ风格的。

cd /home/★user/shogi/AMQP-CPP-master
cd AMQP-CPP-master
# nano Makefile

那么,

# make pure
# ls
amqpcpp.h       include  Makefile   set_cxx_norm.cmake  tests
CMakeLists.txt  LICENSE  README.md  src

这个可执行文件是在哪里创建的?或者库在哪里?

# make
# make install
# make pure
make -C src pure
make[1]: Entering directory '/home/★user/shogi/AMQP-CPP-master/src'
make[1]: Nothing to be done for 'pure'.
make[1]: Leaving directory '/home/★user/shogi/AMQP-CPP-master/src'

嗯。如果查看 src 文件夹里面的内容会怎样呢?

只有一个可执行文件。
libamqpcpp.so.2.6.2

这个怎么使用?

这里是示例程序吗?

「amqpcpp/examples/」(akalend/amqpcpp)在GitHub上的链接为https://github.com/akalend/amqpcpp/tree/master/examples。

哎呀,src文件夹里面也有一个Makefile呢。

/home/★user/shogi/AMQP-CPP-master/src# make pure

似乎生成了一个名为lib.so的可执行文件,但是不清楚具体是关于什么。

root@★:/home/★user/shogi/AMQP-CPP-master# make pure
make -C src pure
make[1]: Entering directory '/home/★user/shogi/AMQP-CPP-master/src'
make[1]: Nothing to be done for 'pure'.
make[1]: Leaving directory '/home/★user/shogi/AMQP-CPP-master/src'
root@★:/home/★user/shogi/AMQP-CPP-master# make install
mkdir -p /usr/include/amqpcpp
mkdir -p /usr/lib
cp -f amqpcpp.h /usr/include
cp -f include/*.h /usr/include/amqpcpp
cp -f src/libamqpcpp.so.2.6.2 /usr/lib
cp -f src/libamqpcpp.a.2.6.2 /usr/lib
ln -r -s -f /usr/lib/libamqpcpp.so.2.6.2 /usr/lib/libamqpcpp.so.2.6
ln -r -s -f /usr/lib/libamqpcpp.so.2.6.2 /usr/lib/libamqpcpp.so
ln -r -s -f /usr/lib/libamqpcpp.a.2.6.2 /usr/lib/libamqpcpp.a

你有没有已经为我设置好使用的目录 “/usr/lib”?那好,让我试着写一下源代码吧。

/home/★user/shogi/cpp_service# nano tamesi29a7_main.cpp

tamesi29a7_main.cpp 文件进行重写的中文版本。

#include <ev.h>
#include <amqpcpp.h>
#include <amqpcpp/libev.h>

int main()
{
    // access to the event loop
    auto *loop = EV_DEFAULT;

    // handler for libev (so we don't have to implement AMQP::TcpHandler!)
    AMQP::LibEvHandler handler(loop);

    // make a connection
    AMQP::TcpConnection connection(&handler, AMQP::Address("amqp://localhost/"));

    // we need a channel too
    AMQP::TcpChannel channel(&connection);

    // create a temporary queue
    channel.declareQueue(AMQP::exclusive).onSuccess([&connection](const std::string &name, uint32_t messagecount, uint32_t consumercount) {

        // report the name of the temporary queue
        std::cout << "declared queue " << name << std::endl;

        // now we can close the connection
        connection.close();
    });

    // run the loop
    ev_run(loop, 0);

    // done
    return 0;
}
# g++ tamesi29a7_main.cpp
tamesi29a7_main.cpp:1:16: fatal error: ev.h: No such file or directory
compilation terminated.

不一样吗?

libev是一个在C语言中使用的高效异步事件库。

“libev的ev_async有什么用途?”

有一个叫做ev图书馆的东西吗?

「利用libev进行异步编程」(Qiita)
http://qiita.com/0xfffffff7/items/c5287067623fdbd2c3cb

那么,来尝试安装一下吧。

# apt-get -y install libev
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package libev

有没有这样的命令呢……。

「如何在Ubuntu 14.04(Trusty Tahr)上安装libev-dev」(安装指南)
https://www.howtoinstall.co/zh/ubuntu/trusty/libev-dev

sudo apt-get update
sudo apt-get install libev-dev

我不知道我是不是进去了。

# g++ tamesi29a7_main.cpp

出现了许多错误。这样如何?

# g++ -std=c++11 tamesi29a7_main.cpp
/tmp/ccMfSVKb.o: In function `main':
tamesi29a7_main.cpp:(.text+0x18f): undefined reference to `ev_default_loop'
tamesi29a7_main.cpp:(.text+0x1e3): undefined reference to `AMQP::TcpConnection::TcpConnection(AMQP::TcpHandler*, AMQP::Address const&)'
tamesi29a7_main.cpp:(.text+0x22d): undefined reference to `AMQP::exclusive'
tamesi29a7_main.cpp:(.text+0x276): undefined reference to `ev_run'
/tmp/ccMfSVKb.o: In function `AMQP::Field::~Field()':
tamesi29a7_main.cpp:(.text._ZN4AMQP5FieldD2Ev[_ZN4AMQP5FieldD5Ev]+0xd): undefined reference to `vtable for AMQP::Field'
/tmp/ccMfSVKb.o: In function `AMQP::Field::Field()':
tamesi29a7_main.cpp:(.text._ZN4AMQP5FieldC2Ev[_ZN4AMQP5FieldC5Ev]+0x9): undefined reference to `vtable for AMQP::Field'
/tmp/ccMfSVKb.o: In function `AMQP::Table::Table()':
tamesi29a7_main.cpp:(.text._ZN4AMQP5TableC2Ev[_ZN4AMQP5TableC5Ev]+0x19): undefined reference to `vtable for AMQP::Table'
/tmp/ccMfSVKb.o: In function `AMQP::Table::~Table()':
tamesi29a7_main.cpp:(.text._ZN4AMQP5TableD2Ev[_ZN4AMQP5TableD5Ev]+0xd): undefined reference to `vtable for AMQP::Table'
/tmp/ccMfSVKb.o: In function `AMQP::Channel::Channel(AMQP::Connection*)':
tamesi29a7_main.cpp:(.text._ZN4AMQP7ChannelC2EPNS_10ConnectionE[_ZN4AMQP7ChannelC5EPNS_10ConnectionE]+0x30): undefined reference to `AMQP::ChannelImpl::ChannelImpl()'
tamesi29a7_main.cpp:(.text._ZN4AMQP7ChannelC2EPNS_10ConnectionE[_ZN4AMQP7ChannelC5EPNS_10ConnectionE]+0x65): undefined reference to `AMQP::ChannelImpl::attach(AMQP::Connection*)'
/tmp/ccMfSVKb.o: In function `AMQP::Channel::~Channel()':
tamesi29a7_main.cpp:(.text._ZN4AMQP7ChannelD2Ev[_ZN4AMQP7ChannelD5Ev]+0x2c): undefined reference to `AMQP::ChannelImpl::close()'
/tmp/ccMfSVKb.o: In function `AMQP::Channel::declareQueue(int)':
tamesi29a7_main.cpp:(.text._ZN4AMQP7Channel12declareQueueEi[_ZN4AMQP7Channel12declareQueueEi]+0x68): undefined reference to `AMQP::ChannelImpl::declareQueue(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int, AMQP::Table const&)'
/tmp/ccMfSVKb.o: In function `AMQP::Connection::~Connection()':
tamesi29a7_main.cpp:(.text._ZN4AMQP10ConnectionD2Ev[_ZN4AMQP10ConnectionD5Ev]+0x24): undefined reference to `AMQP::ConnectionImpl::~ConnectionImpl()'
/tmp/ccMfSVKb.o: In function `AMQP::Connection::close()':
tamesi29a7_main.cpp:(.text._ZN4AMQP10Connection5closeEv[_ZN4AMQP10Connection5closeEv]+0x18): undefined reference to `AMQP::ConnectionImpl::close()'
/tmp/ccMfSVKb.o: In function `AMQP::TcpConnection::~TcpConnection()':
tamesi29a7_main.cpp:(.text._ZN4AMQP13TcpConnectionD2Ev[_ZN4AMQP13TcpConnectionD5Ev]+0xd): undefined reference to `vtable for AMQP::TcpConnection'
tamesi29a7_main.cpp:(.text._ZN4AMQP13TcpConnectionD2Ev[_ZN4AMQP13TcpConnectionD5Ev]+0x19): undefined reference to `vtable for AMQP::TcpConnection'
tamesi29a7_main.cpp:(.text._ZN4AMQP13TcpConnectionD2Ev[_ZN4AMQP13TcpConnectionD5Ev]+0x51): undefined reference to `AMQP::Watchable::~Watchable()'
/tmp/ccMfSVKb.o: In function `AMQP::LibEvHandler::Watcher::callback(ev_loop*, ev_io*, int)':
tamesi29a7_main.cpp:(.text._ZN4AMQP12LibEvHandler7Watcher8callbackEP7ev_loopP5ev_ioi[_ZN4AMQP12LibEvHandler7Watcher8callbackEP7ev_loopP5ev_ioi]+0x33): undefined reference to `AMQP::TcpConnection::process(int, int)'
/tmp/ccMfSVKb.o: In function `AMQP::LibEvHandler::Watcher::Watcher(ev_loop*, AMQP::TcpConnection*, int, int)':
tamesi29a7_main.cpp:(.text._ZN4AMQP12LibEvHandler7WatcherC2EP7ev_loopPNS_13TcpConnectionEii[_ZN4AMQP12LibEvHandler7WatcherC5EP7ev_loopPNS_13TcpConnectionEii]+0xbf): undefined reference to `ev_io_start'
/tmp/ccMfSVKb.o: In function `AMQP::LibEvHandler::Watcher::~Watcher()':
tamesi29a7_main.cpp:(.text._ZN4AMQP12LibEvHandler7WatcherD2Ev[_ZN4AMQP12LibEvHandler7WatcherD5Ev]+0x2f): undefined reference to `ev_io_stop'
/tmp/ccMfSVKb.o: In function `AMQP::LibEvHandler::Watcher::events(int)':
tamesi29a7_main.cpp:(.text._ZN4AMQP12LibEvHandler7Watcher6eventsEi[_ZN4AMQP12LibEvHandler7Watcher6eventsEi]+0x26): undefined reference to `ev_io_stop'
tamesi29a7_main.cpp:(.text._ZN4AMQP12LibEvHandler7Watcher6eventsEi[_ZN4AMQP12LibEvHandler7Watcher6eventsEi]+0x5d): undefined reference to `ev_io_start'
collect2: error: ld returned 1 exit status

怎么办呢。

「C libev的undefined reference to `ev_default_loop’」 (stackoverflow)
http://stackoverflow.com/questions/13844309/c-libev-undefined-reference-to-ev-default-loop

「C libev出现undefined reference to `ev_default_loop’」 (stackoverflow)
http://stackoverflow.com/questions/13844309/c-libev-undefined-reference-to-ev-default-loop

如果加上「-lev」选项,会怎么样呢?

# g++ -std=c++11 tamesi29a7_main.cpp -lev
/tmp/ccbptlAj.o: In function `main':
tamesi29a7_main.cpp:(.text+0x1e3): undefined reference to `AMQP::TcpConnection::TcpConnection(AMQP::TcpHandler*, AMQP::Address const&)'
tamesi29a7_main.cpp:(.text+0x22d): undefined reference to `AMQP::exclusive'
/tmp/ccbptlAj.o: In function `AMQP::Field::~Field()':
tamesi29a7_main.cpp:(.text._ZN4AMQP5FieldD2Ev[_ZN4AMQP5FieldD5Ev]+0xd): undefined reference to `vtable for AMQP::Field'
/tmp/ccbptlAj.o: In function `AMQP::Field::Field()':
tamesi29a7_main.cpp:(.text._ZN4AMQP5FieldC2Ev[_ZN4AMQP5FieldC5Ev]+0x9): undefined reference to `vtable for AMQP::Field'
/tmp/ccbptlAj.o: In function `AMQP::Table::Table()':
tamesi29a7_main.cpp:(.text._ZN4AMQP5TableC2Ev[_ZN4AMQP5TableC5Ev]+0x19): undefined reference to `vtable for AMQP::Table'
/tmp/ccbptlAj.o: In function `AMQP::Table::~Table()':
tamesi29a7_main.cpp:(.text._ZN4AMQP5TableD2Ev[_ZN4AMQP5TableD5Ev]+0xd): undefined reference to `vtable for AMQP::Table'
/tmp/ccbptlAj.o: In function `AMQP::Channel::Channel(AMQP::Connection*)':
tamesi29a7_main.cpp:(.text._ZN4AMQP7ChannelC2EPNS_10ConnectionE[_ZN4AMQP7ChannelC5EPNS_10ConnectionE]+0x30): undefined reference to `AMQP::ChannelImpl::ChannelImpl()'
tamesi29a7_main.cpp:(.text._ZN4AMQP7ChannelC2EPNS_10ConnectionE[_ZN4AMQP7ChannelC5EPNS_10ConnectionE]+0x65): undefined reference to `AMQP::ChannelImpl::attach(AMQP::Connection*)'
/tmp/ccbptlAj.o: In function `AMQP::Channel::~Channel()':
tamesi29a7_main.cpp:(.text._ZN4AMQP7ChannelD2Ev[_ZN4AMQP7ChannelD5Ev]+0x2c): undefined reference to `AMQP::ChannelImpl::close()'
/tmp/ccbptlAj.o: In function `AMQP::Channel::declareQueue(int)':
tamesi29a7_main.cpp:(.text._ZN4AMQP7Channel12declareQueueEi[_ZN4AMQP7Channel12declareQueueEi]+0x68): undefined reference to `AMQP::ChannelImpl::declareQueue(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int, AMQP::Table const&)'
/tmp/ccbptlAj.o: In function `AMQP::Connection::~Connection()':
tamesi29a7_main.cpp:(.text._ZN4AMQP10ConnectionD2Ev[_ZN4AMQP10ConnectionD5Ev]+0x24): undefined reference to `AMQP::ConnectionImpl::~ConnectionImpl()'
/tmp/ccbptlAj.o: In function `AMQP::Connection::close()':
tamesi29a7_main.cpp:(.text._ZN4AMQP10Connection5closeEv[_ZN4AMQP10Connection5closeEv]+0x18): undefined reference to `AMQP::ConnectionImpl::close()'
/tmp/ccbptlAj.o: In function `AMQP::TcpConnection::~TcpConnection()':
tamesi29a7_main.cpp:(.text._ZN4AMQP13TcpConnectionD2Ev[_ZN4AMQP13TcpConnectionD5Ev]+0xd): undefined reference to `vtable for AMQP::TcpConnection'
tamesi29a7_main.cpp:(.text._ZN4AMQP13TcpConnectionD2Ev[_ZN4AMQP13TcpConnectionD5Ev]+0x19): undefined reference to `vtable for AMQP::TcpConnection'
tamesi29a7_main.cpp:(.text._ZN4AMQP13TcpConnectionD2Ev[_ZN4AMQP13TcpConnectionD5Ev]+0x51): undefined reference to `AMQP::Watchable::~Watchable()'
/tmp/ccbptlAj.o: In function `AMQP::LibEvHandler::Watcher::callback(ev_loop*, ev_io*, int)':
tamesi29a7_main.cpp:(.text._ZN4AMQP12LibEvHandler7Watcher8callbackEP7ev_loopP5ev_ioi[_ZN4AMQP12LibEvHandler7Watcher8callbackEP7ev_loopP5ev_ioi]+0x33): undefined reference to `AMQP::TcpConnection::process(int, int)'
collect2: error: ld returned 1 exit status

错误减少了吗?

「针对rabbitmq-c进行构建」(stackoverflow)
http://stackoverflow.com/questions/12028112/针对rabbitmq-c进行构建

我在阅读 GitHub 安装说明时看到,写着要连接库并进行链接。

# g++ -std=c++11 tamesi29a7_main.cpp -lev -lamqpcpp
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libamqpcpp.so: undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status

这是什么呢?

在Linux中存在对pthread_create的未定义引用。(stackoverflow)

添加”-pthread”好吗?

# g++ -std=c++11 tamesi29a7_main.cpp -lev -lamqpcpp -pthread

编译似乎通过了。执行文件的名称好像变成了 a.out……。这样。

# g++ -std=c++11 tamesi29a7_main.cpp -lev -lamqpcpp -pthread -o tamesi29a7_main.exe

好的,我成功创建了一个名为tamesi29a7_main.exe的文件。

# ./tamesi29a7_main.exe
declared queue amq.gen-R8_7pGx8GdY5EmtcMddvzQ

我不知道这是什么意思。

他们可能已经使用这个队列的名称进行了多次拆分,对吗?

# rabbitmqctl list_queues
Listing queues ...
1111    0

嗯。

「amqpcpp/examples/example_consume.cpp」(akalend/amqpcpp)
在github上查看链接:https://github.com/akalend/amqpcpp/blob/master/examples/example_consume.cpp。

让我们试一试这个。

tamesi29a8_example_consume.cpp 的示例消费

#include "AMQPcpp.h"

using namespace std;

int i=0;

int onCancel(AMQPMessage * message ) {
    cout << "cancel tag="<< message->getDeliveryTag() << endl;
    return 0;
}

int  onMessage( AMQPMessage * message  ) {
    uint32_t j = 0;
    char * data = message->getMessage(&j);
    if (data)
          cout << data << endl;

    i++;

    cout << "#" << i << " tag="<< message->getDeliveryTag() << " content-type:"<< message->getHeader("Content-type") ;
    cout << " encoding:"<< message->getHeader("Content-encoding")<< " mode="<<message->getHeader("Delivery-mode")<<endl;

    if (i > 10) {
        AMQPQueue * q = message->getQueue();
        q->Cancel( message->getConsumerTag() );
    }
    return 0;
};


int main () {


    try {
//      AMQP amqp("123123:akalend@localhost/private");

        AMQP amqp("123123:akalend@localhost:5673/private");

        AMQPQueue * qu2 = amqp.createQueue("q2");

        qu2->Declare();
        qu2->Bind( "e", "");

        qu2->setConsumerTag("tag_123");
        qu2->addEvent(AMQP_MESSAGE, onMessage );
        qu2->addEvent(AMQP_CANCEL, onCancel );

        qu2->Consume(AMQP_NOACK);//


    } catch (AMQPException e) {
        std::cout << e.getMessage() << std::endl;
    }

    return 0;

}
# g++ -std=c++11 tamesi29a8_example_consume.cpp -lev -lamqpcpp -pthread -o tamesi29a8_example_consume.exe
tamesi29a8_example_consume.cpp:1:21: fatal error: AMQPcpp.h: No such file or directory
compilation terminated.

那是个毫无参考价值的消息来源。

# ./tamesi29a7_main.exe
declared queue amq.gen-0n2SmfNgiVabWrTBjd5F3w
# ./tamesi29a7_main.exe
declared queue amq.gen-MRpMMZ2dX3VRK-HVAJV62w
# ./tamesi29a7_main.exe
declared queue amq.gen-wCRRD2i98lE52gR8nHjaxw

这个名字是什么意思呢?

「FLAGS AND TABLES」(CopernicaMarketingSoftware/AMQP-CPP)可以在Github上找到,它是由CopernicaMarketingSoftware开发的AMQP-CPP。

我閱讀說明並試著進行改造。

请将下面的内容用中文进行同义转述,只需要一种选项:
tamesi29a7_main.cpp

#include <ev.h>
#include <amqpcpp.h>
#include <amqpcpp/libev.h>

int main()
{
    // access to the event loop
    auto *loop = EV_DEFAULT;

    // handler for libev (so we don't have to implement AMQP::TcpHandler!)
    AMQP::LibEvHandler handler(loop);

    // make a connection
    AMQP::TcpConnection connection(&handler, AMQP::Address("amqp://localhost/"));

    // we need a channel too
    AMQP::TcpChannel channel(&connection);

//    // create a temporary queue
//    channel.declareQueue(AMQP::exclusive).onSuccess([&connection](const std::string &name, uint32_t messagecount, uint32_t consumercount) {
//
//        // report the name of the temporary queue
//        std::cout << "declared queue " << name << std::endl;
//
//        // now we can close the connection
//        connection.close();
//    });
    // create a custom callback
    auto callback = [](const std::string &name, int msgcount, int consumercount) {

        // @todo add your own implementation

        // report the name of the temporary queue
        std::cout << "declared queue " << name << std::endl;

        // now we can close the connection
        connection.close();
    }

    // declare the queue, and install the callback that is called on success
    channel.declareQueue("1111").onSuccess(callback);

    // run the loop
    ev_run(loop, 0);

    // done
    return 0;
}
# nano tamesi29a9_main.cpp
# g++ -std=c++11 tamesi29a9_main.cpp -lev -lamqpcpp -pthread -o tamesi29a9_main.exe
tamesi29a9_main.cpp: In lambda function:
tamesi29a9_main.cpp:37:9: error: ‘connection’ is not captured
         connection.close();
         ^
tamesi29a9_main.cpp:29:22: note: the lambda has no capture-default
     auto callback = [](const std::string &name, int msgcount, int consumercount) {
                      ^
tamesi29a9_main.cpp:14:25: note: ‘AMQP::TcpConnection connection’ declared here
     AMQP::TcpConnection connection(&handler, AMQP::Address("amqp://localhost/"));
                         ^
tamesi29a9_main.cpp: In function ‘int main()’:
tamesi29a9_main.cpp:41:5: error: expected ‘,’ or ‘;’ before ‘channel’
     channel.declareQueue("1111").onSuccess(callback);
     ^

那么,就这样吧。

        // connection.close();
# g++ -std=c++11 tamesi29a9_main.cpp -lev -lamqpcpp -pthread -o tamesi29a9_main.exe
tamesi29a9_main.cpp: In function ‘int main()’:
tamesi29a9_main.cpp:41:5: error: expected ‘,’ or ‘;’ before ‘channel’
     channel.declareQueue("1111").onSuccess(callback);
     ^

半角分号不够用吗?明明是从样本复制过来的。那就加上去吧。

    };
# g++ -std=c++11 tamesi29a9_main.cpp -lev -lamqpcpp -pthread -o tamesi29a9_main.exe

编译通过了。源代码是这样的。

tamesi29a9_main.cpp 取试下述源码的汉语同义表达:

tamesi29a9_main.cpp

#include <ev.h>
#include <amqpcpp.h>
#include <amqpcpp/libev.h>

int main()
{
    // access to the event loop
    auto *loop = EV_DEFAULT;

    // handler for libev (so we don't have to implement AMQP::TcpHandler!)
    AMQP::LibEvHandler handler(loop);

    // make a connection
    AMQP::TcpConnection connection(&handler, AMQP::Address("amqp://localhost/"));

    // we need a channel too
    AMQP::TcpChannel channel(&connection);

//    // create a temporary queue
//    channel.declareQueue(AMQP::exclusive).onSuccess([&connection](const std::string &name, uint32_t messagecount, uint32_t consumercount) {
//
//        // report the name of the temporary queue
//        std::cout << "declared queue " << name << std::endl;
//
//        // now we can close the connection
//        connection.close();
//    });
    // create a custom callback
    auto callback = [](const std::string &name, int msgcount, int consumercount) {

        // @todo add your own implementation

        // report the name of the temporary queue
        std::cout << "declared queue " << name << std::endl;

        // now we can close the connection
        // connection.close();
    };

    // declare the queue, and install the callback that is called on success
    channel.declareQueue("1111").onSuccess(callback);

    // run the loop
    ev_run(loop, 0);

    // done
    return 0;
}
# ./tamesi29a9_main.exe
declared queue 1111

有点在等着。要不要用“ためしさん”系列的东西呢?

请提供一个有效的网址以便我提供中文的翻译。

我是用RabbitMQ进行入队操作的。

[x] Sent 'Flying Dragon!'

没有任何特别的变化。

^Z
[1]+  Stopped                 ./tamesi29a9_main.exe
# bg 1
[1]+ ./tamesi29a9_main.exe &

嗯。

# jobs
[1]+  Running                 ./tamesi29a9_main.exe &

うーむ。ファミコンの さんまの名探偵 を遊んでいたときの気分に似ている。

ls
tamesi28.d  tamesi29a7_main.cpp  tamesi29a7_main.exe  tamesi29a8_example_consume.cpp  tamesi29a9_main.cpp  tamesi29a9_main.exe

何かが出力されている様子もない。

# rabbitmqctl list_queues
Listing queues ...
1111    0

首先,队列是空的。

# rabbitmqctl list_consumers
Listing consumers ...
1111    <rabbit@★> amq.ctag-ztANwsUVbqTvGhE7rnXh6w false   0       []
1111    <rabbit@★> amq.ctag-w-yJ9Fb8Ksb5HY0QX3iRAg false   0       []
1111    <rabbit@★> amq.ctag-xb6boiuc_Ee1girva-GdkQ false   0       []
1111    <rabbit@★> amq.ctag-jV7AUun3pc-FyFcK9M_Owg false   0       []
1111    <rabbit@★>  amq.ctag-Jdt5oPqW4mtCerlx8dUIOw false   0       []
1111    <rabbit@★> amq.ctag-55NHUikpOIFTxgxOEcipsg false   0       []

コンシューマーは いっぱいいるのにな。じゃあ、こいつらが取り合ってるなんてことがあるのだろうか?

# ps aux | grep rabbit
root     12122  0.0  0.0   4500     0 ?        S    Mar07   0:00 /bin/sh /usr/sbin/rabbitmq-server
root     12129  0.0  0.0  55128     0 ?        S    Mar07   0:00 su rabbitmq -s /bin/sh -c /usr/lib/rabbitmq/bin/rabbitmq-server
rabbitmq 12130  0.0  0.0   4500     8 ?        Ss   Mar07   0:00 sh -c /usr/lib/rabbitmq/bin/rabbitmq-server
rabbitmq 12131  0.0  0.0   4500     0 ?        S    Mar07   0:00 /bin/sh -e /usr/lib/rabbitmq/bin/rabbitmq-server
rabbitmq 12237  0.6  2.6 2712560 27192 ?       Sl   Mar07  27:01 /usr/lib/erlang/erts-8.1/bin/beam.smp -W w -A 64 -P 1048576 -t 5000000 -stbt db -zdbbl 32000 -K true -B i -- -root /usr/lib/erlang -progname erl -- -home /var/lib/rabbitmq -- -pa /usr/lib/rabbitmq/lib/rabbitmq_server-3.6.6/ebin -noshell -noinput -s rabbit boot -sname rabbit@★ -boot start_sasl -kernel inet_default_connect_options [{nodelay,true}] -sasl errlog_type error -sasl sasl_error_logger false -rabbit error_logger {file,"/var/log/rabbitmq/rabbit@★.log"} -rabbit sasl_error_logger {file,"/var/log/rabbitmq/rabbit@★-sasl.log"} -rabbit enabled_plugins_file "/etc/rabbitmq/enabled_plugins" -rabbit plugins_dir "/usr/lib/rabbitmq/lib/rabbitmq_server-3.6.6/plugins" -rabbit plugins_expand_dir "/var/lib/rabbitmq/mnesia/rabbit@★-plugins-expand" -os_mon start_cpu_sup false -os_mon start_disksup false -os_mon start_memsup false -mnesia dir "/var/lib/rabbitmq/mnesia/rabbit@★" -kernel inet_dist_listen_min 25672 -kernel inet_dist_listen_max 25672
rabbitmq 12333  0.0  0.0   4356    72 ?        Ss   Mar07   0:18 erl_child_setup 1024
rabbitmq 12343  0.0  0.0  13676    36 ?        Ss   Mar07   0:00 inet_gethost 4
rabbitmq 12344  0.0  0.0  15796   228 ?        S    Mar07   0:01 inet_gethost 4
root     27067  0.0  0.0  12936   988 pts/4    S+   08:07   0:00 grep --color=auto rabbit
rabbitmq 27670  0.0  0.0  13848   252 ?        S    Mar06   0:11 /usr/lib/erlang/erts-8.1/bin/epmd -daemon

嗯。

难道。

/home/★user/shogi/php_log# ls -an
total 8
drwxr-xr-x  2 0 0 4096 Mar  8 09:50 .
drwxr-xrwx 20 0 0 4096 Mar 10 06:04 ..
-rw-r--r--  1 0 0    0 Mar 10 08:05 dequeueBroker.err.log
-rw-r--r--  1 0 0    0 Mar 10 08:05 dequeueBroker.out.log

更新时间已更新。
但是内容是空的。

# ps aux | grep dequeueBroker.php
root     20224  0.0  1.3 232308 14060 ?        S    04:05   0:01 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     21242  0.0  1.3 232308 14060 ?        S    05:05   0:00 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     22336  0.0  1.3 232308 14056 ?        S    06:05   0:00 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     25095  0.0  1.3 232308 14044 ?        S    07:05   0:00 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     26770  0.0  1.3 232308 14040 ?        S    08:05   0:00 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     27318  0.0  0.0  13068   988 pts/4    S+   08:19   0:00 grep --color=auto dequeueBroker.php

你看看有没有在5分钟内增加了dequeueBroker.php。

こんなことができるのは Cron だろう。

# sudo crontab -e
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command
#0-59/30 * * * * /bin/bash /home/★user/shogi/bash_service/tamesi12.sh
5 * * * * /bin/bash /home/★user/shogi/bash_service/kicker_dequeueBroker.sh

嘿,看,有了。我来注释一下。
也一起结束掉这个进程。

kill `ps aux | grep dequeueBroker.php | sed 's/[\t ]\+/\t/g' | cut -f 2`
# ps aux | grep dequeueBroker.php
root     27385  0.0  0.0  12936   988 pts/4    S+   08:23   0:00 grep --color=auto dequeueBroker.php

再来一次。这次是在第14次尝试中使用Enqueue。

http://★.★.★.★/tamesi14.php

あっ、ためしさん14にバグがある。直そう。
と思ったら バグは無かったので 直さなかった。

# rabbitmqctl list_queues
Listing queues ...
1111    0

エンキューされてない。

chmod 666 dequeueBroker.err.log
chmod 666 dequeueBroker.out.log

重新启动RabbitMQ

没有其他选择。那就试着重新启动RabbitMQ。

# rabbitmqctl stop
Stopping and halting node 'rabbit@★' ...
[1]+  Done                    ./tamesi29a9_main.exe  (wd: /home/★user/shogi/cpp_service)
(wd now: /var/www/html)
# rabbitmq-server

              RabbitMQ 3.6.6. Copyright (C) 2007-2016 Pivotal Software, Inc.
  ##  ##      Licensed under the MPL.  See http://www.rabbitmq.com/
  ##  ##
  ##########  Logs: /var/log/rabbitmq/rabbit@tk2-217-18401.log
  ######  ##        /var/log/rabbitmq/rabbit@tk2-217-18401-sasl.log
  ##########
              Starting broker...
 completed with 0 plugins.

按住[Ctrl]键再按[Z]键

# bg 1

你想看看日志吗?

cd /var/log/rabbitmq

嗯。

之前PHP的Enqueue是能够工作的,但今天似乎不行了。

暂时尝试敲击示例2、3和5。

# rabbitmqctl list_queues
Listing queues ...
1111    1

嗯?

那么,让我们试着敲击一下Tamashii。

不变的。

那么,我们来打开“ためしさん3”试一试。

不变的。

那么,让我们试着敲击一下Tameshi先生5。

# rabbitmqctl list_queues
Listing queues ...
1111    2

那么,让我们来试试打击Tameshi San 14。

# rabbitmqctl list_queues
Listing queues ...
1111    3

那么,RabbitMQ没有运行吗?现在它在后台运行,但那就试试退出登录吧。

# logout
$ logout

重新登录。

# sudo su -
※パスワードを入れる
# jobs

空无一物。

# ps aux | grep rabbit
rabbitmq 27670  0.0  0.0  13848   252 ?        S    Mar06   0:12 /usr/lib/erlang/erts-8.1/bin/epmd -daemon
root     27924  0.0  0.0   4500   660 ?        S    08:33   0:00 /bin/sh /usr/sbin/rabbitmq-server
root     27931  0.0  0.1  55128  1820 ?        S    08:33   0:00 su rabbitmq -s /bin/sh -c /usr/lib/rabbitmq/bin/rabbitmq-server
rabbitmq 27932  0.0  0.0   4500   656 ?        Ss   08:33   0:00 sh -c /usr/lib/rabbitmq/bin/rabbitmq-server
rabbitmq 27933  0.0  0.0   4500   756 ?        S    08:33   0:00 /bin/sh -e /usr/lib/rabbitmq/bin/rabbitmq-server
rabbitmq 28038  0.9  5.8 2698680 59836 ?       Sl   08:33   0:09 /usr/lib/erlang/erts-8.1/bin/beam.smp -W w -A 64 -P 1048576 -t 5000000 -stbt db -zdbbl 32000 -K true -B i -- -root /usr/lib/erlang -progname erl -- -home /var/lib/rabbitmq -- -pa /usr/lib/rabbitmq/lib/rabbitmq_server-3.6.6/ebin -noshell -noinput -s rabbit boot -sname rabbit@tk2-217-18401 -boot start_sasl -kernel inet_default_connect_options [{nodelay,true}] -sasl errlog_type error -sasl sasl_error_logger false -rabbit error_logger {file,"/var/log/rabbitmq/rabbit@tk2-217-18401.log"} -rabbit sasl_error_logger {file,"/var/log/rabbitmq/rabbit@tk2-217-18401-sasl.log"} -rabbit enabled_plugins_file "/etc/rabbitmq/enabled_plugins" -rabbit plugins_dir "/usr/lib/rabbitmq/lib/rabbitmq_server-3.6.6/plugins" -rabbit plugins_expand_dir "/var/lib/rabbitmq/mnesia/rabbit@tk2-217-18401-plugins-expand" -os_mon start_cpu_sup false -os_mon start_disksup false -os_mon start_memsup false -mnesia dir "/var/lib/rabbitmq/mnesia/rabbit@tk2-217-18401" -kernel inet_dist_listen_min 25672 -kernel inet_dist_listen_max 25672
rabbitmq 28135  0.0  0.0   4356   524 ?        Ss   08:33   0:00 erl_child_setup 1024
rabbitmq 28145  0.0  0.0  13676   468 ?        Ss   08:33   0:00 inet_gethost 4
rabbitmq 28146  0.0  0.0  15796   608 ?        S    08:33   0:00 inet_gethost 4
root     29169  0.0  0.0  12936   988 pts/0    S+   08:49   0:00 grep --color=auto rabbit

嗯,看了也不懂。

我试着访问了示例先生的账户五次。

# rabbitmqctl list_queues
Listing queues ...
1111    5

那么,虽然看不见工作,但它在运行吗?
如果是这样,那么 .cpp 文件呢?

# cd /home/★user/shogi/cpp_service
# ./tamesi29a9_main.exe
declared queue 1111

什么都没变。虽然确实可能把队列的名称取出来了,但要取得消息怎么办呢?

你在写吗?

「AMQP-CPP」(CopernicaMarketingSoftware/AMQP-CPP)
https://github.com/CopernicaMarketingSoftware/AMQP-CPP

29A-10, Mr. Tameshi.

tamesi29a10_main.cpp的中文重新表述:tamesi29a10主要程序.cpp。

#include <ev.h>
#include <amqpcpp.h>
#include <amqpcpp/libev.h>

int main()
{
    // access to the event loop
    auto *loop = EV_DEFAULT;

    // handler for libev (so we don't have to implement AMQP::TcpHandler!)
    AMQP::LibEvHandler handler(loop);

    // make a connection
    AMQP::TcpConnection connection(&handler, AMQP::Address("amqp://localhost/"));

    // we need a channel too
    AMQP::TcpChannel channel(&connection);

//    // create a temporary queue
//    channel.declareQueue(AMQP::exclusive).onSuccess([&connection](const std::string &name, uint32_t messagecount, uint32_t consumercount) {
//
//        // report the name of the temporary queue
//        std::cout << "declared queue " << name << std::endl;
//
//        // now we can close the connection
//        connection.close();
//    });
    // create a custom callback
    auto callback = [](const std::string &name, int msgcount, int consumercount) {

        // @todo add your own implementation

        // report the name of the temporary queue
        std::cout << "(^q^)declared queue " << name << std::endl;

        // now we can close the connection
        // connection.close();
    };
    // declare the queue, and install the callback that is called on success
    channel.declareQueue("1111").onSuccess(callback);

    // callback function that is called when the consume operation starts
    auto startCb = [](const std::string &consumertag) {

        std::cout << "(^q^)consume operation started" << std::endl;
    };

    // callback function that is called when the consume operation failed
    auto errorCb = [](const char *message) {

        std::cout << "(^q^)consume operation failed" << std::endl;
    }

    // callback operation when a message was received
    auto messageCb = [&channel](const AMQP::Message &message, uint64_t deliveryTag, bool redelivered) {

        std::cout << "(^q^)message received" << std::endl;

        // acknowledge the message
        channel.ack(deliveryTag);
    }

    // start consuming from the queue, and install the callbacks
    channel.consume("1111")
        .onReceived(messageCb)
        .onSuccess(startCb)
        .onError(errorCb);

    // run the loop
    ev_run(loop, 0);

    // done
    return 0;
}

这是否合适呢?

# g++ -std=c++11 tamesi29a10_main.cpp -lev -lamqpcpp -pthread -o tamesi29a10_main.exe
tamesi29a10_main.cpp: In function ‘int main()’:
tamesi29a10_main.cpp:55:5: error: expected ‘,’ or ‘;’ before ‘auto’
     auto messageCb = [&channel](const AMQP::Message &message, uint64_t deliveryTag, bool redelivered) {
     ^
tamesi29a10_main.cpp:65:21: error: ‘messageCb’ was not declared in this scope
         .onReceived(messageCb)
                     ^

可能样本程序有没有漏了分号。

「示例程序中缺少分号。」(CopernicaMarketingSoftware/AMQP-CPP)
https://github.com/CopernicaMarketingSoftware/AMQP-CPP/issues/119

我已经送了。

好的,编译也通过了。

# ./tamesi29a10_main.exe
(^q^)declared queue 1111
(^q^)consume operation started
(^q^)message received
(^q^)message received
(^q^)message received
(^q^)message received
(^q^)message received

通过按下[Ctrl]+[C]组合键退出。

确实,出现了五只猫,这是正确的。但是,请帮忙捡起这个信息。

# rabbitmqctl list_queues
Listing queues ...
1111    0

已经空了。

这样改造之后怎样?

tamesi29a10_main.cpp的汉语翻译不可用。

#include <ev.h>
#include <amqpcpp.h>
#include <amqpcpp/libev.h>

int main()
{
    // access to the event loop
    auto *loop = EV_DEFAULT;

    // handler for libev (so we don't have to implement AMQP::TcpHandler!)
    AMQP::LibEvHandler handler(loop);

    // make a connection
    AMQP::TcpConnection connection(&handler, AMQP::Address("amqp://localhost/"));

    // we need a channel too
    AMQP::TcpChannel channel(&connection);

//    // create a temporary queue
//    channel.declareQueue(AMQP::exclusive).onSuccess([&connection](const std::string &name, uint32_t messagecount, uint32_t consumercount) {
//
//        // report the name of the temporary queue
//        std::cout << "declared queue " << name << std::endl;
//
//        // now we can close the connection
//        connection.close();
//    });
    // create a custom callback
    auto callback = [](const std::string &name, int msgcount, int consumercount) {

        // @todo add your own implementation

        // report the name of the temporary queue
        std::cout << "(^q^)declared queue " << name << std::endl;

        // now we can close the connection
        // connection.close();
    };
    // declare the queue, and install the callback that is called on success
    channel.declareQueue("1111").onSuccess(callback);

    // callback function that is called when the consume operation starts
    auto startCb = [](const std::string &consumertag) {

        // std::cout << "(^q^)consume operation started" << std::endl;
        std::cout << "(^q^)consume operation started [" << consumertag << "]" << std::endl;
    };

    // callback function that is called when the consume operation failed
    auto errorCb = [](const char *message) {

        // std::cout << "(^q^)consume operation failed" << std::endl;
        std::cout << "(^q^)consume operation failed [" << &message << "]" << std::endl;
    };

    // callback operation when a message was received
    auto messageCb = [&channel](const AMQP::Message &message, uint64_t deliveryTag, bool redelivered) {

        // std::cout << "(^q^)message received" << std::endl;
        std::cout << "(^q^)message received [" << &message << "]" << std::endl;

        // acknowledge the message
        channel.ack(deliveryTag);
    };

    // start consuming from the queue, and install the callbacks
    channel.consume("1111")
        .onReceived(messageCb)
        .onSuccess(startCb)
        .onError(errorCb);

    // run the loop
    ev_run(loop, 0);

    // done
    return 0;
}

ためしさん14 を使って、メッセージをエンキューしておく。

# rabbitmqctl list_queues
Listing queues ...
1111    6
# ./tamesi29a10_main.exe
(^q^)declared queue 1111
(^q^)consume operation started [amq.ctag-f9-pEPIwqTgyCHuhYm4wiA]
(^q^)message received [0xc87ea0]
(^q^)message received [0xc87ea0]
(^q^)message received [0xc87ea0]
(^q^)message received [0xc87ea0]
(^q^)message received [0xc87ea0]
(^q^)message received [0xc87ea0]

ぬぎぎ! メッセージだけ なんとかなればいい。しかし異なるメッセージを送ったのに全部 0xc87ea0 とはどういうことだ。
何かのメモリ・アドレスか?

AMQP::消息

AMQP消息不是字符串吗?

/usr/include/amqpcpp.h 可以被中文化为 “/usr/include/amqpcpp.h”。

/usr ディレクトリの下を探していると、
/usr/include/amqpcpp.h というファイルがあった。

看起来目录中还有一个头文件。

/usr/include/amqpcpp# ls
address.h     channel.h            copiedbuffer.h          deferredget.h    exception.h     libevent.h  monitor.h            stringfield.h    watchable.h
array.h       channelimpl.h        decimalfield.h          deferred.h       exchangetype.h  libev.h     numericfield.h       table.h
booleanset.h  classes.h            deferredcancel.h        deferredqueue.h  field.h         libuv.h     outbuffer.h          tcpchannel.h
buffer.h      connection.h         deferredconsumerbase.h  endian.h         fieldproxy.h    login.h     protocolexception.h  tcpconnection.h
bytebuffer.h  connectionhandler.h  deferredconsumer.h      entityimpl.h     flags.h         message.h   receivedframe.h      tcpdefines.h
callbacks.h   connectionimpl.h     deferreddelete.h        envelope.h       frame.h         metadata.h  stack_ptr.h          tcphandler.h

message.h というファイルが気になる。
著作表記を見ると、Copernica BV というところが作っているみたいだ。

const std::string &exchange() const
と、
const std::string &routingkey() const

我只能访问这么多东西。

        std::cout << "(^q^)message received exchange=[" << &message.exchange() << "] routingkey=[" << &message.routingkey() << "]" << std::endl;
# ./tamesi29a10_main.exe
(^q^)declared queue 1111
(^q^)consume operation started [amq.ctag-baH5AqjR3dg-pmSMobsugg]
(^q^)message received exchange=[0xcdf0d8] routingkey=[0xcdf0f8]
(^q^)message received exchange=[0xcdf0d8] routingkey=[0xcdf0f8]

这是不一样的吗?

Message クラスは Envelope クラスを継承しているので、Envelope も見てみよう。

    /**
     *  Access to the full message data
     *  @return buffer
     */
    const char *body() const
    {
        return _body;
    }

こんなのがある。

        std::cout << "(^q^)message received body=[" << message.body() << "]" << std::endl;

还有,头上的“&”符号是不是不需要呢?

# ./tamesi29a10_main.exe
(^q^)declared queue 1111
(^q^)consume operation started [amq.ctag-FYCpWCbtuoRvXl4XoRReHA]
(^q^)message received body=[ClickSitanDaze▒YCpWCbtuoRvXl4XoRReHA]
(^q^)message received body=[Dragon▒]
(^q^)message received body=[Tiger▒]

よしきた道中。なんか 謎の文字が後ろについているが……。

    /**
     *  Size of the body
     *  @return uint64_t
     */
    uint64_t bodySize() const
    {
        return _bodySize;
    }

用中文来说,就是字符串的大小,明白吗?要怎么使用呢?

「convert a char* to std::string」(stack overflow)
http://stackoverflow.com/questions/1195675/convert-a-char-to-stdstring

        std::string myString(message.body(), message.bodySize());
        std::cout << "(^q^)message received myString=[" << myString << "]" << std::endl;
# ./tamesi29a10_main.exe
(^q^)declared queue 1111
(^q^)consume operation started [amq.ctag-M2QG32xTPEkdEn-Swcl2Sw]
(^q^)message received myString=[Tiger]

可以了。

先不管它,等一下再总结成另一篇文章。

广告
将在 10 秒后关闭
bannerAds