2018年05月11日 情報科学類 コンピュータリテラシ 筑波大学 システム情報系 新城 靖 <yas@cs.tsukuba.ac.jp>
このページは、次の URL にあります。
http://www.coins.tsukuba.ac.jp/~yas/coins/literacy-2018/2018-05-11
あるいは、次のページから手繰っていくこともできます。
http://www.coins.tsukuba.ac.jp/~yas/
http://www.cs.tsukuba.ac.jp/~yas/
「端末」を複数開くと、「シェル」のプロセスが2つ。 プログラムとしては、同じ bash だが、プロセスとしては別物になる。
その昔は、1度に1個のプログラムだけしかメモリに入れられなかった。 (プログラムとプロセスの違いが希薄。)
プログラムとプロセス
$ ls
プロセスには、ID (identifier)と属性がある。 psコマンド は、プロセスの一覧と属性を表示するコマンドである。
$ ps
PID TTY TIME CMD
912 ttys000 0:00.03 -bash
947 ttys001 0:00.02 -bash
988 ttys001 0:00.04 ssh www.coins.tsukuba.ac.jp
1027 ttys002 0:00.01 -bash
1070 ttys002 0:00.09 /Applications/MacPorts/Emacs.app/Contents/MacOS/Emacs
$
1行が1プロセスに対応。左から次のような意味を持つ。
/dev/ttys000
は
ttys000
になる。
TIME
CMD
(または COMMAND
)
$ tty
/dev/ttys000
$
$ ps x
PID TT STAT TIME COMMAND
772 ?? Ss 0:00.14 /sbin/launchd
794 ?? S 0:00.28 /usr/libexec/UserEventAgent (Aqua)
795 ?? S 0:01.41 /usr/sbin/distnoted agent
799 ?? S 0:00.30 /usr/sbin/cfprefsd agent
803 ?? Ss 0:04.57 /usr/libexec/xpcd
807 ?? S 0:00.04 /usr/sbin/pboard
809 ?? S 0:00.13 /System/Library/PrivateFrameworks/CalendarAgent.fram
(中略)
1014 ?? S 0:00.04 /System/Library/CoreServices/ScopedBookmarkAgent
1064 ?? S 0:00.09 /System/Library/Frameworks/QuickLook.framework/Resou
1065 ?? Ss 0:00.09 com.apple.quicklook.satellite
911 s000 Ss 0:00.02 login -fp yas
912 s000 S 0:00.03 -bash
1122 s000 R+ 0:00.00 ps x
945 s001 Ss 0:00.01 login -fp yas
947 s001 S 0:00.02 -bash
988 s001 S+ 0:00.07 ssh www.coins.tsukuba.ac.jp
1024 s002 Ss 0:00.01 login -fp yas
1027 s002 S 0:00.01 -bash
1070 s002 S+ 0:00.09 /Applications/MacPorts/Emacs.app/Contents/MacOS/Emac
$
ps に x, l, u, a 等のオプションを付けると、
ps はプロセスの
状態
(STATe)
を表示する。よく表示される状態には、次のようなものがある。
R
(runnable)
U
(uninterruptible), D
(Disk),
S
(Sleep)、I
(Idle)
$ ps a
端末と結びついていないプロセスも含めるには、x オプションもつける。
$ ps ax
練習問題 ps lと親プロセス、 pstreeコマンド参照。 新Linux/UNIX入門 23-167 プロセス一覧をツリー表示する(pstree) 参照。
プロセスを殺すとは、プロセスが自主的に終了しない時に、強制的に終了させ ること。
Unix では、プロセスを殺す方法は、主に次の2つ。
$ cat
Line 1
Line 1
Line 2
Line 2
^C
$
^C
と同様に ^\
(「^\」または「^¥」)
もプロセスを強制終了させるキーで
ある。^\ は、^Cが利かないときでも利くことがある。
^\
の場合、デバッグ用にcore
という名前のファイル
ができる。普段は、core ファイルは不要なので、^C
を使うべ
きである。
coins の環境では、core ファイルが作られないように設定されている。 core ファイルの利用方法は、デバッガ gdb のマニュアル、 man core (core ファイルの man ページ(man コマンドで表示されるページ))、 Unix Super Text 下巻 「第60章 デバッグの方法」を参照。
stty コマンドを使うと、^C
や ^\
を変更でき
る。
$ stty -a
speed 38400 baud; 40 rows; 80 columns;
lflags: icanon isig iexten echo echoe echok echoke -echonl echoctl
-echoprt -altwerase -noflsh -tostop -flusho pendin -nokerninfo
-extproc
iflags: -istrip icrnl -inlcr -igncr ixon -ixoff ixany imaxbel iutf8
-ignbrk brkint -inpck -ignpar -parmrk
oflags: opost onlcr -oxtabs -onocr -onlret
cflags: cread cs8 -parenb -parodd hupcl -clocal -cstopb -crtscts -dsrflow
-dtrflow -mdmbuf
cchars: discard = ^O; dsusp = ^Y; eof = ^D; eol = ;
eol2 = ; erase = ^?; intr = ^C; kill = ^U; lnext = ^V;
min = 1; quit = ^\; reprint = ^R; start = ^Q; status = ^T;
stop = ^S; susp = ^Z; time = 0; werase = ^W;
$
「intr」のところに表示されるものが「^C」,
「quit」のところに表示されるものが「^\」に相当するキー。
「kill」は、1行削除の意味。
^C
や ^\
で死なないプロセス
(lv, emacs など)
を殺すには、kill コマンドを使う。
$ kill pid
これは、プロセス識別子pidのプロセスを殺す。
他人のプロセスは、権限がないので殺せない。
注意:pid
には、プロセス識別子となる番号を入れる。「p」、
「i」「d」と3文字打つのではない。たとえば、PID が 1023 のプロセスを殺す
時には、次のように打つ。
$ kill 1023
-KILL
をつけてkillコマンドを実行する。
$ kill -KILL pid
-KILL
以外にも、様々な種類がある。詳しくは、man kill を参照。
ps プロセスの一覧の表示 ps x プロセスの一覧の表示(BSD,Linux) ps ax プロセスの一覧の表示(BSD,Linux) stty all 端末の状態(キーの割り当て)の表示(BSD) stty -a 端末の状態(キーの割り当て)の表示(System V,Linux) kill PID プロセスを殺す kill -KILL PID プロセスを殺す(必殺)PID はプロセス識別子。
PID
と打つのではない。
BSD は、macOS、FreeBSD 等。
System Vは、Solaris等。
図? プログラムの標準入出力の普通の接続先
$ cat
Line 1
Line 1
Line 2
Line 2
^D
$
$ ls -l > file1
図? プログラムの標準出力のファイルへの切替え
$ head -4 < file1
headコマンド は、ファイルや標準入力の先頭を表示する
ためのコマンド。-n
(nは数)というオプションが与
えられると、先頭の n 行だけ表示する。
図? プログラムの標準入力のファイルへの切替え
|
」があると、次の動作を行う (パイプ(pipe)機能)。
|
」の左のプログラム(プロセス)の標準出力を、右のプログ
ラム(プロセス)の標準入力に(ファイルを介することなく)接続する。
$ ls -l | head -4
total 99
drwx------ 4 yas prof 3072 4 10 17:37 Desktop
drwx------ 3 yas prof 1024 3 11 13:52 Documents
drwx------ 2 yas prof 80 3 10 18:16 Downloads
$
図? プログラムの標準出力のパイプへの切替え
$ ls -l | head -4
total 99
drwx------ 4 yas prof 3072 4 10 17:37 Desktop
drwx------ 3 yas prof 1024 3 11 13:52 Documents
drwx------ 2 yas prof 80 3 10 18:16 Downloads
$ ls -l > file
$ head -4 < file
total 99
drwx------ 4 yas prof 3072 4 10 17:37 Desktop
drwx------ 3 yas prof 1024 3 11 13:52 Documents
drwx------ 2 yas prof 80 3 10 18:16 Downloads
$ rm file
$
パイプは、3つ以上のプログラム(プロセス)を結びつけることもできる。
$ ls -l | cat -n | head -4
1 total 99
2 drwx------ 4 yas prof 3072 4 10 17:37 Desktop
3 drwx------ 3 yas prof 1024 3 11 13:52 Documents
4 drwx------ 2 yas prof 80 3 10 18:16 Downloads
$
cat コマンドは、「-n」オプションを付けると行番号を付加する。
$ ls Document > file
ls: Document: No such file or directory
$ cat file
$
「Document
」 は、「Documents
」の打ち間違い(sがない)である。
ls コマンドは、エラー・メッセージを標準出力ではなく標準エラー出力に書き
出す。
「2>file
」をつかうと、標準エラー出力をファイルに保存
することもできる。
$ ls Document > file 2>file2
$ cat file
$ cat file2
ls: Document: No such file or directory
$
「2>&1
」をつかうと、標準エラー出力(2)を、標準出力
(1)に混ぜて(&
)、ファイルに保存することもできる。
$ ls Document > file 2>&1
$ cat file
ls: Document: No such file or directory
$
「2>&1
」は、順番が大事。ファイルに混ぜて保存する場合
は、「>file
」の後にやること。先にやると、その時点で
の標準出力(画面)が指定されてしまう。
$ ls Document 2>&1 > file
ls: Document: No such file or directory
$ cat file
$
2>&1
を使うと、エラー・メッセージも含めてパイプに出力
することもできる。この場合は、パイプ|
より左に書く。
$ ls Document 2>&1 | head -4
ls: Document: No such file or directory
$
$ echo hello
hello
$ echo hi
hi
$ echo hello > hello.txt
$ cat hello.txt
hello
$ echo hi > hello.txt
$ cat hello.txt
hi
$ echo hello > hello.txt
$ cat hello.txt
hello
$ echo hi >> hello.txt
$ cat hello.txt
hello
hi
$
-1
(数字の 1
であり、アルファベットの L
の小文
字の l
ではない。)
-C
オプション
C-x i
と打つ。
Insert file: ~/
Insert file: ~/挿入したいファイル名
iTerm 等の端末の結果を、emacs にコピーする時に、タブをタブとして挿入し
たい時には、M-x local-set-key
で、次のようにタブ・キーの定義を
self-insert-command に変更する方法がある。
M-x local-set-key![[←]](../icons/screen-return.gif)
self-insert-command
$ pbcopy < literacy-a7.text
$ cat literacy-a7.text | pbcopy
$ ps x | pbcopy
$ ls *.txt | pbcopy
$ pbpaste > web.txt
$ pbpaste > iterm.txt
次の練習問題も合わせて行いたい。
$ chmod 000 ~/Library/Application\ Support/AddressBook
$ ls -ld ~/Library/Application\ Support/AddressBook
d--------- 4 yas prof 58368 4 20 11:30 /home/prof/yas/Library/Application Support/AddressBook
$
chmod については、後でコンピュータリテラシの授業でも取り上げる。
$ ps
$ ps l
ps コマンドに、次のオプションを与えて、動作を確認しなさい。
$ ps l
$ ps -l
「-」の有無で、使い方が違うことがある。
$ ps u
$ ps -u ログイン名
他のオプションについて、man ps を見て見なさい。
/etc/passwd
」の中にある「root
」とい
う文字を含む行を表示している。
$ grep root /etc/passwd
root:*:0:0:System Administrator:/var/root:/bin/sh
daemon:*:1:1:System Services:/var/root:/usr/bin/false
_cvmsroot:*:212:212:CVMS Root:/var/empty:/usr/bin/false
$
同じことを、パイプを使っても実行することができる。
$ cat /etc/passwd | grep root
root:*:0:0:System Administrator:/var/root:/bin/sh
daemon:*:1:1:System Services:/var/root:/usr/bin/false
_cvmsroot:*:212:212:CVMS Root:/var/empty:/usr/bin/false
$
grep コマンドを使って次のようなことを行ってみなさい。
/etc/services
にある http
が含まれる行
/etc/services
にある pop
が含まれる行
/etc/services
にある imap
が含まれる行
ls
が含まれる行
grep コマンドには次のようなオプションを指定することができる。
$ ps | grep bash
34121 ttys000 0:00.03 -bash
75409 ttys000 0:00.00 grep bash
75195 ttys001 0:00.01 -bash
34226 ttys002 0:00.05 -bash
75399 ttys003 0:00.01 -bash
$ ps | grep Emacs
75412 ttys000 0:00.00 grep Emacs
75404 ttys003 0:00.09 /Applications/MacPorts/Emacs.app/Contents/MacOS/Emacs -nw literacy-a7.txt
$
この機能を利用して見なさい。
端末と結びついていないプロセスを調べるには、ps コマンドに x オプション を与える。次のようにして、Finder、Dock、Emacs、Firefox 等のプロセスの PID を調べなさい。
$ ps x | grep Finder
$ ps x | grep Dock
$ ps x | grep fire
$ ps x | grep iTerm
grep コマンドに -i オプションを付けると大文字と小文字を関係なく調べるこ
とができる。このことを確認しなさい。
$ ps x | grep -i finder
$ ps x | grep -i dock
$ ps x | grep -i fire
$ ps x | grep -i iterm
$ ps aux | lv
この方法を、確かめなさい。lv コマンドについては、
手引き 2.7.2 参照。
lv コマンド
を参照しなさい。q(終了)、次のページへの移動(スペースキー)、
ページを戻る(b)は、必ず覚えなさい。
$ ps u
USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND
yas 1070 0.0 0.1 2508388 17016 s002 S+ 5:40PM 0:00.09 /Application
yas 1027 0.0 0.0 2452824 1312 s002 S 5:33PM 0:00.01 -bash
yas 988 0.0 0.0 2461144 2184 s001 S+ 5:25PM 0:00.10 ssh www.coin
yas 947 0.0 0.0 2461016 1248 s001 S 5:25PM 0:00.02 -bash
yas 912 0.0 0.0 2452824 1332 s000 S 5:23PM 0:00.03 -bash
$
%CPU
%MEM
VSZ
(Virtual Size)
RSS
(resident set size)
STARTED
$ ps l
UID PID PPID CPU PRI NI VSZ RSS WCHAN STAT TT TIME COMMAND
1013 912 911 0 31 0 2452824 1332 - S s000 0:00.04 -bash
1013 947 945 0 31 0 2461016 1248 - S s001 0:00.02 -bash
1013 988 947 0 31 0 2461144 2184 - S+ s001 0:00.12 ssh ww
1013 1027 1024 0 31 0 2452824 1312 - S s002 0:00.01 -bash
1013 1070 1027 0 33 0 2508388 17016 - S+ s002 0:00.09 /Appli
$
PID 988 の ssh の親プロセスは、PID 947 の bash。
PID 1070 の Emacs の親プロセスは、PID 1024 の bash。
$ ps u
USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND
yas 1070 0.0 0.1 2508388 17016 s002 S+ 5:40PM 0:00.09 /Application
yas 1027 0.0 0.0 2452824 1312 s002 S 5:33PM 0:00.01 -bash
yas 988 0.0 0.0 2461144 2184 s001 S+ 5:25PM 0:00.19 ssh www.coin
yas 947 0.0 0.0 2461016 1248 s001 S 5:25PM 0:00.02 -bash
yas 912 0.0 0.0 2452824 1332 s000 S 5:23PM 0:00.04 -bash
$ ps uw
USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND
yas 1070 0.0 0.1 2508388 17016 s002 S+ 5:40PM 0:00.09 /Applications
/MacPorts/Emacs.app/Contents/MacOS/Emacs -nw
yas 1027 0.0 0.0 2452824 1312 s002 S 5:33PM 0:00.01 -bash
yas 988 0.0 0.0 2461144 2184 s001 S+ 5:25PM 0:00.19 ssh www.coins
.tsukuba.ac.jp
yas 947 0.0 0.0 2461016 1248 s001 S 5:25PM 0:00.02 -bash
yas 912 0.0 0.0 2452824 1332 s000 S 5:23PM 0:00.04 -bash
$
iTerm の画面を横幅を広げて ps コマンドを打ちなさい。
横幅を広げるには、右下の斜線が入っている部分をドラッグすればよい。
$ top
$ top -o cpu
(CPU を消費している順)
macOS では、top コマンド自分自身が「重たい」と認識される。
その場合は、top コマンドが表示を変更する頻度を下げる。
$ top -s 4 -o cpu
man top でマニュアルを表示して、-o オプションと -U の使い方を
調べなさい。そして、メモリの利用順 (rsize) で表示しなさい。
-U オプションで自分自身のプロセスだけを表示しなさい。
$ top -U 自分のログイン名
$ top
$ ps x | grep top
(結果省略。PID を調べる。)
$ kill PID
$
$ cat > file1.txt
Line 1
Line 2
^D
$ cat file1.txt
Line 1
Line 2
$
$ cat
aaa
aaa
bbb
bbb
^C
$
なお、^C と 入力の終わりを意味する ^D は働きが異な
る。どちらでもプロセスが終了することがあるが、強制終了したい場合には、
^C を使いなさい。
$ ls -R ~/Library
(大量の表示、省略)
^C
$
$ yes | head
y
y
y
y
y
y
y
y
y
y
$
yes コマンドは普通、終了しないので、最後の 10 行を表示しようと tail コ
マンドを使っても表示されない。
$ yes | tail
この時、別の端末で yes コマンドのプロセスを kill してみなさい。
$ ps
(yes のプロセスの PID を調べる)
$ kill PID
$
yes のプロセスが終了すると、tail コマンドが最後の 10 行を表示する。
このことを確認しなさい。
yes と tail を ^C で強制終了するとどうなるか。調べなさい。
$ yes | tail
^C
$
yes コマンドと lv コマンドを実行するとどうなるか調べなさい。
$ yes | lv
yes コマンドを単独で実行し、^C で停止しなさい。
$ yes
...
^C
$
cat -n と合わせて実行し、必要なら強制終了しなさい。
$ yes | cat -n
$ yes | cat -n | lv
$ yes | cat -n | head
$ yes | cat -n | tail
C-c
) は、C-x C-c
のように、その
キーに割り当てられた関数を実行する意味。)
C-c
は、C-x
と同じように、2ストローク以上で1つの意
味をなすことが多い。何もない状態でC-c
を打った場合には、
C-g
によりキャンセルしなさい。
kill コマンドを使って、プロセスを殺してみなさい。
$ lv literacy-a4.txt
$ lv ~/.bashrc
$ ps
$ kill PID
lv コマンドの代わりに、bash からなるプロセスを殺すとどうなるか調べなさい。
$ ps 引数 | grep オプション パタン
$ kill 数
たとえば、次の例のように、
PaperCut
のプロセスを探して、殺してみなさい。
$ ps x | grep -i paper
94070 ?? SN 0:01.40 /Applications/PCClient.app/Contents/MacOS/JavaAppLauncher biz.papercut.pcng.client.uit.UserClient
(端末がないので、ps に x オプションをつける。grep に -i オプションをつけて、大文字小文字を無視する。)
$ kill 94070
(単に kill だけでは、プロセスは死んでいない。)
$ kill 94070
(2回目にエラーが出ないので、プロセスが生きていることがわかる。)
$ kill -KILL 94070
$ kill -KILL 94070
-bash: kill: (94070) - No such process
(2回目にエラーが出たので、プロセスが死んでいることがわかる。)
$ ps x | grep -i paper
94192 s000 S+ 0:00.00 grep -i paper
(ps コマンドでも表示されない。)
$
$ cal 5 2018
5月 2018
日 月 火 水 木 金 土
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
$ cal 5 2018 > cal-2018-05.txt
$ ls -l cal-2018-05.txt
-rw-r--r-- 1 yas prof 197 5 5 15:32 cal-2018-05.txt
$ cat cal-2018-05.txt
5月 2018
日 月 火 水 木 金 土
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
$
動作例:
$ bc
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type warranty.
10+20
30
^D
$
bc コマンドは、プロンプトを表示しないが、ユーザが打ち込んだ数式を行単位
で計算して表示する。終了するには、入力の終わり(end of file) を意味する
^D
(Control+D) を押す。コントロール・キーを押しながら、
D キーを押す。
次のような機能が利用できる。
bc に -l
オプションを付けると小数が扱える。
詳しくは、man bc を見なさい。
$ cat > expressions
10+20
2^40
^D
$ cat expressions
10+20
2^40
$
次に作成した数式を、
キーボードの代わりにファイルからデータを読み込ませる。
$ bc < expressions
30
1099511627776
$
次に、echo コマンドとパイプを使ってみなさい。数式は、ダブルクォーテーショ
ンマーク(「"」)で括るとよい。
$ echo "10+20"
10+20
$ echo "10+20" | bc
30
$ echo "2^40" | bc
1099511627776
$
$ ls -l cal-2018-567.txt
ls: cal-2018-567.txt: No such file or directory
$ cal 5 2018 > cal-2018-567.txt
$ cal 6 2018 >> cal-2018-567.txt
$ cal 7 2018 >> cal-2018-567.txt
$ cat cal-2018-567.txt
5月 2018
日 月 火 水 木 金 土
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
6月 2018
日 月 火 水 木 金 土
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
7月 2018
日 月 火 水 木 金 土
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
$ ls -l cal-2018-567.txt
-rw-r--r-- 1 yas prof 583 5 5 15:34 cal-2018-567.txt
$
$ head /usr/share/dict/words
A
a
aa
aal
aalii
aam
Aani
aardvark
aardwolf
Aaron
$ head -5 /usr/share/dict/words
A
a
aa
aal
aalii
$
このコマンドの動きを確認しなさい。次のファイルを指定してみなさい。
$ head /usr/share/dict/words
$ tail ~/literacy-a4.txt
-n
(nは数)オプションの動きを調べなさい。ファイル
が短い時、10行、または、それ以下の行数しかない時には、head コマンドが何
行表示するかを調べなさい。
$ tail /usr/share/dict/words
zymotoxic
zymurgy
Zyrenian
Zyrian
Zyryan
zythem
Zythia
zythum
Zyzomys
Zyzzogeton
$ tail -5 /usr/share/dict/words
zythem
Zythia
zythum
Zyzomys
Zyzzogeton
$
練習問題(823)
と同じファイルを tail コマンドで表示してみなさい。
$ tail /usr/share/dict/words
$ tail ~/literacy-a4.txt
$ lv /usr/share/dict/words
$ lv ~/literacy-a4.txt
$ ls
file1.txt file2.txt
$ ls | cat
file1.txt
file2.txt
$
このことを確認しなさい。
$ ls
$ ls | cat
$ ls | lv
ls
の -1
オプションは、(端末でパイプでも)縦に表示するオプ
ションである。-1
は、数字の 1
であり、アルファベットの
L
の小文字の l
ではない。ls
の -C
オプショ
ンは、(端末でパイプでも)横に表示するオプション。
$ ls
file1.txt file2.txt
$ ls -1
file1.txt
file2.txt
$ ls -C
file1.txt file2.txt
$ ls -C | cat
file1.txt file2.txt
$
このことを確認しなさい。
$ ls -l /usr/bin
$ ls -l /usr/bin | lv
$ ls -l /usr/bin
$ ls -l /usr/bin | head
$ ls -l /usr/bin | head -20
$ ls -l /usr/bin
$ ls -l /usr/bin | head
$ ls -l /usr/bin | head -20
$ ps aux | lv
$ ps aux | head
treeコマンド
の長い結果をページャで表示してゆっくり見る。
$ tree
$ tree | lv
$ grep yes /usr/share/dict/words | lv
$ tail /usr/share/dict/words | sort -r
まず、pbpaste コマンドを使って、Web ページの一部やiTerm の表示の一部を ファイルに保存しなさい。
$ pbpaste > file1.txt
$ emacs file2.txt
$ pbcopy < ファイル名
cat コマンドとパイプを使う方法もある。
$ cat ファイル名 | pbcopy
cat コマンドではなく、他のコマンドでも良い。
$ ps | pbcopy
$ ls -l | pbcopy
$ ls
(タブ入りの表示、横)
$ ls | expand
(タブを含まない表示、縦)
$ ls -C | expand
(タブを含まない表示、横)
$ ls -C | expand | pbcopy
(タブを含まない表示が、クリップボードにコピーされる。画面には表示されない。)
$
この動きを確認しなさい。
The Unix Super Text 35.2.7 exapnd、unexpand 参照。
新Linux/UNIX入門 34-225 タブをスペースに展開する(exapnd) 参照。
M-x shell-command
この機能を、C-u (Control+u) を付けて実行すると、コマンドを実行
してその結果を現在編集中のファイル(バッファ)に挿入できる。
C-u M-x shell-command
Shell command: 実行したいコマンド
この機能を確認しなさい。
C-u M-x shell-command
Shell command: pbpaste
macOS の仮名漢字変換を行っているプログラム「ことえり」が、頻繁に動作し なくなる時には、次のような設定をすると頻度が減ることがある。
$ ps x | grep JapaneseIM
$ ps x | grep -i japan
$ ps x | grep Ja
仮名漢字変換がうまく動作しなくなった時、ことえりのプロセスを殺すと回復することがある。
$ kill ことえりのプロセスのPID
ことえりのプロセスを殺すと、もう一度ことえりが自動的に実行され、ことえ
りのプロセスが再び現れる。
ps l
コマンドを使うと、親プロセスの PID が表示される。
この仕組みを使って、親プロセスの親プロセスを調べなさい。
さらに、その親プロセスを調べなさい。
次の例は、bash の親プロセス、その親プロセス、さらにその親プロセスを調べ て、iTerm であることを突き止めている。
$ ps alx | grep bash
1013 1506 1230 0 31 0 2436436 1032 - S ?? 0:00.00 /bin/bash /usr/local/MacGPG2/libexec/shutdown-gpg-agent
1013 1643 1642 0 31 0 2461016 1336 - S s000 0:00.01 -bash
1013 1609 1608 0 31 0 2461016 1312 - S s002 0:00.01 -bash
1013 1654 1609 0 31 0 2432784 604 - S+ s002 0:00.00 grep bash
$ ps alx | grep 1608
0 1608 1602 0 33 0 2469444 2388 - Us s002 0:00.01 login -fp yas
1013 1609 1608 0 31 0 2461016 1312 - S s002 0:00.01 -bash
1013 1656 1609 0 31 0 2432784 604 - S+ s002 0:00.00 grep 1608
$ ps alx | grep 1602
1013 1602 1230 0 49 0 2598208 44240 - S ?? 0:00.99 /Applications/iTerm.app/Contents/MacOS/iTerm
0 1642 1602 0 33 0 2468920 2432 - Ss s000 0:00.01 login -fp yas
0 1608 1602 0 33 0 2469444 2388 - Us s002 0:00.01 login -fp yas
1013 1658 1609 0 31 0 2432784 604 - S+ s002 0:00.00 grep 1602
$
最後は、PID が 1 番のプロセスにたどりつく。このことを確認しなさい。
$ pstree
$ pstree | lv
iTerm の画面の横幅を広げると見やすい。
macOS で、Command+Option+Esc キーを押すと 「アプリケーションの強制終了」のウインドウを表示できる。
(まず、Commandキー() と Option キーを左手で押し、
押しっぱなしの状態で、右手で Esc キーを一度押して離す。)
左上のアップル・メニュー(リンゴのアイコン)を押し、
「強制終了」を選んでも良い。
「アプリケーションの強制終了」のウインドウ
この機能を確認しなさい。なお、この機能では、ウインドウを開くプロセスし か強制終了できない。iTerm の中で実行されるプロセスや端末とは結びついて いないプロセスを強制終了するには、kill コマンドを使うしかない。
$ open "/Applications/Utilities/Activity Monitor.app"
または、Finder で、「移動」メニューから「ユーティリティ」を
選択。「アクティビティモニタ.app」のアイコンをダブルクリック。
(クリックで拡大)
アクティビティモニタの表示
表示されたら、「CPU」タブを表示しなさい。そして、 表示されるプロセスの順序を変えてみなさい。
topコマンド の表示と比較しなさい。
(クリックで拡大)
アクティビティモニタの表示、日本語入力プログラム、反応なし
この場合、次の手順で「日本語入力プログラム」のプロセスを殺しなさい。
(1) 以下の方法で、ps コマンドと kill コマンドを実行し、その結果をまとめ なさい。
$ emacs literacy-a8.txt
$ lv ~/.bashrc
$ ps | grep lv
(表示)
$
$ kill pid
$ ps | grep lv
(表示)
$
練習問題 ps コマンドと grep コマンド、 練習問題 killコマンド、 練習問題 ps | grep、kill参照。
(2) シェルが持つ標準入出力の切り替え機能を使って、次のコマンドの結果を 全て含む1つのファイルを作りなさい。
練習問題 追記参照。
(3) 次の Web ページを読みなさい。
そして、次の項目についてついて調べて、簡単に説明しなさい。http://www.coins.tsukuba.ac.jp/~ユーザ名/
(4) [加点] 次のプログラムを実行し、プロセスの木構造を調べなさい。
- 1 /sbin/launchd -- NNN /System/Library/CoreServices/loginwindow.app/Contents/MacOS/loginwindow 省略 --省略 NNN bash --省略 NNN bash木の節には、PID とプログラムの名前(ps コマンド COMMANDの下の表示)を書きなさい。 上記の NNN の部分には実際に調べた PID を書きなさい。 木構造の根の部分は、PID が 1 のプロセスである。 レポートの回答には、「省略」はしないで、きちんと書きなさい。
この木構造には、次のプログラムから作られたプロセスを必ず含めなさい。
なお、PID やプロセスの親子関係は、ログアウトしてログインしたり、 ウィンドウを閉じて開いたりすると変化する。 従って、PID やプロセスの親子関係を調べる時には、ウィンドウを閉じたり開いたりしないこと。
次の練習問題を参考にしなさい。
(5) [加点] 新Linux/UNIX入門の次の部分を読みなさい。