#.pid 검색 - getpid, getppid 함수 사용하기
예제
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <unistd.h> | |
#include <stdio.h> | |
/* | |
pid 는 0번 부터 시작한다. | |
0번 프로세스는 스케줄러로 프로세스에 cpu시간을 할당하는 역할을 한다. | |
0번 프로세스는 커널의 일부분이므로 별도의 실행파일은 없다. | |
1번 프로세스는 init 이다. | |
*/ | |
int main(void) { | |
//현재 프로세스의 id | |
printf("PID : %d\n", (int)getpid()); | |
//이프로세스를 실행시킨 부모의 id | |
printf("PPID : %d\n", (int)getppid()); | |
return 0; | |
} |
결과

#.getpgrp, getpgid 함수 사용하기
예제
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <unistd.h> | |
#include <stdio.h> | |
/* | |
프로세스 그룹은 관련있는 프로세스를 묶은 것으로, 프로세스 그룹 id를 부여 받는다. | |
getpgid 함수는 pid 인자로 지정한 프로세스가 속한 그룹의 pgid를 리턴한다. | |
만일 인자가 0이면 getpgid 함수를 호출한 프로세스가 속한 그룹의 pgid를 리턴한다. | |
*/ | |
int main(void) { | |
//pid 검색해서 출력 | |
printf("PID : %d\n", (int)getpid()); | |
//현재 프로세스의 pgid를 검색 - 1 | |
printf("PGRP : %d\n", (int)getpgrp()); | |
//현재 프로세스의 pgid를 검색 - 2 | |
printf("PGID(0) : %d\n", (int)getpgid(0)); | |
//pid가 6406 인 프로세스가 속한 그룹의 pgid를 검색한다. | |
//6406 의 프로세스는 다음과같이 파이프로 연결해 실행한 프로세스인 sleep의 pid이다. | |
printf("PGID(6406 ) : %d\n", (int)getpgid(6406)); | |
/* | |
root@kk12111:~/ch05# ps | |
PID TTY TIME CMD | |
6365 pts/0 00:00:00 bash | |
6406 pts/0 00:00:00 sleep | |
6407 pts/0 00:00:00 ps | |
*/ | |
return 0; | |
} |
결과

#.getsid 함수 사용하기
예제
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <unistd.h> | |
#include <stdio.h> | |
/* | |
사용자가 로그인해 작업하고 있는 터미널 단위로 프로세스 그룹을 묶은 것이다. | |
프로세스 그룹이 관련있는 프로세스를 그룹으로 묶은 개념이라면, | |
세션은 관련 있는 프로세스 그룹을 모은 개념이다. | |
프로세스가 새로운 세션을 생성하면 해당 프로세스는 세션 리더가 된다. | |
세션 리더의 pid는 세션 id가 된다. | |
*/ | |
int main(void) { | |
//현재 프로세스의 id를 검색해 출력한다. | |
printf("PID : %d\n", (int)getpid()); | |
//현재 프로세스가 속한 그룹의 id를 검색해 출력한다. | |
printf("PGID : %d\n", (int)getpgrp()); | |
//getsid 함수는 pid 인자로 지정한 프로세스가 속한 세션의 id를 리턴한다. | |
//만일 pidr가 0이면 현재 프로세스의 세션 id를 리턴한다. | |
//현재 프로세스가 속한 세션의 id를 검색해서 출력한다. | |
printf("SID : %d\n", (int)getsid(0)); | |
return 0; | |
} |
결과


#.environ 전역변수 사용하기
예제
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
/* | |
프로세스가 실행되는 기본 환경이 있다. | |
이 환경은 사용자의 로그인명, 로그인 쉘, 터미널에 설정된 언어, 경로명 등을 포함한다. | |
기본환경은 변수로 정의 되어 있다. | |
환경변수는 환경변수명 = 값 형태로 구성되며, 환경변수명은 관례적으로 대문자를 사용한다. | |
환경 변수는 쉘에서 값을 설정하거나 변경할 수 있으며, 함수를 이용해서 읽거나 설정할 수도 있다. | |
*/ | |
//전역변수 environ은 환경변수 전체에 대한 포인터로, 이 변수를 사용해서 환경변수를 검색할 수 있다. | |
extern char **environ; | |
int main(void) { | |
char **env; | |
//environ의 주소를 임시 포인터인 env 에 저장한다. | |
env = environ; | |
//env의 주소를 증가시키며 환경변수를 출력한다. | |
while (*env) { | |
printf("%s\n", *env); | |
env++; | |
} | |
return 0; | |
} |
결과

