PID getpid2 include unistd h pidt getpidvoid PID

  • Slides: 24
Download presentation

프로세스 식별 PID 검색: getpid(2) #include <unistd. h> pid_t getpid(void); § 이 함수를 호출한

프로세스 식별 PID 검색: getpid(2) #include <unistd. h> pid_t getpid(void); § 이 함수를 호출한 프로셋의 PID를 리턴 PPID 검색 : getppid(2) #include <unistd. h> pid_t getppid(void); § 부모 프로세스의 PID를 리턴 # ps -ef | more UID PPID root 0 0 root 1 0 root 2 0. . . C 0 0 0 STIME TTY 1월 30 일 ? TIME CMD 175: 28 sched 0: 02 /sbin/init 0: 00 pageout 부모 프로세스ID 8/24

[예제 5 -1] getpid, getppid 함수 사용하기 01 02 03 04 05 06 07

[예제 5 -1] getpid, getppid 함수 사용하기 01 02 03 04 05 06 07 08 09 ex 5_1. c #include <unistd. h> #include <stdio. h> int main(void) { printf("PID : %dn", (int)getpid()); printf("PPID : %dn", (int)getppid()); return 0; } # ex 5_1. out 678 프로세스는 콘쉘 # ps PID TTY 678 pts/3 2206 pts/3 PID : 2205 PPID : 678 TIME CMD 0: 00 ksh 0: 00 ps 9/24

[예제 5 -2] getpgrp, getpgid 함수 사용하기 01 02 03 04 05 06 07

[예제 5 -2] getpgrp, getpgid 함수 사용하기 01 02 03 04 05 06 07 08 09 10 11 ex 5_2. c #include <unistd. h> #include <stdio. h> int main(void) { printf("PID : %dn", (int)getpid()); printf("PGRP : %dn", (int)getpgrp()); printf("PGID(0) : %dn", (int)getpgid(0)); printf("PGID(2287) : %dn", (int)getpgid(2287)); return 0; } 실행방법 2287은 sleep의 PID # ex 5_2. out PID : 2297 PGRP : 2297 PGID(0) : 2297 PGID(2287) : 2285 $ ps -ef | more | sleep 300 & $ ps PID TTY TIME CMD 2278 pts/6 0: 00 ksh 2301 pts/6 0: 00 ps 2287 pts/6 0: 00 sleep 11/24

[예제 5 -3] getsid 함수 사용하기 01 02 03 04 05 06 07 08

[예제 5 -3] getsid 함수 사용하기 01 02 03 04 05 06 07 08 09 10 ex 5_3. c #include <unistd. h> #include <stdio. h> int main(void) { printf("PID : %dn", (int)getpid()); printf("PGID : %dn", (int)getpgrp()); printf("SID : %dn", (int)getsid(0)); return 0; } # ex 5_3. out PID : 2584 PGID : 2584 SID : 678 # ps PID TTY 678 pts/3 2585 pts/3 TIME CMD 0: 01 ksh 0: 00 ps 14/24

[예제 5 -4] times 함수 사용하기 ex 5_4. c . . . 08 int

