システム・プログラム 電子・情報工学系 新城 靖 <yas@is.tsukuba.ac.jp>
このページは、次の URL にあります。
http://www.hlla.is.tsukuba.ac.jp/~yas/coins/syspro-2000/2000-04-17
あるいは、次のページから手繰っていくこともできます。
http://www.hlla.is.tsukuba.ac.jp/~yas/coins/
http://www.hlla.is.tsukuba.ac.jp/~yas/index-j.html
プロセスとは、一言で言えば、プログラムの実行時のイメージ。ここで、 プ ログラムというのは、CPUが実行できる機械命令の集まり(実行形式、ロー ド・モジュール)のことである。
1つのプログラムを同時に2個動かすことを考えるとプログラムとプロセスの 違いがはっきりする。プロセス(process)は、プロセッサ(processor,CPU)と関 係ある。
プロセスには、「利用者の代理」として計算機の中に入り込むものという側面 がある。
プロセスの操作
UNIXでプロセスを観察するには、ps コマンドを使う。
---------------------------------------------------------------------- % psPID TTY TIME CMD 5815 ttyq1 0:00 tcsh 5830 ttyq1 0:00 emacs 5841 ttyq1 0:00 ps % ps -f
UID PID PPID C STIME TTY TIME CMD yas 5815 5814 1 21:40:00 ttyq1 0:00 -tcsh yas 5830 5815 0 21:41:25 ttyq1 0:00 mule -nw yas 5842 5815 3 21:42:20 ttyq1 0:00 ps -f % ps -u $USER
PID TTY TIME CMD 5838 pts/2 0:00 tcsh 5815 pts/1 0:00 tcsh 5830 pts/1 0:00 emacs 5843 pts/1 0:00 ps %
----------------------------------------------------------------------
図2 ソース・プログラムから実行形式まで
cc -c
で実行されるプログラム
cpp(the C language preprocessor)
ccom(C Compiler)
as(Assembler)
図3 cc -c が実行するプログラム
コンパイルの観察。---------------------------------------------------------------------- % nl -ba today.c1 #define TODAY "Tuesday" 2 main() 3 { 4 printf("Today is %s.\n",TODAY ); 5 } % cc today.c
% ./a.out
Today is Tuesday. %
----------------------------------------------------------------------
---------------------------------------------------------------------- % cc -v today.c/usr/lib/cfe -D_MIPS_FPSET=16 -D_MIPS_ISA=2 -D_ABIO32=1 -D_MIPS_SIM=_ABIO32 -D_M IPS_SZINT=32 -D_MIPS_SZLONG=32 -D_MIPS_SZPTR=32 -D__EXTENSIONS__ -DLANGUAGE_C -D _LANGUAGE_C -D__INLINE_INTRINSICS -Dsgi -D__sgi -Dunix -Dmips -Dhost_mips -D__un ix -D__host_mips -D_SVR4_SOURCE -D_MODERN_C -D_SGI_SOURCE -D_PIC -D__DSO__ -DSYS TYPE_SVR4 -D_SYSTYPE_SVR4 -D_LONGLONG -D__mips=2 -I -D_MIPSEB -DMIPSEB -D__STDC_ _=1 -I/usr/include today.c -Xv -D_CFE -Amachine(mips) -Asystem(unix) -call_share d -G 0 -std -XS/tmp/ctmsta001TP -mips2 -EB -Xg0 -O1 > /tmp/ctmfa001TP cfe: main /usr/lib/cfe phase time: 0.03u 0.04s 0:00.1 100% /usr/lib/ugen -v -G 0 -pic2 -mips2 -EB -g0 -O1 /tmp/ctmfa001TP -o /tmp/ctmca001T P -t /tmp/ctmsta001TP -temp /tmp/ctmgta001TP ugen: main /usr/lib/ugen phase time: 0.01u 0.04s 0:00.1 100% /usr/lib/as1 -t5_ll_sc_bug -elf -pic2 -v -G 0 -p0 -mips2 -EB -g0 -O1 /tmp/ctmca0 01TP -o today.o -t /tmp/ctmsta001TP as1: main /usr/lib/as1 phase time: 0.00u 0.02s 0:00.1 25% /usr/lib/ld -elf -_SYSTYPE_SVR4 -require_dynamic_link _rld_new_interface -no_unr esolved -Wx,-G 0 -mips2 -call_shared -g0 -KPIC -L/usr/lib/ -nocount /usr/lib/crt 1.o -count today.o -nocount -lc /usr/lib/crtn.o /usr/lib/ld phase time: 0.02u 0.04s 0:00.1 67% %
----------------------------------------------------------------------
---------------------------------------------------------------------- % cc -E today.c > today.i% nl -ba today.i
1 # 1 "today.c" 2 3 main() 4 { 5 printf("Today is %s.\n","Tuesday" ); 6 } %
----------------------------------------------------------------------
---------------------------------------------------------------------- % nl -ba today.s1 .verstamp 7 20 2 .option pic2 3 .rdata 4 .align 2 5 .align 0 6 $$5: 7 .ascii "Today is %s.\X0A\X00" 8 .rdata 9 .align 2 10 .align 0 11 $$6: 12 .ascii "Tuesday\X00" 13 .text 14 .align 2 15 .file 2 "today.c" 16 .globl main 17 .loc 2 3 18 # 1 #define TODAY "Tuesday" 19 # 2 main() 20 # 3 { 21 .ent main 2 22 main: 23 .option O1 24 .set noreorder 25 .cpload $25 26 .set reorder 27 subu $sp, 32 28 sw $31, 28($sp) 29 .cprestore 24 30 .mask 0x90000000, -4 31 .frame $sp, 32, $31 32 .loc 2 3 33 .loc 2 4 34 # 4 printf("Today is %s.\n",TODAY ); 35 la $4, $$5 36 la $5, $$6 37 .livereg 0x0C00000E,0x00000000 38 jal printf 39 .loc 2 5 40 # 5 } 41 move $2, $0 42 .livereg 0x2000FF0E,0x00000FFF 43 lw $31, 28($sp) 44 addu $sp, 32 45 j $31 46 .end main %
----------------------------------------------------------------------
---------------------------------------------------------------------- % cc -c today.c% nm today.o
Symbols from today.o: [Index] Value Size Class Type Section Name [0] | 0| |File |ref=4 |Text | today. c [1] | 0| |Proc |end=3 int |Text | main [2] | 68| |End |ref=1 |Text | main [3] | 0| |End |ref=0 |Text | today. c [4] | 0| |Proc |ref=1 |Text | main [5] | 0| |Proc | |Undefined| printf [6] | 0| |Global | |Undefined| _gp_di sp % cc -v -o today today.o
/usr/lib/ld -elf -o today -_SYSTYPE_SVR4 -require_dynamic_link _rld_new_interfac e -no_unresolved -Wx,-G 0 -mips2 -call_shared -g0 -KPIC -L/usr/lib/ -nocount /us r/lib/crt1.o -count today.o -nocount -lc /usr/lib/crtn.o /usr/lib/ld phase time: 0.02u 0.05s 0:00.1 88% %
----------------------------------------------------------------------
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_unlocked(void); #define getchar() getc_locked(stdin) #define getchar_locked() getc_locked(stdin) #define getchar_unlocked() getc_unlocked(stdin) #define getchar() getc(stdin) #define getchar_unlocked() getc_unlocked(stdin) #define getchar() getc(stdin) % egrep stdin /usr/include/stdio.h
#define stdin (&__iob[0]) #define getchar() getc_locked(stdin) #define getchar_locked() getc_locked(stdin) #define getchar_unlocked() getc_unlocked(stdin) #define getchar() getc(stdin) #define getchar_unlocked() getc_unlocked(stdin) #define getchar() getc(stdin) %
----------------------------------------------------------------------