情報システム概論 I
筑波大学 システム情報工学研究科
コンピュータサイエンス専攻, 電子・情報工学系
新城 靖
<yas@is.tsukuba.ac.jp>
このページは、次の URL にあります。
http://www.coins.tsukuba.ac.jp/~yas/coins/compsys1-2007/2008-02-21
あるいは、次のページから手繰っていくこともできます。
http://www.coins.tsukuba.ac.jp/~yas/
http://www.cs.tsukuba.ac.jp/~yas/
Unix Super Text 上巻「第28章 World Wide Web」、「第36章 Perl入門 」、下巻「第58章 システムコールとライブラリ」参考。
Gateway とは、WWW で使われている HTTP というプロトコルへの入り口という 意味。 プログラムの実行結果は、普通は、HTML にすることが多いが、普通のテキス トであることもイメージであることもある。
図? CGIの仕組み
CGI の利用例
CGI になるファイル名のパタン
設定によっては、これ以外のものも可能である。
% cc -o counter.cgi counter.c
http://www.coins.tsukuba.ac.jp/~yas/coins/compsys1-2007/2008-02-21/counter.cgi
1: /*
2: counter.c -- Simple access counter in C.
3: CAUTION: This program does not work when multiple clients access it at the same time.
4: */
5: #include <stdio.h> /* printf() */
6: #include <stdlib.h> /* exit() */
7:
8: static void load_counter();
9: static void save_counter();
10: static void print_error();
11:
12: int counter = 0;
13:
14: main()
15: {
16: load_counter();
17: printf("Content-Type: text/plain\n");
18: printf("\n");
19: printf("%d\n", counter );
20: counter = counter + 1 ;
21: save_counter();
22: }
23:
24: static void print_error()
25: {
26: printf("Content-Type: text/plain\n");
27: printf("\n");
28: printf("Sorry, internal error.\n");
29: exit(1);
30: }
31:
32: #include <unistd.h> /* pread() */
33: #include <fcntl.h> /* open() */
34:
35: #define FILENAME "counter.data"
36:
37: static void load_counter()
38: {
39: int fd ;
40: fd = open(FILENAME,O_RDWR|O_CREAT,0666);
41: if( fd < 0 )
42: print_error();
43: read( fd, &counter, sizeof(counter) );
44: close( fd );
45: }
46:
47: static void save_counter()
48: {
49: int fd ;
50: fd = open(FILENAME,O_RDWR|O_CREAT,0666);
51: if( fd < 0 )
52: print_error();
53: write( fd, &counter, sizeof(counter) );
54: close( fd );
55: }
load_counter() と save_counter() は、
先週のcounter-file.c
とほとんど同じ。
% cd ~yas/public_html/coins/compsys1-2007/2008-02-21
% ./counter.cgi
Content-Type: text/plain
16
%
% ./counter.cgi
Content-Type: text/plain
17
%
CGI のプログラムは、画面(標準出力)に結果を表示する。一行目は、
Content-Type: が多い。HTML を出力するなら、Content-Type: text/html と
する。
空行以降に、ブラウザの画面に表示されるべきデータを置く。
(空行以前は、HTTP のヘッダに関係している)。
WWW サーバ Apache は、1行目を解析(parse)して、クライアントに対して返 すべき HTTP のヘッダを作成する。それ以外のヘッダも生成して送る。
% telnet www.coins.tsukuba.ac.jp 80
Trying 130.158.86.207...
Connected to orchid-nwd.coins.tsukuba.ac.jp.
Escape character is '^]'.
GET /~yas/coins/compsys1-2007/2008-02-21/counter.cgi HTTP/1.0
HTTP/1.1 200 OK
Date: Wed, 20 Feb 2008 10:06:36 GMT
Server: Apache/2.0.59 (Unix) PHP/4.4.7
Connection: close
Content-Type: text/plain
18
Connection closed by foreign host.
%
TCP/IP のプロトコルに従って通信メッセージを作成するプログラムは、オペレー ティング・システムのカーネルに含まれている。TCP/IPで通信を行うプロセス は、システム・コールを通じて、カーネル内のプログラムを使う。
オペレーティング・システムのカーネルは、デバイス・ドライバを通じてネッ トワーク通信を行うハードウェア(ネットワーク・インタフェース・カード) を操作して通信メッセージを送受信する。
1: #!/usr/bin/ruby
2: # counter-ruby.cgi -- Simple access counter in Ruby.
3: # CAUTION: This program does not work when multiple clients access it at the same time.
4:
5: def main()
6: load_counter()
7: printf("Content-Type: text/plain\n")
8: printf("\n")
9: printf("%d\n", @counter )
10: @counter = @counter + 1
11: save_counter()
12: end
13:
14: def print_error()
15: printf("Content-Type: text/plain\n");
16: printf("\n");
17: printf("Sorry, internal error.");
18: exit( 1 )
19: end
20:
21: FILENAME="counter.data"
22:
23: def load_counter()
24: begin
25: fo = File.open(FILENAME,"r+",0666)
26: buf1 = fo.read( 4 )
27: buf2 = buf1.unpack("l")
28: @counter = buf2[0]
29: rescue StandardError => e
30: print_error()
31: end
32: end
33:
34: def save_counter()
35: begin
36: fo = File.open(FILENAME,"r+",0666)
37: buf2 = [@counter]
38: buf1 = buf2.pack("l")
39: fo.write( buf1 )
40: fo.close()
41: rescue StandardError => e
42: print_error()
43: end
44: end
45:
46: main()
pack(), unpack() は、C 言語版の整数の読み書きに合わせるために難しくなっている。
Ruby だけなら、文字列にして読み書きする方が簡単である。
スクリプト言語を利用する利点。
キーワード
並行プログラミングは、非常に難易度が高い。「オペレーティングシステム I」、「システムプログラム」、「データベース概論II (トランザクション)」 などで学ぶ。
Unix Super Text 下巻「第69章 プロセス間通信と同期」参考。
Unix Super Text 上巻「第29章 安全な通信」参考。
01で符合化できれば、あとは、なんとかなる。 (01で符合化できないものは、どうしようもない。)
情報システム流の考え方。とにかく速くする、大量のものを扱えるようにすれ ば、すれば質も自ずから変る。相転移が生じる。(<−>知識・知能を直接扱おうとがんばる。)
2年生の科目「データ構造とアルゴリズム」、「数値計算法」、3年生の科目 「計算機アーキテクチャ」、「並列処理アーキテクチャ」などでは、高速化の ための手法を学ぶ。