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

(转载)Linux系统调用--getpid/getppid函数详解

时间:2022-12-18 21:44:09浏览次数:65  
标签:-- getppid pid getpid printf 进程 include

【getpid/getppid系统调用】

功能描述:
getpid返回当前进程标识,getppid返回父进程标识。

用法:

#include <sys/types.h>
#include <unistd.h>

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

例子:

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>

int main(void)
{
 pid_t pid;
 printf("Before fork ...\n");

 switch(pid = fork()) {
  case -1:
   printf("Fock call fail\n");
   exit(1);

  case 0:
   printf("The pid of child is: %d\n", getpid());
   printf("The pid of child's parent is: %d\n", getppid());
   printf("Child exiting...\n");
   exit(0);

  default:
   printf("The pid of parent is: %d\n", getpid());
   printf("the pid of parent's child is: %d\n", pid);
 }

 printf("After fork, program exiting...\n");
 exit(0);
}

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

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

main()

{

#include <stdio.h>

#include <unistd.h>

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

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

exit(0);

}

原文链接:https://blog.csdn.net/shixiaoguo90/article/details/21953959

标签:--,getppid,pid,getpid,printf,进程,include
From: https://www.cnblogs.com/arisskz6/p/16991015.html

相关文章

  • 线性回归
    一、概述寻找一条线,最大程度的“拟合”样本与特征与样本输出标记之间的关系,推算出自变量与因变量关系,是一个预测问题。有关误差可以参考:https://www.cnblogs.com/qiansl......
  • PPT 文字穿插
    软件文字,添加一个形状先选形状,再选文字选择拆分设置对象格式......
  • Hadoop--HDFS
    Hadoop3.1.2算法复杂度​ ]孙发复杂度分为时间复杂度和空间复杂度时间复杂度执行算需要计算工作量而空间复杂度是指执行这个算法所需要的内存空间;时间和空间都是......
  • Java数组(08)稀疏数组
       红标列不打印,第一行为总计数量       ......
  • (转载)Linux C 中断言assert()使用简介
    assert()是一个调试程序时经常使用的宏,在程序运行时它计算括号内的表达式,如果表达式为FALSE(0),程序将报告错误,并终止执行。如果表达式不为0,则继续执行后面的语句,它的作......
  • [seaborn] seaborn学习笔记1 箱形图Boxplot
    1箱形图Boxplot(代码下载)Boxplot可能是最常见的图形类型之一。它能够很好表示数据中的分布规律。箱型图方框的末尾显示了上下四分位数。极线显示最高和最低值,不包括异常......
  • 蓝桥杯今日份练习
    一、题目相信小伙伴们都学过斐波那契数列,它是这样一个数列:1,1,3,5,8,13,21…………用f(n)表示斐波那契数列的第n项,则有:f1=f2=1,fn=fn-1+fn-2(n>2).输入一个n,求出fn对10的9次方+7......
  • 风电随机性动态经济调度模型(Python&Matlab代码)
    目录​​0写在前面​​​​1引言​​​​1.1机会约束规划​​​​1.2基于场景的方法​​​​1.3模糊模型​​​​ 2六种处理风电随机性动态经济调度模型描述​​​​......
  • [seaborn] seaborn学习笔记0 seaborn学习笔记章节
    date:2019-05-2815:54:37+0800tags:-seaborn-Python-数据分析与可视化seaborn学习笔记章节seaborn是一个基于matplotlib的Python数据......
  • Python和Matlab系统比较
    目录​​1概述​​​​2Python和Matlab比较​​​​2.1编程习惯​​​​2.2符号表示对比​​​​2.3基本数据类型 ​​​​2.3.1 Python的Dictionary和Matlab的struc......