进程id
我们知道怎么通过fork函数创建(或者说是复制)一个进程,但是我们要怎么样操作这个被创建出来的进程呢?那就需要用到他的进程id,所以就要获取进程id,一下提供一些获取进程id的函数和其使用方法。
1)getpid和gteppid
s
#include <sys/types.h>
#include <unistd.h> pid_t getpid(void); //返回调用进程的PID号
pid_t getppid(void); //返回调用进程父进程的PID号 例子: #include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h> int main(void)
{
pid_t pid; printf("this is a test\n");
/*
*上面的语句如果没有\n那么就会在子进程中也打印一句
*这个是由于printf的机制造成的,遇到\n就出栈,
*若没有\n那么会在执行下一条语句的时候前出栈,
*而下一条语句是fork,此时已经复制主进程的环境,其中包括打印
*/
pid = fork();//调用fork时,子进程会从下面的语句开始执行 if(pid > )
{
while()
{
printf("I am parent\n");
printf("parent's pid id %d\n",getpid());
printf("parent's father is %d\n\n",getppid());
sleep();
}
}
else if(pid == )
{
while()
{
printf("I am child\n");
printf("child's pid id %d\n",getpid());
printf("child's father is %d\n\n",getppid());
sleep();
}
}
else
{
perror("fork");
exit();
} return ;
}
输出结果:
this is a test
I am parent
parent's pid id 4040
parent's father is 3348
I am child
child's pid id 4041
child's father is 4040
I am parent
parent's pid id 4040
parent's father is 3348
I am child
child's pid id 4041
child's father is 4040
2)getuid和geteuid
概念补充:
1、实际用户ID和实际用户组ID:标识我是谁。也就是登录用户的uid和gid,比如我的Linux以simon登录,在Linux运行的所有的命令的实际用户ID都是simon的uid,实际用户组ID都是simon的gid(可以用id命令查看)。
2、有效用户ID和有效用户组ID:进程用来决定我们对资源的访问权限。一般情况下,有效用户ID等于实际用户ID,有效用户组ID等于实际用户组ID。当设置-用户-ID(SUID)位设置,则有效用户ID等于文件的所有者的uid,而不是实际用户ID;同样,如果设置了设置-用户组-ID(SGID)位,则有效用户组ID等于文件所有者的gid,而不是实际用户组ID。
#include <unistd.h>
#include <sys/types.h>
uid_t getuid(void); //返回实际用户ID
uid_t geteuid(void); //返回有效用户ID
例子:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h> /*
uid_t getuid(void); //返回实际用户ID
uid_t geteuid(void); //返回有效用户ID
*/
int main(void)
{
printf("实际用户ID = %d\n",getuid());
printf("有效用户ID = %d\n",geteuid()); return ;
}
输出结果:
实际用户ID = 1000
有效用户ID = 1000
3)getgid和getegid
#include <unistd.h>
#include <sys/types.h>
gid_t getgid(void); //返回实际用户组ID
gid_t getegid(void); //返回有效用户组ID
例子:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h> /*
gid_t getgid(void); //返回实际用户组ID
gid_t getegid(void); //返回有效用户组ID
*/
int main(void)
{
printf("实际用户组ID = %d\n",getgid());
printf("有效用户组ID = %d\n",getegid()); return ;
}
输出结果:
实际用户组ID = 1000
有效用户组ID = 1000
进程id的更多相关文章
- Linux 内核进程管理之进程ID
Linux 内核使用 task_struct 数据结构来关联所有与进程有关的数据和结构,Linux 内核所有涉及到进程和程序的所有算法都是围绕该数据结构建立的,是内核中最重要的数据结构之一.该数据结构 ...
- C++ Windows 下 根据进程名获取进程ID 以及该进程下所有窗口的句柄
#include <windows.h> #include <stdint.h> #include <tlhelp32.h> #include <stdio. ...
- 如何在脚本中获取进程ID(PID)
我想要知道运行中脚本子shell的进程id.我该如何在shell脚本中得到PID. 当我在执行shell脚本时,它会启动一个叫子shell的进程.作为主shell的子进程,子shell将shell脚本 ...
- dos命令记录以及dos下通过进程id查找工作路径
dos命令 tasklist |findstr "1696" 通过进程id查看进程名 tasklist 列出所有进程 netstat -ano 查看当前网络通信进程连接的各种状态 ...
- C#获取运行程序的进程ID
C#获取运行程序的进程ID [DllImport("User32.dll", CharSet = CharSet.Auto)] public static extern int G ...
- 如何通过进程名获取进程ID
博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:如何通过进程名获取进程ID.
- Linux 内核进程管理之进程ID 。图解
http://www.cnblogs.com/hazir/tag/kernel/ Linux 内核进程管理之进程ID Linux 内核使用 task_struct 数据结构来关联所有与进程有关的数 ...
- 通过命令名称查询进程id
linux 中如何通过命令名称查询出进程的id呢? 例如,我想查询java的进程id: ps -ef |grep java |grep -v grep|awk '{print $2}' 或者: ps ...
- ORA-03113: 通信通道的文件结尾 进程 ID: 764 会话 ID: 125 序列号: 5
昨天因为导入很久数据,最后一看是因为数据文件不够,后来就关机了.现在,开启数据库,总是报“ORA-03113: 通信通道的文件结尾” SQL> conn /as sysdba; 已连接到空闲例程 ...
- jstack:将Process Explorer中看到的进程ID做16进制转换,到ThreadDump中加上0x 前缀即能找到对应线程(转)
原文链接:http://www.iteye.com/topic/1133941 症状: 使用Eclipse win 64位版本,indigo及kepler都重现了,使用tomcat 6.0.39,jd ...
随机推荐
- C#添加二维码带加密带logo
#region 生成QR码,加密与logo在此处修改 public static void CreateQr(string strQrContent, DataTable myTable) { Qr ...
- (mybatis)There is no getter for property named 'isEffective' in 'class java.lang.String
原来代码: <select id="findSpecialOffer" resultType="com.lizard.back.model.SpecialOffer ...
- 【SHOI2015】脑洞治疗仪(恶心的线段树,区间最大子段和)
题目描述: 曾经发明了自动刷题机的发明家 SHTSC 又公开了他的新发明:脑洞治疗仪——一种可以治疗他因为发明而日益增大的脑洞的神秘装置. 为了简单起见,我们将大脑视作一个 01 序列.11代表这个位 ...
- ABAP术语-Document Number
Document Number 原文:http://www.cnblogs.com/qiangsheng/archive/2008/01/28/1055636.html Key which ident ...
- springboot整合swagger笔记
首先,在pom.xml中添加依赖 <!--swagger--> <dependency> <groupId>io.springfox</groupId> ...
- nodejs--http
http模块主要用到四个方法: 1.Server类 const http = require('http'); let server = new Server(); server.on('reques ...
- MySQL wait_timeout参数修改
MySQL wait_timeout参数修改问题,可能经常会有DBA遇到过,下面就试验一下并看看会有什么现象. wait_timeout分为global级及session级别,如未进行配置,默认值为2 ...
- js判断是否为数字
function isNumber(value) { var patrn = /^(-)?\d+(\.\d+)?$/; if (patrn.exec(value) == null || value = ...
- Linux上面安装redis和简单使用
一.安装,redis的官方的网址 https://redis.io/ 目前的最高的版本是4.0,我安装的是2.*的版本 1.下载源码,解压后编译源码. $ wget http://download ...
- PHP(YII2实现) 微信网页授权
参考地址 https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842 实现步骤分析: 获取code->access ...