简体   繁体   English

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

[英]Getting the process id of a current process without unistd.h

I'm working on a deadlock detection algorithm and I'm only given kernel level libraries, ie #include <linux/somelibrary> and nothing else. 我正在研究死锁检测算法,我只给了内核级库,即#include <linux/somelibrary> ,没有别的。 Are there kernel-level facilities that will allow me to get the pid of the current process similar to getpid() of unistd.h ? 是否有内核级设施允许我获取当前进程的pid类似于unistd.h getpid()

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! 再次感谢!

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). 在这种情况下,您可以使用“当前”宏来获取当前任务的pid,该宏指向当前任务的任务结构(其中包含具有pid的成员)。 That would only work if your kernel code is running in a context where a "current task" makes sense (ie not an interrupt, tasklet etc). 只有当你的内核代码在“当前任务”有意义的上下文中运行时(即不是中断,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. 如果你正在编写用户空间代码,那么你就没有理由不能调用getpid,这是一个来自unistd.h(或它包含的东西)中定义的C库的库调用,这会调用系统。 If there is such a reason, please explain it. 如果有这样的原因,请解释一下。

Making a system call in Linux isn't particularly difficult, but does involve architecture-specific code that you don't want to write. 在Linux中进行系统调用并不是特别困难,但确实涉及您不想编写的特定于体系结构的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM