Philosophers” を Go で解く(CLI)

目次

  1. “Philosophers” を Go で解く(問題編)
  2. “Philosophers” を Go で解く(CLI) ← イマココ

コマンドライン・インタフェース

実際に問題を解く前にコマンドライン・インタフェースを整えておく。 CLI には spf13/cobra パッケージを使用する。 詳細については,ちょっと古い内容だが「Cobra でテストしやすい CLI を構成する」を参考にどうぞ。 この記事では結果だけ示す。

$ go run github.com/spiegel-im-spiegel/philo -h
Simulation for philosophers' problem.

Usage:
  philo [flags]
  philo [command]

Available Commands:
  help        Help about any command
  version     print the version number

Flags:
      --debug   for debug
  -h, --help    help for philo

Use "philo [command] --help" for more information about a command.

なおリポジトリは以下にある。

パラメータの定義

問題で指示されているパラメータを今一度挙げる。

シンボル 説明
NumPhilos int 哲学者およびフォークの数($\ge 2$)
TimeDie int 餓死するまでの時間($\gt 0\,\mathrm{ms}$)
TimeEat int 「食べる」時間($\gt 0\,\mathrm{ms}$)
TimeSleep int 「眠る」時間($\gt 0\,\mathrm{ms}$)
TimeThink int 「考える」時間($\ge 0\,\mathrm{ms}$)
NumEat int 各哲学者が最低限食事を行う回数($\ge 0$)

これをコマンドラインから設定できるようにする。 先に指定したパラメータを格納する型を定義しよう。

// Params holds tunable configuration values for a Dining Philosophers simulation.
type Params struct {
	NumPhilos int // Total number of philosophers (and forks). Must be >= 2.
	TimeDie   int // Maximum time in milliseconds a philosopher can go without eating before being considered dead.
	TimeEat   int // Time in milliseconds a philosopher spends eating (holding both forks).
	TimeSleep int // Time in milliseconds a philosopher sleeps after eating.
	TimeThink int // Optional target time in milliseconds a philosopher thinks after sleeping.
	NumEat    int // Optional target number of meals each philosopher must consume; if == 0 the simulation runs until a death occurs.
}

// String returns a human-readable summary of the Params configuration,
// listing the number of philosophers, each lifecycle duration (die, eat,
// sleep, think in milliseconds), and the required number of meals.
// Example:
//
//	Philosophers: 5, Die: 800ms, Eat: 200ms, Sleep: 200ms, Think: 0ms, Eat Count: 3
func (p *Params) String() string {
	return fmt.Sprintf("Philosophers: %d, Die: %dms, Eat: %dms, Sleep: %dms, Think: %dms, Eat Count: %d",
		p.NumPhilos, p.TimeDie, p.TimeEat, p.TimeSleep, p.TimeThink, p.NumEat)
}

いやぁ,今どきは AI がコメントを書いてくれるから楽でいいね(笑) これを前節の CLI と接続する。 こんな感じ。

$ go run github.com/spiegel-im-spiegel/philo -h
Simulation for philosophers' problem.

Usage:
  philo [flags]
  philo [command]

Available Commands:
  help        Help about any command
  version     print the version number

Flags:
      --debug              for debug
  -h, --help               help for philo
  -m, --must-eat int       number of times each philosopher must eat (default 3)
  -p, --philosophers int   number of philosophers (default 5)
  -d, --to-die int         time to die (ms) (default 200)
  -e, --to-eat int         time to eat (ms) (default 20)
  -s, --to-sleep int       time to sleep (ms) (default 80)
  -t, --to-think int       time to think (ms) (default 80)

Use "philo [command] --help" for more information about a command.

デフォルト値は適当で。 今のところはパラメータの内容を表示するだけの処理にしておく。

$ go run github.com/spiegel-im-spiegel/philo -h
Philosophers: 5, Die: 200ms, Eat: 20ms, Sleep: 80ms, Think: 80ms, Eat Count: 3

CLI 部分はここまで。 これで本来のシミュレーション・ロジックを組み込む準備は完了である。 次回から実際に問題を解いていく。

参考図書

photo
Go言語で学ぶ並行プログラミング 他言語にも適用できる原則とベストプラクティス impress top gearシリーズ
James Cutajar (著), 柴田 芳樹 (著)
インプレス 2024-12-04 (Release 2024-12-04)
Kindle版
B0DNYMMBBQ (ASIN)
評価     

読書会のために購入。インプレス社の本は Kindle 版より版元で PDF 版を買うのがオススメ。「並行処理」について原理的な解説から丁寧に書かれている。 Go で解説されているが Go 以外の言語でも応用できる。

reviewed by Spiegel on 2025-01-25 (powered by PA-APIv5)

photo
Go言語による並行処理
Katherine Cox-Buday (著), 山口 能迪 (翻訳)
オライリージャパン 2018-10-26
単行本(ソフトカバー)
4873118468 (ASIN), 9784873118468 (EAN), 4873118468 (ISBN)
評価     

Eブック版もある。感想はこちら。 Go 言語で並行処理を書くならこの本は必読書になるだろう。

reviewed by Spiegel on 2020-01-13 (powered by PA-APIv5)

photo
プログラミング言語Go (ADDISON-WESLEY PROFESSIONAL COMPUTING SERIES)
Alan A.A. Donovan (著), Brian W. Kernighan (著), 柴田 芳樹 (翻訳)
丸善出版 2016-06-20
単行本(ソフトカバー)
4621300253 (ASIN), 9784621300251 (EAN), 4621300253 (ISBN)
評価     

著者のひとりは(あの「バイブル」とも呼ばれる)通称 “K&R” の K のほうである。この本は Go 言語の教科書と言ってもいいだろう。と思ったら絶版状態らしい(2025-01 現在)。復刊を望む!

reviewed by Spiegel on 2016-07-13 (powered by PA-APIv5)

photo
Go言語 100Tips ありがちなミスを把握し、実装を最適化する impress top gearシリーズ
Teiva Harsanyi (著), 柴田 芳樹 (著)
インプレス 2023-08-18 (Release 2023-08-18)
Kindle版
B0CFL1DK8Q (ASIN)
評価     

版元で PDF 版を購入可能。事実上の Effective Go とも言える充実の内容。オリジナルは敢えてタイトルに “tips” という単語を入れるのを避けたのに邦題が「100 Tips」とかなっていて,原作者がお怒りとの噂(あくまで噂)

reviewed by Spiegel on 2023-08-18 (powered by PA-APIv5)

photo
Goならわかるシステムプログラミング 第2版
渋川よしき (著), ごっちん (イラスト)
ラムダノート 2022-03-23
単行本(ソフトカバー)
4908686122 (ASIN), 9784908686122 (EAN), 4908686122 (ISBN)
評価     

第1版はとてもためになる内容だったので第2版も PDF 版で確保しておく。

reviewed by Spiegel on 2022-10-08 (powered by PA-APIv5)