在没有unistd.h的情况下获取当前进程的进程ID

时间:2021-08-14 14:35:32

I'm working on a deadlock detection algorithm and I'm only given kernel level libraries, i.e. #include <linux/somelibrary> and nothing else. Are there kernel-level facilities that will allow me to get the pid of the current process similar to getpid() of unistd.h?

我正在研究死锁检测算法,我只给了内核级库,即#include ,没有别的。是否有内核级设施允许我获取当前进程的pid类似于unistd.h的getpid()?

2 个解决方案

#1


I did some quick research and I found the answer. Thanks so much for your direction. The quick code I used was:

我做了一些快速的研究,我找到了答案。非常感谢你的指导。我使用的快速代码是:

printf("My current process id/pid is %d\n", current->pid);

Thanks again!

#2


This question makes little sense.

这个问题没什么意义。

Are you writing kernel-based code? In which case you can get the pid of the current task by using the "current" macro which points to the current task's task struct (which contains a member with the pid). That would only work if your kernel code is running in a context where a "current task" makes sense (i.e. not an interrupt, tasklet etc).

你在编写基于内核的代码吗?在这种情况下,您可以使用指向当前任务的任务结构(包含具有pid的成员)的“当前”宏来获取当前任务的pid。只有当你的内核代码在“当前任务”有意义的上下文中运行时(即不是中断,tasklet等),这才有效。

If you're writing userspace code, there should be no reason you can't call getpid, which is a library call from the C library defined in unistd.h (or something it includes), which makes the system call. If there is such a reason, please explain it.

如果你正在编写用户空间代码,那么你就没有理由不能调用getpid,这是一个来自unistd.h(或它包含的东西)中定义的C库的库调用,这会调用系统。如果有这样的原因,请解释一下。

Making a system call in Linux isn't particularly difficult, but does involve architecture-specific code that you don't want to write.

在Linux中进行系统调用并不是特别困难,但确实涉及您不想编写的特定于体系结构的代码。

#1


I did some quick research and I found the answer. Thanks so much for your direction. The quick code I used was:

我做了一些快速的研究,我找到了答案。非常感谢你的指导。我使用的快速代码是:

printf("My current process id/pid is %d\n", current->pid);

Thanks again!

#2


This question makes little sense.

这个问题没什么意义。

Are you writing kernel-based code? In which case you can get the pid of the current task by using the "current" macro which points to the current task's task struct (which contains a member with the pid). That would only work if your kernel code is running in a context where a "current task" makes sense (i.e. not an interrupt, tasklet etc).

你在编写基于内核的代码吗?在这种情况下,您可以使用指向当前任务的任务结构(包含具有pid的成员)的“当前”宏来获取当前任务的pid。只有当你的内核代码在“当前任务”有意义的上下文中运行时(即不是中断,tasklet等),这才有效。

If you're writing userspace code, there should be no reason you can't call getpid, which is a library call from the C library defined in unistd.h (or something it includes), which makes the system call. If there is such a reason, please explain it.

如果你正在编写用户空间代码,那么你就没有理由不能调用getpid,这是一个来自unistd.h(或它包含的东西)中定义的C库的库调用,这会调用系统。如果有这样的原因,请解释一下。

Making a system call in Linux isn't particularly difficult, but does involve architecture-specific code that you don't want to write.

在Linux中进行系统调用并不是特别困难,但确实涉及您不想编写的特定于体系结构的代码。