プリンキピア

第4章アルゴリズムの言語による実現:低水準プログラミング言語

Problem Set A問題集A

Exercises 1–5練習問題 1–5
1. For each of the following programming languages: investigate the language. Is the language low-level, middle-level, or high-level? Which of the paradigms covered in this section apply to the language (imperative, declarative, functional, structured, procedural, object-oriented)? Cite your sources.
1. 次の各プログラミング言語を調べなさい。その言語は低水準、中水準、高水準のどれか。この節で扱ったパラダイム(命令型、宣言型、関数型、構造化、手続き型、オブジェクト指向)のうち、どれが当てはまるか。出典を示しなさい。
A. Kotlin B. Lisp C. PASCAL
A. KotlinB. LispC. PASCAL
2. Explain how levels of abstraction affect speed of development and speed of execution.
2. 抽象化の水準が、開発速度と実行速度にどう影響するかを説明しなさい。
3. Explain how a compiler assists in providing abstractions for high-level languages.
3. コンパイラーが、高水準言語で抽象化を提供することをどう助けるか、説明しなさい。
4. Write a module titled “triangle operations” that has two functions: one to compute the area of a triangle given a base and height and one to compute the perimeter of the triangle given three sides. Then, write a main function that iterates three times, increasing each variable by one, and then calls each function.
英語のヒント
4. 「triangle operations」というモジュールに、2つの関数を書きなさい。1つは底辺と高さから三角形の面積を求め、もう1つは3辺から周の長さを求める関数とする。次に、各変数を1ずつ増やしてから各関数を呼ぶ操作を、3回繰り返すmain関数を書きなさい。
5. OpenMP provides the omp_get_thread_num() function in the header file omp.h. To get the number of total running threads in the parallel block, you can use function omp_get_num_threads. How can you modify this program to ensure that only one thread executes the “Greetings from process” printf statement?
5. OpenMPは、ヘッダーファイルomp.hにomp_get_thread_num()関数を備える。並列ブロック内で実行中のスレッドの総数を得るには、omp_get_num_threads関数を使える。「Greetings from process」のprintf文を、1つのスレッドだけが実行するよう、このプログラムをどう変更できるか。
#include <stdio.h>
#include <omp.h>

