我读到 syscall(39) 返回当前进程 ID (pid)
那为什么这2个程序输出2个不同的数字呢?
int main() {
long r = syscall(39);
printf("returned %ld\n", r);
return 0;
}
和:
int main() {
long r = getpid();
printf("returned %ld\n", r);
return 0;
}
我在 clion 中运行我的程序,当我更改第一行时,我得到了不同的结果,这真的很奇怪。
运行我得到的答案中的代码(在 macos 中):
returned getpid()=9390 vs. syscall(39)=8340
这真的很奇怪。
在 ubuntu 中,我得到了相同的 pid,这是为什么?
最佳答案
通过号码进行系统调用是不可移植的。
确实,我们看到 39 是 getpid在 Linux 上,但是 getppid (“获取父 pid”)在 macOS 上。
getpid
在 macOS 上是 20。
这就是为什么您在 macOS 上的 getpid()
和 syscall(39)
之间看到不同结果的原因。
请注意,macOS 作为 BSD 内核的衍生版本,与 Linux 没有任何关系。这不可能,因为它是闭源的。
关于C++ getpid() 与 syscall(39)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67437924/