getppid()和getpid()都是中定义的内置函数 unistd。H 图书馆
null
- getppid(): 返回调用进程父进程的进程ID。如果调用进程是由 fork() 函数和父进程在调用getppid函数时仍然存在,此函数返回父进程的进程ID。否则,此函数将返回值1,该值是的进程id 初始化 过程 语法:
pid_t getppid(void);
返回类型: getppid()返回当前进程父进程的进程ID。它从不抛出任何错误,因此总是成功的。
// C++ Code to demonstrate getppid()
#include <iostream>
#include <unistd.h>
using
namespace
std;
// Driver Code
int
main()
{
int
pid;
pid = fork();
if
(pid == 0)
{
cout <<
"Parent Process id : "
<< getpid() << endl;
cout <<
"Child Process with parent id : "
<< getppid() << endl;
}
return
0;
}
输出(在不同的系统上会有所不同):
Parent Process id of current process : 3849 Child Process with parent id : 3851
注: 在某些情况下,子进程不必首先执行,或者父进程将首先被分配CPU,任何进程都可能在某个时间段被分配CPU。此外,在不同的执行过程中,进程id可能会有所不同。
- getpid(): 返回调用进程的进程ID。这通常由生成唯一临时文件名的例程使用。 语法:
pid_t getpid(void);
返回类型: getpid()返回当前进程的进程ID。它从不抛出任何错误,因此总是成功的。
// C++ Code to demonstrate getpid()
#include <iostream>
#include <unistd.h>
using
namespace
std;
// Driver Code
int
main()
{
int
pid = fork();
if
(pid == 0)
cout <<
"Current process id of Process : "
<< getpid() << endl;
return
0;
}
输出(在不同的系统上会有所不同):
Current process id of Process : 4195
本文由 Pushpanjali Chauhan .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END