int main() {
  #pragma omp parallel num_threads(3)
  {
  int id = omp_get_thread_num();
  int data = id;
  int total = omp_get_num_threads();
  printf("Greetings from process %d out of %d with Data %d\n", id, total, data);
  }
  printf("parallel for ends.\n");
  return 0;
}
Exercises 6練習問題 6
6. Read the documentation on device drivers (https://openstax.org/r/76PSA1) and implement the various examples provided.
6. デバイスドライバーに関する文書(https://openstax.org/r/76PSA1)を読み、そこにあるさまざまな例を実装しなさい。

Problem Set B問題集B

Exercises 1–5練習問題 1–5
1. Rosetta Code (https://openstax.org/r/76RosettaCode) is an archive of computing tasks and source code written in many different languages that accomplish the same task. Explore the page about converting numbers into Roman numerals (https://openstax.org/r//76Rosetta) and study the source code written in 8080 assembly (low-level), C (middle-level), and JavaScript (high-level). Compare and contrast the following aspects of the code:
英語のヒント
1. Rosetta Code(https://openstax.org/r/76RosettaCode)は、計算の課題と、同じ課題を達成するために多くの異なる言語で書いたソースコードのアーカイブである。数値をローマ数字へ変換するページ(https://openstax.org/r//76Rosetta)を調べ、8080アセンブリ言語(低水準)、C(中水準)、JavaScript(高水準)のソースコードを学びなさい。コードの次の側面について、共通点と相違点を比較しなさい。
◦ Length
◦ 長さ
◦ Readability: how easy is it to understand how the code works?
◦ 可読性:コードの動作をどれほど理解しやすいか。
◦ Level of abstraction
◦ 抽象化の水準
◦ Structured or unstructured
◦ 構造化されているか、されていないか。
2. Research x86, ARM, and PowerPC architectures—specifically, how each of them has different assembly language features and syntax. Then research and explain how high-level languages can be compiled on different computer architectures.
2. x86、ARM、PowerPCのアーキテクチャについて、特に、それぞれのアセンブリ言語の機能と構文がどう異なるかを調べなさい。次に、高水準言語を異なるコンピューター構成上でどのようにコンパイルできるかを調べ、説明しなさい。
3. Provide a real-life example of abstraction and explain how it is similar to abstraction in computing.
3. 現実の生活における抽象化の例を挙げ、計算技術の抽象化とどう似ているかを説明しなさい。
4. Research Java Abstract classes. After researching, provide a detailed usage of abstract classes and explain why abstraction is useful in software development.
4. Javaの抽象クラスを調べなさい。調査後、抽象クラスの詳しい使用例を示し、抽象化がソフトウェア開発に役立つ理由を説明しなさい。
5. Let a and b denote object modules or static libraries in the current directory, and let a->b denote that a depends on b, in the sense that b defines a symbol that is referenced by a. For each of the following scenarios, show the minimal command line (i.e., one with the least number of object file and library arguments) that will allow the static linker to resolve all symbol references:
英語のヒント
5. aとbを、現在のディレクトリーにあるオブジェクトモジュールまたは静的ライブラリーとする。a->bは、aがbに依存すること、すなわちaから参照されるシンボルをbが定義することを表すものとする。次の各場合について、静的リンカーがすべてのシンボル参照を解決できる最小のコマンドラインを示しなさい。最小とは、オブジェクトファイルとライブラリーの引数の個数が最も少ないことである。
a. p.o -> libx.a -> p.o b. p.o -> libx.a -> liby.a and liby.a -> libx.a c. p.o -> libx.a -> liby.a -> libz.a and liby.a -> libx.a -> libz.a
a. p.o -> libx.a -> p.ob. p.o -> libx.a -> liby.a、およびliby.a -> libx.ac. p.o -> libx.a -> liby.a -> libz.a、およびliby.a -> libx.a -> libz.a
Exercises 6–7練習問題 6–7
6. Write a program that utilizes parallel computing, then has a safety-critical section of code that only allows one thread, and then the remainder of the program can use the same number of threads the first section used.
英語のヒント
6. 並列計算を使い、その後、安全性が重要で1つのスレッドしか許さないコード部分を実行し、さらにその後の残りの部分では、最初の部分と同じ数のスレッドを使えるプログラムを書きなさい。
7. Use QEMU (https://openstax.org/r/76QEMU) and gdb (https://openstax.org/r/76gdb) to run the kernel source line-by-line.
7. QEMU(https://openstax.org/r/76QEMU)とgdb(https://openstax.org/r/76gdb)を使い、カーネルのソースを1行ずつ実行しなさい。

Thought Provokers考えてみよう

Exercises 1–5練習問題 1–5
1. You are working on a project as a lead software engineer. Your team is tasked with writing a web application and a sensor that will collect and report the temperature of a room over the course of a week on a single battery charge. Your team is well versed in JavaScript and C. What language would you select to write the web application GUI and the code to operate the sensor? Explain the choice of language while connecting the level of each language.
英語のヒント
1. あなたは、主任ソフトウェア技術者としてプロジェクトに取り組んでいる。チームは、1回の電池充電で1週間にわたり部屋の温度を測定して報告するセンサーと、Webアプリケーションを作る仕事を任されている。チームはJavaScriptとCに精通している。WebアプリケーションのGUIと、センサーを動かすコードを、それぞれどの言語で書くか。各言語の水準に関連付けて、選択理由を説明しなさい。
2. TechWorks decided to implement their prosthetic control software with a low-specification (e.g., reduced instruction set) CPU and the C programming language. Alternatively, they could have used the high-level language Java, and a more powerful CPU that consumes more energy. How does this design decision impact the user’s experience?
英語のヒント
2. TechWorksは、低スペック(たとえば縮小命令セット)のCPUとCプログラミング言語で、義肢の制御ソフトウェアを実装することにした。代わりに、高水準言語のJavaと、より多くのエネルギーを使う高性能なCPUを使うこともできた。この設計判断は、ユーザーの体験にどう影響するか。
3. The TIOBE Index (https://openstax.org/r/76TIOBEIndex) is a ranking of the popularity of programming languages. How does the popularity of C, Java, and Python compare? Why do you think that is?
3. TIOBE Index(https://openstax.org/r/76TIOBEIndex)は、プログラミング言語の人気ランキングである。C、Java、Pythonの人気は、比較するとどうか。それはなぜだと思うか。
4. TechWorks is a small, growing startup and has four intern programmers working on their prosthetic product. Suppose that you are their manager. So far, they have made do without using any version control system. One of the intern programmers, Alice, suggests that they should set up and use Git as other programmers do in the company. She estimates that she would need to spend one day setting up the server, and all four intern programmers would need to spend one day to learn how to use Git. Is this a justified use of time? Why or why not?
英語のヒント
4. TechWorksは、成長中の小さなスタートアップで、4人のインターンのプログラマーが義肢製品の開発に取り組んでいる。あなたがその管理者だとしよう。これまでは、バージョン管理システムを使わずに何とかやってきた。インターンの1人、アリスは、社内のほかのプログラマーと同じように、Gitを設定して使うべきだと提案する。彼女の見積りでは、サーバー設定に自分が1日を費やし、4人全員が使い方を学ぶのに1日を費やす必要がある。これは妥当な時間の使い方だろうか。そう考える、または考えない理由は何か。
5. The TechWorks prosthetic CPU has two cores, and the control software is written in C. Currently the code does not use any parallel programming. One of the programmers on the team, Bob, suggests that the software should use threads so that the function that moves the prosthetic, and the function that applies software updates, can run at the same time. What are the advantages and disadvantages of this approach?
5. TechWorksの義肢用CPUには2つのコアがあり、制御ソフトウェアはCで書かれている。現在のコードは、並列プログラミングをまったく使っていない。チームのプログラマーの1人、ボブは、義肢を動かす関数と、ソフトウェア更新を適用する関数を同時に実行できるように、スレッドを使うべきだと提案する。この手法の利点と欠点は何か。
Exercises 6練習問題 6
6. When the Mozilla Foundation created Rust, C was already an established middle-level language. Why was it worth the effort for them to create an entirely new language? You may wish to consult online sources; if you do, cite them.
英語のヒント
6. Mozilla FoundationがRustを作ったとき、Cはすでに確立された中水準言語だった。まったく新しい言語を作るために努力する価値があったのはなぜか。オンライン資料を参照してもよい。参照した場合は、出典を示しなさい。