C,C++ & Linux
C/C++ getpid(2) getppid(2) getuid(2) geteuid(2) getgid(2) getegid(2)
KyooDong
2020. 5. 23. 13:51
728x90
함수 기능
프로세스 아이디 관련한 함수들입니다.
getpid() : 호출한 프로세스의 프로세스 아이디를 알려줍니다.
getppid() : 호출한 프로세스의 부모 프로세스 아이디를 알려줍니다.
getuid() : 호출한 프로세스의 사용자 아이디를 알려줍니다.
geteuid() : 호출한 프로세스의 유효사용자 아이디를 알려줍니다.
getgid() : 호출한 프로세스의 그룹 아이디를 알려줍니다.
getegid() : 호출한 프로세스의 유효 그룹 아이디를 알려줍니다.
함수 원형
#include <unistd.h>
#include <sys/types.h>
pid_t getpid();
pid_t getppid();
uid_t getuid();
uid_t geteuid();
gid_t getgid();
gid_t getegid();
반환값
각 함수에 맞는 아이디를 리턴
예제
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
int main(int argc, char *argv[], char *envpp[]) {
printf("Process id\t\t=%d\n", getpid());
printf("Parent process id\t=%d\n", getppid());
printf("Real user id\t\t=%d\n", getuid());
printf("Effective user id\t=%d\n", geteuid());
printf("Real group id\t\t=%d\n", getgid());
printf("Effective group id\t=%d\n", getegid());
exit(0);
}
결과

리눅스시스템프로그래밍 저자 : 홍지만
https://book.naver.com/bookdb/book_detail.nhn?bid=14623672
책에 기술된 예제 프로그램입니다. 책 내부에는 훨씬 더 많은 자료가 있습니다. (개인적으로 좋았습니다.)