システム・プログラムI 電子・情報工学系 新城 靖 <yas@is.tsukuba.ac.jp>
このページは、次の URL にあります。
http://www.hlla.is.tsukuba.ac.jp/~yas/coins/syspro1-1997/1997-04-15
あるいは、次のページから手繰っていくこともできます。
http://www.hlla.is.tsukuba.ac.jp/~yas/coins/
http://www.hlla.is.tsukuba.ac.jp/~yas/index-j.html
図1 システムの構成要素
プロセスとは、一言で言えば、プログラムの実行時のイメージ。ここで、 プ ログラムというのは、CPUが実行できる機械命令の集まり(実行形式、ロー ド・モジュール)のことである。
1つのプログラムを同時に2個動かすことを考えるとプログラムとプロセスの 違いがはっきりする。プロセス(process)は、プロセッサ(processor,CPU)と関 係ある。
プロセスには、「利用者の代理」として計算機の中に入り込むものという側面 がある。
プロセスの操作
UNIXでプロセスを観察するには、ps コマンドを使う。
---------------------------------------------------------------------- % psPID TTY TIME COMMAND 14182 ttyp6 0:00 telnetd 14183 ttyp6 0:00 tcsh 20628 ttyp6 0:00 ps 19681 ttyp6 0:01 xemacs % ps -l
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME COMD 1 S 0 14182 169 0 154 20 8c35c0 17 2927a0 ttyp6 0:00 telnetd 1 S 1231 14183 14182 1 168 20 8c3600 145 7ffe6000 ttyp6 0:00 tcsh 1 R 1231 20630 14183 5 179 20 8da1c0 16 ttyp6 0:00 ps 21 T 1231 19681 14183 0 178 20 8e96c0 377 ttyp6 0:01 xemacs % ps -f
UID PID PPID C STIME TTY TIME COMMAND root 14182 169 0 19:16:21 ttyp6 0:00 telnetd yas 14183 14182 0 19:16:22 ttyp6 0:00 -tcsh yas 19681 14183 0 21:13:39 ttyp6 0:01 emacs -nw yas 20635 14183 6 21:35:00 ttyp6 0:00 ps -f % ps -u $USER
PID TTY TIME COMMAND 7222 ttysa 0:01 less 14183 ttyp6 0:00 tcsh 7221 ttysa 0:00 zcat 20647 ttyp6 0:00 ps 7214 ttysa 0:00 man 19681 ttyp6 0:01 xemacs 7220 ttysa 0:00 sh 6962 ttysa 0:00 tcsh 5508 ? 0:02 kterm %
----------------------------------------------------------------------
図2 ソース・プログラムから実行形式まで
cc -c
で実行されるプログラム
cpp(the C language preprocessor)
ccom(C Compiler)
as(Assembler)
図3 cc -c が実行するプログラム
コンパイルの観察。リンクの観察---------------------------------------------------------------------- % nl -ba today.c1 #define TODAY "Tuesday" 2 3 main() 4 { 5 printf("Today is %s.\n",TODAY ); 6 } % cc -v -c today.c
cc: CCOPTS is not set. /lib/cpp today.c /tmp/ctmAAAa23721 -$ -D__hp9000s800 -D__hppa -D__hpux -D__unix -D_PA_RISC1_0 cc: Entering Preprocessor. /lib/ccom /tmp/ctmAAAa23721 today.o -Oq00,al,ag,cn,Lm,sz,Ic,vo,lc,mf,Po,es,rs,sp,in,pi,fa,pe,Rr,Fl! -Ac %
% cc -E today.c > today.i
% nl -ba today.i
1 # 1 "today.c" 2 3 4 main() 5 { 6 printf("Today is %s.\n","Tuesday" ); 7 } % cc -S today.c
% nl -ba today.s
1 2 .SPACE $TEXT$,SORT=8 3 .SUBSPA $CODE$,QUAD=0,ALIGN=4,ACCESS=0x2c,CODE_ONLY,SORT=24 4 main 5 .PROC 6 .CALLINFO CALLER,FRAME=16,SAVE_RP 7 .ENTRY 8 STW %r2,-20(%r30) ;offset 0x0 9 LDO 64(%r30),%r30 ;offset 0x4 10 ADDIL LR'M$2-$global$,%r27 ;offset 0x8 11 LDO RR'M$2-$global$(%r1),%r26 ;offset 0xc 12 ADDIL LR'M$2-$global$+16,%r27 ;offset 0x10 13 .CALL ARGW0=GR,ARGW1=GR,RTNVAL=GR ;in=25,26;out=28; 14 BL printf,%r2 ;offset 0x14 15 LDO RR'M$2-$global$+16(%r1),%r25 ;offset 0x18 16 LDW -84(%r30),%r2 ;offset 0x1c 17 BV %r0(%r2) ;offset 0x20 18 .EXIT 19 LDO -64(%r30),%r30 ;offset 0x24 20 .PROCEND ;out=28; 21 22 23 .SPACE $TEXT$ 24 .SUBSPA $CODE$ 25 .SPACE $PRIVATE$,SORT=16 26 .SUBSPA $DATA$,QUAD=1,ALIGN=8,ACCESS=0x1f,SORT=16 27 M$2 28 .ALIGN 8 29 .STRINGZ "Today is %s.\n" 30 .BLOCKZ 2 31 .STRINGZ "Tuesday" 32 .IMPORT $global$,DATA 33 .SPACE $TEXT$ 34 .SUBSPA $CODE$ 35 .EXPORT main,ENTRY,PRIV_LEV=3,RTNVAL=GR 36 .IMPORT printf,CODE 37 .END %
----------------------------------------------------------------------
---------------------------------------------------------------------- % cc -c today.c% nm today.o
Symbols from today.o: Name Value Scope Type Subspace $global$ | |undef |data | main | 0|extern|entry |$CODE$ M$2 |1073741824|static|data |$DATA$ printf | |undef |code | % % cc -v -o today today.o
cc: CCOPTS is not set. cc: LPATH is not set. /bin/ld /lib/crt0.o -u main -Fb /usr/lib/uccom -o today today.o -lc cc: Entering Link editor. % ls -l today
-rwxr-xr-x 1 yas lab 16384 Apr 14 21:44 today % ./today
Today is Tuesday. %
----------------------------------------------------------------------
open(),close(),read(),write()
ライブラリ関数の例
fopen(),fclose(),fread(),fwrite(),printf(),scanf(),getchar(),putchar()
マクロ定義の例
getchar(),putchar(),stdin,stdout,stderr
ヘッダ・ファイル stdio.h の観察
---------------------------------------------------------------------- % egrep getchar /usr/include/stdio.hextern int getchar(void); extern int getchar(); # define getchar() getc(stdin) % egrep putchar /usr/include/stdio.h
extern int putchar(int); extern int putchar(); # define putchar(__c) putc((__c), stdout) % egrep stdin /usr/include/stdio.h
# define stdin (&__iob[0]) # define getchar() getc(stdin) %
----------------------------------------------------------------------
(1) ps コマンドでプロセスを観察しなさい。
(2) cc -v で、cc から実行されるプログラムを観察しなさい。
(3) nm で、オブジェクト・プログラムと実行形式のシンボルとそのアドレス を観察しなさい。