CPUの割り込みって何?

  • このエントリーをはてなブックマークに追加

CPUの割り込み(interrupt)という言葉がよく出てきますが、割り込みってなんでしょうか。

これはプログラミングの上で必要な知識なのですが、Windowsアプリケーションのようなものを作っている人にはあまりピンとこないと思います。

ある意味組み込み独特の概念かもしれません。

 

基本的にCPUはなにか処理を一生懸命やっているわけですが、その間にも周辺機器が通信をしたりAD変換したりいろいろやっているわけです。

他にもポートを監視したりもあります。

割り込みというものがないとどうなるかというと、CPUのメインルーチンの合間合間に「周辺機器の状態チェック」を入れないといけなくなります。

例えば、通信が終了したら1ms以内になにかしないといけないとしましょう。

そうすると、CPUのメインルーチンの中に1msの間隔で「通信終了チェック」という処理をしないといけなくなります。

考えてみればわかると思いますが、プログラムのあちこちに「通信処理チェック」を書くことになるので、プログラムが恐ろしく汚くなります。

その上、バグの原因にもなります。

さらにプログラムの実行速度が低下します。

周辺機器はたくさんあるので、すべての周辺機器の監視を短い間隔でやろうとするとCPUはほとんど他の作業ができなくなってしまうわけです。

 

そこであるのが「割り込み」です。

割り込みは「あるイベントが発生した時にこのサブルーチンを読んで欲しい」と登録しておく仕組みです。

これがあるとCPUはメインの処理に集中できます。

メインルーチンのあちこちに「周辺機器チェック」を書く必要もなくなります。

例えば、「UART通信が完了したらAというサブルーチンを読んで欲しい」と登録すると、

メインルーチンを処理している途中でUART通信が終了すると、メインルーチンの処理が停止して勝手にサブルーチンに移動します。

そしてサブルーチンが終わると何事もなかったかのようにメインルーチンに戻ってきます。

・CPUが余計なチェックしなくていいので処理が速い

・プログラムが汚くならない

・即座にサブルーチンが呼ばれるので、イベントに即時対応ができる

といいことずくめで、すばらしいのです。

 

このように割り込みというのは偉大な機能なんです。

 

以上、小田切でした。

  • このエントリーをはてなブックマークに追加

Warning: extract() expects parameter 1 to be array, string given in /home/kisukunanato/kairo-consulting.com/public_html/blog/wp-content/themes/xeory_base/lib/functions/bzb-functions.php on line 314

Warning: Illegal string offset 'title' in /home/kisukunanato/kairo-consulting.com/public_html/blog/wp-content/themes/xeory_base/lib/functions/bzb-functions.php on line 318

Warning: Cannot assign an empty string to a string offset in /home/kisukunanato/kairo-consulting.com/public_html/blog/wp-content/themes/xeory_base/lib/functions/bzb-functions.php on line 318

Warning: Illegal string offset 'content' in /home/kisukunanato/kairo-consulting.com/public_html/blog/wp-content/themes/xeory_base/lib/functions/bzb-functions.php on line 320

Warning: Cannot assign an empty string to a string offset in /home/kisukunanato/kairo-consulting.com/public_html/blog/wp-content/themes/xeory_base/lib/functions/bzb-functions.php on line 320

Warning: Illegal string offset 'button_text' in /home/kisukunanato/kairo-consulting.com/public_html/blog/wp-content/themes/xeory_base/lib/functions/bzb-functions.php on line 322

Warning: Cannot assign an empty string to a string offset in /home/kisukunanato/kairo-consulting.com/public_html/blog/wp-content/themes/xeory_base/lib/functions/bzb-functions.php on line 322

Warning: Illegal string offset 'button_url' in /home/kisukunanato/kairo-consulting.com/public_html/blog/wp-content/themes/xeory_base/lib/functions/bzb-functions.php on line 323

Warning: Cannot assign an empty string to a string offset in /home/kisukunanato/kairo-consulting.com/public_html/blog/wp-content/themes/xeory_base/lib/functions/bzb-functions.php on line 323

Warning: Illegal string offset 'button_cvtag' in /home/kisukunanato/kairo-consulting.com/public_html/blog/wp-content/themes/xeory_base/lib/functions/bzb-functions.php on line 324

Warning: Cannot assign an empty string to a string offset in /home/kisukunanato/kairo-consulting.com/public_html/blog/wp-content/themes/xeory_base/lib/functions/bzb-functions.php on line 324

Warning: Illegal string offset 'image' in /home/kisukunanato/kairo-consulting.com/public_html/blog/wp-content/themes/xeory_base/lib/functions/bzb-functions.php on line 332

SNSでもご購読できます。