関数仕様
書式
|
1 2 3 4 5 6 |
#include <sys/types.h> #include <unistd.h> pid_t getpid(); pid_t getppid(); |
引数
| なし |
戻り値
| 正の値 | 自分のプロセスID(getpid)/ 親のプロセスID(getppid) |
メモ
getpid() と getppid() は必ず成功し、エラーになることはありません。
機能
- getpid() は、自分のプロセスIDを戻り値として返す
- getppid() は、自分の親のプロセスIDを戻り値として返す
注意すること
注意ポイント
glibc の getpid() を使う場合は、子プロセスで誤ったプロセスIDを取得する場合がある。
glibc バージョン 2.3.4 以降では、getpid() のラッパー関数は PID をキャッシュします。
そのため、fork()、vfork()、clone() などの glibc のラッパー関数を使用せずに子プロセスを生成した場合、
子プロセスにキャッシュがコピーされるため、誤った値を取得してしまう場合があります。
関連
fork() 関数については、「fork関数の使い方と注意点」にまとめています。
サンプルプログラム
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <sys/types.h> #include <unistd.h> #include <stdio.h> int main(void) { pid_t my_pid, ppid; my_pid = getpid(); ppid = getppid(); printf("my_pid=%d, ppid=%d\n", (int)my_pid, (int)ppid); return 0; } |
[参考] getpid() は本当に失敗しないのか?
Linux カーネルのソースコードを元に、本当に失敗しないのか調べてみました。
sys_getpid()
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
/** * sys_getpid - return the thread group id of the current process * * Note, despite the name, this returns the tgid not the pid. The tgid and * the pid are identical unless CLONE_THREAD was specified on clone() in * which case the tgid is the same in all threads of the same group. * * This is SMP safe as current->tgid does not change. */ SYSCALL_DEFINE0(getpid) { return task_tgid_vnr(current); } |
まずは、カーネルがシステムコールを定義している関数です。
task_tgid_vnr() を呼び出す際の引数 current は、getpid() を呼び出したスレッドの管理構造体です。
CPUアーキテクチャによって異なりますが、通常はコンテキストスイッチする際に、
実行するスレッドの管理構造体へのアドレスが汎用レジスタに保存され、それを current で参照します。
task_tgid_vnr()
|
1 2 3 4 5 |
static inline pid_t task_tgid_vnr(struct task_struct *tsk) { return __task_pid_nr_ns(tsk, PIDTYPE_TGID, NULL); } |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type, struct pid_namespace *ns) { pid_t nr = 0; rcu_read_lock(); if (!ns) ns = task_active_pid_ns(current); nr = pid_nr_ns(rcu_dereference(*task_pid_ptr(task, type)), ns); rcu_read_unlock(); return nr; } |
ラッパー関数が続きましたが、__task_pid_nr_ns() が本体のようです。
引数である ns は NULL で呼び出されていますので、if 文内には入って task_active_pid_ns() を呼び出す。
task_active_pid_ns()
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
/* include/linux/sched.h */ struct pid_namespace *task_active_pid_ns(struct task_struct *tsk) { return ns_of_pid(task_pid(tsk)); } static inline struct pid *task_pid(struct task_struct *task) { return task->thread_pid; } /* include/linux/pid.h */ static inline struct pid_namespace *ns_of_pid(struct pid *pid) { struct pid_namespace *ns = NULL; if (pid) ns = pid->numbers[pid->level].ns; return ns; } |
この経路では、pid が NULL になることはありませんので、何かしらのアドレスが ns にセットされます。
task_pid_ptr()
|
1 2 3 4 5 6 7 |
static struct pid **task_pid_ptr(struct task_struct *task, enum pid_type type) { return (type == PIDTYPE_PID) ? &task->thread_pid : &task->signal->pids[type]; } |
引数の type は PIDTYPE_TGID ですので、後者のロジックが実行されます。
rcu_dereference()
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#define rcu_dereference(p) rcu_dereference_check(p, 0) #define rcu_dereference_check(p, c) \ __rcu_dereference_check((p), (c) || rcu_read_lock_held(), __rcu) #define __rcu_dereference_check(p, c, space) \ ({ \ /* Dependency order vs. p above. */ \ typeof(*p) *________p1 = (typeof(*p) *__force)READ_ONCE(p); \ RCU_LOCKDEP_WARN(!(c), "suspicious rcu_dereference_check() usage"); \ rcu_check_sparse(p, space); \ ((typeof(*p) __force __kernel *)(________p1)); \ }) #define rcu_check_sparse(p, space) \ ((void)(((typeof(*p) space *)p) == p)) |
メモリの有効性をチェックしている、と理解しておきましょう。
pid_nr_ns()
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
pid_t pid_nr_ns(struct pid *pid, struct pid_namespace *ns) { struct upid *upid; pid_t nr = 0; if (pid && ns->level <= pid->level) { upid = &pid->numbers[ns->level]; if (upid->ns == ns) nr = upid->nr; } return nr; } |
ユーザーランドから呼び出した場合、if 文の中に入って自身のプロセスIDがセットされます。
長くなりましたが・・・
どこを見ても、システムコールのエラーを示す -1 が返る箇所が無いので、
getpid() が失敗しないというのは正しいようです。
途中で NULL アクセスをした場合は、カーネル内で SIGSEGV が発生することになりますので、
カーネルパニックが発生してシステム自体が停止します。