#.main 함수 인자 사용하기
예제
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
/* | |
유닉스에서는 환경변수를 다음과 같이 main 함수의 3번째에 인자로 지정해 사용할 수 있다. | |
*/ | |
//함수의 인자로 환경 변수에 대한 포인터를 받는다. | |
int main(int argc, char **argv, char **envp) { | |
char **env; | |
env = envp; | |
//인자로 받은 환경 변수의 값을 차례로 출력한다. | |
while (*env) { | |
printf("%s\n", *env); | |
env++; | |
} | |
return 0; | |
} |
결과

#.getenv 함수 사용하기
예제
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
/* | |
환경변수 검색 | |
getenv 함수는 인자로 지정한 환경 변수가 설정되어 있는지 검색해 결과값을 저장하고 주소를 리턴한다. | |
*/ | |
int main(void) { | |
char *val; | |
//getenv 함수를 호출해서 환경변수 shell을 검색하고, | |
//결과값을 문자형 포인터 val 에 저장한다. | |
val = getenv("SHELL"); | |
if (val == NULL) | |
printf("SHELL not defined\n"); | |
else | |
printf("SHELL = %s\n", val); | |
return 0; | |
} |
결과

#.putenv 함수 사용하기
예제
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
/* | |
putenv 함수를 사용하면 프로그램에서 환경 변수를 설정할 수 있다. | |
설정할 환경 변수를 환경변수명 = 값 형태로 지정하면 된다. | |
putenv 함수는 기존 환경 변수의 값은 변경하고, | |
새로운 환경 변수는 malloc 으로 메모리를 할당해 추가한다. | |
putenv 함수는 수행을 성공하면 0을 리턴한다. | |
*/ | |
int main(void) { | |
char *val; | |
val = getenv("SHELL"); | |
if (val == NULL) | |
printf("SHELL not defined\n"); | |
else | |
printf("1. SHELL = %s\n", val); | |
//putenv 함수로 SHELL 값 변경하기 | |
putenv("SHELL=/usr/bin/csh"); | |
val = getenv("SHELL"); | |
printf("2. SHELL = %s\n", val); | |
return 0; | |
} |
#.setenv 함수 사용하기
예제
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
/* | |
setenv 함수도 putenv 함수처럼 환경변수를 설정한다. | |
putenv와 다른점은 환경변수와 환경 변수 값을 각각 인자로 지정한다는 것이다. | |
*/ | |
int main(void) { | |
char *val; | |
//getenv 함수를 호출해 환경변수 SHELL의 값을 검색해서 출력한다. | |
val = getenv("SHELL"); | |
if (val == NULL) | |
printf("SHELL not defined\n"); | |
else | |
printf("1. SHELL = %s\n", val); | |
//setenv 함수로 SHELL 의 값을 /usr/bin/csh로 변경한다. overwrite 값을 0으로 지정(덮어쓰기 하지 않음) | |
setenv("SHELL","/usr/bin/csh", 0); | |
//다시 getenv 함수를 호출해 환경변수 SHELL의 값을 검색해서 출력한다. | |
val = getenv("SHELL"); | |
printf("2. SHELL = %s\n", val); | |
//setenv 함수로 SHELL 의 값을 /usr/bin/csh로 변경한다. overwrite 값을 1로 지정(덮어쓰기 함) | |
setenv("SHELL","/usr/bin/csh", 1); | |
val = getenv("SHELL"); | |
printf("3. SHELL = %s\n", val); | |
return 0; | |
} |
예제파일
ch05.zip
0.01MB
참고 : 유닉스시스템 프로그래밍(한빛미디어)
'컴퓨터 기초 > 운영체제 실습' 카테고리의 다른 글
[운영체제 실습] 11.시그널 (0) | 2020.07.03 |
---|---|
[운영체제 실습] 10.프로세스 생성과 실행 (0) | 2020.06.28 |
[운영체제 실습] 8.시스템 정보 다루기 - (그룹, 시간) (0) | 2020.06.27 |
[운영체제 실습] 7.시스템 정보 다루기 - (로그인, 패스워드 정보) (0) | 2020.06.27 |
[운영체제 실습] 6.파일과 디렉토리 (0) | 2020.06.25 |