Linux 获取当前运行程序的进程 id

ps axu | grep python 查看当前 Python 程序进程

ps axu | grep python

root      1390  0.0  0.0 169356  8048 ?        Ssl  Jan18   0:00 /usr/bin/python3 /usr/bin/networkd-dispatcher --run-startup-triggers
root      1632  0.0  0.0 185948  6908 ?        Ssl  Jan18   0:00 /usr/bin/python3 /usr/share/unattended-upgrades/unattended-upgrade-shutdown --wait-for-signal

zhijian      13586  0.0  0.0  13144  1108 pts/12   S+   07:05   0:00 grep --color=auto python

杀死进程
通常我们运行程序,可以记录程序的 进程id ,隔天查看 进程 id 即可 知道程序是否运行完毕

Kill 13586  

C 代码获取当前程序的 进程 id


getPid.c 代码如下:

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
int main()
{
    // 获取当前目录
    char command[50];
    strcpy(command, "pwd");
    system(command);

    // 查看当前进程id
    printf("Current program's pid is %d \n", getpid());

    // 查看当前程序父进程id
    printf("Current program's ppid is %d \n", getppid());

    return 0;
}

程序运行过程如下:

gcc getPid.c 
./a.out 

/home/zhijian/project/project21/Three/VSCode
Current program's pid is 13577 
Current program's ppid is 13301 

# 可见 进程 id  为 13301 即当前终端 
ps -aux | grep 13301
zql      13301  0.0  0.0  22780  6680 pts/12   Ss   06:43   0:00 -bash
zql      13580  0.0  0.0  13144  1016 pts/12   S+   07:02   0:00 grep --color=auto 13301

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

墨理学AI

不必打赏,关注博主公众号即可

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值