系统相关
首页 > 系统相关> > Linux系统调用--getpid/getppid函数详解

Linux系统调用--getpid/getppid函数详解

作者:互联网

Linux系统调用--getpid/getppid函数详解

 

【getpid/getppid系统调用】   
    
功能描述: 
getpid返回当前进程标识,getppid返回父进程标识。

  
用法:  
#include <sys/types.h>
#include <unistd.h>

pid_t getpid(void);
pid_t getppid(void);

例子:


 
  1. #include <stdlib.h>

  2. #include <stdio.h>

  3. #include <sys/types.h>

  4.  
  5. int main(void)

  6. {

  7. pid_t pid;

  8. printf("Before fork ...\n");

  9.  
  10. switch(pid = fork()) {

  11. case -1:

  12. printf("Fock call fail\n");

  13. exit(1);

  14.  
  15. case 0:

  16. printf("The pid of child is: %d\n", getpid());

  17. printf("The pid of child's parent is: %d\n", getppid());

  18. printf("Child exiting...\n");

  19. exit(0);

  20.  
  21. default:

  22. printf("The pid of parent is: %d\n", getpid());

  23. printf("the pid of parent's child is: %d\n", pid);

  24. }

  25.  
  26. printf("After fork, program exiting...\n");

  27. exit(0);

  28. }

  29.  

 

 

 

 

getpid() getppid()进程和父进程函数

getpid()和getppid()进程和父进程函数,在调用中都不能返回错误,下面的程序输出了他的进程ID和父进程ID,由于不能保证pid_t 能够放进 int类型中去,返回值被转为long 整型输出


 
  1. main()

  2.  
  3. {

  4.  
  5. #include <stdio.h>

  6.  
  7. #include <unistd.h>

  8.  
  9. printf ("I am process %ld",(long)getpid());

  10.  
  11. printf ("My parent is %ld",(long)getppid());

  12.  
  13. return(0);

  14.  
  15. }

 

标签:getppid,Linux,pid,getpid,printf,进程,include
来源: https://blog.csdn.net/ll148305879/article/details/90751800