[예제 5 -4] times 함수 사용하기 ex 5_4. c . . . 08 int main(void) { 09 int i; 10 time_t t; 11 struct tms mytms; 12 clock_t t 1, t 2; 13 14 if ((t 1 = times(&mytms)) == -1) { 15 perror("times 1"); 16 exit(1); 사용자 모드에서 시간을 소비하기 위한 반복문 처리 17 } 18 19 for (i = 0; i < 999999; i++) 20 time(&t); # ex 5_4. out 21 Real time : 0. 4 sec 22 if ((t 2 = times(&mytms)) == -1) { User time : 0. 2 sec 23 perror("times 2"); System time : 0. 1 sec 24 exit(1); 25 } 26 27 printf("Real time : %. 1 f secn“, (double)(t 2 - t 1) / CLK_TCK); 28 printf("User time : %. 1 f secn“, (double)mytms. tms_utime / CLK_TCK); 29 printf("System time : %. 1 f secn“, (double)mytms. tms_stime / CLK_TCK); 30 31 return 0; <limits. h> 32 } #define CLK_TCK ((clock_t) _sysconf(3)) /* 3 is _SC_CLK_TCK */ 16/24

환경변수의 사용[1] 전역변수 사용 : environ #include <stdlib. h> extern char **environ; ex 5_5.

환경변수의 사용[1] 전역변수 사용 : environ #include <stdlib. h> extern char **environ; ex 5_5. c 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 #include <stdlib. h> #include <stdio. h> extern char **environ; # ex 5_5. out _=ex 5_5. out int main(void) { LANG=ko char **env; HZ=100 PATH=/usr/sbin: /usr/local/bin: . env = environ; LOGNAME=jw while (*env) { printf("%sn", *env); MAIL=/usr/mail/jw SHELL=/bin/ksh env++; HOME=/export/home/jw } TERM=ansi PWD=/export/home/jw/syspro/ch 5 return 0; TZ=ROK` } 18/24

환경변수의 사용[2] main 함수 인자 사용 int main(int argc, char **argv, char **envp) {.

환경변수의 사용[2] main 함수 인자 사용 int main(int argc, char **argv, char **envp) {. . . } ex 5_6. c 01 02 03 04 05 06 07 08 09 10 11 12 13 #include <stdio. h> int main(int argc, char **argv, char **envp) { char **env; env = envp; while (*env) { printf("%sn", *env); env++; } return 0; } # ex 5_6. out _=ex 5_6. out LANG=ko HZ=100 PATH=/usr/sbin: /usr/local/bin: . LOGNAME=jw MAIL=/usr/mail/jw SHELL=/bin/ksh HOME=/export/home/jw TERM=ansi PWD=/export/home/jw/syspro/ch 5 TZ=ROK 19/24

환경변수의 사용[3] 환경변수 검색: getenv(3) #include <stdlib. h> char *getenv(const char *name); ex 5_7.

환경변수의 사용[3] 환경변수 검색: getenv(3) #include <stdlib. h> char *getenv(const char *name); ex 5_7. c 01 02 03 04 05 06 07 08 09 10 11 12 13 14 #include <stdlib. h> #include <stdio. h> int main(void) { char *val; val = getenv("SHELL"); if (val == NULL) printf("SHELL not definedn"); else printf("SHELL = %sn", val); return 0; # ex 5_7. out SHELL = /bin/ksh } 20/24

환경변수의 사용[4] 환경변수 설정: putenv(3) #include <stdlib. h> int putenv(char *string); . . .

환경변수의 사용[4] 환경변수 설정: putenv(3) #include <stdlib. h> int putenv(char *string); . . . 04 int main(void) { 05 char *val; 06 07 val = getenv("SHELL"); 08 if (val == NULL) 09 printf("SHELL not definedn"); 10 else 11 printf("1. SHELL = %sn", val); 12 13 putenv("SHELL=/usr/bin/csh"); 14 15 val = getenv("SHELL"); 16 printf("2. SHELL = %sn", val); 18 return 0; 19 } ex 5_8. c # ex 5_8. out 1. SHELL = /usr/bin/ksh 2. SHELL = /usr/bin/csh 설정하려는 환경변수를 “환경변수=값”형태로 지정 21/24

환경변수의 사용[5] 환경변수 설정: setenv(3) #include <stdlib. h> int setenv(const char *envname, const char

환경변수의 사용[5] 환경변수 설정: setenv(3) #include <stdlib. h> int setenv(const char *envname, const char *envval, int overwrite); § envname : 환경변수명 지정 § envval : 환경변수 값 지정 § overwrite : 덮어쓰기 여부 지정, 0이 아니면 덮어쓰고, 0이면 덮어쓰지 않음 환경변수 설정 삭제: unsetenv(3) #include <stdlib. h> int unsetenv(const char *name); 22/24

[예제 5 -9] setenv 함수 사용하기 01 02 03 04 05 06 07 08

[예제 5 -9] setenv 함수 사용하기 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 ex 5_9. c #include <stdlib. h> #include <stdio. h> int main(void) { char *val; val = getenv("SHELL"); if (val == NULL) printf("SHELL not definedn"); else printf("1. SHELL = %sn", val); setenv("SHELL", "/usr/bin/csh", 0); val = getenv("SHELL"); printf("2. SHELL = %sn", val); setenv("SHELL", "/usr/bin/csh", 1); val = getenv("SHELL"); printf("3. SHELL = %sn", val); return 0; } 환경변수의 덮어쓰기가 되지 않음 환경변수의 덮어쓰기 설정 # ex 5_9. out 1. SHELL = /usr/bin/ksh 2. SHELL = /usr/bin/ksh 3. SHELL = /usr/bin/csh 23/24