C++ getpid() 与 syscall(39)?

标签 c++ linux macos unix system-calls

我读到 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/

相关文章:

java - Linux 上的 Gradle "cannot find Tools.jar"

linux - M-f、M-b 绑定(bind)在 Mac X11 上不起作用(通过 NX)

php - 在 Mac OS X Yosemite 中发送邮件

c++ - 为面向对象的 C++ 代码开发 C 包装 API

C++ REST SDK 卡萨布兰卡 Client.request

c++ - 在将几行 ruby​​ 代码转换为 C++ 时需要帮助!

linux - 我如何使用linux编写基本脚本?

c++ - CRT & new - 92 字节的 30 次调用给出 "bad_alloc nomem"

linux - 将 Octave 4.0/4.2.1 中的默认编辑器设置为 Vim

ImageMagick:此图像格式没有解码委托(delegate) `' @ error/constitute.c/ReadImage/504