Updated on 2020-04-28Windows/LinuxでプロセスIDをC/Pythonから取得する方法。
Windows/C
GetCurrentProcessId()
を使う。
1
2
3
4
5
6
7
| #include <stdio.h>
#include <windows.h>
int main(void) {
printf("Process ID = %d\n", GetCurrentProcessId());
return 0;
}
|
Linux/C
getpid()
を使う。
1
2
3
4
5
6
7
8
| #include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(void) {
printf("Process ID = %d\n", getpid());
return 0;
}
|
Python
os.getpid()
を使う。
1
2
3
| import os
print("Process ID = ", os.getpid())
|